From: <lor...@us...> - 2013-05-08 21:03:48
|
Revision: 3963 http://sourceforge.net/p/dl-learner/code/3963 Author: lorenz_b Date: 2013-05-08 21:03:43 +0000 (Wed, 08 May 2013) Log Message: ----------- Refactored pattern eval. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java Modified: trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java 2013-05-08 19:07:53 UTC (rev 3962) +++ trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java 2013-05-08 21:03:43 UTC (rev 3963) @@ -1,5 +1,7 @@ package org.dllearner.utilities.owl; +import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -7,6 +9,7 @@ import java.util.Map; import java.util.Set; import java.util.Stack; +import java.util.TreeSet; import org.aksw.commons.collections.diff.ModelDiff; import org.semanticweb.owlapi.apibinding.OWLManager; @@ -58,6 +61,10 @@ import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.Sets; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.Syntax; @@ -74,6 +81,8 @@ private OWLDataFactory df = new OWLDataFactoryImpl(); + private Multimap<Integer, OWLEntity> properties = HashMultimap.create(); + private Map<Integer, Boolean> intersection; private Set<? extends OWLEntity> variableEntities = new HashSet<OWLEntity>(); @@ -147,6 +156,7 @@ private void reset(){ variablesMapping.clear(); variables.clear(); + properties.clear(); classCnt = 0; propCnt = 0; indCnt = 0; @@ -238,6 +248,9 @@ } else { s = "<" + entity.toStringID() + ">"; } + if(entity.isOWLObjectProperty()){ + properties.put(modalDepth(), entity); + } return s; } @@ -269,6 +282,19 @@ for (OWLClassExpression operand : operands) { operand.accept(this); } + Collection<OWLEntity> props = properties.get(modalDepth()); + if(props.size() > 1){ + Collection<String> vars = new TreeSet<String>(); + for (OWLEntity p : props) { + if(variablesMapping.containsKey(p)){ + vars.add(variablesMapping.get(p)); + } + } + if(vars.size() == 2){ + List<String> varList = new ArrayList<String>(vars); + sparql += "FILTER(" + varList.get(0) + "!=" + varList.get(1) + ")"; + } + } leaveIntersection(); } @@ -728,8 +754,14 @@ //variable entity expr = df.getOWLObjectIntersectionOf( df.getOWLObjectSomeValuesFrom(propR, clsB), + clsB, df.getOWLObjectSomeValuesFrom(propS, clsA)); + query = converter.asQuery(rootVar, expr, Sets.newHashSet(propR, propS)).toString(); + System.out.println(expr + "\n" + query); + + expr = df.getOWLObjectIntersectionOf( + df.getOWLObjectSomeValuesFrom(propR, df.getOWLObjectIntersectionOf(df.getOWLObjectSomeValuesFrom(propS, clsA), clsC)), clsB); - query = converter.asQuery(rootVar, expr, Collections.singleton(propR)).toString(); + query = converter.asQuery(rootVar, expr, Sets.newHashSet(propR, propS)).toString(); System.out.println(expr + "\n" + query); } Modified: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 2013-05-08 19:07:53 UTC (rev 3962) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 2013-05-08 21:03:43 UTC (rev 3963) @@ -6,14 +6,10 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.SocketTimeoutException; import java.net.URI; import java.net.URL; -import java.sql.Connection; -import java.sql.DriverManager; import java.sql.SQLException; import java.text.DecimalFormat; import java.util.ArrayList; @@ -30,7 +26,6 @@ import java.util.Set; import java.util.TreeSet; import java.util.concurrent.TimeUnit; -import java.util.prefs.Preferences; import joptsimple.OptionException; import joptsimple.OptionParser; @@ -52,8 +47,6 @@ import org.dllearner.reasoning.SPARQLReasoner; import org.dllearner.utilities.owl.DLLearnerDescriptionConvertVisitor; import org.dllearner.utilities.owl.OWLClassExpressionToSPARQLConverter; -import org.ini4j.IniPreferences; -import org.ini4j.InvalidFileFormatException; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.OWLObjectRenderer; import org.semanticweb.owlapi.io.ToStringRenderer; @@ -92,7 +85,9 @@ import com.hp.hpl.jena.query.Syntax; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; import com.hp.hpl.jena.vocabulary.RDF; @@ -298,6 +293,7 @@ } catch (FileNotFoundException e) { e.printStackTrace(); } + filterModel(fragment); class2Fragment.put(cls, fragment); logger.info("...got " + fragment.size() + " triples."); continue; @@ -336,6 +332,19 @@ return class2Fragment; } + private void filterModel(Model model){ + List<Statement> statements2Remove = new ArrayList<Statement>(); + for (Statement st : model.listStatements().toSet()) { + if(st.getObject().isLiteral()){ + statements2Remove.add(st); + } + if(st.getPredicate().equals(RDF.type) && !st.getObject().asResource().getURI().startsWith("http://dbpedia.org/ontology/")){ + statements2Remove.add(st); + } + } + model.remove(statements2Remove); + } + private Map<OWLAxiom, Score> evaluate1(OWLAxiom pattern, NamedClass cls){ Map<OWLAxiom, Score> axioms2Score = new HashMap<OWLAxiom, Score>(); @@ -540,15 +549,11 @@ patternSubClass = cls; - // 2. execute SPARQL query on local model - Query query = QueryFactory.create( - "SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", patternSubClass) + "}", - Syntax.syntaxARQ); - int subClassCnt = QueryExecutionFactory.create(query, fragment).execSelect().next().getLiteral("cnt").getInt(); + Set<OWLEntity> signature = patternSuperClass.getSignature(); signature.remove(patternSubClass); - query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(patternSubClass, patternSuperClass), signature); + Query query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(patternSubClass, patternSuperClass), signature); logger.info("Running query\n" + query); Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); com.hp.hpl.jena.query.ResultSet rs = QueryExecutionFactory.create(query, fragment).execSelect(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |