Skip to main content

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.

Jump to: navigation, search

XDI4j Tutorial 5

{{#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