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 5"

m (logo, category, left menu)
Line 1: Line 1:
{{#eclipseproject:technology.higgins}}
+
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}
 
[[Image:Higgins_logo_76Wx100H.jpg|right]]
 
[[Image:Higgins_logo_76Wx100H.jpg|right]]
 
== Example ==
 
== Example ==

Revision as of 11:03, 16 December 2008

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

Higgins logo 76Wx100H.jpg

Example

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.io.XDIWriter;
import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry;
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"));

		name.createLiteral("Markus");
		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