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/03 18:54:01 UTC

svn commit: r1240263 [2/6] - in /incubator/stanbol/branches/ontonet-showcase/explanation: ./ core/ core/src/ core/src/main/ core/src/main/java/ core/src/main/java/org/ core/src/main/java/org/apache/ core/src/main/java/org/apache/stanbol/ core/src/main/...

Added: incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/ClerezzaSchemaMatcherImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/ClerezzaSchemaMatcherImpl.java?rev=1240263&view=auto
==============================================================================
--- incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/ClerezzaSchemaMatcherImpl.java (added)
+++ incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/ClerezzaSchemaMatcherImpl.java Fri Feb  3 17:53:57 2012
@@ -0,0 +1,180 @@
+/*
+ * 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.explanation.impl.clerezza;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.explanation.api.Configuration;
+import org.apache.stanbol.explanation.api.Schema;
+import org.apache.stanbol.explanation.api.SchemaMatcher;
+import org.apache.stanbol.explanation.impl.ClerezzaSchemaMatcher;
+import org.apache.stanbol.ontologymanager.ontonet.api.DuplicateIDException;
+import org.apache.stanbol.ontologymanager.ontonet.api.ONManager;
+import org.apache.stanbol.ontologymanager.ontonet.api.io.BlankOntologySource;
+import org.apache.stanbol.ontologymanager.ontonet.api.io.GraphSource;
+import org.apache.stanbol.ontologymanager.ontonet.api.io.RootOntologySource;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyScope;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologySpace;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.UnmodifiableOntologyCollectorException;
+import org.apache.stanbol.ontologymanager.registry.api.RegistryContentException;
+import org.apache.stanbol.ontologymanager.registry.api.model.Library;
+import org.apache.stanbol.owl.transformation.OWLAPIToClerezzaConverter;
+import org.osgi.service.component.ComponentContext;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(immediate = true, metatype = true)
+@Service(SchemaMatcher.class)
+public class ClerezzaSchemaMatcherImpl implements ClerezzaSchemaMatcher {
+
+    private Graph knowledgeBase;
+
+    private Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference
+    private ONManager onm;
+
+    private OntologyScope scopeSchemaMatching = null;
+
+    /**
+     * This default constructor is <b>only</b> intended to be used by the OSGI environment with Service
+     * Component Runtime support.
+     * <p>
+     * DO NOT USE to manually create instances - the ExplanationGeneratorImpl instances do need to be
+     * configured! YOU NEED TO USE {@link #ExplanationEnvironmentConfiguration(ONManager, Dictionary)} or its
+     * overloads, to parse the configuration and then initialise the rule store if running outside an OSGI
+     * environment.
+     */
+    public ClerezzaSchemaMatcherImpl() {
+        super();
+    }
+
+    /**
+     * To be invoked by non-OSGi environments.
+     * 
+     * @param onm
+     * @param configuration
+     */
+    public ClerezzaSchemaMatcherImpl(ONManager onm, Dictionary<String,Object> configuration) {
+        this();
+        this.onm = onm;
+        try {
+            activate(configuration);
+        } catch (IOException e) {
+            log.error("Unable to access servlet context.", e);
+        }
+    }
+
+    /**
+     * Used to configure an instance within an OSGi container.
+     * 
+     * @throws IOException
+     *             if there is no valid component context.
+     */
+    @SuppressWarnings("unchecked")
+    @Activate
+    protected void activate(ComponentContext context) throws IOException {
+        log.info("in " + Configuration.class + " activate with context " + context);
+        if (context == null) {
+            throw new IllegalStateException("No valid" + ComponentContext.class + " parsed in activate!");
+        }
+        activate((Dictionary<String,Object>) context.getProperties());
+    }
+
+    /**
+     * Called within both OSGi and non-OSGi environments.
+     * 
+     * @param configuration
+     * @throws IOException
+     */
+    protected void activate(Dictionary<String,Object> configuration) throws IOException {
+        // TODO load at least the explanation metamodel here.
+        String id = "ExplanationSchemaMatching";
+
+        try {
+            scopeSchemaMatching = onm.getOntologyScopeFactory().createOntologyScope(id,
+                new BlankOntologySource());
+        } catch (DuplicateIDException e) {
+            scopeSchemaMatching = onm.getScopeRegistry().getScope(e.getDuplicateID());
+        }
+        onm.getScopeRegistry().registerScope(scopeSchemaMatching, true);
+    }
+
+    @Deactivate
+    protected void deactivate(ComponentContext context) {
+        log.info("in " + Configuration.class + " deactivate with context " + context);
+    }
+
+    @Override
+    public Set<Schema> getSatisfiableSchemas(Set<Library> catalogs, UriRef entity) {
+
+        for (Library l : catalogs) {
+            log.info("Library {}", l);
+            try {
+                for (OWLOntology o : l.getOntologies())
+                    log.info("\t{}", o);
+            } catch (RegistryContentException ex) {
+                log.warn("Invalid content in library " + l, ex);
+                continue;
+            }
+        }
+
+        Graph ctx = new GraphNode(entity, knowledgeBase).getNodeContext();
+
+        Iterator<Triple> it = ctx.iterator();
+        while (it.hasNext()) {
+            Triple t = it.next();
+            log.info("In l=1 context of {} : triple {}", entity, t);
+        }
+
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void setKnowledgeBase(Graph knowledgeBase) {
+        this.knowledgeBase = knowledgeBase;
+        OntologySpace cs = null;
+        try {
+            cs = scopeSchemaMatching.getCustomSpace();
+            cs.tearDown();
+            // FIXME Ugly
+            scopeSchemaMatching.getCustomSpace().addOntology(
+                new RootOntologySource(OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(new GraphSource(
+                        knowledgeBase).getRootOntology())));
+        } catch (UnmodifiableOntologyCollectorException e) {
+            log.error("Failed to change knowledge base in unmodifiable ontolgy space {}",
+                e.getOntologyCollector());
+        } finally {
+            cs.setUp();
+        }
+    }
+
+}

