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