You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by al...@apache.org on 2012/02/29 19:15:14 UTC
svn commit: r1295198 - in
/incubator/stanbol/trunk/ontologymanager/ontonet/src:
main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/
test/java/org/apache/stanbol/ontologymanager/ontonet/io/
Author: alexdma
Date: Wed Feb 29 18:15:13 2012
New Revision: 1295198
URL: http://svn.apache.org/viewvc?rev=1295198&view=rev
Log:
The HashMap<IRI,String> in ClerezzaOntologyProvider was replaced with the class OntologyToTcMapper, which wraps the mapping into a Clerezza MGraph with a reserved name (the canonical name of the OntologyToTcMapper class) (STANBOL-518)
Added:
incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/OntologyToTcMapper.java
Modified:
incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/ClerezzaOntologyProvider.java
incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestClerezzaInputSources.java
incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestStorage.java
Modified: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/ClerezzaOntologyProvider.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/ClerezzaOntologyProvider.java?rev=1295198&r1=1295197&r2=1295198&view=diff
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/ClerezzaOntologyProvider.java (original)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/ClerezzaOntologyProvider.java Wed Feb 29 18:15:13 2012
@@ -23,12 +23,10 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.Stack;
@@ -130,7 +128,8 @@ public class ClerezzaOntologyProvider im
* Maps ontology IRIs (logical or physical if the ontology is anonymous) to Clerezza storage keys i.e.
* graph names.
*/
- private Map<IRI,String> ontologyIdsToKeys;
+ private OntologyToTcMapper keymap = null;
+ // private Map<IRI,String> ontologyIdsToKeys;
@Reference
private Parser parser;
@@ -162,7 +161,7 @@ public class ClerezzaOntologyProvider im
*/
public ClerezzaOntologyProvider() {
supported = new Class<?>[] {MGraph.class, OWLOntology.class};
- ontologyIdsToKeys = new HashMap<IRI,String>();
+ // ontologyIdsToKeys = new HashMap<IRI,String>();
}
public ClerezzaOntologyProvider(TcProvider store, OfflineConfiguration offline, Parser parser) {
@@ -195,6 +194,8 @@ public class ClerezzaOntologyProvider im
// Check if the TcManager should be set as the store
if (store == null) store = tcManager;
+ keymap = new OntologyToTcMapper(store);
+
// Parse configuration.
prefix = (String) (configuration.get(OntologyProvider.GRAPH_PREFIX));
if (prefix == null) prefix = _GRAPH_PREFIX_DEFAULT; // Should be already assigned though
@@ -302,13 +303,15 @@ public class ClerezzaOntologyProvider im
@Override
public String getKey(IRI ontologyIri) {
ontologyIri = URIUtils.sanitizeID(ontologyIri);
- log.debug("key for {} is {}", ontologyIri, ontologyIdsToKeys.get(ontologyIri));
- return ontologyIdsToKeys.get(ontologyIri);
+ UriRef ur = keymap.getMapping(ontologyIri);
+ log.debug("key for {} is {}", ontologyIri, ur);
+ return (ur == null) ? null : ur.getUnicodeString();
}
@Override
public Set<String> getKeys() {
- return new HashSet<String>(ontologyIdsToKeys.values());
+ // return new HashSet<String>(ontologyIdsToKeys.values());
+ return keymap.stringValues();
}
@Override
@@ -524,8 +527,9 @@ public class ClerezzaOntologyProvider im
* as the one used by the input source.
*/
UriRef uriref = new UriRef(s);
+ log.debug("Storing ontology with graph ID {}", uriref);
// The policy here is to avoid copying the triples from a graph already in the store.
- // TODO not a good policy for graphs that change
+ // FIXME not a good policy for graphs that change
if (!getStore().listTripleCollections().contains(uriref) || force) {
try {
graph = store.createMGraph(uriref);
@@ -559,9 +563,12 @@ public class ClerezzaOntologyProvider im
if (loaded) {
// All is already sanitized by the time we get here.
- ontologyIdsToKeys.put(ontologyIri, s);
- if (alternateId != null && !alternateId.equals(iri)) ontologyIdsToKeys.put(
- IRI.create(alternateId), s);
+ UriRef urs = new UriRef(s);
+ // ontologyIdsToKeys.put(ontologyIri, s);
+ keymap.setMapping(ontologyIri, urs);
+ if (alternateId != null && !alternateId.equals(iri))
+ // ontologyIdsToKeys.put(
+ keymap.setMapping(IRI.create(alternateId), urs);
log.debug("Ontology \n\t\t{}\n\tstored with keys\n\t\t{}",
ontologyIri + ((alternateId != null && !alternateId.equals(iri)) ? " , " + alternateId : ""),
s);
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/OntologyToTcMapper.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/OntologyToTcMapper.java?rev=1295198&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/OntologyToTcMapper.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/clerezza/OntologyToTcMapper.java Wed Feb 29 18:15:13 2012
@@ -0,0 +1,122 @@
+/*
+ * 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.stanbol.ontologymanager.ontonet.impl.clerezza;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.access.EntityAlreadyExistsException;
+import org.apache.clerezza.rdf.core.access.TcProvider;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.semanticweb.owlapi.model.IRI;
+
+/**
+ * A map from logical/physical ontology IDs to Clerezza graph names, stored using the supplied TcProvider.
+ *
+ * @author alexdma
+ *
+ */
+public class OntologyToTcMapper {
+
+ /**
+ * The basic terms to use for the mapping graph.
+ *
+ * @author alexdma
+ *
+ */
+ private class Vocabulary {
+
+ static final String _BASE_VOCAB = "http://stanbol.apache.org/ontology/ontonet/meta#";
+
+ static final String MAPS_TO_GRAPH = _BASE_VOCAB + "mapsToGraph";
+
+ }
+
+ private MGraph graph;
+
+ private UriRef graphId = new UriRef(getClass().getCanonicalName());
+
+ private TcProvider store;
+
+ public OntologyToTcMapper(TcProvider store) {
+ if (store == null) throw new IllegalArgumentException("TcProvider cannot be null");
+ this.store = store;
+ try {
+ graph = store.createMGraph(graphId);
+ } catch (EntityAlreadyExistsException e) {
+ graph = store.getMGraph(graphId);
+ }
+ }
+
+ public void addMapping(IRI ontologyReference, UriRef graphName) {
+ graph.add(new TripleImpl(new UriRef(ontologyReference.toString()), new UriRef(
+ Vocabulary.MAPS_TO_GRAPH), graphName));
+ }
+
+ public void clearMappings() {
+ graph.clear();
+ }
+
+ public UriRef getMapping(IRI ontologyReference) {
+ Iterator<Triple> it = graph.filter(new UriRef(ontologyReference.toString()), new UriRef(
+ Vocabulary.MAPS_TO_GRAPH), null);
+ while (it.hasNext()) {
+ Resource obj = it.next().getObject();
+ if (obj instanceof UriRef) return (UriRef) obj;
+ }
+ return null;
+ }
+
+ public Set<IRI> keys() {
+ Set<IRI> result = new HashSet<IRI>();
+ Iterator<Triple> it = graph.filter(null, new UriRef(Vocabulary.MAPS_TO_GRAPH), null);
+ while (it.hasNext()) {
+ NonLiteral subj = it.next().getSubject();
+ if (subj instanceof UriRef) result.add(IRI.create(((UriRef) subj).getUnicodeString()));
+ }
+ return result;
+ }
+
+ public void removeMapping(IRI ontologyReference) {
+ Iterator<Triple> it = graph.filter(new UriRef(ontologyReference.toString()), new UriRef(
+ Vocabulary.MAPS_TO_GRAPH), null);
+ // I expect a concurrent modification exception here, but we'll deal with it later.
+ while (it.hasNext())
+ graph.remove(it.next());
+ }
+
+ public void setMapping(IRI ontologyReference, UriRef graphName) {
+ removeMapping(ontologyReference);
+ addMapping(ontologyReference, graphName);
+ }
+
+ public Set<String> stringValues() {
+ Set<String> result = new HashSet<String>();
+ Iterator<Triple> it = graph.filter(null, new UriRef(Vocabulary.MAPS_TO_GRAPH), null);
+ while (it.hasNext()) {
+ Resource obj = it.next().getObject();
+ if (obj instanceof UriRef) result.add(((UriRef) obj).getUnicodeString());
+ }
+ return result;
+ }
+}
Modified: incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestClerezzaInputSources.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestClerezzaInputSources.java?rev=1295198&r1=1295197&r2=1295198&view=diff
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestClerezzaInputSources.java (original)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestClerezzaInputSources.java Wed Feb 29 18:15:13 2012
@@ -72,7 +72,7 @@ public class TestClerezzaInputSources {
@Test
public void testGraphContentSource() throws Exception {
// Make sure the tc manager has been reset
- assertEquals(0, tcManager.listTripleCollections().size());
+ assertEquals(1, tcManager.listTripleCollections().size());
OntologyProvider<TcProvider> provider = new ClerezzaOntologyProvider(tcManager,
new OfflineConfigurationImpl(new Hashtable<String,Object>()), parser);
@@ -86,7 +86,7 @@ public class TestClerezzaInputSources {
.size());
for (UriRef name : tcManager.listTripleCollections())
log.info("-- {} (a {})", name, tcManager.getTriples(name).getClass().getSimpleName());
- assertEquals(0, tcManager.listTripleCollections().size());
+ assertEquals(1, tcManager.listTripleCollections().size());
OntologySpace spc = new CoreOntologySpaceImpl(TestClerezzaInputSources.class.getSimpleName(),
IRI.create("http://stanbol.apache.org/ontologies/"), provider);
spc.addOntology(src);
@@ -95,7 +95,7 @@ public class TestClerezzaInputSources {
for (UriRef name : tcManager.listTripleCollections())
log.info("-- {} (a {})", name, tcManager.getTriples(name).getClass().getSimpleName());
- assertEquals(1, tcManager.listTripleCollections().size());
+ assertEquals(2, tcManager.listTripleCollections().size());
}
@@ -116,8 +116,8 @@ public class TestClerezzaInputSources {
assertNotNull(gis);
assertNotNull(gis.getRootOntology());
Set<TripleCollection> imported = gis.getImports(false);
- // Number of stored graphs minus the importing one = imported graphs
- assertEquals(tcManager.listTripleCollections().size() - 1, imported.size());
+ // Number of stored graphs minus the importing one minus the reserved graph = imported graphs
+ assertEquals(tcManager.listTripleCollections().size() - 2, imported.size());
for (TripleCollection g : imported)
assertNotNull(g);
}
Modified: incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestStorage.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestStorage.java?rev=1295198&r1=1295197&r2=1295198&view=diff
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestStorage.java (original)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/io/TestStorage.java Wed Feb 29 18:15:13 2012
@@ -53,7 +53,7 @@ public class TestStorage {
@Test
public void storageOnScopeCreation() throws Exception {
- assertTrue(ontologyProvider.getStore().listTripleCollections().isEmpty());
+ assertEquals(1,ontologyProvider.getStore().listTripleCollections().size());
OntologyInputSource ois = new RootOntologyIRISource(IRI.create(getClass().getResource(
"/ontologies/minorcharacters.owl")));