org.mulgara.mrg
Class AbstractGraph

java.lang.Object
  extended by org.mulgara.mrg.AbstractGraphExt
      extended by org.mulgara.mrg.AbstractGraph
All Implemented Interfaces:
AppendableGraph, Graph, GraphExt, WritableGraph
Direct Known Subclasses:
GraphImpl, IndexedGraph, PackedGraphImpl

public abstract class AbstractGraph
extends AbstractGraphExt
implements Graph, WritableGraph

An RDF graph. This uses a single index by default, and the indexes are built out of maps and collections that are returned by the abstract methods createMap and createCollection respectively. To implement this graph, simply define these methods to return appropriate Map and Collection implementations. Extra indexes may also be added, which will allow getObjects and getPredicates to be more efficiently implemented. An example of this is shown in IndexedGraph. TODO: test objects on insertion to avoid storing duplicate nodes


Field Summary
 
Fields inherited from interface org.mulgara.mrg.Graph
X
 
Constructor Summary
AbstractGraph()
          Default constructor for an empty graph.
AbstractGraph(Collection<Triple> triples)
          Builds a graph with a collection of triples.
 
Method Summary
protected abstract
<X> Collection<X>
createCollection()
          Defines the collection type to use in this implementation.
protected abstract
<X,Y> Map<X,Y>
createMap()
          Defines the map type to use in this implementation.
 boolean delete(SubjectNode s, PredicateNode p, ObjectNode o)
          Remove a triple from the graph.
 boolean delete(Triple triple)
          Remove a triple from the graph.
 boolean doesResourceExist(Node r)
          Tests if a resource exists anywhere in the graph.
 void exportN3(OutputStream out)
          Writes the contents of the graph to an output stream as N3.
 void exportN3(OutputStream out, URI base)
          Writes the contents of the graph to an output stream as N3.
 void exportXML(OutputStream out)
          Writes the contents of the graph to an output stream as RDF/XML.
 void exportXML(OutputStream out, URI base)
          Writes the contents of the graph to an output stream as RDF/XML.
 Collection<ObjectNode> getObjects()
          Gets all the objects in the graph.
 Collection<PredicateNode> getPredicates()
          Gets all the predicates in the graph.
 List<PropertyValue> getProperties(SubjectNode s)
          Gets all the properties for a given subject.
 List<Pair<PredicateNode,ObjectNode>> getRawProperties(SubjectNode s)
          Gets all the properties for a given subject as raw pairs.
 List<ObjectNode> getRdfList(SubjectNode s, PredicateNode p)
          Gets an rdf:List property from an object.
 Collection<SubjectNode> getSubjects()
          Gets all the subjects in the graph.
 List<SubjectNode> getSubjects(PredicateNode property, ObjectNode value)
          Gets all the subjects that share a given property/value.
 List<Triple> getTriples()
          Gets the entire graph as a list of triples.
 ObjectNode getValue(SubjectNode s, PredicateNode p)
          Gets a single value for a given property on a subject.
 List<ObjectNode> getValues(SubjectNode s, PredicateNode p)
          Gets all the values for a given property on a subject.
 boolean insert(SubjectNode s, PredicateNode p, ObjectNode o)
          Add a new triple to the graph.
 boolean insert(Triple triple)
          Add a new triple to the graph.
 boolean isAsserted(SubjectNode s, PredicateNode p, ObjectNode o)
          Tests if a triple has been asserted.
 boolean isAsserted(Triple t)
          Tests if a triple has been asserted.
 boolean isEmpty()
          Tests if the graph has any entries.
 Iterator<Triple> match(SubjectNode s, PredicateNode p, ObjectNode o)
          Find all triples that match a given pattern.
 Graph matchSubgraph(SubjectNode s, PredicateNode p, ObjectNode o)
          Returns a subgraph of this graph that only contains the triples that match a given pattern.
 Graph mergeInto(Graph g)
          Adds the contents of another graph to this one, creating new blank nodes as required (not re-using the blank nodes from the original graph).
 long size()
          Gets the number of triples in this graph.
 Graph unionInto(Graph g)
          Adds the contents of another graph to this one, re-using all the blank nodes from the new graph.
 