Added: incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/CompatibilityMappingImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/CompatibilityMappingImpl.java?rev=1240263&view=auto
==============================================================================
--- incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/CompatibilityMappingImpl.java (added)
+++ incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/CompatibilityMappingImpl.java Fri Feb  3 17:53:57 2012
@@ -0,0 +1,51 @@
+/*
+ * 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.explanation.impl.clerezza;
+
+import org.apache.stanbol.explanation.api.BinaryRelation;
+import org.apache.stanbol.explanation.api.CompatibilityMapping;
+import org.apache.stanbol.explanation.api.Path;
+
+public class CompatibilityMappingImpl implements CompatibilityMapping {
+
+    private double likelihood = 0.0;
+
+    private Path path;
+
+    private BinaryRelation relation;
+
+    @Override
+    public double getLikelihood() {
+        return likelihood;
+    }
+
+    @Override
+    public Path getPath() {
+        return path;
+    }
+
+    @Override
+    public BinaryRelation getRelation() {
+        return relation;
+    }
+
+    @Override
+    public void setLikelihood(double likelihood) {
+        this.likelihood = likelihood;
+    }
+
+}

Added: incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/PathConstructor.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/PathConstructor.java?rev=1240263&view=auto
==============================================================================
--- incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/PathConstructor.java (added)
+++ incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/impl/clerezza/PathConstructor.java Fri Feb  3 17:53:57 2012
@@ -0,0 +1,139 @@
+/*
+ * 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.explanation.impl.clerezza;
+
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.access.TcManager;
+import org.apache.clerezza.rdf.core.sparql.ParseException;
+import org.apache.clerezza.rdf.core.sparql.QueryParser;
+import org.apache.clerezza.rdf.core.sparql.ResultSet;
+import org.apache.clerezza.rdf.core.sparql.SolutionMapping;
+import org.apache.clerezza.rdf.core.sparql.query.Query;
+import org.apache.clerezza.rdf.core.sparql.query.Variable;
+import org.apache.stanbol.explanation.api.CompatibilityMapping;
+
+public class PathConstructor {
+
+    public static Set<CompatibilityMapping> computePathCompatibility(UriRef from,
+                                                                     UriRef to,
+                                                                     Set<TripleCollection> kb,
+
+                                                                     int maxLength) {
+
+        if (maxLength < 1) throw new IllegalArgumentException("Maximum length cannot be lower than 1.");
+
+        StringBuffer sparql = new StringBuffer("SELECT ");
+
+        sparql.append(from == null ? "?y " : "");
+        sparql.append(" ?r0 ");
+
+        for (int i = 1; i < maxLength; i++) {
+            sparql.append("?x");
+            sparql.append(i - 1);
+            sparql.append(" ");
+
+            sparql.append(" ?r");
+            sparql.append(i);
+            sparql.append(" ");
+        }
+
+        sparql.append(to == null ? "?z" : "");
+        sparql.append(" ");
+        
+
+        sparql.append("WHERE { ");
+
+         sparql.append("{ ");
+
+        sparql.append(from == null ? "?y " : from);
+        sparql.append(" ");
+
+        sparql.append("?r0 ");
+
+        // We already did it once.
+        for (int i = 1; i < maxLength; i++) {
+            sparql.append("?x");
+            sparql.append(i - 1);
+            if (i > 1) sparql.append(" } ");
+            sparql.append(" OPTIONAL { ?x");
+            sparql.append(i - 1);
+            sparql.append(" ?r");
+            sparql.append(i);
+            sparql.append(" ");
+        }
+
+        sparql.append(to == null ? "?z" : to);
+
+        sparql.append(maxLength > 1 ? " } " : " ");
+
+         sparql.append(" } UNION { ");
+        
+         sparql.append(to == null ? "?z" : to);
+        
+         // We already did it once.
+         for (int i = maxLength - 1; i > 0; i--) {
+        
+         sparql.append(" ?r");
+         sparql.append(i);
+         sparql.append(" ");
+        
+         sparql.append("?x");
+         sparql.append(i - 1);
+         sparql.append(" . ?x");
+         sparql.append(i - 1);
+         }
+        
+         sparql.append(" ?r0 ");
+         sparql.append(from == null ? "?y " : from);
+        
+         sparql.append(" }");
+
+        sparql.append(" }");
+        //
+        // sparql = new StringBuffer();
+        // sparql.append("SELECT ?r ?y WHERE { { <http://semanticweb.org/dumps/people/teddypolar> ?r ?y } UNION { ?y ?r <http://semanticweb.org/dumps/people/teddypolar> } }");
+        //
+        System.out.println(sparql);
+        Query q;
+        try {
+            q = QueryParser.getInstance().parse(sparql.toString());
+
+            for (TripleCollection g : kb) {
+                Object o = TcManager.getInstance().executeSparqlQuery(q, g);
+                if (o instanceof ResultSet) {
+                    ResultSet result = (ResultSet) o;
+                    while (result.hasNext()) {
+                        System.out.println();
+                        SolutionMapping sm = result.next();
+                        for (Variable key : sm.keySet())
+                            System.out.println("##### " + key.getName() + " **** " + sm.get(key));
+                    }
+
+                }
+            }
+
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
+
+}

Added: incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/schema/SchemaExtractor.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/schema/SchemaExtractor.java?rev=1240263&view=auto
==============================================================================
--- incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/schema/SchemaExtractor.java (added)
+++ incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/schema/SchemaExtractor.java Fri Feb  3 17:53:57 2012
@@ -0,0 +1,18 @@
+package org.apache.stanbol.explanation.schema;
+
+import org.apache.stanbol.explanation.api.Explainable;
+import org.apache.stanbol.explanation.api.KnowledgeItem;
+import org.apache.stanbol.explanation.api.Schema;
+
+public class SchemaExtractor {
+
+    public static Schema guessSchema(Explainable<?> object) {
+
+        if (object instanceof KnowledgeItem) {
+
+        }
+
+        return null;
+    }
+
+}

Added: incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/util/SimpleOWLUtils.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/util/SimpleOWLUtils.java?rev=1240263&view=auto
==============================================================================
--- incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/util/SimpleOWLUtils.java (added)
+++ incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/java/org/apache/stanbol/explanation/util/SimpleOWLUtils.java Fri Feb  3 17:53:57 2012
@@ -0,0 +1,13 @@
+package org.apache.stanbol.explanation.util;
+
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLIndividual;
+
+public class SimpleOWLUtils {
+
+    public static OWLIndividual getIndividual(IRI iri) {
+        return OWLManager.getOWLDataFactory().getOWLNamedIndividual(iri);
+    }
+    
+}

Added: incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1240263&view=auto
==============================================================================
--- incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/resources/OSGI-INF/metatype/metatype.properties (added)
+++ incubator/stanbol/branches/ontonet-showcase/explanation/core/src/main/resources/OSGI-INF/metatype/metatype.properties Fri Feb  3 17:53:57 2012
@@ -0,0 +1,17 @@
+#===============================================================================
+#Properties defined by the Explanation Engine
+#===============================================================================
+org.apache.stanbol.explanation.impl.ExplanationGeneratorImpl.name = Apache Stanbol Explanation Generator
+org.apache.stanbol.explanation.impl.ExplanationGeneratorImpl.description = Creates information objects aimed at justifying the mention or presentation of knowledge objects.
+
+#===============================================================================
+#Properties defined by the Explanation Engine Configuration
+#===============================================================================
+org.apache.stanbol.explanation.impl.ExplanationEnvironmentConfiguration.name = Apache Stanbol Explanation Environment Configuration
+org.apache.stanbol.explanation.impl.ExplanationEnvironmentConfiguration.description = Defines the logical environment (ontology scope, etc.) where the explanation generator should work.
+
+#===============================================================================
+#Properties defined by the Schema Catalog
+#===============================================================================
+org.apache.stanbol.explanation.impl.SchemaCatalogImpl.name = Apache Stanbol Explanation Schema Catalog
+org.apache.stanbol.explanation.impl.SchemaCatalogImpl.description = The set of Knowledge Patterns, or Schemas, to be checked for satisfiability and selection from explanation generators.
\ No newline at end of file