Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "XDI4j Tutorial 5"
Line 58: | Line 58: | ||
// or we can output the whole statement the literal is a part of | // or we can output the whole statement the literal is a part of | ||
− | XDIWriter writer = XDIWriterRegistry.forFormat(" | + | XDIWriter writer = XDIWriterRegistry.forFormat("X3 Simple"); |
writer.write(literal.getStatement(), System.out, null); | writer.write(literal.getStatement(), System.out, null); | ||
Revision as of 11:50, 1 September 2009
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}
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("X3 Simple"); 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); } }