Previous Topic

Next Topic

Searching the Graph

You can search a graph using it's find(Triple triple) method. The nodes in the triple are used to match the nodes in the graph. Any null nodes in the triple are treated as wildcards and match to any node in the graph.

Triples are returned in the form of a ClosableIterator, which extends Iterator and has a close() method that is used to free resources. Calling next() on a ClosableIterator returns a triple object. Triples are not guaranteed to be returned in any specific order.

The following code examples demonstrate using the find(Triple triple) method to search a graph.

To search for all triples:

//get all Triples 
Triple findAll = elementFactory.createTriple(null, null, null); 
ClosableIterator allTriples = graph.find(findAll);

This returns:

http://example.org/staffid#85740  http://example.org/terms#address     _blankNode123
_blankNode123                     http://example.org/terms#street      "1501 Grant Avenue"
_blankNode123                     http://example.org/terms#city        "Bedford"
_blankNode123                     http://example.org/terms#state       "Massachusetts"
_blankNode123                     http://example.org/terms#postalCode  "01730"

To search for all addresses:

//search for address (as a subject)
Triple findAddress = elementFactory.createTriple(address, null, null);
ClosableIterator addressSubject = graph.find(findAddress);

This returns:

_blankNode123  http://example.org/terms#street      "1501 Grant Avenue"
_blankNode123  http://example.org/terms#city        "Bedford"
_blankNode123  http://example.org/terms#state       "Massachusetts"
_blankNode123  http://example.org/terms#postalCode  "01730"

To search for a city:

//search for the city: "Bedford"
Triple findCity = elementFactory.createTriple(null, null, city);
ClosableIterator bedfordCity = graph.find(findCity);

This returns:

_blankNode123  http://example.org/terms#city  "Bedford"

To search for subjects with an address property:

//search for any subject that has an address
Triple findAddresses = elementFactory.createTriple(null, hasAddress, null);
ClosableIterator addresses = graph.find(findAddresses);

This returns:

http://example.org/staffid#85740  http://example.org/terms#address     _blankNode123

See Also

JRDF Tutorial

Prerequisites

Obtaining a Session from a Mulgara Server

Comparison of JRDF Graph Implementations

Creating the Graph

Creating Nodes and Triples

Adding Triples to the Graph

Reify Triples

Removing Triples from the Graph

Open Source logo

© 2001-2004 Tucana Technologies, Inc. Some rights reserved.

© 2006 The Mulgara Project. Some rights reserved.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".