Methods inherited from class org.mulgara.mrg.AbstractGraphExt
doesResourceExist, doesResourceExist, getProperties, getProperties, getRdfList, getRdfList, getRdfList, getRdfList, getRdfList, getRdfList, getRdfList, getRdfList, getSubjects, getSubjects, getSubjects, getSubjects, getSubjects, getSubjects, getSubjects, getSubjects, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValues, getValues, getValues, getValues, getValues, getValues, getValues, getValues, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.mulgara.mrg.GraphExt
doesResourceExist, doesResourceExist, getProperties, getProperties, getRdfList, getRdfList, getRdfList, getRdfList, getRdfList, getRdfList, getRdfList, getRdfList, getSubjects, getSubjects, getSubjects, getSubjects, getSubjects, getSubjects, getSubjects, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValues, getValues, getValues, getValues, getValues, getValues, getValues, getValues, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, isAsserted, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchSubgraphX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX, matchX
 

Constructor Detail

AbstractGraph

public AbstractGraph()
Default constructor for an empty graph.


AbstractGraph

public AbstractGraph(Collection<Triple> triples)
Builds a graph with a collection of triples.

Parameters:
triples - The initial triples for the graph.
Method Detail

insert

public boolean insert(SubjectNode s,
                      PredicateNode p,
                      ObjectNode o)
Add a new triple to the graph.

Specified by:
insert in interface AppendableGraph
Parameters:
s - The subject of the triple.
p - The predicate of the triple.
o - The object of the triple.
Returns:
true if the triple was already in the graph.

insert

public boolean insert(Triple triple)
Add a new triple to the graph.

Specified by:
insert in interface AppendableGraph
Parameters:
triple - The complete triple to add.
Returns:
true if the triple was already in the graph.

mergeInto

public Graph mergeInto(Graph g)
Adds the contents of another graph to this one, creating new blank nodes as required (not re-using the blank nodes from the original graph).

Specified by:
mergeInto in interface AppendableGraph
Parameters:
g - The graph to merge into this one.
Returns:
The current graph, after modification.

unionInto

public Graph unionInto(Graph g)
Adds the contents of another graph to this one, re-using all the blank nodes from the new graph.

Specified by:
unionInto in interface AppendableGraph
Parameters:
g - The graph to merge into this one.
Returns:
The current graph, after modification.

delete

public boolean delete(SubjectNode s,
                      PredicateNode p,
                      ObjectNode o)
Remove a triple from the graph.

Specified by:
delete in interface WritableGraph
Parameters:
s - The subject of the triple.
p - The predicate of the triple.
o - The object of the triple.
Returns:
true if the triple was already in the graph.

delete

public boolean delete(Triple triple)
Remove a triple from the graph.

Specified by:
delete in interface WritableGraph
Parameters:
triple - The complete triple to remove.
Returns:
true if the triple was already in the graph.

getProperties

public List<PropertyValue> getProperties(SubjectNode s)
Gets all the properties for a given subject.

Specified by:
getProperties in interface Graph
Parameters:
s - The subject.
Returns:
A list of property/value pairs.

isAsserted

public boolean isAsserted(Triple t)
Tests if a triple has been asserted. Be careful of blank nodes, as they will only match if they are exactly alike.

Specified by:
isAsserted in interface Graph
Parameters:
t - The triple to test for.
Returns:
true only if the triple exists in the graph.

isAsserted

public boolean isAsserted(SubjectNode s,
                          PredicateNode p,
                          ObjectNode o)
Tests if a triple has been asserted. Be careful of blank nodes, as they will only match if they are exactly alike.

Specified by:
isAsserted in interface Graph
Parameters:
s - The subject of the triple to search for.
p - The predicate of the triple to search for.
o - The object of the triple to search for.
Returns:
true only if the triple exists in the graph.

getRawProperties

public List<Pair<PredicateNode,ObjectNode>> getRawProperties(SubjectNode s)
Gets all the properties for a given subject as raw pairs.

Parameters:
s - The subject.
Returns:
A list of property/value pairs.

getValues

public List<ObjectNode> getValues(SubjectNode s,
                                  PredicateNode p)
Gets all the values for a given property on a subject.

Specified by:
getValues in interface Graph
Parameters:
s - The subject to get the properties for.
p - The property of interest.
Returns:
The list of values for the property on that subject.

getValue

public ObjectNode getValue(SubjectNode s,
                           PredicateNode p)
Gets a single value for a given property on a subject.

Specified by:
getValue in interface Graph
Parameters:
s - The subject to get the properties for.
p - The property of interest.
Returns:
The first values for the property on that subject.

getRdfList

public List<ObjectNode> getRdfList(SubjectNode s,
                                   PredicateNode p)
