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"

 
Line 1: Line 1:
 
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}
 
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}
 
[[Image:Higgins_logo_76Wx100H.jpg|right]]
 
[[Image:Higgins_logo_76Wx100H.jpg|right]]
== Example ==
 
  
This example app shows how addressing works in XDI graphs.
+
This tutorial explains how XDI Addressing can be used to navigate an XDI graph.
 +
 
 +
Every subject, predicate, reference and literals has an address inside the graph. Addresses are expressed as XRIs.
 +
 
 +
XDI Addressing therefore provides a convenient way for pointing at any point (e.g. literal value) inside an XDI graph.
 +
 
 +
== XDI Addressing ==
 +
 
 +
'''Example Code''':
  
 
<pre>
 
<pre>
 
package org.eclipse.higgins.xdi4j.tutorial;
 
package org.eclipse.higgins.xdi4j.tutorial;
 +
 +
import java.io.File;
 +
import java.io.FileInputStream;
  
 
import org.eclipse.higgins.xdi4j.Graph;
 
import org.eclipse.higgins.xdi4j.Graph;
 
import org.eclipse.higgins.xdi4j.GraphFactory;
 
import org.eclipse.higgins.xdi4j.GraphFactory;
 
import org.eclipse.higgins.xdi4j.Literal;
 
import org.eclipse.higgins.xdi4j.Literal;
import org.eclipse.higgins.xdi4j.Predicate;
+
import org.eclipse.higgins.xdi4j.Statement;
import org.eclipse.higgins.xdi4j.Subject;
+
 
import org.eclipse.higgins.xdi4j.addressing.Addressing;
 
import org.eclipse.higgins.xdi4j.addressing.Addressing;
 
import org.eclipse.higgins.xdi4j.impl.memory.MemoryGraphFactory;
 
import org.eclipse.higgins.xdi4j.impl.memory.MemoryGraphFactory;
 +
import org.eclipse.higgins.xdi4j.io.XDIReader;
 +
import org.eclipse.higgins.xdi4j.io.XDIReaderRegistry;
 
import org.eclipse.higgins.xdi4j.io.XDIWriter;
 
import org.eclipse.higgins.xdi4j.io.XDIWriter;
 
import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry;
 
import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry;
 
import org.eclipse.higgins.xdi4j.xri3.impl.XRI3;
 
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
+
  * This example app shows how addressing works in XDI graphs.
*
+
* @author msabadello at parityinc dot not
+
*
+
 
  */
 
  */
 
public class Tutorial5 {
 
public class Tutorial5 {
Line 33: Line 40:
 
Graph graph = factory.openGraph();
 
Graph graph = factory.openGraph();
  
// let's create a basic graph (the same one as in tutorial 1)
+
// read a graph from file
  
Subject markus = graph.createSubject(new XRI3Segment("=markus"));
+
File file = new File("test.graph");
 +
XDIReader reader = XDIReaderRegistry.getAuto();
  
Predicate name = markus.createPredicate(new XRI3Segment("+name"));
+
reader.read(graph, new FileInputStream(file), null);
Predicate friend = markus.createPredicate(new XRI3Segment("+friend"));
+
  
name.createLiteral("Markus");
+
// now using XDI addressing, we can find e.g. a literal
friend.createReference(new XRI3Segment("=drummond"));
+
  
graph.createStatement(new XRI3Segment("=markus"), new XRI3Segment("+friend"), new XRI3Segment("=giovanni"));
+
XRI3 address = new XRI3("=alice/+name");
graph.createStatement(new XRI3Segment("=giovanni"), new XRI3Segment("+friend"), new XRI3Segment("=drummond"));
+
Literal literal = (Literal) Addressing.findByAddress(graph, address)[0];
  
// now using XDI addressing, we can find e.g. a literal by providing an XRI
+
System.out.println("Literal value: " + literal.getData());
  
XRI3 address = new XRI3("=markus/+name");
+
// or even simpler, we can get the literal data directly
  
Literal literal = (Literal) Addressing.findByAddress(graph, address)[0];
+
String literalData = Addressing.findLiteralData(graph, address);
  
// we can simply output the value of that literal
+
System.out.println("Literal value: " + literalData);
  
System.out.println("Literal value: " + literal.getData());
+
// we can print the whole XDI statement to which the literal belongs
  
// or we can output the whole statement the literal is a part of
+
Statement statement = literal.getStatement();
  
 
XDIWriter writer = XDIWriterRegistry.forFormat("X3 Simple");
 
XDIWriter writer = XDIWriterRegistry.forFormat("X3 Simple");
writer.write(literal.getStatement(), System.out, null);
+
writer.write(statement, System.out, null);
+
 
 
// another thing we can do is get the address of any given graph component
 
// another thing we can do is get the address of any given graph component
+
 
address = Addressing.getAddress(literal);
+
address = Addressing.getAddress(literal, false);
 
System.out.println("Address: " + address);
 
System.out.println("Address: " + address);
 
}
 
}

Latest revision as of 08:21, 29 January 2010

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

Higgins logo 76Wx100H.jpg

This tutorial explains how XDI Addressing can be used to navigate an XDI graph.

Every subject, predicate, reference and literals has an address inside the graph. Addresses are expressed as XRIs.

XDI Addressing therefore provides a convenient way for pointing at any point (e.g. literal value) inside an XDI graph.

XDI Addressing

Example Code:

package org.eclipse.higgins.xdi4j.tutorial;

import java.io.File;
import java.io.FileInputStream;

import org.eclipse.higgins.xdi4j.Graph;
import org.eclipse.higgins.xdi4j.GraphFactory;
import org.eclipse.higgins.xdi4j.Literal;
import org.eclipse.higgins.xdi4j.Statement;
import org.eclipse.higgins.xdi4j.addressing.Addressing;
import org.eclipse.higgins.xdi4j.impl.memory.MemoryGraphFactory;
import org.eclipse.higgins.xdi4j.io.XDIReader;
import org.eclipse.higgins.xdi4j.io.XDIReaderRegistry;
import org.eclipse.higgins.xdi4j.io.XDIWriter;
import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry;
import org.eclipse.higgins.xdi4j.xri3.impl.XRI3;

/**
 * This example app shows how addressing works in XDI graphs.
 */
public class Tutorial5 {

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

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

		// read a graph from file

		File file = new File("test.graph");
		XDIReader reader = XDIReaderRegistry.getAuto();

		reader.read(graph, new FileInputStream(file), null);

		// now using XDI addressing, we can find e.g. a literal

		XRI3 address = new XRI3("=alice/+name");
		Literal literal = (Literal) Addressing.findByAddress(graph, address)[0];

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

		// or even simpler, we can get the literal data directly

		String literalData = Addressing.findLiteralData(graph, address);

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

		// we can print the whole XDI statement to which the literal belongs

		Statement statement = literal.getStatement();

		XDIWriter writer = XDIWriterRegistry.forFormat("X3 Simple");
		writer.write(statement, System.out, null);

		// another thing we can do is get the address of any given graph component

		address = Addressing.getAddress(literal, false);
		System.out.println("Address: " + address);
	}
}

Back to the top