From: <lor...@us...> - 2013-05-02 07:36:03
|
Revision: 3931 http://sourceforge.net/p/dl-learner/code/3931 Author: lorenz_b Date: 2013-05-02 07:35:58 +0000 (Thu, 02 May 2013) Log Message: ----------- Some improvements in pattern detection workflow. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java 2013-04-30 11:06:03 UTC (rev 3930) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java 2013-05-02 07:35:58 UTC (rev 3931) @@ -14,9 +14,11 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Queue; +import java.util.Set; import java.util.prefs.Preferences; import org.dllearner.kb.dataset.OWLOntologyDataset; @@ -26,6 +28,7 @@ import org.ini4j.InvalidFileFormatException; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.OWLObjectRenderer; +import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLDataFactory; @@ -70,6 +73,7 @@ private Connection conn; private PreparedStatement selectOntologyIdPs; private PreparedStatement insertOntologyPs; + private PreparedStatement insertOntologyErrorPs; private PreparedStatement selectPatternIdPs; private PreparedStatement insertPatternIdPs; private PreparedStatement insertOntologyPatternPs; @@ -91,7 +95,9 @@ createTables(); try { selectOntologyIdPs = conn.prepareStatement("SELECT id FROM Ontology WHERE url=?"); - insertOntologyPs = conn.prepareStatement("INSERT INTO Ontology (url, iri, repository) VALUES(?,?,?)"); + insertOntologyPs = conn.prepareStatement("INSERT INTO Ontology (url, iri, repository, logical_axioms, tbox_axioms, rbox_axioms" + + ", abox_axioms, classes, object_properties, data_properties, individuals) VALUES(?,?,?,?,?,?,?,?,?,?,?)"); + insertOntologyErrorPs = conn.prepareStatement("INSERT INTO Ontology (url, iri, repository) VALUES(?,?,?)"); selectPatternIdPs = conn.prepareStatement("SELECT id FROM Pattern WHERE pattern=?"); insertPatternIdPs = conn.prepareStatement("INSERT INTO Pattern (pattern,pattern_pretty) VALUES(?,?)"); insertOntologyPatternPs = conn.prepareStatement("INSERT INTO Ontology_Pattern (ontology_id, pattern_id, occurrences) VALUES(?,?,?)"); @@ -156,6 +162,14 @@ + "url VARCHAR(2000) NOT NULL," + "iri VARCHAR(2000) NOT NULL," + "repository VARCHAR(200) NOT NULL," + + "logical_axioms MEDIUMINT DEFAULT 0," + + "tbox_axioms MEDIUMINT DEFAULT 0," + + "rbox_axioms MEDIUMINT DEFAULT 0," + + "abox_axioms MEDIUMINT DEFAULT 0," + + "classes MEDIUMINT DEFAULT 0," + + "object_properties MEDIUMINT DEFAULT 0," + + "data_properties MEDIUMINT DEFAULT 0," + + "individuals MEDIUMINT DEFAULT 0," + "PRIMARY KEY(id)," + "INDEX(url)) DEFAULT CHARSET=utf8"); @@ -222,10 +236,10 @@ String url = physicalURI.toString(); //add ontology loading/parsing/... error entry try { - insertOntologyPs.setString(1, url); - insertOntologyPs.setString(2, "ERROR:" + ex.getClass().getSimpleName() + "->" + ex.getMessage()); - insertOntologyPs.setString(3, repository.getName()); - insertOntologyPs.execute(); + insertOntologyErrorPs.setString(1, url); + insertOntologyErrorPs.setString(2, "ERROR:" + ex.getClass().getSimpleName() + (ex.getMessage() != null ? ("->" + ex.getMessage()) : "")); + insertOntologyErrorPs.setString(3, repository.getName()); + insertOntologyErrorPs.execute(); } catch (SQLException e) { e.printStackTrace(); } @@ -233,7 +247,10 @@ private int addOntology(URI physicalURI, OWLOntology ontology){ String url = physicalURI.toString(); - String ontologyIRI = ontology.getOntologyID().getOntologyIRI().toString(); + String ontologyIRI = "Anonymous"; + if(!ontology.getOntologyID().isAnonymous()){ + ontologyIRI = ontology.getOntologyID().getOntologyIRI().toString(); + } //check for existing entry try { selectOntologyIdPs.setString(1, url); @@ -249,6 +266,20 @@ insertOntologyPs.setString(1, url); insertOntologyPs.setString(2, ontologyIRI); insertOntologyPs.setString(3, repository.getName()); + Set<OWLAxiom> logicalAxioms = new HashSet<OWLAxiom>(); + logicalAxioms.addAll(ontology.getLogicalAxioms()); + Set<OWLAxiom> tbox = AxiomType.getAxiomsOfTypes(logicalAxioms, new ArrayList<AxiomType>(AxiomType.TBoxAxiomTypes).toArray(new AxiomType[AxiomType.TBoxAxiomTypes.size()])); + Set<OWLAxiom> rbox = AxiomType.getAxiomsOfTypes(logicalAxioms, new ArrayList<AxiomType>(AxiomType.RBoxAxiomTypes).toArray(new AxiomType[AxiomType.RBoxAxiomTypes.size()])); + Set<OWLAxiom> abox = AxiomType.getAxiomsOfTypes(logicalAxioms, new ArrayList<AxiomType>(AxiomType.ABoxAxiomTypes).toArray(new AxiomType[AxiomType.ABoxAxiomTypes.size()])); + + insertOntologyPs.setInt(4, ontology.getLogicalAxiomCount()); + insertOntologyPs.setInt(5, tbox.size()); + insertOntologyPs.setInt(6, rbox.size()); + insertOntologyPs.setInt(7, abox.size()); + insertOntologyPs.setInt(8, ontology.getClassesInSignature(true).size()); + insertOntologyPs.setInt(9, ontology.getObjectPropertiesInSignature(true).size()); + insertOntologyPs.setInt(10, ontology.getDataPropertiesInSignature(true).size()); + insertOntologyPs.setInt(11, ontology.getIndividualsInSignature(true).size()); insertOntologyPs.execute(); } catch (SQLException e) { e.printStackTrace(); @@ -293,9 +324,11 @@ Multiset<OWLAxiom> allAxiomPatterns = HashMultiset.create(); for (OntologyRepositoryEntry entry : entries) { URI uri = entry.getPhysicalURI(); +// if(uri.toString().startsWith("http://rest.bioontology.org/bioportal/ontologies/download/42764")){ if (!ontologyProcessed(uri)) { System.out.println("Loading \"" + entry.getOntologyShortName() + "\" from "+ uri); try { + manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntology(IRI.create(uri)); Multiset<OWLAxiom> axiomPatterns = HashMultiset.create(); for (OWLLogicalAxiom axiom : ontology.getLogicalAxioms()) { 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-04-30 11:06:03 UTC (rev 3930) +++ trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java 2013-05-02 07:35:58 UTC (rev 3931) @@ -1,8 +1,10 @@ package org.dllearner.utilities.owl; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Stack; import org.aksw.commons.collections.diff.ModelDiff; @@ -31,6 +33,7 @@ import org.semanticweb.owlapi.model.OWLEntity; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLLiteral; +import org.semanticweb.owlapi.model.OWLNamedIndividual; import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; import org.semanticweb.owlapi.model.OWLObjectComplementOf; import org.semanticweb.owlapi.model.OWLObjectExactCardinality; @@ -62,7 +65,7 @@ private String sparql = ""; private Stack<String> variables = new Stack<String>(); - private Map<OWLEntity, String> variablesMapping; + private Map<OWLEntity, String> variablesMapping = new HashMap<OWLEntity, String>(); private int classCnt = 0; private int propCnt = 0; @@ -71,6 +74,7 @@ private OWLDataFactory df = new OWLDataFactoryImpl(); private Map<Integer, Boolean> intersection; + private Set<? extends OWLEntity> variableEntities; public OWLClassExpressionToSPARQLConverter() { } @@ -90,13 +94,37 @@ } public Query asQuery(String rootVariable, OWLClassExpression expr){ - String queryString = "SELECT DISTINCT " + rootVariable + " WHERE {"; - queryString += convert(rootVariable, expr); + return asQuery(rootVariable, expr, Collections.<OWLEntity>emptySet()); + } + + public Query asQuery(String rootVariable, OWLClassExpression expr, Set<? extends OWLEntity> variableEntities){ + this.variableEntities = variableEntities; + String queryString = "SELECT DISTINCT "; + String triplePattern = convert(rootVariable, expr); + if(variableEntities.isEmpty()){ + queryString += rootVariable + " WHERE {"; + } else { + for (OWLEntity owlEntity : variableEntities) { + String var = variablesMapping.get(owlEntity); + queryString += var + " "; + } + queryString += "COUNT(" + rootVariable + ") WHERE {"; + } + + queryString += triplePattern; queryString += "}"; + if(!variableEntities.isEmpty()){ + queryString += "GROUP BY "; + for (OWLEntity owlEntity : variableEntities) { + String var = variablesMapping.get(owlEntity); + queryString += var; + } + }System.out.println(queryString); return QueryFactory.create(queryString, Syntax.syntaxARQ); } private void reset(){ + variablesMapping.clear(); variables.clear(); classCnt = 0; propCnt = 0; @@ -143,7 +171,7 @@ private String triple(String subject, String predicate, String object){ return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + (predicate.startsWith("?") || predicate.equals("a") ? predicate : "<" + predicate + ">") + " " + - (object.startsWith("?") ? object : "<" + object + ">") + ".\n"; + (object.startsWith("?") ? object : object) + ".\n"; } private String triple(String subject, String predicate, OWLLiteral object){ @@ -152,12 +180,46 @@ render(object) + ".\n"; } + private String triple(String subject, String predicate, OWLEntity object){ + return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + + (predicate.startsWith("?") || predicate.equals("a") ? predicate : "<" + predicate + ">") + " " + + render(object) + ".\n"; + } + + private String triple(String subject, OWLEntity predicate, OWLEntity object){ + return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + + render(predicate) + " " + + render(object) + ".\n"; + } + + private String triple(String subject, OWLEntity predicate, String object){ + return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + + render(predicate) + " " + + object + ".\n"; + } + + private String triple(String subject, OWLEntity predicate, OWLLiteral object){ + return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + + render(predicate) + " " + + render(object) + ".\n"; + } + private String triple(String subject, String predicate, OWLIndividual object){ return (subject.startsWith("?") ? subject : "<" + subject + ">") + " " + (predicate.startsWith("?") || predicate.equals("a") ? predicate : "<" + predicate + ">") + " " + "<" + object.toStringID() + ">.\n"; } + private String render(OWLEntity entity){ + String s; + if(variableEntities.contains(entity)){ + s = getVariable(entity); + } else { + s = "<" + entity.toStringID() + ">"; + } + return s; + } + private String render(OWLLiteral literal){ return "\"" + literal + "\"^^<" + literal.getDatatype().toStringID() + ">"; } @@ -176,7 +238,7 @@ @Override public void visit(OWLClass ce) { - sparql += triple(variables.peek(), "a", ce.toStringID()); + sparql += triple(variables.peek(), "a", render(ce)); } @Override @@ -220,9 +282,9 @@ OWLObjectPropertyExpression propertyExpression = ce.getProperty(); if(propertyExpression.isAnonymous()){ //property expression is inverse of a property - sparql += triple(objectVariable, propertyExpression.getNamedProperty().toStringID(), variables.peek()); + sparql += triple(objectVariable, propertyExpression.getNamedProperty(), variables.peek()); } else { - sparql += triple(variables.peek(), propertyExpression.getNamedProperty().toStringID(), objectVariable); + sparql += triple(variables.peek(), propertyExpression.getNamedProperty(), objectVariable); } OWLClassExpression filler = ce.getFiller(); if(filler.isAnonymous()){ @@ -230,7 +292,7 @@ filler.accept(this); variables.pop(); } else { - sparql += triple(objectVariable, "a", filler.asOWLClass().toStringID()); + sparql += triple(objectVariable, "a", filler.asOWLClass()); } } @@ -240,7 +302,7 @@ String subject = variables.peek(); String objectVariable = buildIndividualVariable(); OWLObjectPropertyExpression propertyExpression = ce.getProperty(); - String predicate = propertyExpression.getNamedProperty().toStringID(); + OWLObjectProperty predicate = propertyExpression.getNamedProperty(); OWLClassExpression filler = ce.getFiller(); if(propertyExpression.isAnonymous()){ //property expression is inverse of a property @@ -269,12 +331,12 @@ @Override public void visit(OWLObjectHasValue ce) { OWLObjectPropertyExpression propertyExpression = ce.getProperty(); - OWLIndividual value = ce.getValue(); + OWLNamedIndividual value = ce.getValue().asOWLNamedIndividual(); if(propertyExpression.isAnonymous()){ //property expression is inverse of a property - sparql += triple(value.toStringID(), propertyExpression.getNamedProperty().toStringID(), variables.peek()); + sparql += triple(value.toStringID(), propertyExpression.getNamedProperty(), variables.peek()); } else { - sparql += triple(variables.peek(), propertyExpression.getNamedProperty().toStringID(), value.toStringID()); + sparql += triple(variables.peek(), propertyExpression.getNamedProperty(), value); } } @@ -287,9 +349,9 @@ sparql += "{SELECT " + subjectVariable + " WHERE {"; if(propertyExpression.isAnonymous()){ //property expression is inverse of a property - sparql += triple(objectVariable, propertyExpression.getNamedProperty().toStringID(), subjectVariable); + sparql += triple(objectVariable, propertyExpression.getNamedProperty(), subjectVariable); } else { - sparql += triple(subjectVariable, propertyExpression.getNamedProperty().toStringID(), objectVariable); + sparql += triple(subjectVariable, propertyExpression.getNamedProperty(), objectVariable); } OWLClassExpression filler = ce.getFiller(); if(filler.isAnonymous()){ @@ -299,7 +361,7 @@ filler.accept(this); variables.pop(); } else { - sparql += triple(objectVariable, "a", filler.asOWLClass().toStringID()); + sparql += triple(objectVariable, "a", filler.asOWLClass()); } sparql += "} GROUP BY " + subjectVariable + " HAVING(COUNT(" + objectVariable + ")>=" + cardinality + ")}"; @@ -315,9 +377,9 @@ sparql += "{SELECT " + subjectVariable + " WHERE {"; if(propertyExpression.isAnonymous()){ //property expression is inverse of a property - sparql += triple(objectVariable, propertyExpression.getNamedProperty().toStringID(), subjectVariable); + sparql += triple(objectVariable, propertyExpression.getNamedProperty(), subjectVariable); } else { - sparql += triple(subjectVariable, propertyExpression.getNamedProperty().toStringID(), objectVariable); + sparql += triple(subjectVariable, propertyExpression.getNamedProperty(), objectVariable); } OWLClassExpression filler = ce.getFiller(); if(filler.isAnonymous()){ @@ -327,7 +389,7 @@ filler.accept(this); variables.pop(); } else { - sparql += triple(objectVariable, "a", filler.asOWLClass().toStringID()); + sparql += triple(objectVariable, "a", filler.asOWLClass()); } sparql += "} GROUP BY " + subjectVariable + " HAVING(COUNT(" + objectVariable + ")=" + cardinality + ")}"; @@ -342,9 +404,9 @@ sparql += "{SELECT " + subjectVariable + " WHERE {"; if(propertyExpression.isAnonymous()){ //property expression is inverse of a property - sparql += triple(objectVariable, propertyExpression.getNamedProperty().toStringID(), subjectVariable); + sparql += triple(objectVariable, propertyExpression.getNamedProperty(), subjectVariable); } else { - sparql += triple(subjectVariable, propertyExpression.getNamedProperty().toStringID(), objectVariable); + sparql += triple(subjectVariable, propertyExpression.getNamedProperty(), objectVariable); } OWLClassExpression filler = ce.getFiller(); if(filler.isAnonymous()){ @@ -354,7 +416,7 @@ filler.accept(this); variables.pop(); } else { - sparql += triple(objectVariable, "a", filler.asOWLClass().toStringID()); + sparql += triple(objectVariable, "a", filler.asOWLClass()); } sparql += "} GROUP BY " + subjectVariable + " HAVING(COUNT(" + objectVariable + ")<=" + cardinality + ")}"; @@ -364,7 +426,7 @@ public void visit(OWLObjectHasSelf ce) { String subject = variables.peek(); OWLObjectPropertyExpression property = ce.getProperty(); - sparql += triple(subject, property.getNamedProperty().toStringID(), subject); + sparql += triple(subject, property.getNamedProperty(), subject); } @Override @@ -390,7 +452,7 @@ public void visit(OWLDataSomeValuesFrom ce) { String objectVariable = buildIndividualVariable(); OWLDataPropertyExpression propertyExpression = ce.getProperty(); - sparql += triple(variables.peek(), propertyExpression.asOWLDataProperty().toStringID(), objectVariable); + sparql += triple(variables.peek(), propertyExpression.asOWLDataProperty(), objectVariable); OWLDataRange filler = ce.getFiller(); variables.push(objectVariable); filler.accept(this); @@ -426,7 +488,7 @@ public void visit(OWLDataHasValue ce) { OWLDataPropertyExpression propertyExpression = ce.getProperty(); OWLLiteral value = ce.getValue(); - sparql += triple(variables.peek(), propertyExpression.asOWLDataProperty().toStringID(), value); + sparql += triple(variables.peek(), propertyExpression.asOWLDataProperty(), value); } @Override @@ -436,7 +498,7 @@ OWLDataPropertyExpression propertyExpression = ce.getProperty(); int cardinality = ce.getCardinality(); sparql += "{SELECT " + subjectVariable + " WHERE {"; - sparql += triple(subjectVariable, propertyExpression.asOWLDataProperty().toStringID(), objectVariable); + sparql += triple(subjectVariable, propertyExpression.asOWLDataProperty(), objectVariable); OWLDataRange filler = ce.getFiller(); variables.push(objectVariable); filler.accept(this); @@ -452,7 +514,7 @@ OWLDataPropertyExpression propertyExpression = ce.getProperty(); int cardinality = ce.getCardinality(); sparql += "{SELECT " + subjectVariable + " WHERE {"; - sparql += triple(subjectVariable, propertyExpression.asOWLDataProperty().toStringID(), objectVariable); + sparql += triple(subjectVariable, propertyExpression.asOWLDataProperty(), objectVariable); OWLDataRange filler = ce.getFiller(); variables.push(objectVariable); filler.accept(this); @@ -468,7 +530,7 @@ OWLDataPropertyExpression propertyExpression = ce.getProperty(); int cardinality = ce.getCardinality(); sparql += "{SELECT " + subjectVariable + " WHERE {"; - sparql += triple(subjectVariable, propertyExpression.asOWLDataProperty().toStringID(), objectVariable); + sparql += triple(subjectVariable, propertyExpression.asOWLDataProperty(), objectVariable); OWLDataRange filler = ce.getFiller(); variables.push(objectVariable); filler.accept(this); @@ -642,6 +704,13 @@ query = converter.asQuery(rootVar, expr).toString(); System.out.println(expr + "\n" + query); + //variable entity + expr = df.getOWLObjectIntersectionOf( + df.getOWLObjectSomeValuesFrom(propR, clsB), + clsB); + query = converter.asQuery(rootVar, expr, Collections.singleton(propR)).toString(); + System.out.println(expr + "\n" + query); + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |