Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "XDI4j Tutorial 1"

(Example)
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}
 +
[[Image:Higgins_logo_76Wx100H.jpg|right]]
 +
 +
This tutorial explains how to create and manipulate a simple XDI graph.
 +
 
== Interfaces and classes ==
 
== Interfaces and classes ==
 +
 +
This section briefly describes the core Java interfaces that represent various parts of an XDI graph.
  
 
=== Graph ===
 
=== Graph ===
  
  org.eclipse.higgins.xdi4j.Graph;
+
org.eclipse.higgins.xdi4j.Graph;
  
This interface represents a whole XDI graph.
+
This interface represents an XDI graph.
 
XDI graphs consist of subjects, predicates, literals, references and inner graphs.
 
XDI graphs consist of subjects, predicates, literals, references and inner graphs.
 
Operations on the graph include creating these graph components, finding and manipulating them
 
Operations on the graph include creating these graph components, finding and manipulating them
Line 11: Line 18:
 
=== Subject ===
 
=== Subject ===
  
  org.eclipse.higgins.xdi4j.Subject;
+
org.eclipse.higgins.xdi4j.Subject;
  
 
This interface represents a subject in an XDI graph. Methods include
 
This interface represents a subject in an XDI graph. Methods include
Line 25: Line 32:
 
=== Reference ===
 
=== Reference ===
  
  org.eclipse.higgins.xdi4j.Reference;
+
org.eclipse.higgins.xdi4j.Reference;
  
 
This interface represents a reference in an XDI graph.
 
This interface represents a reference in an XDI graph.
Line 31: Line 38:
 
=== Literal ===
 
=== Literal ===
  
  org.eclipse.higgins.xdi4j.Literal;
+
org.eclipse.higgins.xdi4j.Literal;
  
 
This interface represents a literal in an XDI graph.
 
This interface represents a literal in an XDI graph.
Line 37: Line 44:
 
=== Statement ===
 
=== Statement ===
  
  org.eclipse.higgins.xdi4j.Statement;
+
org.eclipse.higgins.xdi4j.Statement;
  
 
This interface represents a statement in an XDI graph.
 
This interface represents a statement in an XDI graph.
Line 48: Line 55:
 
* A subject and a predicate and a literal.
 
* A subject and a predicate and a literal.
 
* A subject and a predicate and an inner graph.
 
* A subject and a predicate and an inner graph.
 
A statement never has BOTH an reference and a literal.
 
  
 
=== GraphFactory ===
 
=== GraphFactory ===
  
  org.eclipse.higgins.xdi4j.GraphFactory;
+
org.eclipse.higgins.xdi4j.GraphFactory;
  
Graph factories exist for all implementations of the Graph interface (e.g. Memory, XML, Hibernate). They can be used to create a new XDI document, or to load an existing one.
+
Graph factories can create new XDI graphs and load existing ones. Different graph factories use different backing stores (e.g. In-Memory, XML file, Hibernate). Some graph factories have configuration properties that should be set before creating an XDI graph.
  
== Example ==
+
== Constructing an XDI graph ==
  
This example app shows how to create a new XDI document and how to perform basic graph operations on it.
+
We will write a simple Java program that creates the following XDI graph using the ''MemoryGraphFactory'':
 +
 
 +
<pre>
 +
=markus
 +
+name
 +
"Markus"
 +
+friend
 +
=giovanni
 +
=drummond
 +
=giovanni
 +
+friend
 +
=drummond
 +
</pre>
 +
 
 +
Note that subjects, predicates and references have no specific order, i.e. when the graph is serialized and printed, the subject '''=giovanni''' could just as well come before '''=markus'''.
 +
 
 +
There are several ways of working with an XDI graph. For example, we could create the XDI subject '''=markus''', then the XDI predicate '''+name''', and then the XDI literal '''"Markus"'''. Or, we could create an entire XDI statement with all three parts in a single step. The result is the same.
 +
 
 +
'''Example Code:'''
  
 
<pre>
 
<pre>
Line 74: Line 97:
 
  * This example app shows how to create a new XDI
 
  * This example app shows how to create a new XDI
 
  * document and how to perform basic graph operations on it.
 
  * document and how to perform basic graph operations on it.
*
 
* @author msabadello at parityinc dot not
 
*
 
 
  */
 
  */
 