Gets an rdf:List property from an object. If more than one value exists for this property, then returns the first and assumes it's a list.

Specified by:
getRdfList in interface Graph
Parameters:
s - The subject to get the property for.
p - The property of interest.
Returns:
The list associates with the property on that subject.

getSubjects

public List<SubjectNode> getSubjects(PredicateNode property,
                                     ObjectNode value)
Gets all the subjects that share a given property/value. This method accumulates the subjects, though a graph with more complete indexes could just look it up.

Specified by:
getSubjects in interface Graph
Parameters:
property - The property being looked for.
value - The value being looked for.
Returns:
The subjects that have the value for the property.

doesResourceExist

public boolean doesResourceExist(Node r)
Tests if a resource exists anywhere in the graph.

Specified by:
doesResourceExist in interface Graph
Parameters:
r - The resource to test.
Returns:
true only if the resource is used somewhere in the graph.

getSubjects

public Collection<SubjectNode> getSubjects()
Gets all the subjects in the graph.

Specified by:
getSubjects in interface Graph
Returns:
All the subjects in the graph.

getPredicates

public Collection<PredicateNode> getPredicates()
Gets all the predicates in the graph.

Specified by:
getPredicates in interface Graph
Returns:
All the predicatess in the graph.

getObjects

public Collection<ObjectNode> getObjects()
Gets all the objects in the graph.

Specified by:
getObjects in interface Graph
Returns:
All the objects in the graph.

exportN3

public void exportN3(OutputStream out)
              throws IOException
Writes the contents of the graph to an output stream as N3.

Specified by:
exportN3 in interface Graph
Parameters:
out - The stream to write to.
Throws:
IOException

exportN3

public void exportN3(OutputStream out,
                     URI base)
              throws IOException
Writes the contents of the graph to an output stream as N3.

Specified by:
exportN3 in interface Graph
Parameters:
out - The stream to write to.
base - The base to write to.
Throws:
IOException

exportXML

public void exportXML(OutputStream out)
               throws IOException
Writes the contents of the graph to an output stream as RDF/XML.

Specified by:
exportXML in interface Graph
Parameters:
out - The stream to write to.
Throws:
IOException

exportXML

public void exportXML(OutputStream out,
                      URI base)
               throws IOException
Writes the contents of the graph to an output stream as RDF/XML.

Specified by:
exportXML in interface Graph
Parameters:
out - The stream to write to.
base - The base to write to.
Throws:
IOException

getTriples

public List<Triple> getTriples()
Gets the entire graph as a list of triples.

Specified by:
getTriples in interface Graph
Returns:
All the triples in the graph.

match

public Iterator<Triple> match(SubjectNode s,
                              PredicateNode p,
                              ObjectNode o)
Find all triples that match a given pattern. The resulting iterator is live, and can lead to a ConcurrentModificationException if the graph is modified. If this is a possibility, then use matchSubgraph(SubjectNode, PredicateNode, ObjectNode) instead.

Specified by:
match in interface Graph
Parameters:
s - The subject of the triples to match. If null that all subjects match.
p - The predicate of the triple to add. If null that all predicates match.
o - The object of the triple to add. If null that all objects match.
Returns:
An Iterator on the matching triples.

matchSubgraph

public Graph matchSubgraph(SubjectNode s,
                           PredicateNode p,
                           ObjectNode o)
Returns a subgraph of this graph that only contains the triples that match a given pattern. This method copies all of the required triples. To avoid the overhead of this operation, use match(SubjectNode, PredicateNode, ObjectNode) instead.

Specified by:
matchSubgraph in interface Graph
Parameters:
s - The subject of the triples to match. If null that all subjects match.
p - The predicate of the triple to add. If null that all predicates match.
o - The object of the triple to add. If null that all objects match.
Returns:
A Graph containing only the matching triples.

size

public long size()
Gets the number of triples in this graph.

Specified by:
size in interface Graph
Returns:
the number of triples in the graph.

isEmpty

public boolean isEmpty()
Tests if the graph has any entries.

Specified by:
isEmpty in interface Graph
Returns:
true if there are no entries.

createMap

protected abstract <X,Y> Map<X,Y> createMap()
Defines the map type to use in this implementation.

Returns:
an instance of a Map.

createCollection

protected abstract <X> Collection<X> createCollection()
Defines the collection type to use in this implementation.

Returns:
an instance of a Collection.


Copyright © 2011. All Rights Reserved.