Class TreeModel
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<Statement>
-
- org.eclipse.rdf4j.model.impl.AbstractModel
-
- org.eclipse.rdf4j.model.impl.TreeModel
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<Statement>
,java.util.Collection<Statement>
,java.util.Set<Statement>
,java.util.SortedSet<Statement>
,Model
,NamespaceAware
public class TreeModel extends AbstractModel implements java.util.SortedSet<Statement>
A Red-Black tree basedModel
implementation. The model is sorted according to the lexical ordering of terms.This implementation provides guaranteed log(n) time cost for filtered access by any number of terms. If an index is not yet available for a set of positions, it is created at runtime using a
TreeSet
.Note that this implementation is not synchronized. If multiple threads access a model concurrently, even if all of them are read operations, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the model. If no such object exists, the set should be "wrapped" using the Models.synchronizedModel method.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
TreeModel.GraphComparator
private class
TreeModel.ModelIterator
(package private) class
TreeModel.ObjectComparator
(package private) class
TreeModel.PredicateComparator
(package private) static class
TreeModel.StatementComparator
(package private) class
TreeModel.StatementTree
(package private) class
TreeModel.SubjectComparator
(package private) static class
TreeModel.SubSet
(package private) static class
TreeModel.TreeStatement
-
Field Summary
Fields Modifier and Type Field Description (package private) static IRI
AFTER
(package private) static IRI
BEFORE
(package private) java.util.Set<Namespace>
namespaces
private static long
serialVersionUID
(package private) java.util.List<TreeModel.StatementTree>
trees
private LexicalValueComparator
vc
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(Resource subj, IRI pred, Value obj, Resource... contexts)
Adds one or more statements to the model.private Statement
after(Value subj, Value pred, Value obj, Value ctx)
private Statement
before(Value subj, Value pred, Value obj, Value ctx)
Statement
ceiling(Statement e)
private TreeModel.StatementTree
choose(Value subj, Value pred, Value obj, Value ctx)
void
clear()
java.util.Comparator<? super Statement>
comparator()
(package private) int
compareValue(Value o1, Value o2)
boolean
contains(Resource subj, IRI pred, Value obj, Resource... contexts)
Determines if statements with the specified subject, predicate, object and (optionally) context exist in this model.Model
filter(Resource subj, IRI pred, Value obj, Resource... contexts)
Returns a filtered view of the statements with the specified subject, predicate, object and (optionally) context.Statement
first()
Statement
floor(Statement e)
java.util.Optional<Namespace>
getNamespace(java.lang.String prefix)
Gets the namespace that is associated with the specified prefix, if any.java.util.Set<Namespace>
getNamespaces()
Gets the set that contains the assigned namespaces.java.util.SortedSet<Statement>
headSet(Statement toElement)
Statement
higher(Statement e)
private TreeModel.StatementTree
index(Value subj, Value pred, Value obj, Value ctx)
boolean
isEmpty()
private boolean
isResourceURIResource(Value subj, Value pred, Value ctx)
java.util.Iterator<Statement>
iterator()
Statement
last()
Statement
lower(Statement e)
(package private) java.util.Iterator<Statement>
matchPattern(Resource subj, IRI pred, Value obj, Resource ctx)
private Value[]
notEmpty(Value[] contexts)
Statement
pollFirst()
Statement
pollLast()
boolean
remove(Resource subj, IRI pred, Value obj, Resource... contexts)
Removes statements with the specified subject, predicate, object and (optionally) context exist in this model.private void
removeAll(java.util.TreeSet<Statement> owner, TreeModel.StatementTree chosen, java.util.Iterator<Statement> iter)
java.util.Optional<Namespace>
removeNamespace(java.lang.String prefix)
Removes a namespace declaration by removing the association between a prefix and a namespace name.void
removeTermIteration(java.util.Iterator<Statement> iterator, Resource subj, IRI pred, Value obj, Resource... contexts)
Called by aggregate sets when a term has been removed from a term iterator.Namespace
setNamespace(java.lang.String prefix, java.lang.String name)
Sets the prefix for a namespace.void
setNamespace(Namespace namespace)
Sets the prefix for a namespace.int
size()
(package private) java.util.SortedSet<Statement>
subSet(Statement lo, boolean loInclusive, Statement hi, boolean hiInclusive)
java.util.SortedSet<Statement>
subSet(Statement fromElement, Statement toElement)
java.util.SortedSet<Statement>
tailSet(Statement fromElement)
-
Methods inherited from class org.eclipse.rdf4j.model.impl.AbstractModel
add, addAll, clear, closeIterator, contains, containsAll, contexts, objects, predicates, remove, removeAll, retainAll, subjects, toArray, toArray, unmodifiable
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.eclipse.rdf4j.model.Model
getStatements
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
BEFORE
static final IRI BEFORE
-
AFTER
static final IRI AFTER
-
vc
private final LexicalValueComparator vc
-
namespaces
final java.util.Set<Namespace> namespaces
-
trees
final java.util.List<TreeModel.StatementTree> trees
-
-
Constructor Detail
-
TreeModel
public TreeModel()
-
TreeModel
public TreeModel(Model model)
-
TreeModel
public TreeModel(java.util.Collection<? extends Statement> c)
-
TreeModel
public TreeModel(java.util.Set<Namespace> namespaces, java.util.Collection<? extends Statement> c)
-
TreeModel
public TreeModel(java.util.Set<Namespace> namespaces)
-
-
Method Detail
-
getNamespace
public java.util.Optional<Namespace> getNamespace(java.lang.String prefix)
Description copied from interface:NamespaceAware
Gets the namespace that is associated with the specified prefix, if any. If multiple namespaces match the given prefix, the result may not be consistent over successive calls to this method.- Specified by:
getNamespace
in interfaceNamespaceAware
- Parameters:
prefix
- A namespace prefix.- Returns:
- The namespace name that is associated with the specified prefix, or
Optional.empty()
if there is no such namespace.
-
getNamespaces
public java.util.Set<Namespace> getNamespaces()
Description copied from interface:NamespaceAware
Gets the set that contains the assigned namespaces.- Specified by:
getNamespaces
in interfaceNamespaceAware
- Returns:
- A
Set
containing theNamespace
objects that are available.
-
setNamespace
public Namespace setNamespace(java.lang.String prefix, java.lang.String name)
Description copied from interface:Model
Sets the prefix for a namespace. This will replace any existing namespace associated to the prefix.- Specified by:
setNamespace
in interfaceModel
- Parameters:
prefix
- The new prefix.name
- The namespace name that the prefix maps to.- Returns:
- The
Namespace
object for the given namespace.
-
setNamespace
public void setNamespace(Namespace namespace)
Description copied from interface:Model
Sets the prefix for a namespace. This will replace any existing namespace associated to the prefix.- Specified by:
setNamespace
in interfaceModel
- Parameters:
namespace
- ANamespace
object to use in this Model.
-
removeNamespace
public java.util.Optional<Namespace> removeNamespace(java.lang.String prefix)
Description copied from interface:Model
Removes a namespace declaration by removing the association between a prefix and a namespace name.- Specified by:
removeNamespace
in interfaceModel
- Parameters:
prefix
- The namespace prefix of which the assocation with a namespace name is to be removed.- Returns:
- the previous namespace bound to the prefix or
Optional.empty()
-
size
public int size()
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Collection<Statement>
- Specified by:
clear
in interfacejava.util.Set<Statement>
- Overrides:
clear
in classAbstractModel
-
comparator
public java.util.Comparator<? super Statement> comparator()
- Specified by:
comparator
in interfacejava.util.SortedSet<Statement>
-
pollFirst
public Statement pollFirst()
-
pollLast
public Statement pollLast()
-
subSet
public java.util.SortedSet<Statement> subSet(Statement fromElement, Statement toElement)
- Specified by:
subSet
in interfacejava.util.SortedSet<Statement>
-
headSet
public java.util.SortedSet<Statement> headSet(Statement toElement)
- Specified by:
headSet
in interfacejava.util.SortedSet<Statement>
-
tailSet
public java.util.SortedSet<Statement> tailSet(Statement fromElement)
- Specified by:
tailSet
in interfacejava.util.SortedSet<Statement>
-
add
public boolean add(Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from interface:Model
Adds one or more statements to the model. This method creates a statement for each specified context and adds those to the model. If no contexts are specified, a single statement with no associated context is added. If this Model is a filtered Model then null (if context empty) values are permitted and will use the corresponding filtered values.
-
contains
public boolean contains(Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from interface:Model
Determines if statements with the specified subject, predicate, object and (optionally) context exist in this model. Thesubject
,predicate
andobject
parameters can benull
to indicate wildcards. Thecontexts
parameter is a wildcard and accepts zero or more values. If no contexts are specified, statements will match disregarding their context. If one or more contexts are specified, statements with a context matching one of these will match. Note: to match statements without an associated context, specify the valuenull
and explicitly cast it to typeResource
.Examples:
model.contains(s1, null, null)
is true if any statements in this model have subjects1
,
model.contains(null, null, null, c1)
is true if any statements in this model have contextc1
,
model.contains(null, null, null, (Resource)null)
is true if any statements in this model have no associated context,
model.contains(null, null, null, c1, c2, c3)
is true if any statements in this model have contextc1
,c2
orc3
.- Specified by:
contains
in interfaceModel
- Parameters:
subj
- The subject of the statements to match,null
to match statements with any subject.pred
- The predicate of the statements to match,null
to match statements with any predicate.obj
- The object of the statements to match,null
to match statements with any object.contexts
- The contexts of the statements to match. If no contexts are specified, statements will match disregarding their context. If one or more contexts are specified, statements with a context matching one of these will match.- Returns:
true
if statements match the specified pattern.
-
remove
public boolean remove(Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from interface:Model
Removes statements with the specified subject, predicate, object and (optionally) context exist in this model. Thesubject
,predicate
andobject
parameters can benull
to indicate wildcards. Thecontexts
parameter is a wildcard and accepts zero or more values. If no contexts are specified, statements will be removed disregarding their context. If one or more contexts are specified, statements with a context matching one of these will be removed. Note: to remove statements without an associated context, specify the valuenull
and explicitly cast it to typeResource
.Examples:
model.remove(s1, null, null)
removes any statements in this model have subjects1
,
model.remove(null, null, null, c1)
removes any statements in this model have contextc1
,
model.remove(null, null, null, (Resource)null)
removes any statements in this model have no associated context,
model.remove(null, null, null, c1, c2, c3)
removes any statements in this model have contextc1
,c2
orc3
.- Specified by:
remove
in interfaceModel
- Parameters:
subj
- The subject of the statements to remove,null
to remove statements with any subject.pred
- The predicate of the statements to remove,null
to remove statements with any predicate.obj
- The object of the statements to remove,null
to remove statements with any object.contexts
- The contexts of the statements to remove. If no contexts are specified, statements will be removed disregarding their context. If one or more contexts are specified, statements with a context matching one of these will be removed.- Returns:
true
if one or more statements have been removed.
-
iterator
public java.util.Iterator<Statement> iterator()
-
filter
public Model filter(Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from interface:Model
Returns a filtered view of the statements with the specified subject, predicate, object and (optionally) context. Thesubject
,predicate
andobject
parameters can benull
to indicate wildcards. Thecontexts
parameter is a wildcard and accepts zero or more values. If no contexts are specified, statements will match disregarding their context. If one or more contexts are specified, statements with a context matching one of these will match. Note: to match statements without an associated context, specify the valuenull
and explicitly cast it to typeResource
.The returned model is backed by this Model, so changes to this Model are reflected in the returned model, and vice-versa. If this Model is modified while an iteration over the returned model is in progress (except through the iterator's own
remove
operation), the results of the iteration are undefined. The model supports element removal, which removes the corresponding statement from this Model, via theIterator.remove
,Set.remove
,removeAll
,retainAll
, andclear
operations. The statements passed to theadd
andaddAll
operations must match the parameter pattern.Examples:
model.filter(s1, null, null)
matches all statements that have subjects1
,
model.filter(null, null, null, c1)
matches all statements that have contextc1
,
model.filter(null, null, null, (Resource)null)
matches all statements that have no associated context,
model.filter(null, null, null, c1, c2, c3)
matches all statements that have contextc1
,c2
orc3
.- Specified by:
filter
in interfaceModel
- Parameters:
subj
- The subject of the statements to match,null
to match statements with any subject.pred
- The predicate of the statements to match,null
to match statements with any predicate.obj
- The object of the statements to match,null
to match statements with any object.contexts
- The contexts of the statements to match. If no contexts are specified, statements will match disregarding their context. If one or more contexts are specified, statements with a context matching one of these will match.- Returns:
- The statements that match the specified pattern.
- See Also:
Model.getStatements(Resource, IRI, Value, Resource...)
-
removeTermIteration
public void removeTermIteration(java.util.Iterator<Statement> iterator, Resource subj, IRI pred, Value obj, Resource... contexts)
Description copied from class:AbstractModel
Called by aggregate sets when a term has been removed from a term iterator. Exactly one of the last four terms will be non-empty.- Specified by:
removeTermIteration
in classAbstractModel
- Parameters:
iterator
- The iterator used to navigate the live set (never null)subj
- the subject term to be removed or nullpred
- the predicate term to be removed or nullobj
- the object term to be removed or nullcontexts
- an array of one context term to be removed or an empty array
-
matchPattern
java.util.Iterator<Statement> matchPattern(Resource subj, IRI pred, Value obj, Resource ctx)
-
subSet
java.util.SortedSet<Statement> subSet(Statement lo, boolean loInclusive, Statement hi, boolean hiInclusive)
-
removeAll
private void removeAll(java.util.TreeSet<Statement> owner, TreeModel.StatementTree chosen, java.util.Iterator<Statement> iter)
-
choose
private TreeModel.StatementTree choose(Value subj, Value pred, Value obj, Value ctx)
-
index
private TreeModel.StatementTree index(Value subj, Value pred, Value obj, Value ctx)
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Collection<Statement>
- Specified by:
isEmpty
in interfacejava.util.Set<Statement>
- Overrides:
isEmpty
in classAbstractModel
-
-