Return-Path: X-Original-To: apmail-clerezza-commits-archive@www.apache.org Delivered-To: apmail-clerezza-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CAEBA17A22 for ; Mon, 12 Jan 2015 19:27:09 +0000 (UTC) Received: (qmail 74795 invoked by uid 500); 12 Jan 2015 19:27:11 -0000 Delivered-To: apmail-clerezza-commits-archive@clerezza.apache.org Received: (qmail 74743 invoked by uid 500); 12 Jan 2015 19:27:11 -0000 Mailing-List: contact commits-help@clerezza.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@clerezza.apache.org Delivered-To: mailing list commits@clerezza.apache.org Received: (qmail 74685 invoked by uid 99); 12 Jan 2015 19:27:11 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Jan 2015 19:27:11 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id E662EA01F5E; Mon, 12 Jan 2015 19:27:10 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: reto@apache.org To: commits@clerezza.apache.org Date: Mon, 12 Jan 2015 19:27:12 -0000 Message-Id: In-Reply-To: <60d7b2c42a9248cd999498fcb283c231@git.apache.org> References: <60d7b2c42a9248cd999498fcb283c231@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [3/4] clerezza git commit: Adapted to updated RDF Commons draft http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractGraph.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractGraph.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractGraph.java index 7827d61..86f000b 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractGraph.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractGraph.java @@ -18,89 +18,234 @@ */ package org.apache.clerezza.rdf.core.impl; +import java.lang.ref.WeakReference; +import java.util.AbstractCollection; import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; -import org.apache.commons.rdf.BlankNode; -import org.apache.commons.rdf.Graph; +import java.util.Set; +import org.apache.commons.rdf.BlankNodeOrIri; import org.apache.commons.rdf.RdfTerm; import org.apache.commons.rdf.Triple; -import org.apache.clerezza.rdf.core.impl.graphmatching.GraphMatcher; +import org.apache.commons.rdf.Graph; +import org.apache.commons.rdf.Iri; +import org.apache.commons.rdf.event.AddEvent; +import org.apache.commons.rdf.event.FilterTriple; +import org.apache.commons.rdf.event.GraphEvent; +import org.apache.commons.rdf.event.GraphListener; +import org.apache.commons.rdf.event.RemoveEvent; /** - * AbstractGraph is an abstract implementation of Graph - * implementing the equals and the hashCode methods. - * + * An abstract implementation of Graph implementing + * iterator and contains calling filter. + * * @author reto - * */ -public abstract class AbstractGraph extends AbstractTripleCollection +public abstract class AbstractGraph extends AbstractCollection implements Graph { - public final synchronized int hashCode() { - int result = 0; - for (Iterator iter = iterator(); iter.hasNext();) { - result += getBlankNodeBlindHash(iter.next()); + //all listeners + private final Set listenerConfigs = Collections.synchronizedSet( + new HashSet()); + private DelayedNotificator delayedNotificator = new DelayedNotificator(); + + @Override + public Iterator iterator() { + return filter(null, null, null); + } + + @Override + public boolean contains(Object o) { + if (!(o instanceof Triple)) { + return false; } - return result; + Triple t = (Triple) o; + return filter(t.getSubject(), t.getPredicate(), t.getObject()).hasNext(); + } + + @Override + public Iterator filter(BlankNodeOrIri subject, Iri predicate, + RdfTerm object) { + final Iterator baseIter = performFilter(subject, predicate, object); + return new Iterator() { + + Triple currentTriple = null; + + @Override + public boolean hasNext() { + return baseIter.hasNext(); + } + + @Override + public Triple next() { + currentTriple = baseIter.next(); + return currentTriple; + } + + @Override + public void remove() { + baseIter.remove(); + dispatchEvent(new RemoveEvent(AbstractGraph.this, currentTriple)); + } + }; } /** - * @param triple - * @return hash without BNode hashes + * A subclass of AbstractGraph should override + * this method instead of filter for ImmutableGraph event support to be + * added. The Iterator returned by filter will dispatch a + * GraphEvent after invoking the remove method of the iterator returned by + * this method. + * + * @param subject + * @param predicate + * @param object + * @return */ - private int getBlankNodeBlindHash(Triple triple) { - int hash = triple.getPredicate().hashCode(); - RdfTerm subject = triple.getSubject(); + protected abstract Iterator performFilter(BlankNodeOrIri subject, Iri predicate, + RdfTerm object); - if (!(subject instanceof BlankNode)) { - hash ^= subject.hashCode() >> 1; - } - RdfTerm object = triple.getObject(); - if (!(object instanceof BlankNode)) { - hash ^= object.hashCode() << 1; + @Override + public boolean add(Triple triple) { + boolean success = performAdd(triple); + if (success) { + dispatchEvent(new AddEvent(this, triple)); } + return success; + } - return hash; + /** + * A subclass of AbstractGraph should override + * this method instead of add for ImmutableGraph event support to be + * added. + * + * @param e The triple to be added to the triple collection + * @return + */ + protected boolean performAdd(Triple e) { + return super.add(e); } @Override - public boolean add(Triple e) { - throw new UnsupportedOperationException("Graphs are not mutable, use MGraph"); - + public boolean remove(Object o) { + Triple triple = (Triple) o; + boolean success = performRemove(triple); + if (success) { + dispatchEvent(new RemoveEvent(this, triple)); + } + return success; } @Override - public boolean addAll(Collection c) { - throw new UnsupportedOperationException("Graphs are not mutable, use MGraph"); + public boolean removeAll(Collection c) { + boolean modified = false; + for (Iterator it = c.iterator(); it.hasNext();) { + Object object = it.next(); + if (remove(object)) { + modified = true; + } + } + return modified; } - @Override - public boolean remove(Object o) { - throw new UnsupportedOperationException("Graphs are not mutable, use MGraph"); + /** + * A subclass of AbstractGraph should override + * this method instead of remove for ImmutableGraph event support to be + * added. + * + * @param o The triple to be removed from the triple collection + * @return + */ + protected boolean performRemove(Triple triple) { + Iterator e = performFilter(null, null, null); + while (e.hasNext()) { + if (triple.equals(e.next())) { + e.remove(); + return true; + } + } + return false; + } + + /** + * Dispatches a GraphEvent to all registered listeners for which + * the specified Triple matches the FilterTriples + * of the listeners. + * + * @param triple The Triple that was modified + * @param type The type of modification + */ + protected void dispatchEvent(GraphEvent event) { + synchronized(listenerConfigs) { + Iterator iter = listenerConfigs.iterator(); + while (iter.hasNext()) { + ListenerConfiguration config = iter.next(); + GraphListener registeredListener = config.getListener(); + if (registeredListener == null) { + iter.remove(); + continue; + } + if (config.getFilter().match(event.getTriple())) { + delayedNotificator.sendEventToListener(registeredListener, event); + } + } + } } @Override - public boolean removeAll(Collection c) { - throw new UnsupportedOperationException("Graphs are not mutable, use MGraph"); + public void addGraphListener(GraphListener listener, FilterTriple filter) { + addGraphListener(listener, filter, 0); } @Override - public void clear() { - throw new UnsupportedOperationException("Graphs are not mutable, use MGraph"); + public void addGraphListener(GraphListener listener, FilterTriple filter, + long delay) { + listenerConfigs.add(new ListenerConfiguration(listener, filter)); + if (delay > 0) { + delayedNotificator.addDelayedListener(listener, delay); + } } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; + public void removeGraphListener(GraphListener listener) { + synchronized(listenerConfigs) { + Iterator iter = listenerConfigs.iterator(); + while (iter.hasNext()) { + ListenerConfiguration listenerConfig = iter.next(); + GraphListener registeredListener = listenerConfig.getListener(); + if ((registeredListener == null) || (registeredListener.equals(listener))) { + iter.remove(); + } + } } - if (!(obj instanceof Graph)) { - return false; + delayedNotificator.removeDelayedListener(listener); + } + + private static class ListenerConfiguration { + + private WeakReference listenerRef; + private FilterTriple filter; + + private ListenerConfiguration(GraphListener listener, FilterTriple filter) { + this.listenerRef = new WeakReference(listener); + this.filter = filter; } - if (hashCode() != obj.hashCode()) { - return false; + + /** + * @return the listener + */ + GraphListener getListener() { + GraphListener listener = listenerRef.get(); + return listener; + } + + /** + * @return the filter + */ + FilterTriple getFilter() { + return filter; } - return GraphMatcher.getValidMapping(this, (Graph) obj) != null; } } http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractImmutableGraph.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractImmutableGraph.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractImmutableGraph.java new file mode 100644 index 0000000..9a52c88 --- /dev/null +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractImmutableGraph.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.clerezza.rdf.core.impl; + +import java.util.Collection; +import java.util.Iterator; + +import org.apache.commons.rdf.BlankNode; +import org.apache.commons.rdf.ImmutableGraph; +import org.apache.commons.rdf.RdfTerm; +import org.apache.commons.rdf.Triple; +import org.apache.clerezza.rdf.core.impl.graphmatching.GraphMatcher; + +/** + * AbstractGraph is an abstract implementation of ImmutableGraph + * implementing the equals and the hashCode methods. + * + * @author reto + * + */ +public abstract class AbstractImmutableGraph extends AbstractGraph + implements ImmutableGraph { + + public final synchronized int hashCode() { + int result = 0; + for (Iterator iter = iterator(); iter.hasNext();) { + result += getBlankNodeBlindHash(iter.next()); + } + return result; + } + + /** + * @param triple + * @return hash without BNode hashes + */ + private int getBlankNodeBlindHash(Triple triple) { + int hash = triple.getPredicate().hashCode(); + RdfTerm subject = triple.getSubject(); + + if (!(subject instanceof BlankNode)) { + hash ^= subject.hashCode() >> 1; + } + RdfTerm object = triple.getObject(); + if (!(object instanceof BlankNode)) { + hash ^= object.hashCode() << 1; + } + + return hash; + } + + @Override + public boolean add(Triple e) { + throw new UnsupportedOperationException("Graphs are not mutable, use Graph"); + + } + + @Override + public boolean addAll(Collection c) { + throw new UnsupportedOperationException("Graphs are not mutable, use Graph"); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException("Graphs are not mutable, use Graph"); + } + + @Override + public boolean removeAll(Collection c) { + throw new UnsupportedOperationException("Graphs are not mutable, use Graph"); + } + + @Override + public void clear() { + throw new UnsupportedOperationException("Graphs are not mutable, use Graph"); + } + + + @Override + public ImmutableGraph getImmutableGraph() { + return this; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof ImmutableGraph)) { + return false; + } + if (hashCode() != obj.hashCode()) { + return false; + } + return GraphMatcher.getValidMapping(this, (ImmutableGraph) obj) != null; + } +} http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractMGraph.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractMGraph.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractMGraph.java index 6f993f2..55bb2c3 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractMGraph.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractMGraph.java @@ -18,19 +18,19 @@ */ package org.apache.clerezza.rdf.core.impl; +import org.apache.commons.rdf.ImmutableGraph; import org.apache.commons.rdf.Graph; -import org.apache.commons.rdf.MGraph; /** - * AbstractMGraph is an abstract implementation of MGraph + * AbstractMGraph is an abstract implementation of Graph * implementing the getGraph method. * * @author reto */ -public abstract class AbstractMGraph extends AbstractTripleCollection implements MGraph { +public abstract class AbstractMGraph extends AbstractGraph implements Graph { @Override - public Graph getGraph() { - return new SimpleGraph(this); + public ImmutableGraph getImmutableGraph() { + return new SimpleImmutableGraph(this); } } http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractTripleCollection.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractTripleCollection.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractTripleCollection.java deleted file mode 100644 index c3528da..0000000 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/AbstractTripleCollection.java +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.clerezza.rdf.core.impl; - -import java.lang.ref.WeakReference; -import java.util.AbstractCollection; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; - -import java.util.Set; -import org.apache.commons.rdf.BlankNodeOrIri; -import org.apache.commons.rdf.RdfTerm; -import org.apache.commons.rdf.Triple; -import org.apache.commons.rdf.TripleCollection; -import org.apache.commons.rdf.Iri; -import org.apache.commons.rdf.event.AddEvent; -import org.apache.commons.rdf.event.FilterTriple; -import org.apache.commons.rdf.event.GraphEvent; -import org.apache.commons.rdf.event.GraphListener; -import org.apache.commons.rdf.event.RemoveEvent; - -/** - * An abstract implementation of TripleCollection implementing - * iterator and contains calling filter. - * - * @author reto - */ -public abstract class AbstractTripleCollection extends AbstractCollection - implements TripleCollection { - - //all listeners - private final Set listenerConfigs = Collections.synchronizedSet( - new HashSet()); - private DelayedNotificator delayedNotificator = new DelayedNotificator(); - - @Override - public Iterator iterator() { - return filter(null, null, null); - } - - @Override - public boolean contains(Object o) { - if (!(o instanceof Triple)) { - return false; - } - Triple t = (Triple) o; - return filter(t.getSubject(), t.getPredicate(), t.getObject()).hasNext(); - } - - @Override - public Iterator filter(BlankNodeOrIri subject, Iri predicate, - RdfTerm object) { - final Iterator baseIter = performFilter(subject, predicate, object); - return new Iterator() { - - Triple currentTriple = null; - - @Override - public boolean hasNext() { - return baseIter.hasNext(); - } - - @Override - public Triple next() { - currentTriple = baseIter.next(); - return currentTriple; - } - - @Override - public void remove() { - baseIter.remove(); - dispatchEvent(new RemoveEvent(AbstractTripleCollection.this, currentTriple)); - } - }; - } - - /** - * A subclass of AbstractTripleCollection should override - * this method instead of filter for graph event support to be - * added. The Iterator returned by filter will dispatch a - * GraphEvent after invoking the remove method of the iterator returned by - * this method. - * - * @param subject - * @param predicate - * @param object - * @return - */ - protected abstract Iterator performFilter(BlankNodeOrIri subject, Iri predicate, - RdfTerm object); - - @Override - public boolean add(Triple triple) { - boolean success = performAdd(triple); - if (success) { - dispatchEvent(new AddEvent(this, triple)); - } - return success; - } - - /** - * A subclass of AbstractTripleCollection should override - * this method instead of add for graph event support to be - * added. - * - * @param e The triple to be added to the triple collection - * @return - */ - protected boolean performAdd(Triple e) { - return super.add(e); - } - - @Override - public boolean remove(Object o) { - Triple triple = (Triple) o; - boolean success = performRemove(triple); - if (success) { - dispatchEvent(new RemoveEvent(this, triple)); - } - return success; - } - - @Override - public boolean removeAll(Collection c) { - boolean modified = false; - for (Iterator it = c.iterator(); it.hasNext();) { - Object object = it.next(); - if (remove(object)) { - modified = true; - } - } - return modified; - } - - /** - * A subclass of AbstractTripleCollection should override - * this method instead of remove for graph event support to be - * added. - * - * @param o The triple to be removed from the triple collection - * @return - */ - protected boolean performRemove(Triple triple) { - Iterator e = performFilter(null, null, null); - while (e.hasNext()) { - if (triple.equals(e.next())) { - e.remove(); - return true; - } - } - return false; - } - - /** - * Dispatches a GraphEvent to all registered listeners for which - * the specified Triple matches the FilterTriples - * of the listeners. - * - * @param triple The Triple that was modified - * @param type The type of modification - */ - protected void dispatchEvent(GraphEvent event) { - synchronized(listenerConfigs) { - Iterator iter = listenerConfigs.iterator(); - while (iter.hasNext()) { - ListenerConfiguration config = iter.next(); - GraphListener registeredListener = config.getListener(); - if (registeredListener == null) { - iter.remove(); - continue; - } - if (config.getFilter().match(event.getTriple())) { - delayedNotificator.sendEventToListener(registeredListener, event); - } - } - } - } - - @Override - public void addGraphListener(GraphListener listener, FilterTriple filter) { - addGraphListener(listener, filter, 0); - } - - @Override - public void addGraphListener(GraphListener listener, FilterTriple filter, - long delay) { - listenerConfigs.add(new ListenerConfiguration(listener, filter)); - if (delay > 0) { - delayedNotificator.addDelayedListener(listener, delay); - } - } - - @Override - public void removeGraphListener(GraphListener listener) { - synchronized(listenerConfigs) { - Iterator iter = listenerConfigs.iterator(); - while (iter.hasNext()) { - ListenerConfiguration listenerConfig = iter.next(); - GraphListener registeredListener = listenerConfig.getListener(); - if ((registeredListener == null) || (registeredListener.equals(listener))) { - iter.remove(); - } - } - } - delayedNotificator.removeDelayedListener(listener); - } - - private static class ListenerConfiguration { - - private WeakReference listenerRef; - private FilterTriple filter; - - private ListenerConfiguration(GraphListener listener, FilterTriple filter) { - this.listenerRef = new WeakReference(listener); - this.filter = filter; - } - - /** - * @return the listener - */ - GraphListener getListener() { - GraphListener listener = listenerRef.get(); - return listener; - } - - /** - * @return the filter - */ - FilterTriple getFilter() { - return filter; - } - } -} http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/DelayedNotificator.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/DelayedNotificator.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/DelayedNotificator.java index 78446b2..bc49042 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/DelayedNotificator.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/DelayedNotificator.java @@ -67,7 +67,7 @@ class DelayedNotificator { try { listener.graphChanged(eventsLocal); } catch (Exception e) { - log.warn("Exception delivering graph event", e); + log.warn("Exception delivering ImmutableGraph event", e); } } } http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleGraph.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleGraph.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleGraph.java index 707f95a..334a7d9 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleGraph.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleGraph.java @@ -18,61 +18,203 @@ */ package org.apache.clerezza.rdf.core.impl; +import java.lang.ref.SoftReference; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.ConcurrentModificationException; +import java.util.HashSet; import java.util.Iterator; +import java.util.List; +import java.util.Set; import org.apache.commons.rdf.BlankNodeOrIri; +import org.apache.commons.rdf.ImmutableGraph; import org.apache.commons.rdf.RdfTerm; import org.apache.commons.rdf.Triple; -import org.apache.commons.rdf.TripleCollection; import org.apache.commons.rdf.Iri; /** + * For now this is a minimalistic implementation, without any indexes or other + * optimizations. + * + * This class is not public, implementations should use {@link SimpleGraph} or + * {@link SimpleMGraph}. * * @author reto */ -public class SimpleGraph extends AbstractGraph { +class SimpleGraph extends AbstractGraph { + + final Set triples; + + private boolean checkConcurrency = false; + + class SimpleIterator implements Iterator { + + private Iterator listIter; + private boolean isValid = true; + + public SimpleIterator(Iterator listIter) { + this.listIter = listIter; + } + private Triple currentNext; - private TripleCollection tripleCollection; + @Override + public boolean hasNext() { + checkValidity(); + return listIter.hasNext(); + } + + @Override + public Triple next() { + checkValidity(); + currentNext = listIter.next(); + return currentNext; + } + + @Override + public void remove() { + checkValidity(); + listIter.remove(); + triples.remove(currentNext); + invalidateIterators(this); + } + + private void checkValidity() throws ConcurrentModificationException { + if (checkConcurrency && !isValid) { + throw new ConcurrentModificationException(); + } + } + + private void invalidate() { + isValid = false; + } + } + + private final Set> iterators = + Collections.synchronizedSet(new HashSet>()); /** - * Creates a graph with the triples in tripleCollection - * - * @param tripleCollection the collection of triples this Graph shall consist of + * Creates an empty SimpleGraph */ - public SimpleGraph(TripleCollection tripleCollection) { - this.tripleCollection = new SimpleTripleCollection(tripleCollection.iterator()); + public SimpleGraph() { + triples = Collections.synchronizedSet(new HashSet()); } /** - * Creates a graph with the triples in tripleCollection. - * - * This construction allows to specify if the tripleCollection might change - * in future. If tripleCollectionWillNeverChange is set to true it will - * assume that the collection never changes, in this case the collection - * isn't copied making things more efficient. - * - * @param tripleCollection the collection of triples this Graph shall consist of - * @param tripleCollectionWillNeverChange true if the caller promises tripleCollection will never change + * Creates a SimpleGraph using the passed iterator, the iterator + * is consumed before the constructor returns + * + * @param iterator */ - public SimpleGraph(TripleCollection tripleCollection, boolean tripleCollectionWillNeverChange) { - if (!tripleCollectionWillNeverChange) { - this.tripleCollection = new SimpleTripleCollection(tripleCollection.iterator()); - } else { - this.tripleCollection = tripleCollection; + public SimpleGraph(Iterator iterator) { + triples = new HashSet(); + while (iterator.hasNext()) { + Triple triple = iterator.next(); + triples.add(triple); } } - - public SimpleGraph(Iterator tripleIter) { - this.tripleCollection = new SimpleTripleCollection(tripleIter); + + /** + * Creates a SimpleGraph for the specified set of triples, + * subsequent modification of baseSet do affect the created instance. + * + * @param baseSet + */ + public SimpleGraph(Set baseSet) { + this.triples = baseSet; + } + + /** + * Creates a SimpleGraph for the specified collection of triples, + * subsequent modification of baseSet do not affect the created instance. + * + * @param baseSet + */ + public SimpleGraph(Collection baseCollection) { + this.triples = new HashSet(baseCollection); } @Override public int size() { - return tripleCollection.size(); + return triples.size(); } @Override - public Iterator performFilter(BlankNodeOrIri subject, Iri predicate, RdfTerm object) { - return tripleCollection.filter(subject, predicate, object); + public Iterator performFilter(final BlankNodeOrIri subject, final Iri predicate, final RdfTerm object) { + final List tripleList = new ArrayList(); + synchronized (triples) { + Iterator baseIter = triples.iterator(); + while (baseIter.hasNext()) { + Triple triple = baseIter.next(); + if ((subject != null) + && (!triple.getSubject().equals(subject))) { + continue; + } + if ((predicate != null) + && (!triple.getPredicate().equals(predicate))) { + continue; + } + if ((object != null) + && (!triple.getObject().equals(object))) { + continue; + } + tripleList.add(triple); + } + + final Iterator listIter = tripleList.iterator(); + SimpleIterator resultIter = new SimpleIterator(listIter); + if (checkConcurrency) { + iterators.add(new SoftReference(resultIter)); + } + return resultIter; + } + } + + + @Override + public boolean performAdd(Triple e) { + boolean modified = triples.add(e); + if (modified) { + invalidateIterators(null); + } + return modified; + } + + private void invalidateIterators(SimpleIterator caller) { + if (!checkConcurrency) { + return; + } + Set oldReferences = new HashSet(); + synchronized(iterators) { + for (SoftReference softReference : iterators) { + SimpleIterator simpleIterator = softReference.get(); + if (simpleIterator == null) { + oldReferences.add(softReference); + continue; + } + if (simpleIterator != caller) { + simpleIterator.invalidate(); + } + } + } + iterators.removeAll(oldReferences); + } + + /** + * Specifies whether or not to throw ConcurrentModificationExceptions, + * if this simple triple collection is modified concurrently. Concurrency + * check is set to false by default. + * + * @param bool Specifies whether or not to check concurrent modifications. + */ + public void setCheckConcurrency(boolean bool) { + checkConcurrency = bool; + } + + + @Override + public ImmutableGraph getImmutableGraph() { + return new SimpleImmutableGraph(this); } } http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleImmutableGraph.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleImmutableGraph.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleImmutableGraph.java new file mode 100644 index 0000000..d0c6a61 --- /dev/null +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleImmutableGraph.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.clerezza.rdf.core.impl; + +import java.util.Iterator; + +import org.apache.commons.rdf.BlankNodeOrIri; +import org.apache.commons.rdf.RdfTerm; +import org.apache.commons.rdf.Triple; +import org.apache.commons.rdf.Graph; +import org.apache.commons.rdf.Iri; + +/** + * + * @author reto + */ +public class SimpleImmutableGraph extends AbstractImmutableGraph { + + private Graph graph; + + /** + * Creates a ImmutableGraph with the triples in Graph + * + * @param Graph the collection of triples this ImmutableGraph shall consist of + */ + public SimpleImmutableGraph(Graph Graph) { + this.graph = new SimpleGraph(Graph.iterator()); + } + + /** + * Creates a ImmutableGraph with the triples in Graph. + * + * This construction allows to specify if the Graph might change + * in future. If GraphWillNeverChange is set to true it will + * assume that the collection never changes, in this case the collection + * isn't copied making things more efficient. + * + * @param Graph the collection of triples this ImmutableGraph shall consist of + * @param GraphWillNeverChange true if the caller promises Graph will never change + */ + public SimpleImmutableGraph(Graph Graph, boolean GraphWillNeverChange) { + if (!GraphWillNeverChange) { + this.graph = new SimpleGraph(Graph.iterator()); + } else { + this.graph = Graph; + } + } + + public SimpleImmutableGraph(Iterator tripleIter) { + this.graph = new SimpleGraph(tripleIter); + } + + @Override + public int size() { + return graph.size(); + } + + @Override + public Iterator performFilter(BlankNodeOrIri subject, Iri predicate, RdfTerm object) { + return graph.filter(subject, predicate, object); + } +} http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleMGraph.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleMGraph.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleMGraph.java index a27f395..19a4b72 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleMGraph.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleMGraph.java @@ -22,15 +22,15 @@ import java.util.Collection; import java.util.Iterator; import java.util.Set; +import org.apache.commons.rdf.ImmutableGraph; import org.apache.commons.rdf.Graph; -import org.apache.commons.rdf.MGraph; import org.apache.commons.rdf.Triple; /** * * @author reto */ -public class SimpleMGraph extends SimpleTripleCollection implements MGraph { +public class SimpleMGraph extends SimpleGraph implements Graph { /** * Creates an empty SimpleMGraph @@ -50,10 +50,6 @@ public class SimpleMGraph extends SimpleTripleCollection implements MGraph { super(iterator); } - @Override - public Graph getGraph() { - return new SimpleGraph(this); - } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleTripleCollection.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleTripleCollection.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleTripleCollection.java deleted file mode 100644 index 124f0ff..0000000 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/SimpleTripleCollection.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.clerezza.rdf.core.impl; - -import java.lang.ref.SoftReference; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.ConcurrentModificationException; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import org.apache.commons.rdf.BlankNodeOrIri; -import org.apache.commons.rdf.RdfTerm; -import org.apache.commons.rdf.Triple; -import org.apache.commons.rdf.Iri; - -/** - * For now this is a minimalistic implementation, without any indexes or other - * optimizations. - * - * This class is not public, implementations should use {@link SimpleGraph} or - * {@link SimpleMGraph}. - * - * @author reto - */ -class SimpleTripleCollection extends AbstractTripleCollection { - - final Set triples; - - private boolean checkConcurrency = false; - - class SimpleIterator implements Iterator { - - private Iterator listIter; - private boolean isValid = true; - - public SimpleIterator(Iterator listIter) { - this.listIter = listIter; - } - private Triple currentNext; - - @Override - public boolean hasNext() { - checkValidity(); - return listIter.hasNext(); - } - - @Override - public Triple next() { - checkValidity(); - currentNext = listIter.next(); - return currentNext; - } - - @Override - public void remove() { - checkValidity(); - listIter.remove(); - triples.remove(currentNext); - invalidateIterators(this); - } - - private void checkValidity() throws ConcurrentModificationException { - if (checkConcurrency && !isValid) { - throw new ConcurrentModificationException(); - } - } - - private void invalidate() { - isValid = false; - } - } - - private final Set> iterators = - Collections.synchronizedSet(new HashSet>()); - - /** - * Creates an empty SimpleTripleCollection - */ - public SimpleTripleCollection() { - triples = Collections.synchronizedSet(new HashSet()); - } - - /** - * Creates a SimpleTripleCollection using the passed iterator, the iterator - * is consumed before the constructor returns - * - * @param iterator - */ - public SimpleTripleCollection(Iterator iterator) { - triples = new HashSet(); - while (iterator.hasNext()) { - Triple triple = iterator.next(); - triples.add(triple); - } - } - - /** - * Creates a SimpleTripleCollection for the specified set of triples, - * subsequent modification of baseSet do affect the created instance. - * - * @param baseSet - */ - public SimpleTripleCollection(Set baseSet) { - this.triples = baseSet; - } - - /** - * Creates a SimpleTripleCollection for the specified collection of triples, - * subsequent modification of baseSet do not affect the created instance. - * - * @param baseSet - */ - public SimpleTripleCollection(Collection baseCollection) { - this.triples = new HashSet(baseCollection); - } - - @Override - public int size() { - return triples.size(); - } - - @Override - public Iterator performFilter(final BlankNodeOrIri subject, final Iri predicate, final RdfTerm object) { - final List tripleList = new ArrayList(); - synchronized (triples) { - Iterator baseIter = triples.iterator(); - while (baseIter.hasNext()) { - Triple triple = baseIter.next(); - if ((subject != null) - && (!triple.getSubject().equals(subject))) { - continue; - } - if ((predicate != null) - && (!triple.getPredicate().equals(predicate))) { - continue; - } - if ((object != null) - && (!triple.getObject().equals(object))) { - continue; - } - tripleList.add(triple); - } - - final Iterator listIter = tripleList.iterator(); - SimpleIterator resultIter = new SimpleIterator(listIter); - if (checkConcurrency) { - iterators.add(new SoftReference(resultIter)); - } - return resultIter; - } - } - - - @Override - public boolean performAdd(Triple e) { - boolean modified = triples.add(e); - if (modified) { - invalidateIterators(null); - } - return modified; - } - - private void invalidateIterators(SimpleIterator caller) { - if (!checkConcurrency) { - return; - } - Set oldReferences = new HashSet(); - synchronized(iterators) { - for (SoftReference softReference : iterators) { - SimpleIterator simpleIterator = softReference.get(); - if (simpleIterator == null) { - oldReferences.add(softReference); - continue; - } - if (simpleIterator != caller) { - simpleIterator.invalidate(); - } - } - } - iterators.removeAll(oldReferences); - } - - /** - * Specifies whether or not to throw ConcurrentModificationExceptions, - * if this simple triple collection is modified concurrently. Concurrency - * check is set to false by default. - * - * @param bool Specifies whether or not to check concurrent modifications. - */ - public void setCheckConcurrency(boolean bool) { - checkConcurrency = bool; - } -} http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedGraph.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedGraph.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedGraph.java new file mode 100644 index 0000000..7812a76 --- /dev/null +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedGraph.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.clerezza.rdf.core.impl; + +import java.util.AbstractCollection; +import java.util.Collection; +import java.util.Iterator; + +import org.apache.commons.rdf.BlankNodeOrIri; +import org.apache.commons.rdf.RdfTerm; +import org.apache.commons.rdf.Triple; +import org.apache.commons.rdf.Graph; +import org.apache.commons.rdf.Iri; +import org.apache.clerezza.rdf.core.access.ReadOnlyException; +import org.apache.commons.rdf.ImmutableGraph; +import org.apache.commons.rdf.event.FilterTriple; +import org.apache.commons.rdf.event.GraphListener; + +/** + * + * This is a wrapper object for Graph. If SecurityManger + * is not null TcManager checks the TcPermission. + * If read-only permissions are set this wrapper is used instead of Graph + * and throws exceptions when add or remove methods are called. + * + * @author tsuy + */ +public class WriteBlockedGraph extends AbstractCollection + implements Graph { + + private Graph triples; + + public WriteBlockedGraph(Graph triples) { + this.triples = triples; + } + + @Override + public int size() { + return triples.size(); + } + + @Override + public Iterator filter(final BlankNodeOrIri subject, final Iri predicate, final RdfTerm object) { + final Iterator baseIter = triples.filter(subject, predicate, object); + return new Iterator() { + + @Override + public boolean hasNext() { + return baseIter.hasNext(); + } + + @Override + public Triple next() { + return baseIter.next(); + } + + @Override + public void remove() { + throw new ReadOnlyException("remove"); + } + + + }; + } + + @Override + public boolean add(Triple e) { + throw new ReadOnlyException("add"); + } + + @Override + public boolean addAll(Collection c) { + throw new ReadOnlyException("add all"); + } + + @Override + public void clear() { + throw new ReadOnlyException("clear"); + } + + @Override + public boolean remove(Object o) { + throw new ReadOnlyException("remove"); + } + + @Override + public boolean removeAll(Collection c) { + throw new ReadOnlyException("remove all"); + } + + @Override + public boolean retainAll(Collection c) { + throw new ReadOnlyException("retain all"); + } + + @Override + public void addGraphListener(GraphListener listener, FilterTriple filter, + long delay) { + triples.addGraphListener(listener, filter, delay); + } + + @Override + public void addGraphListener(GraphListener listener, FilterTriple filter) { + triples.addGraphListener(listener, filter); + } + + @Override + public void removeGraphListener(GraphListener listener) { + triples.removeGraphListener(listener); + } + + @Override + public Iterator iterator() { + return filter(null, null, null); + } + + @Override + public ImmutableGraph getImmutableGraph() { + return this.triples.getImmutableGraph(); + } +} http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedMGraph.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedMGraph.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedMGraph.java index 6ed3e06..129a803 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedMGraph.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedMGraph.java @@ -19,38 +19,33 @@ package org.apache.clerezza.rdf.core.impl; import java.util.concurrent.locks.ReadWriteLock; -import org.apache.commons.rdf.Graph; +import org.apache.commons.rdf.ImmutableGraph; import org.apache.clerezza.rdf.core.access.LockableMGraph; /** * -* This is a wrapper object for MGraph. If SecurityManger +* This is a wrapper object for Graph. If SecurityManger * is not null TcManager checks the TcPermission. -* If read-only permissions are set this wrapper is used instead of MGraph. +* If read-only permissions are set this wrapper is used instead of Graph. * * @author tsuy */ -public class WriteBlockedMGraph extends WriteBlockedTripleCollection +public class WriteBlockedMGraph extends WriteBlockedGraph implements LockableMGraph { - private LockableMGraph mGraph; + private LockableMGraph graph; /** * Creates a wrapper of SimpleMGraph */ - public WriteBlockedMGraph(LockableMGraph mGraph) { - super(mGraph); - this.mGraph = mGraph; - } - - @Override - public Graph getGraph() { - return this.mGraph.getGraph(); + public WriteBlockedMGraph(LockableMGraph graph) { + super(graph); + this.graph = graph; } @Override public ReadWriteLock getLock() { - return mGraph.getLock(); + return graph.getLock(); } } http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedTripleCollection.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedTripleCollection.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedTripleCollection.java deleted file mode 100644 index 19a7271..0000000 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/WriteBlockedTripleCollection.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.clerezza.rdf.core.impl; - -import java.util.AbstractCollection; -import java.util.Collection; -import java.util.Iterator; - -import org.apache.commons.rdf.BlankNodeOrIri; -import org.apache.commons.rdf.RdfTerm; -import org.apache.commons.rdf.Triple; -import org.apache.commons.rdf.TripleCollection; -import org.apache.commons.rdf.Iri; -import org.apache.clerezza.rdf.core.access.ReadOnlyException; -import org.apache.commons.rdf.event.FilterTriple; -import org.apache.commons.rdf.event.GraphListener; - -/** - * - * This is a wrapper object for TripleCollection. If SecurityManger - * is not null TcManager checks the TcPermission. - * If read-only permissions are set this wrapper is used instead of TripleCollection - * and throws exceptions when add or remove methods are called. - * - * @author tsuy - */ -public class WriteBlockedTripleCollection extends AbstractCollection - implements TripleCollection { - - private TripleCollection triples; - - public WriteBlockedTripleCollection(TripleCollection triples) { - this.triples = triples; - } - - @Override - public int size() { - return triples.size(); - } - - @Override - public Iterator filter(final BlankNodeOrIri subject, final Iri predicate, final RdfTerm object) { - final Iterator baseIter = triples.filter(subject, predicate, object); - return new Iterator() { - - @Override - public boolean hasNext() { - return baseIter.hasNext(); - } - - @Override - public Triple next() { - return baseIter.next(); - } - - @Override - public void remove() { - throw new ReadOnlyException("remove"); - } - - - }; - } - - @Override - public boolean add(Triple e) { - throw new ReadOnlyException("add"); - } - - @Override - public boolean addAll(Collection c) { - throw new ReadOnlyException("add all"); - } - - @Override - public void clear() { - throw new ReadOnlyException("clear"); - } - - @Override - public boolean remove(Object o) { - throw new ReadOnlyException("remove"); - } - - @Override - public boolean removeAll(Collection c) { - throw new ReadOnlyException("remove all"); - } - - @Override - public boolean retainAll(Collection c) { - throw new ReadOnlyException("retain all"); - } - - @Override - public void addGraphListener(GraphListener listener, FilterTriple filter, - long delay) { - triples.addGraphListener(listener, filter, delay); - } - - @Override - public void addGraphListener(GraphListener listener, FilterTriple filter) { - triples.addGraphListener(listener, filter); - } - - @Override - public void removeGraphListener(GraphListener listener) { - triples.removeGraphListener(listener); - } - - @Override - public Iterator iterator() { - return filter(null, null, null); - } -} http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/GraphMatcher.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/GraphMatcher.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/GraphMatcher.java index eced71e..2ea5389 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/GraphMatcher.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/GraphMatcher.java @@ -26,9 +26,9 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; import org.apache.commons.rdf.BlankNode; -import org.apache.commons.rdf.MGraph; +import org.apache.commons.rdf.Graph; import org.apache.commons.rdf.BlankNodeOrIri; -import org.apache.commons.rdf.TripleCollection; +import org.apache.commons.rdf.Graph; import org.apache.commons.rdf.RdfTerm; import org.apache.commons.rdf.Triple; import org.apache.clerezza.rdf.core.impl.SimpleMGraph; @@ -49,7 +49,7 @@ public class GraphMatcher { * get a mapping from g1 to g2 or null if the graphs are not isomorphic. The * returned map maps each BNodes from g1 to one * of g2. If the graphs are ground graphs the method return an empty map if - * the graph are equals and null otherwise. + * the ImmutableGraph are equals and null otherwise. *

* NOTE: This method does not returned mapping from blank nodes to grounded * nodes, a bnode in g1 is not a vraiable that may match any node, but must @@ -72,9 +72,9 @@ public class GraphMatcher { * @param g2 * @return a Set of NodePairs */ - public static Map getValidMapping(TripleCollection og1, TripleCollection og2) { - MGraph g1 = new SimpleMGraph(og1); - MGraph g2 = new SimpleMGraph(og2); + public static Map getValidMapping(Graph og1, Graph og2) { + Graph g1 = new SimpleMGraph(og1); + Graph g2 = new SimpleMGraph(og2); if (!Utils.removeGrounded(g1,g2)) { return null; } @@ -100,7 +100,7 @@ public class GraphMatcher { return matchings; } - private static Map trialAndErrorMatching(MGraph g1, MGraph g2, + private static Map trialAndErrorMatching(Graph g1, Graph g2, Map, Set> matchingGroups) { if (log.isDebugEnabled()) { Set bn1 = Utils.getBNodes(g1); @@ -118,7 +118,7 @@ public class GraphMatcher { return null; } - private static boolean checkMapping(MGraph g1, MGraph g2, Map map) { + private static boolean checkMapping(Graph g1, Graph g2, Map map) { for (Triple triple : g1) { if (!g2.contains(map(triple, map))) { return false; http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/HashMatching.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/HashMatching.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/HashMatching.java index b5289e6..7b3d540 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/HashMatching.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/graphmatching/HashMatching.java @@ -27,11 +27,11 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; import org.apache.commons.rdf.BlankNode; -import org.apache.commons.rdf.MGraph; +import org.apache.commons.rdf.Graph; import org.apache.commons.rdf.BlankNodeOrIri; import org.apache.commons.rdf.RdfTerm; import org.apache.commons.rdf.Triple; -import org.apache.commons.rdf.TripleCollection; +import org.apache.commons.rdf.Graph; import org.apache.commons.rdf.Iri; import org.apache.clerezza.rdf.core.impl.TripleImpl; import org.apache.clerezza.rdf.core.impl.graphmatching.collections.IntIterator; @@ -53,7 +53,7 @@ public class HashMatching { * @param tc2 * @throws GraphNotIsomorphicException */ - HashMatching(MGraph tc1, MGraph tc2) throws GraphNotIsomorphicException { + HashMatching(Graph tc1, Graph tc2) throws GraphNotIsomorphicException { int foundMatchings = 0; int foundMatchingGroups = 0; Map bNodeHashMap = new HashMap(); @@ -101,14 +101,14 @@ public class HashMatching { } /* * returns a Map from bnodes to hash that can be used for future - * refinements, this could be separate for each graph. + * refinements, this could be separate for each ImmutableGraph. * * triples no longer containing an unmatched bnodes ae removed. * * Note that the matched node are not guaranteed to be equals, but only to * be the correct if the graphs are isomorphic. */ - private Map matchByHashes(MGraph g1, MGraph g2, + private Map matchByHashes(Graph g1, Graph g2, Map bNodeHashMap) { Map> bNodePropMap1 = getBNodePropMap(g1); Map> bNodePropMap2 = getBNodePropMap(g2); @@ -155,7 +155,7 @@ public class HashMatching { } return result; } - private static Map> getBNodePropMap(MGraph g) { + private static Map> getBNodePropMap(Graph g) { Set bNodes = Utils.getBNodes(g); Map> result = new HashMap>(); for (BlankNode bNode : bNodes) { @@ -163,7 +163,7 @@ public class HashMatching { } return result; } - private static Set getProperties(BlankNode bNode, MGraph g) { + private static Set getProperties(BlankNode bNode, Graph g) { Set result = new HashSet(); Iterator ti = g.filter(bNode, null, null); while (ti.hasNext()) { @@ -189,16 +189,16 @@ public class HashMatching { return resource.hashCode(); } } - private static void replaceNode(MGraph mGraph, BlankNode bNode, BlankNodeOrIri replacementNode) { + private static void replaceNode(Graph graph, BlankNode bNode, BlankNodeOrIri replacementNode) { Set triplesToRemove = new HashSet(); - for (Triple triple : mGraph) { + for (Triple triple : graph) { Triple replacementTriple = getReplacement(triple, bNode, replacementNode); if (replacementTriple != null) { triplesToRemove.add(triple); - mGraph.add(replacementTriple); + graph.add(replacementTriple); } } - mGraph.removeAll(triplesToRemove); + graph.removeAll(triplesToRemove); } private static Triple getReplacement(Triple triple, BlankNode bNode, BlankNodeOrIri replacementNode) { if (triple.getSubject().equals(bNode)) { http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedGraphWrapper.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedGraphWrapper.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedGraphWrapper.java index 8236e75..97547c0 100644 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedGraphWrapper.java +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedGraphWrapper.java @@ -18,19 +18,253 @@ */ package org.apache.clerezza.rdf.core.impl.util; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Collection; +import java.util.Iterator; +import org.apache.clerezza.rdf.core.impl.SimpleImmutableGraph; +import org.apache.commons.rdf.BlankNodeOrIri; +import org.apache.commons.rdf.RdfTerm; +import org.apache.commons.rdf.Triple; import org.apache.commons.rdf.Graph; +import org.apache.commons.rdf.ImmutableGraph; +import org.apache.commons.rdf.Iri; +import org.apache.commons.rdf.event.FilterTriple; +import org.apache.commons.rdf.event.GraphListener; /** * Calls the methods of the wrapped Graph as privileged * code, because they may need permissions like writing to disk or accessing * network. - * + * * @author mir */ -public class PrivilegedGraphWrapper extends PrivilegedTripleCollectionWrapper - implements Graph { +public class PrivilegedGraphWrapper implements Graph { + + private Graph graph; + + public PrivilegedGraphWrapper(Graph Graph) { + this.graph = Graph; + } + + @Override + public Iterator filter(final BlankNodeOrIri subject, final Iri predicate, + final RdfTerm object) { + return AccessController.doPrivileged(new PrivilegedAction>() { + + @Override + public Iterator run() { + return graph.filter(subject, predicate, object); + } + }); + } + + @Override + public int size() { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Integer run() { + return graph.size(); + } + }); + } + + @Override + public boolean isEmpty() { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return graph.isEmpty(); + } + }); + } + + @Override + public boolean contains(final Object o) { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return graph.contains(o); + } + }); + } + + @Override + public Iterator iterator() { + return AccessController.doPrivileged(new PrivilegedAction>() { + + @Override + public Iterator run() { + return graph.iterator(); + } + }); + } + + @Override + public Object[] toArray() { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Object[] run() { + return graph.toArray(); + } + }); + } + + @Override + public T[] toArray(final T[] a) { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public T[] run() { + return graph.toArray(a); + } + }); + } + + @Override + public boolean add(final Triple triple) { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return graph.add(triple); + } + }); + } + + @Override + public boolean remove(final Object o) { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return graph.remove(o); + } + }); + } + + @Override + public boolean containsAll(final Collection c) { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return graph.containsAll(c); + } + }); + } + + @Override + public boolean addAll(final Collection c) { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return graph.addAll(c); + } + }); + } + + @Override + public boolean removeAll(final Collection c) { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return graph.removeAll(c); + } + }); + } + + @Override + public boolean retainAll(final Collection c) { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return graph.retainAll(c); + } + }); + } + + @Override + public void clear() { + AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Object run() { + graph.clear(); + return null; + } + }); + } + + @Override + public void addGraphListener(GraphListener listener, FilterTriple filter, long delay) { + graph.addGraphListener(listener, filter, delay); + } + + @Override + public void addGraphListener(GraphListener listener, FilterTriple filter) { + graph.addGraphListener(listener, filter); + } + + @Override + public void removeGraphListener(GraphListener listener) { + graph.removeGraphListener(listener); + } + + private static class PriviledgedTripleIterator implements Iterator { + + private final Iterator wrappedIterator; + + public PriviledgedTripleIterator(Iterator wrappedIterator) { + this.wrappedIterator = wrappedIterator; + } + + @Override + public boolean hasNext() { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Boolean run() { + return wrappedIterator.hasNext(); + } + }); + } + + @Override + public Triple next() { + return AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Triple run() { + return wrappedIterator.next(); + } + }); + } + + @Override + public void remove() { + AccessController.doPrivileged(new PrivilegedAction() { + + @Override + public Object run() { + wrappedIterator.remove(); + return null; + } + }); + } + } + + - public PrivilegedGraphWrapper(Graph graph) { - super(graph); + @Override + public ImmutableGraph getImmutableGraph() { + return new SimpleImmutableGraph(this); } } http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedImmuatbleGraphWrapper.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedImmuatbleGraphWrapper.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedImmuatbleGraphWrapper.java new file mode 100644 index 0000000..fdf0b5a --- /dev/null +++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedImmuatbleGraphWrapper.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.clerezza.rdf.core.impl.util; + +import org.apache.commons.rdf.ImmutableGraph; + +/** + * Calls the methods of the wrapped ImmutableGraph as privileged + * code, because they may need permissions like writing to disk or accessing + * network. + * + * @author mir + */ +public class PrivilegedImmuatbleGraphWrapper extends PrivilegedGraphWrapper + implements ImmutableGraph { + + public PrivilegedImmuatbleGraphWrapper(ImmutableGraph ImmutableGraph) { + super(ImmutableGraph); + } +} http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedMGraphWrapper.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedMGraphWrapper.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedMGraphWrapper.java deleted file mode 100644 index 4575bea..0000000 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedMGraphWrapper.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.clerezza.rdf.core.impl.util; - -import org.apache.commons.rdf.Graph; -import org.apache.commons.rdf.MGraph; -import org.apache.clerezza.rdf.core.impl.SimpleGraph; - -/** - * Calls the methods of the wrapped MGraph as privileged - * code, because they may need permissions like writing to disk or accessing - * network. - * - * @author mir - */ -public class PrivilegedMGraphWrapper extends PrivilegedTripleCollectionWrapper implements MGraph { - - public PrivilegedMGraphWrapper(MGraph wrappedMGraph) { - super(wrappedMGraph); - } - - @Override - public Graph getGraph() { - return new SimpleGraph(this); - } - -} http://git-wip-us.apache.org/repos/asf/clerezza/blob/9ae0a1bc/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedTripleCollectionWrapper.java ---------------------------------------------------------------------- diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedTripleCollectionWrapper.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedTripleCollectionWrapper.java deleted file mode 100644 index 85a9dbd..0000000 --- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/PrivilegedTripleCollectionWrapper.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.clerezza.rdf.core.impl.util; - -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.Collection; -import java.util.Iterator; -import org.apache.commons.rdf.BlankNodeOrIri; -import org.apache.commons.rdf.RdfTerm; -import org.apache.commons.rdf.Triple; -import org.apache.commons.rdf.TripleCollection; -import org.apache.commons.rdf.Iri; -import org.apache.commons.rdf.event.FilterTriple; -import org.apache.commons.rdf.event.GraphListener; - -/** - * Calls the methods of the wrapped TripleCollection as privileged - * code, because they may need permissions like writing to disk or accessing - * network. - * - * @author mir - */ -public class PrivilegedTripleCollectionWrapper implements TripleCollection { - - private TripleCollection tripleCollection; - - public PrivilegedTripleCollectionWrapper(TripleCollection tripleCollection) { - this.tripleCollection = tripleCollection; - } - - @Override - public Iterator filter(final BlankNodeOrIri subject, final Iri predicate, - final RdfTerm object) { - return AccessController.doPrivileged(new PrivilegedAction>() { - - @Override - public Iterator run() { - return tripleCollection.filter(subject, predicate, object); - } - }); - } - - @Override - public int size() { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Integer run() { - return tripleCollection.size(); - } - }); - } - - @Override - public boolean isEmpty() { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return tripleCollection.isEmpty(); - } - }); - } - - @Override - public boolean contains(final Object o) { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return tripleCollection.contains(o); - } - }); - } - - @Override - public Iterator iterator() { - return AccessController.doPrivileged(new PrivilegedAction>() { - - @Override - public Iterator run() { - return tripleCollection.iterator(); - } - }); - } - - @Override - public Object[] toArray() { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Object[] run() { - return tripleCollection.toArray(); - } - }); - } - - @Override - public T[] toArray(final T[] a) { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public T[] run() { - return tripleCollection.toArray(a); - } - }); - } - - @Override - public boolean add(final Triple triple) { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return tripleCollection.add(triple); - } - }); - } - - @Override - public boolean remove(final Object o) { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return tripleCollection.remove(o); - } - }); - } - - @Override - public boolean containsAll(final Collection c) { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return tripleCollection.containsAll(c); - } - }); - } - - @Override - public boolean addAll(final Collection c) { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return tripleCollection.addAll(c); - } - }); - } - - @Override - public boolean removeAll(final Collection c) { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return tripleCollection.removeAll(c); - } - }); - } - - @Override - public boolean retainAll(final Collection c) { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return tripleCollection.retainAll(c); - } - }); - } - - @Override - public void clear() { - AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Object run() { - tripleCollection.clear(); - return null; - } - }); - } - - @Override - public void addGraphListener(GraphListener listener, FilterTriple filter, long delay) { - tripleCollection.addGraphListener(listener, filter, delay); - } - - @Override - public void addGraphListener(GraphListener listener, FilterTriple filter) { - tripleCollection.addGraphListener(listener, filter); - } - - @Override - public void removeGraphListener(GraphListener listener) { - tripleCollection.removeGraphListener(listener); - } - - private static class PriviledgedTripleIterator implements Iterator { - - private final Iterator wrappedIterator; - - public PriviledgedTripleIterator(Iterator wrappedIterator) { - this.wrappedIterator = wrappedIterator; - } - - @Override - public boolean hasNext() { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Boolean run() { - return wrappedIterator.hasNext(); - } - }); - } - - @Override - public Triple next() { - return AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Triple run() { - return wrappedIterator.next(); - } - }); - } - - @Override - public void remove() { - AccessController.doPrivileged(new PrivilegedAction() { - - @Override - public Object run() { - wrappedIterator.remove(); - return null; - } - }); - } - } -}