public class Tutorial1 {
 
public class Tutorial1 {
Line 89: Line 109:
 
// 1) work with subjects, predicates, references and literals
 
// 1) work with subjects, predicates, references and literals
  
Subject markus = graph.createSubject(new XRI3Segment("=markus"));
+
Subject markus = graph.createSubject(new XRI3Segment("=alice"));
  
 
Predicate name = markus.createPredicate(new XRI3Segment("+name"));
 
Predicate name = markus.createPredicate(new XRI3Segment("+name"));
 
Predicate friend = markus.createPredicate(new XRI3Segment("+friend"));
 
Predicate friend = markus.createPredicate(new XRI3Segment("+friend"));
  
name.createLiteral("Markus");
+
name.createLiteral("Alice X.");
friend.createReference(new XRI3Segment("=drummond"));
+
friend.createReference(new XRI3Segment("=bob"));
  
 
// 2) work with statements
 
// 2) work with statements
  
graph.createStatement(new XRI3Segment("=markus"), new XRI3Segment("+friend"), new XRI3Segment("=giovanni"));
+
graph.createStatement(new XRI3Segment("=alice"), new XRI3Segment("+friend"), new XRI3Segment("=charlie"));
graph.createStatement(new XRI3Segment("=giovanni"), new XRI3Segment("+friend"), new XRI3Segment("=drummond"));
+
graph.createStatement(new XRI3Segment("=charlie"), new XRI3Segment("+friend"), new XRI3Segment("=bob"));
  
 
// after our graph is filled, we can output it
 
// after our graph is filled, we can output it
  
 
System.out.println(graph.toString());
 
System.out.println(graph.toString());
 +
 +
// and we can navigate and read from it
 +
 +
String aliceName = graph.getSubject(new XRI3Segment("=alice")).getPredicate(new XRI3Segment("+name")).getLiteral().getData();
 +
 +
System.out.println("=alice's name: " + aliceName);
 +
 +
// don't forget to close the graph
  
 
graph.close();
 
graph.close();
Line 110: Line 138:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
[[Category:Higgins XDI4j]]

Latest revision as of 08:55, 29 January 2010

{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}

Higgins logo 76Wx100H.jpg

This tutorial explains how to create and manipulate a simple XDI graph.

Interfaces and classes

This section briefly describes the core Java interfaces that represent various parts of an XDI graph.

Graph

org.eclipse.higgins.xdi4j.Graph;

This interface represents an XDI graph. XDI graphs consist of subjects, predicates, literals, references and inner graphs. Operations on the graph include creating these graph components, finding and manipulating them

Subject

org.eclipse.higgins.xdi4j.Subject;

This interface represents a subject in an XDI graph. Methods include creating and finding predicates of the subject.

Predicate

 org.eclipse.higgins.xdi4j.Predicate;

This interface represents a predicate in an XDI graph. Methods include creating and finding references, literals and inner graphs of the predicate.

Reference

org.eclipse.higgins.xdi4j.Reference;

This interface represents a reference in an XDI graph.

Literal

org.eclipse.higgins.xdi4j.Literal;

This interface represents a literal in an XDI graph.

Statement

org.eclipse.higgins.xdi4j.Statement;

This interface represents a statement in an XDI graph.

A statement consists of either:

  • A subject (if the subject has no predicates).
  • A subject and a predicate (if the predicate has no references or literals).
  • A subject and a predicate and a reference.
  • A subject and a predicate and a literal.
  • A subject and a predicate and an inner graph.

GraphFactory

org.eclipse.higgins.xdi4j.GraphFactory;

Graph factories can create new XDI graphs and load existing ones. Different graph factories use different backing stores (e.g. In-Memory, XML file, Hibernate). Some graph factories have configuration properties that should be set before creating an XDI graph.

Constructing an XDI graph

We will write a simple Java program that creates the following XDI graph using the MemoryGraphFactory:

=markus
	+name
		"Markus"
	+friend
		=giovanni
		=drummond
=giovanni
	+friend
		=drummond

Note that subjects, predicates and references have no specific order, i.e. when the graph is serialized and printed, the subject =giovanni could just as well come before =markus.

There are several ways of working with an XDI graph. For example, we could create the XDI subject =markus, then the XDI predicate +name, and then the XDI literal "Markus". Or, we could create an entire XDI statement with all three parts in a single step. The result is the same.

Example Code:

package org.eclipse.higgins.xdi4j.tutorial;

import org.eclipse.higgins.xdi4j.Graph;
import org.eclipse.higgins.xdi4j.GraphFactory;
import org.eclipse.higgins.xdi4j.Predicate;
import org.eclipse.higgins.xdi4j.Subject;
import org.eclipse.higgins.xdi4j.impl.memory.MemoryGraphFactory;
import org.eclipse.higgins.xdi4j.xri3.impl.XRI3Segment;

/**
 * This example app shows how to create a new XDI
 * document and how to perform basic graph operations on it.
 */
public class Tutorial1 {

	public static void main(String[] args) throws Exception {

		GraphFactory factory = MemoryGraphFactory.getInstance();
		Graph graph = factory.openGraph();

		// there are basically two ways of working with graphs

		// 1) work with subjects, predicates, references and literals

		Subject markus = graph.createSubject(new XRI3Segment("=alice"));

		Predicate name = markus.createPredicate(new XRI3Segment("+name"));
		Predicate friend = markus.createPredicate(new XRI3Segment("+friend"));

		name.createLiteral("Alice X.");
		friend.createReference(new XRI3Segment("=bob"));

		// 2) work with statements

		graph.createStatement(new XRI3Segment("=alice"), new XRI3Segment("+friend"), new XRI3Segment("=charlie"));
		graph.createStatement(new XRI3Segment("=charlie"), new XRI3Segment("+friend"), new XRI3Segment("=bob"));

		// after our graph is filled, we can output it

		System.out.println(graph.toString());

		// and we can navigate and read from it

		String aliceName = graph.getSubject(new XRI3Segment("=alice")).getPredicate(new XRI3Segment("+name")).getLiteral().getData();

		System.out.println("=alice's name: " + aliceName);

		// don't forget to close the graph

		graph.close();
	}
}

Back to the top