Skip to main content
Jump to: navigation, search

Difference between revisions of "XDI4j Tutorial 5"

m (logo, category, left menu)
Line 1: Line 1:
== Example ==
== Example ==

Revision as of 11:03, 16 December 2008


Higgins logo 76Wx100H.jpg


This example app shows how addressing works in XDI graphs.

package org.eclipse.higgins.xdi4j.tutorial;

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

 * This example app shows how addressing works in XDI graphs
 * @author msabadello at parityinc dot not
public class Tutorial5 {

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

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

		// let's create a basic graph (the same one as in tutorial 1)

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

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

		friend.createReference(new XRI3Segment("=drummond"));

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

		// now using XDI addressing, we can find e.g. a literal by providing an XRI

		XRI3 address = new XRI3("=markus/+name");

		Literal literal = (Literal) Addressing.findGraphComponent(graph, address);

		// we can simply output the value of that literal

		System.out.println("Literal value: " + literal.getData());

		// or we can output the whole statement the literal is a part of

		XDIWriter writer = XDIWriterRegistry.forFormat("X3D");
		writer.write(literal.getStatement(), System.out, null);
		// another thing we can do is get the address of any given graph component
		address = Addressing.buildAddress(literal);
		System.out.println("Address: " + address);

Back to the top