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 1

Revision as of 16:21, 19 February 2008 by Unnamed Poltroon (Talk) (New page: This example app shows how to perform basic graph operations and how to serialize a graph. <pre> package org.eclipse.higgins.xdi4j.tutorial; import org.eclipse.higgins.xdi4j.Graph; impor...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This example app shows how to perform basic graph operations and how to serialize a graph.

package org.eclipse.higgins.xdi4j.tutorial;

import org.eclipse.higgins.xdi4j.Graph;
import org.eclipse.higgins.xdi4j.GraphFactory;
import org.eclipse.higgins.xdi4j.Predicate;
import org.eclipse.higgins.xdi4j.Subject;
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.XRI3Segment;

/**
 * This example app shows how to perform basic graph operations
 * and how to serialize a graph.
 * 
 * @author msabadello at parityinc dot not
 *
 */
public class Tutorial1 {

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

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

		// there are basically two ways of working with graphs

		// 1) work with subjects, predicates, references and literals

		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"));

		// 2) work with statements

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

		// after our graph is filled, we can output it

		XDIWriter writer = XDIWriterRegistry.forFormat("X3D");
		writer.write(graph, System.out, null);

		graph.close();
	}
}

Back to the top