Skip to content
peacekeeper edited this page Feb 26, 2013 · 42 revisions

Implementation of the XDI graph model and several higher-level features [.jar]

Topics

Maven Dependency

<dependency>
	<groupId>xdi2</groupId>
	<artifactId>xdi2-core</artifactId>
	<version>0.1-SNAPSHOT</version>
	<scope>compile</scope>
</dependency>

Quick Example

package xdi2.samples.core;

import xdi2.core.ContextNode;
import xdi2.core.Graph;
import xdi2.core.Literal;
import xdi2.core.Relation;
import xdi2.core.impl.memory.MemoryGraphFactory;
import xdi2.core.io.XDIWriterRegistry;
import xdi2.core.xri3.impl.XDI3Segment;
import xdi2.core.xri3.impl.XDI3Statement;
import xdi2.core.xri3.impl.XDI3SubSegment;

public class SimpleCoreSample {

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

		// create a simple graph with context nodes, a relation, and a literal

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

		ContextNode root = graph.getRootContextNode();
		ContextNode markus = root.createContextNode(new XDI3SubSegment("=markus"));
		ContextNode animesh = root.createContextNode(new XDI3SubSegment("=animesh"));
		ContextNode name = markus.createContextNode(new XDI3SubSegment("+name"));
		Relation relation = markus.createRelation(new XDI3Segment("+friend"), animesh);
		Literal literal = name.createLiteral("Markus Sabadello");

		// write some statements from our graph

		System.out.println("Statement associated with a context node: " + markus.getStatement());
		System.out.println("Statement associated with a relation: " + relation.getStatement());
		System.out.println("Statement associated with a literal: " + literal.getStatement());

		// we can also add a whole new statement to the graph

		graph.createStatement(new XDI3Statement("=alice/+friend/=bob"));

		// write the whole graph in different serialization formats

		System.out.println("Serialization in XDI/JSON: \n");
		XDIWriterRegistry.forFormat("XDI/JSON", null).write(graph, System.out);
		System.out.println();

		System.out.println("Serialization in XDI statements:\n");
		XDIWriterRegistry.forFormat("XDI DISPLAY", null).write(graph, System.out);

		// close the graph

		graph.close();
	}
}
Clone this wiki locally