Class DynamicModel

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<Statement>, java.util.Collection<Statement>, java.util.Set<Statement>, Model, NamespaceAware

    public class DynamicModel
    extends java.util.AbstractSet<Statement>
    implements Model
    A LinkedHashModel or a TreeModel achieves fast data access at the cost of higher indexing time. The DynamicModel postpones this cost until such access is actually needed. It stores all data in a LinkedHashMap and supports adding, retrieving and removing data. The model will upgrade to a full model (provided by the modelFactory) if more complex operations are called, for instance removing data according to a pattern (eg. all statements with rdf:type as predicate).

    DynamicModel is thread safe to the extent that the underlying LinkedHashMap or Model is. The upgrade path is protected by the actual upgrade method being synchronized. The LinkedHashMap storage is not removed once upgraded, so concurrent reads that have started reading from the LinkedHashMap can continue to read even during an upgrade. We do make the LinkedHashMap unmodifiable to reduce the chance of there being a bug.

    See Also:
    Serialized Form
    • 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.
      boolean add​(Statement statement)  
      boolean addAll​(java.util.Collection<? extends Statement> c)  
      void clear()  
      boolean clear​(Resource... context)
      Removes statements with the specified context exist in this model.
      boolean contains​(java.lang.Object o)  
      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.
      boolean containsAll​(java.util.Collection<?> c)  
      java.util.Set<Resource> contexts()
      Returns a Set view of the contexts contained in this model.
      boolean equals​(java.lang.Object o)  
      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.
      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.lang.Iterable<Statement> getStatements​(Resource subject, IRI predicate, Value object, Resource... contexts)
      Returns an Iterable over all Statements in this Model that match the supplied criteria.
      int hashCode()  
      boolean isEmpty()  
      java.util.Iterator<Statement> iterator()  
      java.util.Set<Value> objects()
      Returns a Set view of the objects contained in this model.
      java.util.Set<IRI> predicates()
      Returns a Set view of the predicates contained in this model.
      boolean remove​(java.lang.Object o)  
      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.
      boolean removeAll​(java.util.Collection<?> c)  
      java.util.Optional<Namespace> removeNamespace​(java.lang.String prefix)
      Removes a namespace declaration by removing the association between a prefix and a namespace name.
      boolean retainAll​(java.util.Collection<?> c)  
      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()  
      java.util.Set<Resource> subjects()
      Returns a Set view of the subjects contained in this model.
      private void synchronizedUpgrade()  
      java.lang.Object[] toArray()  
      <T> T[] toArray​(T[] a)  
      Model unmodifiable()
      Returns an unmodifiable view of this model.
      private void upgrade()  
      • Methods inherited from class java.util.AbstractCollection

        toString
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        spliterator
    • Constructor Detail

      • DynamicModel

        public DynamicModel​(ModelFactory modelFactory)
    • Method Detail

      • unmodifiable

        public Model unmodifiable()
        Description copied from interface: Model
        Returns an unmodifiable view of this model. This method provides "read-only" access to this model. Query operations on the returned model "read through" to this model, and attempts to modify the returned model, whether direct or via its iterator, result in an UnsupportedOperationException.

        Specified by:
        unmodifiable in interface Model
        Returns:
        an unmodifiable view of the specified set.
      • 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 interface NamespaceAware
        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.
      • 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 interface Model
        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 interface Model
        Parameters:
        namespace - A Namespace 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 interface Model
        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()
      • 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. The subject, predicate and object parameters can be null to indicate wildcards. The contexts 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 value null and explicitly cast it to type Resource.

        Examples: model.contains(s1, null, null) is true if any statements in this model have subject s1,
        model.contains(null, null, null, c1) is true if any statements in this model have context c1,
        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 context c1, c2 or c3 .

        Specified by:
        contains in interface Model
        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.
      • 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.
        Specified by:
        add in interface Model
        Parameters:
        subj - The statement's subject.
        pred - The statement's predicate.
        obj - The statement's object.
        contexts - The contexts to add statements to.
      • clear

        public boolean clear​(Resource... context)
        Description copied from interface: Model
        Removes statements with the specified context exist in this model.
        Specified by:
        clear in interface Model
        Parameters:
        context - The context of the statements to remove.
        Returns:
        true if one or more statements have been removed.
      • 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. The subject, predicate and object parameters can be null to indicate wildcards. The contexts 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 value null and explicitly cast it to type Resource.

        Examples: model.remove(s1, null, null) removes any statements in this model have subject s1,
        model.remove(null, null, null, c1) removes any statements in this model have context c1 ,
        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 context c1, c2 or c3.

        Specified by:
        remove in interface Model
        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.
      • 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. The subject, predicate and object parameters can be null to indicate wildcards. The contexts 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 value null and explicitly cast it to type Resource.

        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 the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. The statements passed to the add and addAll operations must match the parameter pattern.

        Examples: model.filter(s1, null, null) matches all statements that have subject s1,
        model.filter(null, null, null, c1) matches all statements that have context c1,
        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 context c1, c2 or c3.

        Specified by:
        filter in interface Model
        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...)
      • subjects

        public java.util.Set<Resource> subjects()
        Description copied from interface: Model
        Returns a Set view of the subjects contained in this model. The set is backed by the model, so changes to the model are reflected in the set, and vice-versa. If the model is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes all statements from the model for which that element is a subject value, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations if the parameters pred or obj are null.
        Specified by:
        subjects in interface Model
        Returns:
        a set view of the subjects contained in this model
      • predicates

        public java.util.Set<IRI> predicates()
        Description copied from interface: Model
        Returns a Set view of the predicates contained in this model. The set is backed by the model, so changes to the model are reflected in the set, and vice-versa. If the model is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes all statements from the model for which that element is a predicate value, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations if the parameters subj or obj are null.
        Specified by:
        predicates in interface Model
        Returns:
        a set view of the predicates contained in this model
      • objects

        public java.util.Set<Value> objects()
        Description copied from interface: Model
        Returns a Set view of the objects contained in this model. The set is backed by the model, so changes to the model are reflected in the set, and vice-versa. If the model is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes all statements from the model for which that element is an object value, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations if the parameters subj or pred are null.
        Specified by:
        objects in interface Model
        Returns:
        a set view of the objects contained in this model
      • contexts

        public java.util.Set<Resource> contexts()
        Description copied from interface: Model
        Returns a Set view of the contexts contained in this model. The set is backed by the model, so changes to the model are reflected in the set, and vice-versa. If the model is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes all statements from the model for which that element is a context value, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations if the parameters subj , pred or obj are null.
        Specified by:
        contexts in interface Model
        Returns:
        a set view of the contexts contained in this model
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<Statement>
        Specified by:
        size in interface java.util.Set<Statement>
        Specified by:
        size in class java.util.AbstractCollection<Statement>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<Statement>
        Specified by:
        isEmpty in interface java.util.Set<Statement>
        Overrides:
        isEmpty in class java.util.AbstractCollection<Statement>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<Statement>
        Specified by:
        contains in interface java.util.Set<Statement>
        Overrides:
        contains in class java.util.AbstractCollection<Statement>
      • iterator

        public java.util.Iterator<Statement> iterator()
        Specified by:
        iterator in interface java.util.Collection<Statement>
        Specified by:
        iterator in interface java.lang.Iterable<Statement>
        Specified by:
        iterator in interface java.util.Set<Statement>
        Specified by:
        iterator in class java.util.AbstractCollection<Statement>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<Statement>
        Specified by:
        toArray in interface java.util.Set<Statement>
        Overrides:
        toArray in class java.util.AbstractCollection<Statement>
      • toArray

        public <T> T[] toArray​(T[] a)
        Specified by:
        toArray in interface java.util.Collection<Statement>
        Specified by:
        toArray in interface java.util.Set<Statement>
        Overrides:
        toArray in class java.util.AbstractCollection<Statement>
      • add

        public boolean add​(Statement statement)
        Specified by:
        add in interface java.util.Collection<Statement>
        Specified by:
        add in interface java.util.Set<Statement>
        Overrides:
        add in class java.util.AbstractCollection<Statement>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<Statement>
        Specified by:
        remove in interface java.util.Set<Statement>
        Overrides:
        remove in class java.util.AbstractCollection<Statement>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<Statement>
        Specified by:
        containsAll in interface java.util.Set<Statement>
        Overrides:
        containsAll in class java.util.AbstractCollection<Statement>
      • addAll

        public boolean addAll​(java.util.Collection<? extends Statement> c)
        Specified by:
        addAll in interface java.util.Collection<Statement>
        Specified by:
        addAll in interface java.util.Set<Statement>
        Overrides:
        addAll in class java.util.AbstractCollection<Statement>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<Statement>
        Specified by:
        retainAll in interface java.util.Set<Statement>
        Overrides:
        retainAll in class java.util.AbstractCollection<Statement>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<Statement>
        Specified by:
        removeAll in interface java.util.Set<Statement>
        Overrides:
        removeAll in class java.util.AbstractSet<Statement>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<Statement>
        Specified by:
        clear in interface java.util.Set<Statement>
        Overrides:
        clear in class java.util.AbstractCollection<Statement>
      • getStatements

        public java.lang.Iterable<Statement> getStatements​(Resource subject,
                                                           IRI predicate,
                                                           Value object,
                                                           Resource... contexts)
        Description copied from interface: Model
        Returns an Iterable over all Statements in this Model that match the supplied criteria.

        Examples:

        • model.getStatements(s1, null, null) matches all statements that have subject s1
        • model.getStatements(s1, p1, null) matches all statements that have subject s1 and predicate p1
        • model.getStatements(null, null, null, c1) matches all statements that have context c1
        • model.getStatements(null, null, null, (Resource)null) matches all statements that have no associated context
        • model.getStatements(null, null, null, c1, c2, c3) matches all statements that have context c1, c2 or c3
        Specified by:
        getStatements in interface Model
        Parameters:
        subject - The subject of the statements to match, null to match statements with any subject.
        predicate - The predicate of the statements to match, null to match statements with any predicate.
        object - 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 any one of these will match. To match statements without an associated context, specify the value null and explicitly cast it to type Resource.
        Returns:
        an Iterable over the statements in this Model that match the specified pattern.
        See Also:
        Model.filter(Resource, IRI, Value, Resource...)
      • upgrade

        private void upgrade()
      • synchronizedUpgrade

        private void synchronizedUpgrade()
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Collection<Statement>
        Specified by:
        equals in interface java.util.Set<Statement>
        Overrides:
        equals in class java.util.AbstractSet<Statement>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<Statement>
        Specified by:
        hashCode in interface java.util.Set<Statement>
        Overrides:
        hashCode in class java.util.AbstractSet<Statement>