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"

(New page: This example app shows how to perform basic graph operations and how to serialize a graph. <pre> package org.eclipse.higgins.xdi4j.tutorial; import org.eclipse.higgins.xdi4j.Graph; impor...)
 
Line 1: Line 1:
This example app shows how to perform basic graph operations and how to serialize a graph.
+
== Interfaces and classes ==
 +
 
 +
=== Graph ===
 +
 
 +
  org.eclipse.higgins.xdi4j.Graph;
 +
 
 +
This interface represents a whole XDI graph.
 +
XDI graphs consist of subjects and statements between them.
 +
Operations on the graph include creating new statements and subjects, 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 statement never has BOTH an reference and a literal.
 +
 
 +
=== 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.
 +
 
 +
== Example ==
 +
 
 +
This example app shows how to create a new XDI document and how to perform basic graph operations on it.
  
 
<pre>
 
<pre>
Line 14: Line 73:
  
 
/**
 
/**
  * This example app shows how to perform basic graph operations
+
  * This example app shows how to create a new XDI
  * and how to serialize a graph.
+
  * document and how to perform basic graph operations on it.
 
  *  
 
  *  
 
  * @author msabadello at parityinc dot not
 
  * @author msabadello at parityinc dot not
Line 46: Line 105:
 
// after our graph is filled, we can output it
 
// after our graph is filled, we can output it
  
XDIWriter writer = XDIWriterRegistry.forFormat("X3D");
+
System.out.println(graph.toString());
writer.write(graph, System.out, null);
+
  
 
graph.close();
 
graph.close();

Revision as of 16:31, 19 February 2008

Interfaces and classes

Graph

 org.eclipse.higgins.xdi4j.Graph;

This interface represents a whole XDI graph. XDI graphs consist of subjects and statements between them. Operations on the graph include creating new statements and subjects, 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 statement never has BOTH an reference and a literal.

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.

Example

This example app shows how to create a new XDI document and how to perform basic graph operations on it.

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.io.XDIWriter;
import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry;
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.
 * 
 * @author msabadello at parityinc dot not
 *
 */
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("=markus"));

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

		name.createLiteral("Markus");
		friend.createReference(new XRI3Segment("=drummond"));

		// 2) work with statements

		graph.createStatement(new XRI3Segment("=markus"), new XRI3Segment("+friend"), new XRI3Segment("=giovanni"));
		graph.createStatement(new XRI3Segment("=giovanni"), new XRI3Segment("+friend"), new XRI3Segment("=drummond"));

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

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

		graph.close();
	}
}

Back to the top