You can subscribe to this list here.
2007 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
(120) |
Sep
(36) |
Oct
(116) |
Nov
(17) |
Dec
(44) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2008 |
Jan
(143) |
Feb
(192) |
Mar
(74) |
Apr
(84) |
May
(105) |
Jun
(64) |
Jul
(49) |
Aug
(120) |
Sep
(159) |
Oct
(156) |
Nov
(51) |
Dec
(28) |
2009 |
Jan
(17) |
Feb
(55) |
Mar
(33) |
Apr
(57) |
May
(54) |
Jun
(28) |
Jul
(6) |
Aug
(16) |
Sep
(38) |
Oct
(30) |
Nov
(26) |
Dec
(52) |
2010 |
Jan
(7) |
Feb
(91) |
Mar
(65) |
Apr
(2) |
May
(14) |
Jun
(25) |
Jul
(38) |
Aug
(48) |
Sep
(80) |
Oct
(70) |
Nov
(75) |
Dec
(77) |
2011 |
Jan
(68) |
Feb
(53) |
Mar
(51) |
Apr
(35) |
May
(65) |
Jun
(101) |
Jul
(29) |
Aug
(230) |
Sep
(95) |
Oct
(49) |
Nov
(110) |
Dec
(63) |
2012 |
Jan
(41) |
Feb
(42) |
Mar
(25) |
Apr
(46) |
May
(51) |
Jun
(44) |
Jul
(45) |
Aug
(29) |
Sep
(12) |
Oct
(9) |
Nov
(17) |
Dec
(2) |
2013 |
Jan
(12) |
Feb
(14) |
Mar
(7) |
Apr
(16) |
May
(54) |
Jun
(27) |
Jul
(11) |
Aug
(5) |
Sep
(85) |
Oct
(27) |
Nov
(37) |
Dec
(32) |
2014 |
Jan
(8) |
Feb
(29) |
Mar
(5) |
Apr
(3) |
May
(22) |
Jun
(3) |
Jul
(4) |
Aug
(3) |
Sep
|
Oct
|
Nov
|
Dec
|
From: <lor...@us...> - 2013-05-08 16:37:55
|
Revision: 3960 http://sourceforge.net/p/dl-learner/code/3960 Author: lorenz_b Date: 2013-05-08 16:37:49 +0000 (Wed, 08 May 2013) Log Message: ----------- Refactored pattern eval. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 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 15:05:35 UTC (rev 3959) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 2013-05-08 16:37:49 UTC (rev 3960) @@ -91,6 +91,7 @@ import com.hp.hpl.jena.rdf.model.Resource; 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; public class OWLAxiomPatternUsageEvaluation { @@ -118,7 +119,7 @@ private double threshold = 0.6; private OWLAnnotationProperty confidenceProperty = df.getOWLAnnotationProperty(IRI.create("http://dl-learner.org/pattern/confidence")); - private long maxFragmentExtractionTime = TimeUnit.SECONDS.toMillis(60); + private long maxFragmentExtractionTime = TimeUnit.SECONDS.toMillis(10); private OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); private long maxExecutionTime = TimeUnit.SECONDS.toMillis(20); private int queryLimit = 10000; @@ -321,7 +322,7 @@ long startTime = System.currentTimeMillis(); int offset = 0; boolean hasMoreResults = true; - while(hasMoreResults && (System.currentTimeMillis() - startTime)<= maxExecutionTime){ + while(hasMoreResults && (System.currentTimeMillis() - startTime)<= maxFragmentExtractionTime){ query.setOffset(offset);System.out.println(query); Model m = executeConstructQuery(query); fragment.add(m); @@ -563,26 +564,38 @@ qs = rs.next(); // get the IRIs for each variable Map<OWLEntity, IRI> entity2IRIMap = new HashMap<OWLEntity, IRI>(); - entity2IRIMap.put(patternSubClass.asOWLClass(), cls.getIRI()); + entity2IRIMap.put(pattern.getSubClass().asOWLClass(), cls.getIRI()); + boolean skip = false; for (OWLEntity entity : signature) { String var = variablesMapping.get(entity); + if(qs.get(var).isLiteral()){ + skip = true; + break; + } Resource resource = qs.getResource(var); + if(entity.isOWLObjectProperty() && resource.hasURI(RDF.type.getURI())){ + skip = true; + break; + } entity2IRIMap.put(entity, IRI.create(resource.getURI())); } - // instantiate the pattern - OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); - OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); - int patternInstantiationCnt = qs.getLiteral("cnt").getInt(); - // compute score - Score score; - try { - score = computeScore(subClassCnt, patternInstantiationCnt); - axioms2Score.put(patternInstantiation, score); - } catch (IllegalArgumentException e) { - // sometimes Virtuosos returns 'wrong' cnt values such that the - // success number as bigger than the total number of instances - e.printStackTrace(); + if(!skip){ + // instantiate the pattern + OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); + OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); + int patternInstantiationCnt = qs.getLiteral("cnt").getInt(); + // compute score + Score score; + try { + score = computeScore(subClassCnt, patternInstantiationCnt); + axioms2Score.put(patternInstantiation, score); + } catch (IllegalArgumentException e) { + // sometimes Virtuosos returns 'wrong' cnt values such that the + // success number as bigger than the total number of instances + e.printStackTrace(); + } } + } return axioms2Score; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-08 15:06:02
|
Revision: 3959 http://sourceforge.net/p/dl-learner/code/3959 Author: lorenz_b Date: 2013-05-08 15:05:35 +0000 (Wed, 08 May 2013) Log Message: ----------- Refactored pattern eval. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 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 12:59:45 UTC (rev 3958) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 2013-05-08 15:05:35 UTC (rev 3959) @@ -107,7 +107,9 @@ private Connection conn; private ExtractionDBCache cache = new ExtractionDBCache("pattern-cache"); - private SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia(), cache);//new LocalModelBasedSparqlEndpointKS(model); + private SparqlEndpoint endpoint = SparqlEndpoint.getEndpointDBpedia(); + + private SparqlEndpointKS ks = new SparqlEndpointKS(endpoint, cache);//new LocalModelBasedSparqlEndpointKS(model); private String ns = "http://dbpedia.org/ontology/"; private boolean fancyLatex = false; @@ -298,10 +300,12 @@ if (negation) { modalDepth--; } + System.out.println(modalDepth + ": " + pattern); maxModalDepth = Math.max(modalDepth, maxModalDepth); } } - return maxModalDepth; + return 3; +// return maxModalDepth; } private Map<NamedClass, Model> extractFragments(Collection<NamedClass> classes, int depth){ @@ -455,15 +459,15 @@ Map<OWLAxiom, Score> axioms2Score = new HashMap<OWLAxiom, Score>(); OWLClassExpression patternSubClass = ((OWLSubClassOfAxiom)pattern).getSubClass(); - OWLClassExpression superClass = ((OWLSubClassOfAxiom)pattern).getSuperClass(); + OWLClassExpression patternSuperClass = ((OWLSubClassOfAxiom)pattern).getSuperClass(); //set the subclass as a class from the KB - OWLClass subClass = df.getOWLClass(IRI.create(cls.getName())); + patternSubClass = df.getOWLClass(IRI.create(cls.getName())); //check if pattern is negation of something - boolean negation = superClass instanceof OWLObjectComplementOf; + boolean negation = patternSuperClass instanceof OWLObjectComplementOf; //build the query - Description d = DLLearnerDescriptionConvertVisitor.getDLLearnerDescription(superClass); + Description d = DLLearnerDescriptionConvertVisitor.getDLLearnerDescription(patternSuperClass); int modalDepth = d.getDepth(); if(negation){ modalDepth--; @@ -495,13 +499,13 @@ logger.info("...got " + fragment.size() + " triples."); //2. execute SPARQL query on local model - query = QueryFactory.create("SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", subClass) + "}",Syntax.syntaxARQ); + 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(); System.out.println(subClassCnt); - Set<OWLEntity> signature = superClass.getSignature(); - signature.remove(subClass); - query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature, true); + Set<OWLEntity> signature = patternSuperClass.getSignature(); + signature.remove(patternSubClass); + query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(patternSubClass, patternSuperClass), signature, true); Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); com.hp.hpl.jena.query.ResultSet rs = QueryExecutionFactory.create(query, fragment).execSelect(); QuerySolution qs; @@ -509,7 +513,7 @@ qs = rs.next(); //get the IRIs for each variable Map<OWLEntity, IRI> entity2IRIMap = new HashMap<OWLEntity, IRI>(); - entity2IRIMap.put(patternSubClass.asOWLClass(), subClass.getIRI()); + entity2IRIMap.put(patternSubClass.asOWLClass(), patternSubClass.asOWLClass().getIRI()); for (OWLEntity entity : signature) { String var = variablesMapping.get(entity); Resource resource = qs.getResource(var); @@ -539,6 +543,8 @@ OWLClassExpression patternSubClass = pattern.getSubClass(); OWLClassExpression patternSuperClass = pattern.getSuperClass(); + patternSubClass = cls; + // 2. execute SPARQL query on local model Query query = QueryFactory.create( "SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", patternSubClass) + "}", @@ -549,6 +555,7 @@ Set<OWLEntity> signature = patternSuperClass.getSignature(); signature.remove(patternSubClass); query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(patternSubClass, patternSuperClass), signature, true); + System.out.println(query); Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); com.hp.hpl.jena.query.ResultSet rs = QueryExecutionFactory.create(query, fragment).execSelect(); QuerySolution qs; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-08 12:59:48
|
Revision: 3958 http://sourceforge.net/p/dl-learner/code/3958 Author: lorenz_b Date: 2013-05-08 12:59:45 +0000 (Wed, 08 May 2013) Log Message: ----------- Continued pattern eval. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java 2013-05-07 20:50:56 UTC (rev 3957) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java 2013-05-08 12:59:45 UTC (rev 3958) @@ -579,7 +579,6 @@ writeSpace(); write(EQUIV); writeSpace(); - write(axiom.getSecondProperty()); axiom.getSecondProperty().accept(this); write("\\ensuremath{^-}"); } 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-07 20:50:56 UTC (rev 3957) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 2013-05-08 12:59:45 UTC (rev 3958) @@ -7,6 +7,7 @@ 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; @@ -33,13 +34,12 @@ import joptsimple.OptionParser; import joptsimple.OptionSet; -import org.apache.commons.lang.text.StrTokenizer; +import org.aksw.commons.collections.diff.ModelDiff; import org.apache.log4j.Logger; -import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; import org.coode.owlapi.turtle.TurtleOntologyFormat; import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.Score; -import org.dllearner.core.owl.EquivalentClassesAxiom; +import org.dllearner.core.owl.Description; import org.dllearner.core.owl.NamedClass; import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; import org.dllearner.kb.SparqlEndpointKS; @@ -49,13 +49,12 @@ import org.dllearner.learningproblems.AxiomScore; import org.dllearner.learningproblems.Heuristics; 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.OWLParserException; -import org.semanticweb.owlapi.io.StringDocumentSource; import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.IRI; @@ -66,30 +65,29 @@ import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLEntity; import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; +import org.semanticweb.owlapi.model.OWLObjectComplementOf; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; import org.semanticweb.owlapi.model.OWLOntologyStorageException; import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; -import org.semanticweb.owlapi.model.UnloadableImportException; import org.semanticweb.owlapi.util.OWLObjectDuplicator; import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; -import com.google.common.base.Charsets; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; -import com.google.common.hash.HashCode; -import com.google.common.hash.HashFunction; -import com.google.common.hash.Hashing; +import com.hp.hpl.jena.query.ParameterizedSparqlString; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.QuerySolution; 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.Resource; import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; @@ -109,7 +107,7 @@ private Connection conn; private ExtractionDBCache cache = new ExtractionDBCache("pattern-cache"); - private SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW(), cache);//new LocalModelBasedSparqlEndpointKS(model); + private SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia(), cache);//new LocalModelBasedSparqlEndpointKS(model); private String ns = "http://dbpedia.org/ontology/"; private boolean fancyLatex = false; @@ -118,6 +116,7 @@ private double threshold = 0.6; private OWLAnnotationProperty confidenceProperty = df.getOWLAnnotationProperty(IRI.create("http://dl-learner.org/pattern/confidence")); + private long maxFragmentExtractionTime = TimeUnit.SECONDS.toMillis(60); private OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); private long maxExecutionTime = TimeUnit.SECONDS.toMillis(20); private int queryLimit = 10000; @@ -185,7 +184,8 @@ for (NamedClass cls : classes) { logger.info("Processing " + cls + "..."); - Map<OWLAxiom, Score> result = evaluate2(pattern, cls); + Map<OWLAxiom, Score> result = evaluateUsingFragmentExtraction(pattern, cls); + axioms2Score.putAll(result); for (Entry<OWLAxiom, Score> entry : result.entrySet()) { OWLAxiom axiom = entry.getKey(); @@ -208,6 +208,135 @@ } } + public void runUsingFragmentExtraction(SparqlEndpoint endpoint, OWLOntology ontology, File outputFile, int maxNrOfTestedClasses){ + ks = new SparqlEndpointKS(endpoint, cache); + SPARQLReasoner reasoner = new SPARQLReasoner(ks, cache); + + //get the axiom patterns to evaluate + List<OWLAxiom> patterns = getPatternsToEvaluate(ontology); + + //get all classes in KB + Collection<NamedClass> classes = reasoner.getTypes(ns); + + //randomize and extract a chunk + List<NamedClass> classesList = new ArrayList<NamedClass>(classes); + Collections.shuffle(classesList, new Random(123)); + classesList = classesList.subList(0, maxNrOfTestedClasses); + classes = classesList; + + //get the maximum modal depth in the pattern axioms + int maxModalDepth = maxModalDepth(patterns); + + //extract fragment for each class only once + Map<NamedClass, Model> class2Fragment = extractFragments(classes, maxModalDepth); + + //for each pattern + for (OWLAxiom pattern : patterns) { + logger.info("Applying pattern " + pattern + "..."); + //if pattern is equivalent classes axiom, we need to get the subclass axiom where the named class is the subclass + if(pattern.isOfType(AxiomType.EQUIVALENT_CLASSES)){ + Set<OWLSubClassOfAxiom> subClassOfAxioms = ((OWLEquivalentClassesAxiom)pattern).asOWLSubClassOfAxioms(); + for (OWLSubClassOfAxiom axiom : subClassOfAxioms) { + if(!axiom.getSubClass().isAnonymous()){ + pattern = axiom; + break; + } + } + } + if(pattern.isOfType(AxiomType.SUBCLASS_OF)){ + Map<OWLAxiom, Score> axioms2Score = new LinkedHashMap<OWLAxiom, Score>(); + //for each class + int i = 1; + for (NamedClass cls : classes) { + logger.info("...on class " + cls + "..."); + Model fragment = class2Fragment.get(cls); + Map<OWLAxiom, Score> result = applyPattern((OWLSubClassOfAxiom) pattern, df.getOWLClass(IRI.create(cls.getName())), fragment); + axioms2Score.putAll(result); + + for (Entry<OWLAxiom, Score> entry : result.entrySet()) { + OWLAxiom axiom = entry.getKey(); + Score score = entry.getValue(); + if(score.getAccuracy() >= threshold){ + logger.info(axiom + "(" + format.format(score.getAccuracy()) + ")"); + } + } + + //wait some time to avoid flooding of endpoint + try { + Thread.sleep(waitingTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } +// if(i++ == 3) break; + } + save(pattern, axioms2Score); + } + } + } + + private int maxModalDepth(List<OWLAxiom> patterns) { + int maxModalDepth = 1; + for (OWLAxiom pattern : patterns) { + // get the superclass of the pattern + if (pattern.isOfType(AxiomType.EQUIVALENT_CLASSES)) { + Set<OWLSubClassOfAxiom> subClassOfAxioms = ((OWLEquivalentClassesAxiom) pattern) + .asOWLSubClassOfAxioms(); + for (OWLSubClassOfAxiom axiom : subClassOfAxioms) { + if (!axiom.getSubClass().isAnonymous()) { + pattern = axiom; + break; + } + } + } + if (pattern.isOfType(AxiomType.SUBCLASS_OF)) { + OWLClassExpression superClass = ((OWLSubClassOfAxiom) pattern).getSuperClass(); + // check if pattern is negation of something + boolean negation = superClass instanceof OWLObjectComplementOf; + // build the query + Description d = DLLearnerDescriptionConvertVisitor.getDLLearnerDescription(superClass); + int modalDepth = d.getDepth(); + if (negation) { + modalDepth--; + } + maxModalDepth = Math.max(modalDepth, maxModalDepth); + } + } + return maxModalDepth; + } + + private Map<NamedClass, Model> extractFragments(Collection<NamedClass> classes, int depth){ + Map<NamedClass, Model> class2Fragment = new HashMap<NamedClass, Model>(); + //get the maximum modal depth in the patterns + for (NamedClass cls : classes) { + logger.info("Extracting fragment for " + cls + "..."); + Model fragment = ModelFactory.createDefaultModel(); + //build the CONSTRUCT query + Query query = buildConstructQuery(cls, depth); + query.setLimit(queryLimit); + //get triples until time elapsed + long startTime = System.currentTimeMillis(); + int offset = 0; + boolean hasMoreResults = true; + while(hasMoreResults && (System.currentTimeMillis() - startTime)<= maxExecutionTime){ + query.setOffset(offset);System.out.println(query); + Model m = executeConstructQuery(query); + fragment.add(m); + if(m.size() == 0){ + hasMoreResults = false; + } + offset += queryLimit; + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + logger.info("...got " + fragment.size() + " triples."); + class2Fragment.put(cls, fragment); + } + return class2Fragment; + } + private Map<OWLAxiom, Score> evaluate1(OWLAxiom pattern, NamedClass cls){ Map<OWLAxiom, Score> axioms2Score = new HashMap<OWLAxiom, Score>(); @@ -322,7 +451,7 @@ return axioms2Score; } - private Map<OWLAxiom, Score> evaluate3(OWLAxiom pattern, NamedClass cls){ + private Map<OWLAxiom, Score> evaluateUsingFragmentExtraction(OWLAxiom pattern, NamedClass cls){ Map<OWLAxiom, Score> axioms2Score = new HashMap<OWLAxiom, Score>(); OWLClassExpression patternSubClass = ((OWLSubClassOfAxiom)pattern).getSubClass(); @@ -331,18 +460,51 @@ //set the subclass as a class from the KB OWLClass subClass = df.getOWLClass(IRI.create(cls.getName())); - //1. count number of instances in subclass expression - Query query = QueryFactory.create("SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", subClass) + "}",Syntax.syntaxARQ); - int subClassCnt = executeSelectQuery(query).next().getLiteral("cnt").getInt(); + //check if pattern is negation of something + boolean negation = superClass instanceof OWLObjectComplementOf; + //build the query + Description d = DLLearnerDescriptionConvertVisitor.getDLLearnerDescription(superClass); + int modalDepth = d.getDepth(); + if(negation){ + modalDepth--; + } + //depending on the modal depth, we have to get triples + Query query = buildConstructQuery(cls, modalDepth); + query.setLimit(queryLimit); - //2. get result + //1. get fragment until time elapsed + logger.info("Extracting fragment..."); + Model fragment = ModelFactory.createDefaultModel(); + long startTime = System.currentTimeMillis(); + int offset = 0; + boolean hasMoreResults = true; + while(hasMoreResults && (System.currentTimeMillis() - startTime)<= maxExecutionTime){ + query.setOffset(offset); + Model m = executeConstructQuery(query); + fragment.add(m); + if(m.size() == 0){ + hasMoreResults = false; + } + offset += queryLimit; + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + logger.info("...got " + fragment.size() + " triples."); + + //2. execute SPARQL query on local model + query = QueryFactory.create("SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", subClass) + "}",Syntax.syntaxARQ); + int subClassCnt = QueryExecutionFactory.create(query, fragment).execSelect().next().getLiteral("cnt").getInt(); + System.out.println(subClassCnt); + Set<OWLEntity> signature = superClass.getSignature(); signature.remove(subClass); - query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature); + query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature, true); Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); - com.hp.hpl.jena.query.ResultSet rs = executeSelectQuery(query); + com.hp.hpl.jena.query.ResultSet rs = QueryExecutionFactory.create(query, fragment).execSelect(); QuerySolution qs; - Multiset<OWLAxiom> instantiations = HashMultiset.create(); while(rs.hasNext()){ qs = rs.next(); //get the IRIs for each variable @@ -356,19 +518,98 @@ //instantiate the pattern OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); - - instantiations.add(patternInstantiation); + int patternInstantiationCnt = qs.getLiteral("cnt").getInt(); + //compute score + Score score; + try { + score = computeScore(subClassCnt, patternInstantiationCnt);//System.out.println(patternInstantiation + "(" + score.getAccuracy() + ")"); + axioms2Score.put(patternInstantiation, score); + } catch (IllegalArgumentException e) { + //sometimes Virtuosos returns 'wrong' cnt values such that the success number as bigger than the total number of instances + e.printStackTrace(); + } } - //compute the score - for (OWLAxiom axiom : instantiations.elementSet()) { - int frequency = instantiations.count(axiom); - Score score = new AxiomScore(frequency); - axioms2Score.put(axiom, score); - } return axioms2Score; } + private Map<OWLAxiom, Score> applyPattern(OWLSubClassOfAxiom pattern, OWLClass cls, Model fragment) { + Map<OWLAxiom, Score> axioms2Score = new HashMap<OWLAxiom, Score>(); + + OWLClassExpression patternSubClass = pattern.getSubClass(); + OWLClassExpression patternSuperClass = pattern.getSuperClass(); + + // 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(); + System.out.println(subClassCnt); + + Set<OWLEntity> signature = patternSuperClass.getSignature(); + signature.remove(patternSubClass); + query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(patternSubClass, patternSuperClass), signature, true); + Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); + com.hp.hpl.jena.query.ResultSet rs = QueryExecutionFactory.create(query, fragment).execSelect(); + QuerySolution qs; + while (rs.hasNext()) { + qs = rs.next(); + // get the IRIs for each variable + Map<OWLEntity, IRI> entity2IRIMap = new HashMap<OWLEntity, IRI>(); + entity2IRIMap.put(patternSubClass.asOWLClass(), cls.getIRI()); + for (OWLEntity entity : signature) { + String var = variablesMapping.get(entity); + Resource resource = qs.getResource(var); + entity2IRIMap.put(entity, IRI.create(resource.getURI())); + } + // instantiate the pattern + OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); + OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); + int patternInstantiationCnt = qs.getLiteral("cnt").getInt(); + // compute score + Score score; + try { + score = computeScore(subClassCnt, patternInstantiationCnt); + axioms2Score.put(patternInstantiation, score); + } catch (IllegalArgumentException e) { + // sometimes Virtuosos returns 'wrong' cnt values such that the + // success number as bigger than the total number of instances + e.printStackTrace(); + } + } + + return axioms2Score; + } + + private Query buildConstructQuery(NamedClass cls, int depth){ + StringBuilder sb = new StringBuilder(); + int maxVarCnt = 0; + sb.append("CONSTRUCT {\n"); + sb.append("?s").append("?p0 ").append("?o0").append(".\n"); + for(int i = 1; i < depth-1; i++){ + sb.append("?o").append(i-1).append(" ").append("?p").append(i).append(" ").append("?o").append(i).append(".\n"); + maxVarCnt++; + } + sb.append("?o").append(maxVarCnt).append(" a ?type.\n"); + sb.append("}\n"); + sb.append("WHERE {\n"); + sb.append("?s a ?cls."); + sb.append("?s").append("?p0 ").append("?o0").append(".\n"); + for(int i = 1; i < depth-1; i++){ + sb.append("OPTIONAL{\n"); + sb.append("?o").append(i-1).append(" ").append("?p").append(i).append(" ").append("?o").append(i).append(".\n"); + } + sb.append("OPTIONAL{?o").append(maxVarCnt).append(" a ?type}.\n"); + for(int i = 1; i < depth-1; i++){ + sb.append("}"); + } + + sb.append("}\n"); + ParameterizedSparqlString template = new ParameterizedSparqlString(sb.toString()); + template.setIri("cls", cls.getName()); + return template.asQuery(); + } + private void save(OWLAxiom pattern, Map<OWLAxiom, Score> axioms2Score){ try { Set<OWLAxiom> annotatedAxioms = new HashSet<OWLAxiom>(); @@ -382,11 +623,8 @@ } OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLOntology ontology = man.createOntology(annotatedAxioms); - HashFunction hf = Hashing.md5(); - HashCode hc = hf.newHasher() - .putString(pattern.toString(), Charsets.UTF_8) - .hash(); - man.saveOntology(ontology, new TurtleOntologyFormat(), new FileOutputStream(hc.toString() + "-pattern-instantiations.ttl")); + String filename = axiomRenderer.render(pattern).replace(" ", "_"); + man.saveOntology(ontology, new TurtleOntologyFormat(), new FileOutputStream(filename + "-instantiations.ttl")); } catch (OWLOntologyCreationException e) { e.printStackTrace(); } catch (OWLOntologyStorageException e) { @@ -484,6 +722,44 @@ return rs; } + protected Model executeConstructQuery(Query query) { + if(ks.isRemote()){ + SparqlEndpoint endpoint = ((SparqlEndpointKS) ks).getEndpoint(); + ExtractionDBCache cache = ks.getCache(); + Model model = null; + try { + if(cache != null){ + try { + model = cache.executeConstructQuery(endpoint, query.toString()); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } + } else { + QueryEngineHTTP queryExecution = new QueryEngineHTTP(endpoint.getURL().toString(), + query); + queryExecution.setDefaultGraphURIs(endpoint.getDefaultGraphURIs()); + queryExecution.setNamedGraphURIs(endpoint.getNamedGraphURIs()); + model = queryExecution.execConstruct(); + } + logger.debug("Got " + model.size() + " triples."); + return model; + } catch (QueryExceptionHTTP e) { + if(e.getCause() instanceof SocketTimeoutException){ + logger.warn("Got timeout"); + } else { + logger.error("Exception executing query", e); + } + return ModelFactory.createDefaultModel(); + } + } else { + QueryExecution queryExecution = QueryExecutionFactory.create(query, ((LocalModelBasedSparqlEndpointKS)ks).getModel()); + Model model = queryExecution.execConstruct(); + return model; + } + } + private Score computeScore(int total, int success){ double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(total, success); @@ -573,7 +849,7 @@ System.exit(0); } int maxNrOfTestedClasses = (Integer) options.valueOf("limit"); - new OWLAxiomPatternUsageEvaluation().run(endpoint, patternsOntology, outputFile, maxNrOfTestedClasses); + new OWLAxiomPatternUsageEvaluation().runUsingFragmentExtraction(endpoint, patternsOntology, outputFile, maxNrOfTestedClasses); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-07 20:50:59
|
Revision: 3957 http://sourceforge.net/p/dl-learner/code/3957 Author: lorenz_b Date: 2013-05-07 20:50:56 +0000 (Tue, 07 May 2013) Log Message: ----------- Added option to set a maximum number of tested classes for each pattern. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java Modified: trunk/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java 2013-05-07 06:24:03 UTC (rev 3956) +++ trunk/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java 2013-05-07 20:50:56 UTC (rev 3957) @@ -24,7 +24,7 @@ private Model baseModel; private List<String> namespaces; - private int maxRecursionDepth = 3; + private int maxRecursionDepth = 1; public ConciseBoundedDescriptionGeneratorImpl(SparqlEndpoint endpoint, ExtractionDBCache cache) { this.endpoint = endpoint; 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-07 06:24:03 UTC (rev 3956) +++ trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java 2013-05-07 20:50:56 UTC (rev 3957) @@ -103,6 +103,10 @@ } public Query asQuery(String rootVariable, OWLClassExpression expr, Set<? extends OWLEntity> variableEntities){ + return asQuery(rootVariable, expr, variableEntities, false); + } + + public Query asQuery(String rootVariable, OWLClassExpression expr, Set<? extends OWLEntity> variableEntities, boolean count){ this.variableEntities = variableEntities; String queryString = "SELECT DISTINCT "; String triplePattern = convert(rootVariable, expr); @@ -113,18 +117,25 @@ String var = variablesMapping.get(owlEntity); queryString += var + " "; } - queryString += "(COUNT(DISTINCT " + rootVariable + ") AS ?cnt) WHERE {"; + if(count){ + queryString += "(COUNT(DISTINCT " + rootVariable + ") AS ?cnt)"; + } else { + queryString += rootVariable; + } + queryString += " WHERE {"; } queryString += triplePattern; queryString += "}"; if(!variableEntities.isEmpty()){ - queryString += "GROUP BY "; - for (OWLEntity owlEntity : variableEntities) { - String var = variablesMapping.get(owlEntity); - queryString += var; + if(count){ + queryString += "GROUP BY "; + for (OWLEntity owlEntity : variableEntities) { + String var = variablesMapping.get(owlEntity); + queryString += var; + } + queryString += " ORDER BY DESC(?cnt)"; } - queryString += " ORDER BY DESC(?cnt)"; } return QueryFactory.create(queryString, Syntax.syntaxARQ); } Modified: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java 2013-05-07 06:24:03 UTC (rev 3956) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java 2013-05-07 20:50:56 UTC (rev 3957) @@ -248,7 +248,8 @@ write(SOME); writeSpace(); node.getProperty().accept(this); - writeSpace(); +// writeSpace(); + writeDot(); writeOpenBrace(); node.getValue().accept(this); writeCloseBrace(); Modified: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-07 06:24:03 UTC (rev 3956) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-07 20:50:56 UTC (rev 3957) @@ -378,7 +378,7 @@ } if(formatNumbers){ - latexTable += i + " & " + axiomColumn + " & " + "\\num{" + frequency + "} & " + df; + latexTable += i + ". & " + axiomColumn + " & " + "\\num{" + frequency + "} & " + df; for (OntologyRepository repository : repositories) { int rank = 0; boolean contained = false; 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-07 06:24:03 UTC (rev 3956) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 2013-05-07 20:50:56 UTC (rev 3957) @@ -12,11 +12,10 @@ import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; import java.text.DecimalFormat; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -25,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Random; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.prefs.Preferences; @@ -33,12 +33,13 @@ import joptsimple.OptionParser; import joptsimple.OptionSet; -import org.aksw.commons.util.Pair; +import org.apache.commons.lang.text.StrTokenizer; import org.apache.log4j.Logger; import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; import org.coode.owlapi.turtle.TurtleOntologyFormat; import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.Score; +import org.dllearner.core.owl.EquivalentClassesAxiom; import org.dllearner.core.owl.NamedClass; import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; import org.dllearner.kb.SparqlEndpointKS; @@ -64,6 +65,7 @@ import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLEntity; +import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; @@ -77,6 +79,8 @@ import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; import com.google.common.base.Charsets; +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multiset; import com.google.common.hash.HashCode; import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; @@ -105,7 +109,7 @@ private Connection conn; private ExtractionDBCache cache = new ExtractionDBCache("pattern-cache"); - private SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia(), cache);//new LocalModelBasedSparqlEndpointKS(model); + private SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW(), cache);//new LocalModelBasedSparqlEndpointKS(model); private String ns = "http://dbpedia.org/ontology/"; private boolean fancyLatex = false; @@ -113,6 +117,10 @@ private long waitingTime = TimeUnit.SECONDS.toMillis(3); private double threshold = 0.6; private OWLAnnotationProperty confidenceProperty = df.getOWLAnnotationProperty(IRI.create("http://dl-learner.org/pattern/confidence")); + + private OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); + private long maxExecutionTime = TimeUnit.SECONDS.toMillis(20); + private int queryLimit = 10000; public OWLAxiomPatternUsageEvaluation() { initDBConnection(); @@ -143,83 +151,50 @@ } } - public void run(SparqlEndpoint endpoint, OWLOntology ontology, File outputFile){ - ks = new SparqlEndpointKS(endpoint); + public void run(SparqlEndpoint endpoint, OWLOntology ontology, File outputFile, int maxNrOfTestedClasses){ + ks = new SparqlEndpointKS(endpoint, cache); SPARQLReasoner reasoner = new SPARQLReasoner(ks, cache); - OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); - //get the axiom patterns to evaluate - List<OWLAxiom> patterns = getPatternsToEvaluate(); + List<OWLAxiom> patterns = getPatternsToEvaluate(ontology); //get all classes in KB - Set<NamedClass> classes = reasoner.getTypes(ns); + Collection<NamedClass> classes = reasoner.getTypes(ns); + List<NamedClass> classesList = new ArrayList<NamedClass>(classes); + Collections.shuffle(classesList, new Random(123)); + classesList = classesList.subList(0, maxNrOfTestedClasses); + classes = classesList; //for each pattern for (OWLAxiom pattern : patterns) { + //if pattern is equivalent classes axiom, we need to get the subclass axiom where the named class is the subclass + if(pattern.isOfType(AxiomType.EQUIVALENT_CLASSES)){ + Set<OWLSubClassOfAxiom> subClassOfAxioms = ((OWLEquivalentClassesAxiom)pattern).asOWLSubClassOfAxioms(); + for (OWLSubClassOfAxiom axiom : subClassOfAxioms) { + if(!axiom.getSubClass().isAnonymous()){ + pattern = axiom; + break; + } + } + } if(pattern.isOfType(AxiomType.SUBCLASS_OF)){ logger.info("Processing " + pattern + "..."); - Set<EvaluatedAxiom> evaluatedAxioms = new HashSet<EvaluatedAxiom>(); Map<OWLAxiom, Score> axioms2Score = new LinkedHashMap<OWLAxiom, Score>(); - OWLClassExpression patternSubClass = ((OWLSubClassOfAxiom)pattern).getSubClass(); - OWLClassExpression superClass = ((OWLSubClassOfAxiom)pattern).getSuperClass(); //for each class int i = 1; for (NamedClass cls : classes) { logger.info("Processing " + cls + "..."); - //set the subclass as a class from the KB - OWLClass subClass = df.getOWLClass(IRI.create(cls.getName())); - //1. count number of instances in subclass expression - Query query = QueryFactory.create("SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", subClass) + "}",Syntax.syntaxARQ); - int subClassCnt = executeSelectQuery(query).next().getLiteral("cnt").getInt(); + Map<OWLAxiom, Score> result = evaluate2(pattern, cls); - //2. count number of instances in subclass AND superclass expression - //we have 2 options here to evaluate the whole axiom pattern: - //a) we replace all entities in the signature of the super class expression(except the subclass) with variables - //and GROUP BY them - //b) we replace only 1 entity with a variable, thus we have to try it for several combinations -// for (OWLEntity entity : signature) { -// //replace current entity with variable and for the rest use existing entities in KB -// query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature); -// } - Set<OWLEntity> signature = superClass.getSignature(); - signature.remove(subClass); - query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature); - query.setLimit(100); - Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping();System.out.println(query); - com.hp.hpl.jena.query.ResultSet rs = executeSelectQuery(query); - QuerySolution qs; - while(rs.hasNext()){ - qs = rs.next(); - //get the IRIs for each variable - Map<OWLEntity, IRI> entity2IRIMap = new HashMap<OWLEntity, IRI>(); - entity2IRIMap.put(patternSubClass.asOWLClass(), subClass.getIRI()); - for (OWLEntity entity : signature) { - String var = variablesMapping.get(entity); - Resource resource = qs.getResource(var); - entity2IRIMap.put(entity, IRI.create(resource.getURI())); + for (Entry<OWLAxiom, Score> entry : result.entrySet()) { + OWLAxiom axiom = entry.getKey(); + Score score = entry.getValue(); + if(score.getAccuracy() >= threshold){ + logger.info(axiom + "(" + format.format(score.getAccuracy()) + ")"); } - //instantiate the pattern - OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); - OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); - int patternInstantiationCnt = qs.getLiteral("cnt").getInt(); - //compute score - Score score; - try { - score = computeScore(subClassCnt, patternInstantiationCnt); - axioms2Score.put(patternInstantiation, score); - logger.info(patternInstantiation + "(" + format.format(score.getAccuracy()) + ")"); - } catch (IllegalArgumentException e) { - //sometimes Virtuosos returns 'wrong' cnt values such that the success number as bigger than the total number of instances - e.printStackTrace(); - } - -// //convert into EvaluatedAxiom such we can serialize it as RDF with accuracy value as annotation -// EvaluatedAxiom evaluatedAxiom = new EvaluatedAxiom(DLLearnerAxiomConvertVisitor.getDLLearnerAxiom(patternInstantiation), score); -// evaluatedAxioms.add(evaluatedAxiom); - } + //wait some time to avoid flooding of endpoint try { Thread.sleep(waitingTime); @@ -233,6 +208,167 @@ } } + private Map<OWLAxiom, Score> evaluate1(OWLAxiom pattern, NamedClass cls){ + Map<OWLAxiom, Score> axioms2Score = new HashMap<OWLAxiom, Score>(); + + OWLClassExpression patternSubClass = ((OWLSubClassOfAxiom)pattern).getSubClass(); + OWLClassExpression superClass = ((OWLSubClassOfAxiom)pattern).getSuperClass(); + + //set the subclass as a class from the KB + OWLClass subClass = df.getOWLClass(IRI.create(cls.getName())); + + //1. count number of instances in subclass expression + Query query = QueryFactory.create("SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", subClass) + "}",Syntax.syntaxARQ); + int subClassCnt = executeSelectQuery(query).next().getLiteral("cnt").getInt(); + + //2. replace all entities which are not the subclass, GROUP BY and COUNT + Set<OWLEntity> signature = superClass.getSignature(); + signature.remove(subClass); + query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature, true); + Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); + com.hp.hpl.jena.query.ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + //get the IRIs for each variable + Map<OWLEntity, IRI> entity2IRIMap = new HashMap<OWLEntity, IRI>(); + entity2IRIMap.put(patternSubClass.asOWLClass(), subClass.getIRI()); + for (OWLEntity entity : signature) { + String var = variablesMapping.get(entity); + Resource resource = qs.getResource(var); + entity2IRIMap.put(entity, IRI.create(resource.getURI())); + } + //instantiate the pattern + OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); + OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); + int patternInstantiationCnt = qs.getLiteral("cnt").getInt(); + //compute score + Score score; + try { + score = computeScore(subClassCnt, patternInstantiationCnt); + axioms2Score.put(patternInstantiation, score); + } catch (IllegalArgumentException e) { + //sometimes Virtuosos returns 'wrong' cnt values such that the success number as bigger than the total number of instances + e.printStackTrace(); + } + } + + return axioms2Score; + } + + private Map<OWLAxiom, Score> evaluate2(OWLAxiom pattern, NamedClass cls){ + Map<OWLAxiom, Score> axioms2Score = new HashMap<OWLAxiom, Score>(); + + OWLClassExpression patternSubClass = ((OWLSubClassOfAxiom)pattern).getSubClass(); + OWLClassExpression superClass = ((OWLSubClassOfAxiom)pattern).getSuperClass(); + + //set the subclass as a class from the KB + OWLClass subClass = df.getOWLClass(IRI.create(cls.getName())); + + //1. convert class expression, replace non-subclass entities and get result + Set<OWLEntity> signature = superClass.getSignature(); + signature.remove(subClass); + + Query query; + Multiset<OWLAxiom> instantiations = HashMultiset.create(); + Set<String> resources = new HashSet<String>();//we need the number of distinct resources (?x) to compute the score + long startTime = System.currentTimeMillis(); + int offset = 0; + boolean hasMoreResults = true; + while(hasMoreResults && (System.currentTimeMillis() - startTime)<= maxExecutionTime){ + query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature); + query.setLimit(queryLimit); + query.setOffset(offset); + System.out.println(query); + Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); + com.hp.hpl.jena.query.ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + if(!rs.hasNext()){ + hasMoreResults = false; + } + while(rs.hasNext()){ + qs = rs.next(); + resources.add(qs.getResource("x").getURI()); + //get the IRIs for each variable + Map<OWLEntity, IRI> entity2IRIMap = new HashMap<OWLEntity, IRI>(); + entity2IRIMap.put(patternSubClass.asOWLClass(), subClass.getIRI()); + for (OWLEntity entity : signature) { + String var = variablesMapping.get(entity); + Resource resource = qs.getResource(var); + entity2IRIMap.put(entity, IRI.create(resource.getURI())); + } + //instantiate the pattern + OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); + OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); + + instantiations.add(patternInstantiation); + } + offset += queryLimit; + try { + Thread.sleep(300); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + //compute the score + int total = resources.size(); + for (OWLAxiom axiom : instantiations.elementSet()) { + int frequency = instantiations.count(axiom); + Score score = computeScore(total, Math.min(total, frequency)); + axioms2Score.put(axiom, score); + } + + return axioms2Score; + } + + private Map<OWLAxiom, Score> evaluate3(OWLAxiom pattern, NamedClass cls){ + Map<OWLAxiom, Score> axioms2Score = new HashMap<OWLAxiom, Score>(); + + OWLClassExpression patternSubClass = ((OWLSubClassOfAxiom)pattern).getSubClass(); + OWLClassExpression superClass = ((OWLSubClassOfAxiom)pattern).getSuperClass(); + + //set the subclass as a class from the KB + OWLClass subClass = df.getOWLClass(IRI.create(cls.getName())); + + //1. count number of instances in subclass expression + Query query = QueryFactory.create("SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", subClass) + "}",Syntax.syntaxARQ); + int subClassCnt = executeSelectQuery(query).next().getLiteral("cnt").getInt(); + + //2. get result + Set<OWLEntity> signature = superClass.getSignature(); + signature.remove(subClass); + query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature); + Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); + com.hp.hpl.jena.query.ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + Multiset<OWLAxiom> instantiations = HashMultiset.create(); + while(rs.hasNext()){ + qs = rs.next(); + //get the IRIs for each variable + Map<OWLEntity, IRI> entity2IRIMap = new HashMap<OWLEntity, IRI>(); + entity2IRIMap.put(patternSubClass.asOWLClass(), subClass.getIRI()); + for (OWLEntity entity : signature) { + String var = variablesMapping.get(entity); + Resource resource = qs.getResource(var); + entity2IRIMap.put(entity, IRI.create(resource.getURI())); + } + //instantiate the pattern + OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); + OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); + + instantiations.add(patternInstantiation); + } + //compute the score + for (OWLAxiom axiom : instantiations.elementSet()) { + int frequency = instantiations.count(axiom); + Score score = new AxiomScore(frequency); + axioms2Score.put(axiom, score); + } + + return axioms2Score; + } + private void save(OWLAxiom pattern, Map<OWLAxiom, Score> axioms2Score){ try { Set<OWLAxiom> annotatedAxioms = new HashSet<OWLAxiom>(); @@ -278,15 +414,6 @@ } } - public List<OWLAxiom> getPatternsToEvaluate(){ - List<OWLAxiom> axiomPatterns = new ArrayList<OWLAxiom>(); - - Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(AxiomTypeCategory.TBox, 10); - axiomPatterns.addAll(topNAxiomPatterns.keySet()); - - return axiomPatterns; - } - public List<OWLAxiom> getPatternsToEvaluate(OWLOntology ontology){ List<OWLAxiom> axiomPatterns = new ArrayList<OWLAxiom>(); @@ -295,53 +422,6 @@ return axiomPatterns; } - - private String asLatex(String title, Map<OWLAxiom, Pair<Integer, Integer>> topN){ - String latexTable = "\\begin{table}\n"; - latexTable += "\\begin{tabular}{lrr}\n"; - latexTable += "\\toprule\n"; - latexTable += "Pattern & Frequency & \\#Ontologies\\\\\\midrule\n"; - - for (Entry<OWLAxiom, Pair<Integer, Integer>> entry : topN.entrySet()) { - OWLAxiom axiom = entry.getKey(); - Integer frequency = entry.getValue().getKey(); - Integer idf = entry.getValue().getValue(); - - if(axiom != null){ - String axiomColumn = axiomRenderer.render(axiom); - if(fancyLatex){ - axiomColumn = "\\begin{lstlisting}[language=manchester]" + axiomColumn + "\\end{lstlisting}"; - } - latexTable += axiomColumn + " & " + frequency + " & " + idf + "\\\\\n"; - } - } - latexTable += "\\bottomrule\n\\end{tabular}\n"; - latexTable += "\\caption{" + title + "}\n"; - latexTable += "\\end{table}\n"; - return latexTable; - } - - private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(AxiomTypeCategory axiomType, int n){ - Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); - PreparedStatement ps; - ResultSet rs; - try { - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences),COUNT(ontology_id) FROM " + - "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + - "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + - "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, axiomType.name()); - ps.setInt(2, n); - rs = ps.executeQuery(); - while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), new Pair<Integer, Integer>(rs.getInt(2), rs.getInt(3))); - } - } catch(SQLException e){ - e.printStackTrace(); - } - return topN; - } - protected com.hp.hpl.jena.query.ResultSet executeSelectQuery(Query query) { com.hp.hpl.jena.query.ResultSet rs = null; if(ks.isRemote()){ @@ -414,27 +494,6 @@ return new AxiomScore(accuracy, confidence, total, success, total-success); } - private OWLAxiom asOWLAxiom(String functionalSyntaxAxiomString){ - try { - StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org> " + functionalSyntaxAxiomString + ")"); - OWLFunctionalSyntaxOWLParser p = new OWLFunctionalSyntaxOWLParser(); - OWLOntology newOntology = OWLManager.createOWLOntologyManager().createOntology(); - p.parse(s, newOntology); - if(!newOntology.getLogicalAxioms().isEmpty()){ - return newOntology.getLogicalAxioms().iterator().next(); - } - } catch (UnloadableImportException e) { - e.printStackTrace(); - } catch (OWLOntologyCreationException e) { - e.printStackTrace(); - } catch (OWLParserException e) { - System.err.println("Parsing failed for axiom " + functionalSyntaxAxiomString); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - public static void main(String[] args) throws Exception { ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); @@ -450,6 +509,8 @@ "The ontology file which contains the patterns.").withOptionalArg().ofType(File.class); parser.acceptsAll(asList("o", "output"), "Specify a file where the output can be written.") .withOptionalArg().ofType(File.class); + parser.acceptsAll(asList("l", "limit"), "Specify the maximum number of classes tested for each pattern.") + .withRequiredArg().ofType(Integer.class); // parse options and display a message for the user in case of problems OptionSet options = null; @@ -503,7 +564,7 @@ System.out.println("The specified ontology patterns file can not be found."); System.exit(0); } - OWLOntology ontology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(patternsFile); + OWLOntology patternsOntology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(patternsFile); File outputFile = null; try { outputFile = (File) options.valueOf("output"); @@ -511,7 +572,8 @@ System.out.println("The specified output file can not be found."); System.exit(0); } - new OWLAxiomPatternUsageEvaluation().run(endpoint, ontology, outputFile); + int maxNrOfTestedClasses = (Integer) options.valueOf("limit"); + new OWLAxiomPatternUsageEvaluation().run(endpoint, patternsOntology, outputFile, maxNrOfTestedClasses); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-07 06:24:07
|
Revision: 3956 http://sourceforge.net/p/dl-learner/code/3956 Author: lorenz_b Date: 2013-05-07 06:24:03 +0000 (Tue, 07 May 2013) Log Message: ----------- Added Latex DLSyntax renderer. Added Paths: ----------- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java Added: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java (rev 0) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/LatexObjectVisitor.java 2013-05-07 06:24:03 UTC (rev 3956) @@ -0,0 +1,938 @@ +/* + * This file is part of the OWL API. + * + * The contents of this file are subject to the LGPL License, Version 3.0. + * + * Copyright (C) 2011, The University of Manchester + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + * + * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 + * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. + * + * Copyright 2011, University of Manchester + * + * Licensed 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.dllearner.scripts.pattern; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.coode.owlapi.latex.LatexBracketChecker; +import org.coode.owlapi.latex.LatexWriter; +import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.util.CollectionFactory; +import org.semanticweb.owlapi.util.ShortFormProvider; +import org.semanticweb.owlapi.util.SimpleShortFormProvider; + +/** + * Author: Matthew Horridge<br> + * The University Of Manchester<br> + * Medical Informatics Group<br> + * Date: 15-Jun-2006<br> + * <br> + * + * NOTE: this class was not designed as a general purpose renderer, i.e., + * some ontologies might be misrepresented in the output. Please report + * any formatting error you find to the bug tracker or the mailing list. + */ +@SuppressWarnings({"unused","javadoc"}) +public class LatexObjectVisitor implements OWLObjectVisitor { + public static final String AND = "\\ensuremath{\\sqcap}"; + public static final String OR = "\\ensuremath{\\sqcup}"; + public static final String NOT = "\\ensuremath{\\lnot}"; + public static final String ALL = "\\ensuremath{\\forall}"; + public static final String SOME = "\\ensuremath{\\exists}"; + public static final String HASVALUE = "\\ensuremath{hasValue}"; + public static final String MIN = "\\ensuremath{\\geq}"; + public static final String MAX = "\\ensuremath{\\leq}"; + public static final String EQUAL = "\\ensuremath{=}"; + public static final String SUBCLASS = "\\ensuremath{\\sqsubseteq}"; + public static final String EQUIV = "\\ensuremath{\\equiv}"; + public static final String NOT_EQUIV = "\\ensuremath{\\not\\equiv}"; + public static final String TOP = "\\ensuremath{\\top}"; + public static final String BOTTOM = "\\ensuremath{\\bot}"; + public static final String SELF = "\\ensuremath{\\Self}"; + public static final String CIRC = "\\ensuremath{\\circ}"; + private OWLObject subject; + private LatexWriter writer; + private boolean prettyPrint = true; + private OWLDataFactory df; + private ShortFormProvider shortFormProvider; + + public LatexObjectVisitor(LatexWriter writer, OWLDataFactory df) { + this.writer = writer; + this.df = df; + shortFormProvider = new SimpleShortFormProvider(); + subject = df.getOWLThing(); + } + + public void setSubject(OWLObject subject) { + this.subject = subject; + } + + public void setShortFormProvider(ShortFormProvider shortFormProvder) { + this.shortFormProvider = shortFormProvder; + } + + private void writeSpace() { + writer.writeSpace(); + } + + private void writeDot() { + writer.write("."); + } + + private void write(Object o) { + writer.write(o); + } + + private void writeOpenBrace() { + writer.writeOpenBrace(); + } + + private void writeCloseBrace() { + writer.writeCloseBrace(); + } + + public boolean isPrettyPrint() { + return prettyPrint; + } + + public void setPrettyPrint(boolean prettyPrint) { + this.prettyPrint = prettyPrint; + } + + // + // private void beginTabbing() { + // write("\\begin{tabbing}\n"); + // } + // + // private void endTabbing() { + // write("\\end{tabbing}\n"); + // } + public void visit(OWLObjectIntersectionOf node) { + for (Iterator<OWLClassExpression> it = node.getOperands().iterator(); it + .hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + writeSpace(); + write(AND); + writeSpace(); + } + } + } + + public void visit(OWLDataAllValuesFrom node) { + write(ALL); + writeSpace(); + node.getProperty().accept(this); + writeSpace(); + node.getFiller().accept(this); + } + + public void visit(OWLDataExactCardinality desc) { + write(EQUAL); + writeSpace(); + desc.getProperty().accept(this); + } + + public void visit(OWLDataMaxCardinality desc) { + write(MAX); + writeSpace(); + write(desc.getCardinality()); + writeSpace(); + desc.getProperty().accept(this); + } + + public void visit(OWLDataMinCardinality desc) { + write(MIN); + writeSpace(); + write(desc.getCardinality()); + writeSpace(); + desc.getProperty().accept(this); + } + + public void visit(OWLDataSomeValuesFrom node) { + write(SOME); + writeSpace(); + node.getProperty().accept(this); +// writeSpace(); + writeDot(); + node.getFiller().accept(this); + } + + public void visit(OWLDataHasValue node) { + write(HASVALUE); + writeSpace(); + node.getProperty().accept(this); +// writeSpace(); + writeDot(); + node.getValue().accept(this); + } + + public void visit(OWLObjectAllValuesFrom node) { + write(ALL); + writeSpace(); + node.getProperty().accept(this); +// writeSpace(); + writeDot(); + writeNested(node.getFiller()); + } + + public void visit(OWLObjectExactCardinality desc) { + write(EQUAL); + writeSpace(); + desc.getProperty().accept(this); +// writeSpace(); + writeDot(); + writeNested(desc.getFiller()); + } + + public void visit(OWLObjectMaxCardinality desc) { + write(MAX); + writeSpace(); + write(desc.getCardinality()); + writeSpace(); + desc.getProperty().accept(this); +// writeSpace(); + writeDot(); + writeNested(desc.getFiller()); + } + + public void visit(OWLObjectMinCardinality desc) { + write(MIN); + writeSpace(); + write(desc.getCardinality()); + writeSpace(); + desc.getProperty().accept(this); +// writeSpace(); + writeDot(); + writeNested(desc.getFiller()); + } + + public void visit(OWLObjectSomeValuesFrom node) { + write(SOME); + writeSpace(); + node.getProperty().accept(this); +// writeSpace(); + writeDot(); + writeNested(node.getFiller()); + } + + public void visit(OWLObjectHasValue node) { + write(SOME); + writeSpace(); + node.getProperty().accept(this); + writeSpace(); + writeOpenBrace(); + node.getValue().accept(this); + writeCloseBrace(); + } + + public void visit(OWLObjectComplementOf node) { + write(NOT); + writeNested(node.getOperand()); + } + + public void visit(OWLObjectUnionOf node) { + for (Iterator<OWLClassExpression> it = node.getOperands().iterator(); it + .hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + writeSpace(); + write(OR); + writeSpace(); + } + } + } + + public void visit(OWLClass node) { + write(escapeName(shortFormProvider.getShortForm(node))); + } + + public void visit(OWLObjectOneOf node) { + for (Iterator<OWLIndividual> it = node.getIndividuals().iterator(); it + .hasNext();) { + writeOpenBrace(); + it.next().accept(this); + writeCloseBrace(); + if (it.hasNext()) { + writeSpace(); + write(OR); + writeSpace(); + } + } + } + + public void visit(OWLDataProperty entity) { + write(escapeName(shortFormProvider.getShortForm(entity))); + } + + public void visit(OWLObjectProperty entity) { + write(escapeName(shortFormProvider.getShortForm(entity))); + } + + public void visit(OWLNamedIndividual entity) { + write(escapeName(shortFormProvider.getShortForm(entity))); + } + + public void visit(OWLObjectHasSelf desc) { + write(SOME); + writeSpace(); + desc.getProperty().accept(this); + writeSpace(); + write(SELF); + } + + public void visit(OWLDisjointClassesAxiom axiom) { + if (axiom.getClassExpressions().size() != 2) { + for (OWLClassExpression left : axiom.getClassExpressions()) { + for (OWLClassExpression right : axiom.getClassExpressions()) { + if (left != right) { + if (left.equals(subject)) { + left.accept(this); + writeSpace(); + write(SUBCLASS); + writeSpace(); + write(NOT); + writeSpace(); + right.accept(this); + } else { + right.accept(this); + writeSpace(); + write(SUBCLASS); + writeSpace(); + write(NOT); + writeSpace(); + left.accept(this); + } + writer.writeNewLine(); + } + } + } + } else { + Iterator<OWLClassExpression> it = axiom.getClassExpressions() + .iterator(); + OWLClassExpression descA = it.next(); + OWLClassExpression descB = it.next(); + OWLClassExpression lhs; + OWLClassExpression rhs; + if (descA.equals(subject)) { + lhs = descA; + rhs = descB; + } else { + lhs = descB; + rhs = descA; + } + lhs.accept(this); + writeSpace(); + write(SUBCLASS); + writeSpace(); + write(NOT); + writeSpace(); + rhs.accept(this); + } + } + + public void visit(OWLEquivalentClassesAxiom axiom) { + if (axiom.getClassExpressions().size() > 2) { + Set<Set<OWLClassExpression>> rendered = new HashSet<Set<OWLClassExpression>>(); + for (OWLClassExpression left : axiom.getClassExpressions()) { + for (OWLClassExpression right : axiom.getClassExpressions()) { + if (left != right) { + Set<OWLClassExpression> cur = CollectionFactory + .createSet(left, right); + if (!rendered.contains(cur)) { + rendered.add(cur); + left.accept(this); + writeSpace(); + write(EQUIV); + writeSpace(); + right.accept(this); + } + } + } + } + } else if (axiom.getClassExpressions().size() == 2) { + Iterator<OWLClassExpression> it = axiom.getClassExpressions() + .iterator(); + OWLClassExpression descA = it.next(); + OWLClassExpression descB = it.next(); + OWLClassExpression lhs; + OWLClassExpression rhs; + if (subject.equals(descA)) { + lhs = descA; + rhs = descB; + } else { + lhs = descB; + rhs = descA; + } + rhs.accept(this); + writeSpace(); + write(EQUIV); + writeSpace(); + lhs.accept(this); + } + } + + public void visit(OWLSubClassOfAxiom axiom) { + this.setPrettyPrint(false); + axiom.getSubClass().accept(this); + writeSpace(); + write(SUBCLASS); + writeSpace(); + axiom.getSuperClass().accept(this); + writeSpace(); + this.setPrettyPrint(true); + } + + public void visit(OWLClassAssertionAxiom axiom) { + axiom.getIndividual().accept(this); + writeSpace(); + write(":"); + writeSpace(); + axiom.getClassExpression().accept(this); + } + + public void visit(OWLAsymmetricObjectPropertyAxiom axiom) { + write("AsymmetricProperty"); + axiom.getProperty().accept(this); + } + + public void visit(OWLDataPropertyAssertionAxiom axiom) { + axiom.getProperty().accept(this); + writeSpace(); + write("("); + axiom.getSubject().accept(this); + writeSpace(); + axiom.getObject().accept(this); + write(")"); + } + + public void visit(OWLDataPropertyDomainAxiom axiom) { + df.getOWLDataSomeValuesFrom(axiom.getProperty(), df.getTopDatatype()) + .accept(this); + writeSpace(); + write(SUBCLASS); + writeSpace(); + axiom.getDomain().accept(this); + } + + public void visit(OWLDataPropertyRangeAxiom axiom) { + write(TOP); + writeSpace(); + write(SUBCLASS); + writeSpace(); + df.getOWLDataAllValuesFrom(axiom.getProperty(), axiom.getRange()) + .accept(this); + } + + public void visit(OWLSubDataPropertyOfAxiom axiom) { + axiom.getSubProperty(); + writeSpace(); + write(SUBCLASS); + writeSpace(); + axiom.getSuperProperty().accept(this); + } + + public void visit(OWLDeclarationAxiom axiom) { + write("Declaration"); + axiom.getEntity().accept(this); + } + + public void visit(OWLDifferentIndividualsAxiom axiom) { + for (Iterator<OWLIndividual> it = axiom.getIndividuals().iterator(); it + .hasNext();) { + write("\\{"); + it.next().accept(this); + write("\\}"); + if (it.hasNext()) { + writeSpace(); + write(NOT_EQUIV); + writeSpace(); + } + } + } + + public void visit(OWLDisjointDataPropertiesAxiom axiom) { + for (Iterator<OWLDataPropertyExpression> it = axiom.getProperties() + .iterator(); it.hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + writeSpace(); + write(NOT_EQUIV); + writeSpace(); + } + } + } + + public void visit(OWLDisjointObjectPropertiesAxiom axiom) { + write("DisjointObjectProperties"); + writeSpace(); + for (OWLObjectPropertyExpression p : axiom.getProperties()) { + p.accept(this); + writeSpace(); + } + } + + public void visit(OWLDisjointUnionAxiom axiom) { + write("DisjointUnion"); + writeSpace(); + for (OWLClassExpression p : axiom.getClassExpressions()) { + p.accept(this); + writeSpace(); + } + } + + public void visit(OWLAnnotationAssertionAxiom axiom) { + write("Annotation"); + axiom.getSubject().accept(this); + writeSpace(); + axiom.getProperty().accept(this); + writeSpace(); + axiom.getValue().accept(this); + } + + public void visit(OWLEquivalentDataPropertiesAxiom axiom) { + for (Iterator<OWLDataPropertyExpression> it = axiom.getProperties() + .iterator(); it.hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + writeSpace(); + write(NOT_EQUIV); + writeSpace(); + } + } + } + + public void visit(OWLEquivalentObjectPropertiesAxiom axiom) { + for (Iterator<OWLObjectPropertyExpression> it = axiom.getProperties() + .iterator(); it.hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + writeSpace(); + write(EQUIV); + writeSpace(); + } + } + } + + public void visit(OWLFunctionalDataPropertyAxiom axiom) { + write(TOP); + writeSpace(); + write(SUBCLASS); + writeSpace(); + df.getOWLDataMaxCardinality(1, axiom.getProperty()).accept(this); + } + + public void visit(OWLFunctionalObjectPropertyAxiom axiom) { + write(TOP); + writeSpace(); + write(SUBCLASS); + writeSpace(); + df.getOWLObjectMaxCardinality(1, axiom.getProperty()).accept(this); + } + + public void visit(OWLImportsDeclaration axiom) { + write("ImportsDeclaration"); + axiom.getIRI().accept(this); + } + + public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) { + write(TOP); + writeSpace(); + write(SUBCLASS); + writeSpace(); + OWLObjectPropertyExpression prop = df.getOWLObjectInverseOf(axiom + .getProperty()); + df.getOWLObjectMaxCardinality(1, prop).accept(this); + } + + public void visit(OWLInverseObjectPropertiesAxiom axiom) { + axiom.getFirstProperty().accept(this); + writeSpace(); + write(EQUIV); + writeSpace(); + write(axiom.getSecondProperty()); + axiom.getSecondProperty().accept(this); + write("\\ensuremath{^-}"); + } + + public void visit(OWLIrreflexiveObjectPropertyAxiom axiom) { + write("IrreflexiveObjectProperty"); + axiom.getProperty().accept(this); + } + + public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) { + write(NOT); + axiom.getProperty().accept(this); + write("("); + axiom.getSubject().accept(this); + write(", "); + axiom.getObject().accept(this); + write(")"); + } + + public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) { + write(NOT); + axiom.getProperty().accept(this); + write("("); + axiom.getSubject().accept(this); + write(", "); + axiom.getObject().accept(this); + write(")"); + } + + public void visit(OWLObjectPropertyAssertionAxiom axiom) { + axiom.getProperty().accept(this); + write("("); + axiom.getSubject().accept(this); + write(", "); + axiom.getObject().accept(this); + write(")"); + } + + public void visit(OWLSubPropertyChainOfAxiom axiom) { + for (Iterator<OWLObjectPropertyExpression> it = axiom + .getPropertyChain().iterator(); it.hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + writeSpace(); + write(CIRC); + writeSpace(); + } + } + writeSpace(); + write(SUBCLASS); + writeSpace(); + axiom.getSuperProperty().accept(this); + } + + public void visit(OWLObjectPropertyDomainAxiom axiom) { + df.getOWLObjectSomeValuesFrom(axiom.getProperty(), df.getOWLThing()) + .accept(this); + writeSpace(); + write(SUBCLASS); + writeSpace(); + axiom.getDomain().accept(this); + } + + public void visit(OWLObjectPropertyRangeAxiom axiom) { + write(TOP); + writeSpace(); + write(SUBCLASS); + writeSpace(); + df.getOWLObjectAllValuesFrom(axiom.getProperty(), axiom.getRange()) + .accept(this); + } + + public void visit(OWLSubObjectPropertyOfAxiom axiom) { + axiom.getSubProperty(); + writeSpace(); + write(SUBCLASS); + writeSpace(); + axiom.getSuperProperty().accept(this); + } + + public void visit(OWLReflexiveObjectPropertyAxiom axiom) { + write("ReflexiveProperty"); + axiom.getProperty().accept(this); + } + + public void visit(OWLSameIndividualAxiom axiom) { + for (Iterator<OWLIndividual> it = axiom.getIndividuals().iterator(); it + .hasNext();) { + write("\\{"); + it.next().accept(this); + write("\\}"); + if (it.hasNext()) { + writeSpace(); + write(EQUIV); + writeSpace(); + } + } + } + + public void visit(OWLSymmetricObjectPropertyAxiom axiom) { + axiom.getProperty().accept(this); + writeSpace(); + write(EQUIV); + writeSpace(); + axiom.getProperty().accept(this); + write("\\ensuremath{^-}"); + } + + public void visit(OWLDatatypeDefinitionAxiom axiom) { + write("Datatype("); + axiom.getDatatype().accept(this); + write(EQUIV); + axiom.getDataRange().accept(this); + } + + public void visit(OWLTransitiveObjectPropertyAxiom axiom) { + write("TransitiveProperty"); + writer.writeSpace(); + axiom.getProperty().accept(this); + } + + public void visit(SWRLRule rule) { + write("SWRLRule"); + for (SWRLAtom a : rule.getHead()) { + a.accept(this); + } + write("\\rightarrow"); + for (SWRLAtom a : rule.getBody()) { + a.accept(this); + } + } + + public void visit(SWRLVariable node) { + write(node.getIRI()); + } + + private void writeNested(OWLClassExpression classExpression) { + openBracket(classExpression); + classExpression.accept(this); + closeBracket(classExpression); + } + + private void writeNested(OWLObject expression) { + expression.accept(this); + } + + private void openBracket(OWLClassExpression classExpression) { + if (LatexBracketChecker.requiresBracket(classExpression)) { + write("("); + } + } + + private void closeBracket(OWLClassExpression classExpression) { + if (LatexBracketChecker.requiresBracket(classExpression)) { + write(")"); + } + } + + private String escapeName(String name) { + return name.replace("_", "\\_"); + } + + public void visit(OWLOntology ontology) { + } + + public void visit(OWLObjectInverseOf property) { + property.getInverse().accept(this); + write("\\ensuremath{^-}"); + } + + ////////////////////////////////////////////////////////////////////////////////////////////////// + public void visit(OWLDataComplementOf node) { + write(NOT); + writeNested(node.getDataRange()); + } + + public void visit(OWLDataOneOf node) { + for (Iterator<OWLLiteral> it = node.getValues().iterator(); it + .hasNext();) { + writeOpenBrace(); + it.next().accept(this); + writeCloseBrace(); + if (it.hasNext()) { + writeSpace(); + write(OR); + writeSpace(); + } + } + } + + public void visit(OWLFacetRestriction node) { + write("Facet"); + write(node.getFacet()); + node.getFacetValue().accept(this); + } + + public void visit(OWLDatatypeRestriction node) { + write("DatatypeRestriction"); + node.getDatatype().accept(this); + for (OWLFacetRestriction r : node.getFacetRestrictions()) { + writeSpace(); + r.accept(this); + } + } + + public void visit(OWLDatatype node) { + write("Datatype"); +// write(node.getIRI()); + writer.writeOpenBrace(); + write(escapeName(shortFormProvider.getShortForm(node))); + writer.writeCloseBrace(); + } + + public void visit(OWLLiteral node) { + write("\""); + write(node.getLiteral()); + write("\"\\^\\^"); + write(node.getDatatype().getIRI()); + } + + ////////////////////////////////////////////////////////////////////////////////////////////// + public void visit(SWRLLiteralArgument node) { + node.getLiteral().accept(this); + } + + public void visit(SWRLIndividualArgument node) { + node.getIndividual().accept(this); + } + + public void visit(SWRLBuiltInAtom node) { + node.getPredicate().accept(this); + for (SWRLDArgument d : node.getArguments()) { + writeSpace(); + d.accept(this); + } + } + + public void visit(SWRLClassAtom node) { + node.getArgument().accept(this); + } + + public void visit(SWRLDataRangeAtom node) { + node.getPredicate().accept(this); + } + + public void visit(SWRLDataPropertyAtom node) { + node.getPredicate().accept(this); + } + + public void visit(SWRLDifferentIndividualsAtom node) { + for (SWRLArgument a : node.getAllArguments()) { + writeSpace(); + a.accept(this); + } + } + + public void visit(SWRLObjectPropertyAtom node) { + node.getPredicate().accept(this); + } + + public void visit(SWRLSameIndividualAtom node) { + for (SWRLArgument a : node.getAllArguments()) { + writeSpace(); + a.accept(this); + } + } + + public void visit(OWLAnnotationProperty property) { + write("AnnotationProperty"); + property.getIRI().accept(this); + } + + public void visit(OWLAnnotation annotation) { + write("Annotation"); + annotation.getProperty().getIRI().accept(this); + annotation.getValue().accept(this); + } + + public void visit(OWLAnnotationPropertyDomainAxiom axiom) { + write("Domain"); + axiom.getProperty().getIRI().accept(this); + writeSpace(); + axiom.getDomain().accept(this); + } + + public void visit(OWLAnnotationPropertyRangeAxiom axiom) { + write("Range"); + axiom.getProperty().getIRI().accept(this); + writeSpace(); + axiom.getRange().accept(this); + } + + public void visit(OWLSubAnnotationPropertyOfAxiom axiom) { + axiom.getSubProperty(); + writeSpace(); + write(SUBCLASS); + writeSpace(); + axiom.getSuperProperty().accept(this); + } + + public void visit(OWLAnnotationValue value) { + value.accept(new OWLAnnotationValueVisitor() { + public void visit(IRI iri) { + iri.accept(LatexObjectVisitor.this); + } + + public void visit(OWLAnonymousIndividual individual) { + individual.accept(LatexObjectVisitor.this); + } + + public void visit(OWLLiteral literal) { + literal.accept(LatexObjectVisitor.this); + } + }); + } + + public void visit(OWLHasKeyAxiom axiom) { + write("HasKey"); + axiom.getClassExpression().accept(this); + for (OWLPropertyExpression<?, ?> p : axiom.getPropertyExpressions()) { + writeSpace(); + p.accept(this); + } + } + + public void visit(OWLDataIntersectionOf node) { + for (Iterator<OWLDataRange> it = node.getOperands().iterator(); it + .hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + writeSpace(); + write(AND); + writeSpace(); + } + } + } + + public void visit(OWLDataUnionOf node) { + for (Iterator<OWLDataRange> it = node.getOperands().iterator(); it + .hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + writeSpace(); + write(OR); + writeSpace(); + } + } + } + + public void visit(OWLAnonymousIndividual individual) { + write(individual.getID().toString()); + } + + public void visit(IRI iri) { + write(iri.getFragment()); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-07 06:23:02
|
Revision: 3955 http://sourceforge.net/p/dl-learner/code/3955 Author: lorenz_b Date: 2013-05-07 06:22:59 +0000 (Tue, 07 May 2013) Log Message: ----------- Improved Latex output. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-06 21:02:45 UTC (rev 3954) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-07 06:22:59 UTC (rev 3955) @@ -13,45 +13,32 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import java.util.prefs.Preferences; import org.aksw.commons.util.Pair; import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; -import org.coode.owlapi.latex.LatexObjectVisitor; import org.coode.owlapi.latex.LatexWriter; import org.dllearner.algorithms.pattern.OWLAxiomPatternFinder; -import org.dllearner.core.owl.NamedClass; -import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.repository.OntologyRepository; import org.dllearner.kb.repository.bioportal.BioPortalRepository; import org.dllearner.kb.repository.oxford.OxfordRepository; import org.dllearner.kb.repository.tones.TONESRepository; -import org.dllearner.kb.sparql.SparqlEndpoint; -import org.dllearner.reasoning.SPARQLReasoner; -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.OWLParserException; import org.semanticweb.owlapi.io.StringDocumentSource; -import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; import org.semanticweb.owlapi.model.UnloadableImportException; -import com.hp.hpl.jena.query.Query; - import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; @@ -66,7 +53,7 @@ private Connection conn; private boolean fancyLatex = false; - private boolean dlSyntax = false; + private boolean dlSyntax = true; private boolean formatNumbers = true; private int numberOfRowsPerTable = 25; @@ -338,7 +325,8 @@ } if(dlSyntax){ axiom.accept(renderer); - axiomColumn = sw.toString();sw.getBuffer().setLength(0); + axiomColumn = sw.toString(); + sw.getBuffer().setLength(0); } if(formatNumbers){ @@ -393,14 +381,16 @@ latexTable += i + " & " + axiomColumn + " & " + "\\num{" + frequency + "} & " + df; for (OntologyRepository repository : repositories) { int rank = 0; + boolean contained = false; Map<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> topNAxiomPatternsWithId = getTopNAxiomPatternsWithId(repository, axiomTypeCategory, 100); for (Entry<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> entry2 : topNAxiomPatternsWithId.entrySet()) { rank++; if(entry2.getKey() == patternId){ + contained = true; break; } } - latexTable += " & " + rank; + latexTable += " & " + (contained ? rank : "n/a"); } latexTable += "\\\\\n"; } else { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-06 21:02:48
|
Revision: 3954 http://sourceforge.net/p/dl-learner/code/3954 Author: lorenz_b Date: 2013-05-06 21:02:45 +0000 (Mon, 06 May 2013) Log Message: ----------- Improved Latex output. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java 2013-05-06 10:40:57 UTC (rev 3953) +++ trunk/components-core/src/main/java/org/dllearner/kb/sparql/ConciseBoundedDescriptionGeneratorImpl.java 2013-05-06 21:02:45 UTC (rev 3954) @@ -24,7 +24,7 @@ private Model baseModel; private List<String> namespaces; - private int maxRecursionDepth = 2; + private int maxRecursionDepth = 3; public ConciseBoundedDescriptionGeneratorImpl(SparqlEndpoint endpoint, ExtractionDBCache cache) { this.endpoint = endpoint; Modified: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-06 10:40:57 UTC (rev 3953) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-06 21:02:45 UTC (rev 3954) @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -67,6 +68,8 @@ private boolean fancyLatex = false; private boolean dlSyntax = false; private boolean formatNumbers = true; + + private int numberOfRowsPerTable = 25; public OWLAxiomPatternDetectionEvaluation() { initDBConnection(); @@ -107,7 +110,7 @@ makeRepositoryStatistics(repositories); //get top n TBox, RBox and ABox patterns - makePatternStatistics(repositories); + makePatternStatisticsSingleTable(repositories); } public void run(Collection<OntologyRepository> repositories){ @@ -132,7 +135,7 @@ } private void makePatternStatistics(Collection<OntologyRepository> repositories){ - int n = 10; + int n = numberOfRowsPerTable; String latex = ""; @@ -158,6 +161,26 @@ } } + private void makePatternStatisticsSingleTable(Collection<OntologyRepository> repositories){ + int n = numberOfRowsPerTable; + + String latex = ""; + + //total pattern statistics + for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { + Map<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> topNAxiomPatterns = getTopNAxiomPatternsWithId(axiomTypeCategory, n); + latex += asLatexWithId(axiomTypeCategory, topNAxiomPatterns, repositories, n) + "\n\n"; + } + + try { + new FileOutputStream("pattern-statistics.tex").write(latex.getBytes()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + private void makeRepositoryStatistics(Collection<OntologyRepository> repositories){ String latexTable = "\\begin{tabular}{lrr|rrr|rrr|rrr|rrr}"; latexTable += "\\toprule\n"; @@ -331,6 +354,66 @@ return latexTable; } + private String asLatexWithId(AxiomTypeCategory axiomTypeCategory, Map<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> topNAxiomPatterns, Collection<OntologyRepository> repositories, int n){ + StringWriter sw = new StringWriter(); + LatexWriter w = new LatexWriter(sw); + LatexObjectVisitor renderer = new LatexObjectVisitor(w, df); + String latexTable = "\\begin{table}\n"; + latexTable += "\\begin{tabular}{rlrr"; + for (int i = 0; i < repositories.size(); i++) { + latexTable += "r"; + } + latexTable += "}\n"; + latexTable += "\\toprule\n"; + latexTable += " & Pattern & Frequency & \\#Ontologies"; + for (OntologyRepository repository : repositories) { + latexTable += " & " + repository.getName(); + } + latexTable += "\\\\\\midrule\n"; + + int i = 0; + for (Entry<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> entry : topNAxiomPatterns.entrySet()) { + i++; + int patternId = entry.getKey(); + OWLAxiom axiom = entry.getValue().keySet().iterator().next(); + Integer frequency = entry.getValue().values().iterator().next().getKey(); + Integer df = entry.getValue().values().iterator().next().getValue(); + + if(axiom != null){ + String axiomColumn = axiomRenderer.render(axiom); + if(fancyLatex){ + axiomColumn = "\\begin{lstlisting}[language=manchester]" + axiomColumn + "\\end{lstlisting}"; + } + if(dlSyntax){ + axiom.accept(renderer); + axiomColumn = sw.toString();sw.getBuffer().setLength(0); + + } + if(formatNumbers){ + latexTable += i + " & " + axiomColumn + " & " + "\\num{" + frequency + "} & " + df; + for (OntologyRepository repository : repositories) { + int rank = 0; + Map<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> topNAxiomPatternsWithId = getTopNAxiomPatternsWithId(repository, axiomTypeCategory, 100); + for (Entry<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> entry2 : topNAxiomPatternsWithId.entrySet()) { + rank++; + if(entry2.getKey() == patternId){ + break; + } + } + latexTable += " & " + rank; + } + latexTable += "\\\\\n"; + } else { + latexTable += axiomColumn + " & " + frequency + " & " + df + "\\\\\n"; + } + } + } + latexTable += "\\bottomrule\n\\end{tabular}\n"; + latexTable += "\\caption{" + "Top " + n + " " + axiomTypeCategory.name() + " axiom patterns." + "}\n"; + latexTable += "\\end{table}\n"; + return latexTable; + } + private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(AxiomTypeCategory axiomType, int n){ Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); PreparedStatement ps; @@ -352,6 +435,53 @@ return topN; } + private Map<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> getTopNAxiomPatternsWithId(AxiomTypeCategory axiomType, int n){ + Map<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> topN = new LinkedHashMap<Integer, Map<OWLAxiom, Pair<Integer, Integer>>>(); + PreparedStatement ps; + ResultSet rs; + try { + ps = conn.prepareStatement("SELECT P.id, pattern,SUM(occurrences),COUNT(ontology_id) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, axiomType.name()); + ps.setInt(2, n); + rs = ps.executeQuery(); + while(rs.next()){ + Map<OWLAxiom, Pair<Integer, Integer>> m = new LinkedHashMap<OWLAxiom, Pair<Integer,Integer>>(); + m.put(asOWLAxiom(rs.getString(2)), new Pair<Integer, Integer>(rs.getInt(3), rs.getInt(4))); + topN.put(rs.getInt(1), m); + } + } catch(SQLException e){ + e.printStackTrace(); + } + return topN; + } + + private Map<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> getTopNAxiomPatternsWithId(OntologyRepository repository, AxiomTypeCategory axiomType, int n){ + Map<Integer, Map<OWLAxiom, Pair<Integer, Integer>>> topN = new LinkedHashMap<Integer, Map<OWLAxiom, Pair<Integer, Integer>>>(); + PreparedStatement ps; + ResultSet rs; + try { + ps = conn.prepareStatement("SELECT P.id, pattern,SUM(occurrences),COUNT(ontology_id) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=? AND O.repository=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, axiomType.name()); + ps.setString(2, repository.getName()); + ps.setInt(3, n); + rs = ps.executeQuery(); + while(rs.next()){ + Map<OWLAxiom, Pair<Integer, Integer>> m = new LinkedHashMap<OWLAxiom, Pair<Integer,Integer>>(); + m.put(asOWLAxiom(rs.getString(2)), new Pair<Integer, Integer>(rs.getInt(3), rs.getInt(4))); + topN.put(rs.getInt(1), m); + } + } catch(SQLException e){ + e.printStackTrace(); + } + return topN; + } + private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(OntologyRepository repository, AxiomTypeCategory axiomType, int n){ Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); PreparedStatement ps; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-06 10:41:00
|
Revision: 3953 http://sourceforge.net/p/dl-learner/code/3953 Author: lorenz_b Date: 2013-05-06 10:40:57 +0000 (Mon, 06 May 2013) Log Message: ----------- Improved Latex output. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-06 10:34:52 UTC (rev 3952) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-06 10:40:57 UTC (rev 3953) @@ -246,7 +246,7 @@ if(formatNumbers){ latexTable += - repository.getName() + "} & " + + repository.getName() + " & " + "\\num{" + numberOfOntologies + "} & " + "\\num{" + numberOfErrorOntologies + "} & " + "\\num{" + minNumberOfLogicalAxioms + "} & " + @@ -301,7 +301,7 @@ String latexTable = "\\begin{table}\n"; latexTable += "\\begin{tabular}{lrrr}\n"; latexTable += "\\toprule\n"; - latexTable += "Pattern & Frequency & \\#Ontologies & tf-idf \\\\\\midrule\n"; + latexTable += "Pattern & Frequency & \\#Ontologies\\\\\\midrule\n"; for (Entry<OWLAxiom, Pair<Integer, Integer>> entry : topN.entrySet()) { OWLAxiom axiom = entry.getKey(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-06 10:34:57
|
Revision: 3952 http://sourceforge.net/p/dl-learner/code/3952 Author: lorenz_b Date: 2013-05-06 10:34:52 +0000 (Mon, 06 May 2013) Log Message: ----------- Improved Latex output. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/kb/repository/bioportal/BioPortalRepository.java trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/components-core/src/main/java/org/dllearner/kb/repository/bioportal/BioPortalRepository.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/repository/bioportal/BioPortalRepository.java 2013-05-05 14:56:53 UTC (rev 3951) +++ trunk/components-core/src/main/java/org/dllearner/kb/repository/bioportal/BioPortalRepository.java 2013-05-06 10:34:52 UTC (rev 3952) @@ -26,6 +26,8 @@ private String apiKey = "8fadfa2c-47de-4487-a1f5-b7af7378d693"; private String serviceURL = "http://rest.bioontology.org/bioportal/ontologies"; + private boolean initialized = false; + private List<OntologyRepositoryEntry> entries = new ArrayList<OntologyRepositoryEntry>(); @Override @@ -41,6 +43,7 @@ @Override public void initialize() { refresh(); + initialized = true; } @Override @@ -90,6 +93,9 @@ @Override public Collection<OntologyRepositoryEntry> getEntries() { + if(!initialized){ + initialize(); + } return entries; } @@ -121,8 +127,6 @@ for(OntologyRepositoryEntry entry : entries){ System.out.println("Loading " + entry.getOntologyShortName()); System.out.println("From " + entry.getPhysicalURI()); - OWLOntology ont = OWLManager.createOWLOntologyManager().loadOntology(IRI.create(entry.getPhysicalURI())); - System.out.println(ont.getAxiomCount()); } } Modified: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-05 14:56:53 UTC (rev 3951) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-06 10:34:52 UTC (rev 3952) @@ -4,6 +4,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -21,6 +22,8 @@ import org.aksw.commons.util.Pair; import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; +import org.coode.owlapi.latex.LatexObjectVisitor; +import org.coode.owlapi.latex.LatexWriter; import org.dllearner.algorithms.pattern.OWLAxiomPatternFinder; import org.dllearner.core.owl.NamedClass; import org.dllearner.kb.SparqlEndpointKS; @@ -62,6 +65,8 @@ private Connection conn; private boolean fancyLatex = false; + private boolean dlSyntax = false; + private boolean formatNumbers = true; public OWLAxiomPatternDetectionEvaluation() { initDBConnection(); @@ -239,22 +244,41 @@ ps.setString(1, repository.getName()); avgNumberOfAboxAxioms = count(ps); - latexTable += - repository.getName() + "&" + - numberOfOntologies + "&" + - numberOfErrorOntologies + "&" + - minNumberOfLogicalAxioms + "&" + - avgNumberOfLogicalAxioms + "&" + - maxNumberOfLogicalAxioms + "&" + - minNumberOfTboxAxioms + "&" + - avgNumberOfTboxAxioms + "&" + - maxNumberOfTboxAxioms + "&" + - minNumberOfRboxAxioms + "&" + - avgNumberOfRboxAxioms + "&" + - maxNumberOfRboxAxioms + "&" + - minNumberOfAboxAxioms + "&" + - avgNumberOfAboxAxioms + "&" + - maxNumberOfAboxAxioms + "\\\\\n"; + if(formatNumbers){ + latexTable += + repository.getName() + "} & " + + "\\num{" + numberOfOntologies + "} & " + + "\\num{" + numberOfErrorOntologies + "} & " + + "\\num{" + minNumberOfLogicalAxioms + "} & " + + "\\num{" + avgNumberOfLogicalAxioms + "} & " + + "\\num{" + maxNumberOfLogicalAxioms + "} & " + + "\\num{" + minNumberOfTboxAxioms + "} & " + + "\\num{" + avgNumberOfTboxAxioms + "} & " + + "\\num{" + maxNumberOfTboxAxioms + "} & " + + "\\num{" + minNumberOfRboxAxioms + "} & " + + "\\num{" + avgNumberOfRboxAxioms + "} & " + + "\\num{" + maxNumberOfRboxAxioms + "} & " + + "\\num{" + minNumberOfAboxAxioms + "} & " + + "\\num{" + avgNumberOfAboxAxioms + "} & " + + "\\num{" + maxNumberOfAboxAxioms + "}\\\\\n"; + } else { + latexTable += + repository.getName() + " & " + + numberOfOntologies + " & " + + numberOfErrorOntologies + " & " + + minNumberOfLogicalAxioms + " & " + + avgNumberOfLogicalAxioms + " & " + + maxNumberOfLogicalAxioms + " & " + + minNumberOfTboxAxioms + " & " + + avgNumberOfTboxAxioms + " & " + + maxNumberOfTboxAxioms + " & " + + minNumberOfRboxAxioms + " & " + + avgNumberOfRboxAxioms + " & " + + maxNumberOfRboxAxioms + " & " + + minNumberOfAboxAxioms + " & " + + avgNumberOfAboxAxioms + " & " + + maxNumberOfAboxAxioms + "\\\\\n"; + } } catch (SQLException e) { e.printStackTrace(); @@ -271,22 +295,34 @@ } private String asLatex(String title, Map<OWLAxiom, Pair<Integer, Integer>> topN){ + StringWriter sw = new StringWriter(); + LatexWriter w = new LatexWriter(sw); + LatexObjectVisitor renderer = new LatexObjectVisitor(w, df); String latexTable = "\\begin{table}\n"; - latexTable += "\\begin{tabular}{lrr}\n"; + latexTable += "\\begin{tabular}{lrrr}\n"; latexTable += "\\toprule\n"; - latexTable += "Pattern & Frequency & \\#Ontologies\\\\\\midrule\n"; + latexTable += "Pattern & Frequency & \\#Ontologies & tf-idf \\\\\\midrule\n"; for (Entry<OWLAxiom, Pair<Integer, Integer>> entry : topN.entrySet()) { OWLAxiom axiom = entry.getKey(); Integer frequency = entry.getValue().getKey(); - Integer idf = entry.getValue().getValue(); + Integer df = entry.getValue().getValue(); if(axiom != null){ String axiomColumn = axiomRenderer.render(axiom); if(fancyLatex){ axiomColumn = "\\begin{lstlisting}[language=manchester]" + axiomColumn + "\\end{lstlisting}"; } - latexTable += axiomColumn + " & " + frequency + " & " + idf + "\\\\\n"; + if(dlSyntax){ + axiom.accept(renderer); + axiomColumn = sw.toString();sw.getBuffer().setLength(0); + + } + if(formatNumbers){ + latexTable += axiomColumn + " & " + "\\num{" + frequency + "} & " + df + "\\\\\n"; + } else { + latexTable += axiomColumn + " & " + frequency + " & " + df + "\\\\\n"; + } } } latexTable += "\\bottomrule\n\\end{tabular}\n"; @@ -394,6 +430,16 @@ return rs.getInt(1); } + private double tf_idf(int nrOfOccurrences, int nrOfOntologies, int nrOfOntologiesWithAxiom){ + double tf = nrOfOccurrences; + double idf = Math.log10(nrOfOntologies / nrOfOntologiesWithAxiom); + return tf * idf; + } + + private double popularity(int totalNrOfOntologies, int patternFrequency, int nrOfOntologiesWithPattern){ + return (double)Math.log10(patternFrequency) * (nrOfOntologiesWithPattern / (double)totalNrOfOntologies); + } + public static void main(String[] args) throws Exception { // ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); boolean analyzeRepositories = true; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-05 14:56:56
|
Revision: 3951 http://sourceforge.net/p/dl-learner/code/3951 Author: lorenz_b Date: 2013-05-05 14:56:53 +0000 (Sun, 05 May 2013) Log Message: ----------- Continued script to evaluate patterns. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 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-05 14:24:32 UTC (rev 3950) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 2013-05-05 14:56:53 UTC (rev 3951) @@ -1,10 +1,15 @@ package org.dllearner.scripts.pattern; +import static java.util.Arrays.asList; + +import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.SocketTimeoutException; +import java.net.URI; +import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -16,6 +21,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -23,6 +29,10 @@ import java.util.concurrent.TimeUnit; import java.util.prefs.Preferences; +import joptsimple.OptionException; +import joptsimple.OptionParser; +import joptsimple.OptionSet; + import org.aksw.commons.util.Pair; import org.apache.log4j.Logger; import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; @@ -38,7 +48,6 @@ import org.dllearner.learningproblems.AxiomScore; import org.dllearner.learningproblems.Heuristics; import org.dllearner.reasoning.SPARQLReasoner; -import org.dllearner.utilities.owl.DLLearnerAxiomConvertVisitor; import org.dllearner.utilities.owl.OWLClassExpressionToSPARQLConverter; import org.ini4j.IniPreferences; import org.ini4j.InvalidFileFormatException; @@ -67,6 +76,10 @@ import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; +import com.google.common.base.Charsets; +import com.google.common.hash.HashCode; +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; @@ -130,7 +143,8 @@ } } - public void run(){ + public void run(SparqlEndpoint endpoint, OWLOntology ontology, File outputFile){ + ks = new SparqlEndpointKS(endpoint); SPARQLReasoner reasoner = new SPARQLReasoner(ks, cache); OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); @@ -214,12 +228,12 @@ } // if(i++ == 3) break; } - save(axioms2Score); + save(pattern, axioms2Score); } } } - private void save(Map<OWLAxiom, Score> axioms2Score){ + private void save(OWLAxiom pattern, Map<OWLAxiom, Score> axioms2Score){ try { Set<OWLAxiom> annotatedAxioms = new HashSet<OWLAxiom>(); for (Entry<OWLAxiom, Score> entry : axioms2Score.entrySet()) { @@ -232,7 +246,11 @@ } OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLOntology ontology = man.createOntology(annotatedAxioms); - man.saveOntology(ontology, new TurtleOntologyFormat(), new FileOutputStream("pattern.ttl")); + HashFunction hf = Hashing.md5(); + HashCode hc = hf.newHasher() + .putString(pattern.toString(), Charsets.UTF_8) + .hash(); + man.saveOntology(ontology, new TurtleOntologyFormat(), new FileOutputStream(hc.toString() + "-pattern-instantiations.ttl")); } catch (OWLOntologyCreationException e) { e.printStackTrace(); } catch (OWLOntologyStorageException e) { @@ -269,7 +287,15 @@ return axiomPatterns; } + public List<OWLAxiom> getPatternsToEvaluate(OWLOntology ontology){ + List<OWLAxiom> axiomPatterns = new ArrayList<OWLAxiom>(); + + axiomPatterns.addAll(ontology.getLogicalAxioms()); + + return axiomPatterns; + } + private String asLatex(String title, Map<OWLAxiom, Pair<Integer, Integer>> topN){ String latexTable = "\\begin{table}\n"; latexTable += "\\begin{tabular}{lrr}\n"; @@ -411,8 +437,82 @@ public static void main(String[] args) throws Exception { ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); - new OWLAxiomPatternUsageEvaluation().run(); + + OptionParser parser = new OptionParser(); + parser.acceptsAll(asList("h", "?", "help"), "Show help."); +// parser.acceptsAll(asList("v", "verbose"), "Verbosity level.").withOptionalArg().ofType(Boolean.class).defaultsTo(false); + parser.acceptsAll(asList("e", "endpoint"), "SPARQL endpoint URL to be used.") + .withRequiredArg().ofType(URL.class); + parser.acceptsAll(asList("g", "graph"), + "URI of default graph for queries on SPARQL endpoint.").withOptionalArg() + .ofType(URI.class); + parser.acceptsAll(asList("p", "patterns"), + "The ontology file which contains the patterns.").withOptionalArg().ofType(File.class); + parser.acceptsAll(asList("o", "output"), "Specify a file where the output can be written.") + .withOptionalArg().ofType(File.class); + + // parse options and display a message for the user in case of problems + OptionSet options = null; + try { + options = parser.parse(args); + } catch (Exception e) { + System.out.println("Error: " + e.getMessage() + ". Use -? to get help."); + System.exit(0); + } + if (options.has("?")) { + parser.printHelpOn(System.out); + String addHelp = "Additional explanations: The resource specified should " + + "be a class, object \nproperty or data property. DL-Learner will try to " + + "automatically detect its \ntype. If no resource is specified, DL-Learner will " + + "generate enrichment \nsuggestions for all detected classes and properties in " + + "the given endpoint \nand graph. This can take several hours."; + System.out.println(); + System.out.println(addHelp); + // main script + } else { + // check that endpoint was specified + if(!options.hasArgument("endpoint")) { + System.out.println("Please specify a SPARQL endpoint (using the -e option)."); + System.exit(0); + } + + // create SPARQL endpoint object (check that indeed a URL was given) + URL endpointURL = null; + try { + endpointURL = (URL) options.valueOf("endpoint"); + } catch(OptionException e) { + System.out.println("The specified endpoint appears not be a proper URL."); + System.exit(0); + } + URI graph = null; + try { + graph = (URI) options.valueOf("graph"); + } catch(OptionException e) { + System.out.println("The specified graph appears not be a proper URL."); + System.exit(0); + } + LinkedList<String> defaultGraphURIs = new LinkedList<String>(); + if(graph != null) { + defaultGraphURIs.add(graph.toString()); + } + SparqlEndpoint endpoint = new SparqlEndpoint(endpointURL, defaultGraphURIs, new LinkedList<String>()); + File patternsFile = null; + try { + patternsFile = (File) options.valueOf("patterns"); + } catch(OptionException e) { + System.out.println("The specified ontology patterns file can not be found."); + System.exit(0); + } + OWLOntology ontology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(patternsFile); + File outputFile = null; + try { + outputFile = (File) options.valueOf("output"); + } catch(OptionException e) { + System.out.println("The specified output file can not be found."); + System.exit(0); + } + new OWLAxiomPatternUsageEvaluation().run(endpoint, ontology, outputFile); + } + } - - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-05 14:24:35
|
Revision: 3950 http://sourceforge.net/p/dl-learner/code/3950 Author: lorenz_b Date: 2013-05-05 14:24:32 +0000 (Sun, 05 May 2013) Log Message: ----------- Added script to evaluate patterns. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java Added Paths: ----------- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java Removed Paths: ------------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java 2013-05-04 11:27:51 UTC (rev 3949) +++ trunk/components-core/src/main/java/org/dllearner/core/EvaluatedAxiom.java 2013-05-05 14:24:32 UTC (rev 3950) @@ -26,11 +26,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.TreeSet; import org.apache.commons.codec.digest.DigestUtils; import org.dllearner.core.owl.Axiom; import org.dllearner.utilities.EnrichmentVocabulary; import org.dllearner.utilities.PrefixCCMap; +import org.dllearner.utilities.owl.AxiomComparator; import org.dllearner.utilities.owl.OWLAPIConverter; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAnnotation; @@ -44,9 +47,10 @@ import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxObjectRenderer; import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxPrefixNameShortFormProvider; -public class EvaluatedAxiom { +public class EvaluatedAxiom implements Comparable<EvaluatedAxiom>{ private static DecimalFormat df = new DecimalFormat("##0.0"); + private AxiomComparator axiomComparator = new AxiomComparator(); private Axiom axiom; private Score score; @@ -136,5 +140,38 @@ return str; } + public static List<EvaluatedAxiom> getBestEvaluatedAxioms(Set<EvaluatedAxiom> evaluatedAxioms, int nrOfAxioms) { + return getBestEvaluatedAxioms(evaluatedAxioms, nrOfAxioms, 0.0); + } + + public static List<EvaluatedAxiom> getBestEvaluatedAxioms(Set<EvaluatedAxiom> evaluatedAxioms, double accuracyThreshold) { + return getBestEvaluatedAxioms(evaluatedAxioms, Integer.MAX_VALUE, accuracyThreshold); + } + public static List<EvaluatedAxiom> getBestEvaluatedAxioms(Set<EvaluatedAxiom> evaluatedAxioms, int nrOfAxioms, + double accuracyThreshold) { + List<EvaluatedAxiom> returnList = new ArrayList<EvaluatedAxiom>(); + + //get the currently best evaluated axioms + Set<EvaluatedAxiom> orderedEvaluatedAxioms = new TreeSet<EvaluatedAxiom>(evaluatedAxioms); + + for(EvaluatedAxiom evAx : orderedEvaluatedAxioms){ + if(evAx.getScore().getAccuracy() >= accuracyThreshold && returnList.size() < nrOfAxioms){ + returnList.add(evAx); + } + } + + return returnList; + } + + @Override + public int compareTo(EvaluatedAxiom other) { + int ret = Double.compare(score.getAccuracy(), other.getScore().getAccuracy()); + if(ret == 0){ + ret = axiomComparator.compare(axiom, other.getAxiom()); + } + return ret; + } + + } Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2013-05-04 11:27:51 UTC (rev 3949) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2013-05-05 14:24:32 UTC (rev 3950) @@ -450,8 +450,12 @@ } public Set<NamedClass> getTypes() { + return getTypes((String)null); + } + + public Set<NamedClass> getTypes(String namespace) { Set<NamedClass> types = new TreeSet<NamedClass>(); - String query = String.format("SELECT DISTINCT ?class WHERE {[] a ?class.}"); + String query = String.format("SELECT DISTINCT ?class WHERE {[] a ?class." + (namespace != null ? ("FILTER(REGEX(?class,'^" + namespace + "'))") : "") + "}"); ResultSet rs = executeSelectQuery(query); QuerySolution qs; while(rs.hasNext()){ 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-04 11:27:51 UTC (rev 3949) +++ trunk/components-core/src/main/java/org/dllearner/utilities/owl/OWLClassExpressionToSPARQLConverter.java 2013-05-05 14:24:32 UTC (rev 3950) @@ -2,6 +2,7 @@ import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -74,7 +75,7 @@ private OWLDataFactory df = new OWLDataFactoryImpl(); private Map<Integer, Boolean> intersection; - private Set<? extends OWLEntity> variableEntities; + private Set<? extends OWLEntity> variableEntities = new HashSet<OWLEntity>(); public OWLClassExpressionToSPARQLConverter() { } @@ -97,6 +98,10 @@ return asQuery(rootVariable, expr, Collections.<OWLEntity>emptySet()); } + public Query asQuery(String rootVariable, OWLClassExpression expr, boolean countQuery){ + 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 "; @@ -108,7 +113,7 @@ String var = variablesMapping.get(owlEntity); queryString += var + " "; } - queryString += "COUNT(" + rootVariable + ") WHERE {"; + queryString += "(COUNT(DISTINCT " + rootVariable + ") AS ?cnt) WHERE {"; } queryString += triplePattern; @@ -119,10 +124,15 @@ String var = variablesMapping.get(owlEntity); queryString += var; } + queryString += " ORDER BY DESC(?cnt)"; } return QueryFactory.create(queryString, Syntax.syntaxARQ); } + public Map<OWLEntity, String> getVariablesMapping() { + return variablesMapping; + } + private void reset(){ variablesMapping.clear(); variables.clear(); Deleted: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 11:27:51 UTC (rev 3949) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-05 14:24:32 UTC (rev 3950) @@ -1,383 +0,0 @@ -package org.dllearner.scripts; - -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.prefs.Preferences; - -import org.aksw.commons.util.Pair; -import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; -import org.dllearner.algorithms.pattern.OWLAxiomPatternFinder; -import org.dllearner.kb.repository.OntologyRepository; -import org.dllearner.kb.repository.bioportal.BioPortalRepository; -import org.dllearner.kb.repository.oxford.OxfordRepository; -import org.dllearner.kb.repository.tones.TONESRepository; -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.OWLParserException; -import org.semanticweb.owlapi.io.StringDocumentSource; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.UnloadableImportException; - -import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; - -public class OWLAxiomPatternDetectionEvaluation { - - enum AxiomTypeCategory{ - TBox, RBox, ABox - } - - private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); - private Connection conn; - - private boolean fancyLatex = false; - - public OWLAxiomPatternDetectionEvaluation() { - initDBConnection(); - } - - private void initDBConnection() { - try { - InputStream is = this.getClass().getClassLoader().getResourceAsStream("db_settings.ini"); - Preferences prefs = new IniPreferences(is); - String dbServer = prefs.node("database").get("server", null); - String dbName = prefs.node("database").get("name", null); - String dbUser = prefs.node("database").get("user", null); - String dbPass = prefs.node("database").get("pass", null); - - Class.forName("com.mysql.jdbc.Driver"); - String url = "jdbc:mysql://" + dbServer + "/" + dbName; - conn = DriverManager.getConnection(url, dbUser, dbPass); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (SQLException e) { - e.printStackTrace(); - } catch (InvalidFileFormatException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void run(boolean analyzeRepositories, Collection<OntologyRepository> repositories){ - //analyze repositories - if(analyzeRepositories){ - analyze(repositories); - } - - //create statistics for the repositories - makeRepositoryStatistics(repositories); - - //get top n TBox, RBox and ABox patterns - makePatternStatistics(repositories); - } - - public void run(Collection<OntologyRepository> repositories){ - run(true, repositories); - } - - private void analyze(Collection<OntologyRepository> repositories){ - for (OntologyRepository repository : repositories) { - repository.initialize(); - OWLAxiomPatternFinder patternFinder = new OWLAxiomPatternFinder(repository, conn); - patternFinder.start(); - } - } - - private void makePatternStatistics(Collection<OntologyRepository> repositories){ - int n = 10; - - String latex = ""; - - //total pattern statistics - for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { - Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(axiomTypeCategory, n); - latex += asLatex("Top " + n + " " + axiomTypeCategory.name() + " axiom patterns.", topNAxiomPatterns) + "\n\n"; - } - - //get top n TBox, RBox and ABox patterns by repository - for (OntologyRepository repository : repositories) { - for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { - Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(repository, axiomTypeCategory, n); - latex += asLatex("Top " + n + " " + axiomTypeCategory.name() + " axiom patterns for " + repository.getName() + " repository.", topNAxiomPatterns) + "\n\n"; - } - } - try { - new FileOutputStream("pattern-statistics.tex").write(latex.getBytes()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private void makeRepositoryStatistics(Collection<OntologyRepository> repositories){ - String latexTable = "\\begin{tabular}{lrr|rrr|rrr|rrr|rrr}"; - latexTable += "\\toprule\n"; - latexTable += "Repository & \\multicolumn{2}{c}{\\#Ontologies} & \\multicolumn{12}{c}{\\#Axioms} \\\\\n"; - latexTable += "& Total & Error & \\multicolumn{3}{c}{Total} & \\multicolumn{3}{c}{Tbox} & \\multicolumn{3}{c}{RBox} & \\multicolumn{3}{c}{Abox} \\\\\\midrule\n"; - latexTable += "& & & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max \\\\\\midrule\n"; - - - PreparedStatement ps; - ResultSet rs; - - int numberOfOntologies; - int numberOfErrorOntologies; - int minNumberOfLogicalAxioms; - int maxNumberOfLogicalAxioms; - int avgNumberOfLogicalAxioms; - int minNumberOfTboxAxioms; - int maxNumberOfTboxAxioms; - int avgNumberOfTboxAxioms; - int minNumberOfRboxAxioms; - int maxNumberOfRboxAxioms; - int avgNumberOfRboxAxioms; - int minNumberOfAboxAxioms; - int maxNumberOfAboxAxioms; - int avgNumberOfAboxAxioms; - - //for each repository - for (OntologyRepository repository : repositories) { - try { - //get number of ontologies - ps = conn.prepareStatement("SELECT COUNT(*) FROM Ontology WHERE repository=?"); - ps.setString(1, repository.getName()); - numberOfOntologies = count(ps); - //get number of error causing ontologies - ps = conn.prepareStatement("SELECT COUNT(*) FROM Ontology WHERE repository=? AND iri LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - numberOfErrorOntologies = count(ps); - //get min number of logical axioms - ps = conn.prepareStatement("SELECT MIN(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - minNumberOfLogicalAxioms = count(ps); - //get max number of logical axioms - ps = conn.prepareStatement("SELECT MAX(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - maxNumberOfLogicalAxioms = count(ps); - //get avg number of logical axioms - ps = conn.prepareStatement("SELECT AVG(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - avgNumberOfLogicalAxioms = count(ps); - //get min number of tbox axioms - ps = conn.prepareStatement("SELECT MIN(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - minNumberOfTboxAxioms = count(ps); - //get max number of tbox axioms - ps = conn.prepareStatement("SELECT MAX(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - maxNumberOfTboxAxioms = count(ps); - //get avg number of tbox axioms - ps = conn.prepareStatement("SELECT AVG(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - avgNumberOfTboxAxioms = count(ps); - //get min number of rbox axioms - ps = conn.prepareStatement("SELECT MIN(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - minNumberOfRboxAxioms = count(ps); - //get max number of rbox axioms - ps = conn.prepareStatement("SELECT MAX(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - maxNumberOfRboxAxioms = count(ps); - //get avg number of rbox axioms - ps = conn.prepareStatement("SELECT AVG(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - avgNumberOfRboxAxioms = count(ps); - //get min number of abox axioms - ps = conn.prepareStatement("SELECT MIN(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - minNumberOfAboxAxioms = count(ps); - //get max number of abox axioms - ps = conn.prepareStatement("SELECT MAX(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - maxNumberOfAboxAxioms = count(ps); - //get avg number of abox axioms - ps = conn.prepareStatement("SELECT AVG(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); - ps.setString(1, repository.getName()); - avgNumberOfAboxAxioms = count(ps); - - latexTable += - repository.getName() + "&" + - numberOfOntologies + "&" + - numberOfErrorOntologies + "&" + - minNumberOfLogicalAxioms + "&" + - avgNumberOfLogicalAxioms + "&" + - maxNumberOfLogicalAxioms + "&" + - minNumberOfTboxAxioms + "&" + - avgNumberOfTboxAxioms + "&" + - maxNumberOfTboxAxioms + "&" + - minNumberOfRboxAxioms + "&" + - avgNumberOfRboxAxioms + "&" + - maxNumberOfRboxAxioms + "&" + - minNumberOfAboxAxioms + "&" + - avgNumberOfAboxAxioms + "&" + - maxNumberOfAboxAxioms + "\\\\\n"; - - } catch (SQLException e) { - e.printStackTrace(); - } - } - latexTable += "\\bottomrule\\end{tabular}"; - try { - new FileOutputStream("repository-statistics.tex").write(latexTable.getBytes()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private String asLatex(String title, Map<OWLAxiom, Pair<Integer, Integer>> topN){ - String latexTable = "\\begin{table}\n"; - latexTable += "\\begin{tabular}{lrr}\n"; - latexTable += "\\toprule\n"; - latexTable += "Pattern & Frequency & \\#Ontologies\\\\\\midrule\n"; - - for (Entry<OWLAxiom, Pair<Integer, Integer>> entry : topN.entrySet()) { - OWLAxiom axiom = entry.getKey(); - Integer frequency = entry.getValue().getKey(); - Integer idf = entry.getValue().getValue(); - - if(axiom != null){ - String axiomColumn = axiomRenderer.render(axiom); - if(fancyLatex){ - axiomColumn = "\\begin{lstlisting}[language=manchester]" + axiomColumn + "\\end{lstlisting}"; - } - latexTable += axiomColumn + " & " + frequency + " & " + idf + "\\\\\n"; - } - } - latexTable += "\\bottomrule\n\\end{tabular}\n"; - latexTable += "\\caption{" + title + "}\n"; - latexTable += "\\end{table}\n"; - return latexTable; - } - - private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(AxiomTypeCategory axiomType, int n){ - Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); - PreparedStatement ps; - ResultSet rs; - try { - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences),COUNT(ontology_id) FROM " + - "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + - "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + - "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, axiomType.name()); - ps.setInt(2, n); - rs = ps.executeQuery(); - while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), new Pair<Integer, Integer>(rs.getInt(2), rs.getInt(3))); - } - } catch(SQLException e){ - e.printStackTrace(); - } - return topN; - } - - private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(OntologyRepository repository, AxiomTypeCategory axiomType, int n){ - Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); - PreparedStatement ps; - ResultSet rs; - try { - //get number of ontologies - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences),COUNT(ontology_id) FROM " + - "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + - "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + - "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, repository.getName()); - ps.setString(2, axiomType.name()); - ps.setInt(3, n); - rs = ps.executeQuery(); - while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), new Pair<Integer, Integer>(rs.getInt(2), rs.getInt(3))); - } - } catch(SQLException e){ - e.printStackTrace(); - } - return topN; - } - - private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNAxiomPatterns(Collection<OntologyRepository> repositories, AxiomTypeCategory axiomType, int n){ - Map<OntologyRepository, Map<OWLAxiom, Integer>> topNByRepository = new LinkedHashMap<OntologyRepository, Map<OWLAxiom,Integer>>(); - PreparedStatement ps; - ResultSet rs; - //for each repository - for (OntologyRepository repository : repositories) { - Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); - try { - //get number of ontologies - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + - "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + - "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + - "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, repository.getName()); - ps.setString(2, "RBox"); - ps.setInt(3, n); - rs = ps.executeQuery(); - while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); - } - } catch(SQLException e){ - e.printStackTrace(); - } - topNByRepository.put(repository, topN); - } - return topNByRepository; - } - - private OWLAxiom asOWLAxiom(String functionalSyntaxAxiomString){ - try { - StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org> " + functionalSyntaxAxiomString + ")"); - OWLFunctionalSyntaxOWLParser p = new OWLFunctionalSyntaxOWLParser(); - OWLOntology newOntology = OWLManager.createOWLOntologyManager().createOntology(); - p.parse(s, newOntology); - if(!newOntology.getLogicalAxioms().isEmpty()){ - return newOntology.getLogicalAxioms().iterator().next(); - } - } catch (UnloadableImportException e) { - e.printStackTrace(); - } catch (OWLOntologyCreationException e) { - e.printStackTrace(); - } catch (OWLParserException e) { - System.err.println("Parsing failed for axiom " + functionalSyntaxAxiomString); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - private int count(PreparedStatement ps) throws SQLException{ - ResultSet rs = ps.executeQuery(); - rs.next(); - return rs.getInt(1); - } - - public static void main(String[] args) throws Exception { -// ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); - boolean analyzeRepositories = true; - if(args.length == 1){ - analyzeRepositories = Boolean.parseBoolean(args[0]); - } - new OWLAxiomPatternDetectionEvaluation().run(analyzeRepositories, Arrays.asList( - new TONESRepository(), new BioPortalRepository(), new OxfordRepository())); - } - - -} Added: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java (rev 0) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternDetectionEvaluation.java 2013-05-05 14:24:32 UTC (rev 3950) @@ -0,0 +1,408 @@ +package org.dllearner.scripts.pattern; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.prefs.Preferences; + +import org.aksw.commons.util.Pair; +import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; +import org.dllearner.algorithms.pattern.OWLAxiomPatternFinder; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.repository.OntologyRepository; +import org.dllearner.kb.repository.bioportal.BioPortalRepository; +import org.dllearner.kb.repository.oxford.OxfordRepository; +import org.dllearner.kb.repository.tones.TONESRepository; +import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.reasoning.SPARQLReasoner; +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.OWLParserException; +import org.semanticweb.owlapi.io.StringDocumentSource; +import org.semanticweb.owlapi.model.AxiomType; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; +import org.semanticweb.owlapi.model.UnloadableImportException; + +import com.hp.hpl.jena.query.Query; + +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; + +public class OWLAxiomPatternDetectionEvaluation { + + enum AxiomTypeCategory{ + TBox, RBox, ABox + } + + private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); + private OWLDataFactory df = new OWLDataFactoryImpl(); + private Connection conn; + + private boolean fancyLatex = false; + + public OWLAxiomPatternDetectionEvaluation() { + initDBConnection(); + } + + private void initDBConnection() { + try { + InputStream is = this.getClass().getClassLoader().getResourceAsStream("db_settings.ini"); + Preferences prefs = new IniPreferences(is); + String dbServer = prefs.node("database").get("server", null); + String dbName = prefs.node("database").get("name", null); + String dbUser = prefs.node("database").get("user", null); + String dbPass = prefs.node("database").get("pass", null); + + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://" + dbServer + "/" + dbName; + conn = DriverManager.getConnection(url, dbUser, dbPass); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (InvalidFileFormatException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void run(boolean analyzeRepositories, Collection<OntologyRepository> repositories){ + //analyze repositories + if(analyzeRepositories){ + analyze(repositories); + } + + //create statistics for the repositories + makeRepositoryStatistics(repositories); + + //get top n TBox, RBox and ABox patterns + makePatternStatistics(repositories); + } + + public void run(Collection<OntologyRepository> repositories){ + run(true, repositories); + } + + public List<OWLAxiom> getPatternsToEvaluate(){ + List<OWLAxiom> axiomPatterns = new ArrayList<OWLAxiom>(); + + Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(AxiomTypeCategory.TBox, 10); + axiomPatterns.addAll(topNAxiomPatterns.keySet()); + + return axiomPatterns; + } + + private void analyze(Collection<OntologyRepository> repositories){ + for (OntologyRepository repository : repositories) { + repository.initialize(); + OWLAxiomPatternFinder patternFinder = new OWLAxiomPatternFinder(repository, conn); + patternFinder.start(); + } + } + + private void makePatternStatistics(Collection<OntologyRepository> repositories){ + int n = 10; + + String latex = ""; + + //total pattern statistics + for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { + Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(axiomTypeCategory, n); + latex += asLatex("Top " + n + " " + axiomTypeCategory.name() + " axiom patterns.", topNAxiomPatterns) + "\n\n"; + } + + //get top n TBox, RBox and ABox patterns by repository + for (OntologyRepository repository : repositories) { + for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { + Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(repository, axiomTypeCategory, n); + latex += asLatex("Top " + n + " " + axiomTypeCategory.name() + " axiom patterns for " + repository.getName() + " repository.", topNAxiomPatterns) + "\n\n"; + } + } + try { + new FileOutputStream("pattern-statistics.tex").write(latex.getBytes()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void makeRepositoryStatistics(Collection<OntologyRepository> repositories){ + String latexTable = "\\begin{tabular}{lrr|rrr|rrr|rrr|rrr}"; + latexTable += "\\toprule\n"; + latexTable += "Repository & \\multicolumn{2}{c}{\\#Ontologies} & \\multicolumn{12}{c}{\\#Axioms} \\\\\n"; + latexTable += "& Total & Error & \\multicolumn{3}{c}{Total} & \\multicolumn{3}{c}{Tbox} & \\multicolumn{3}{c}{RBox} & \\multicolumn{3}{c}{Abox} \\\\\\midrule\n"; + latexTable += "& & & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max \\\\\\midrule\n"; + + + PreparedStatement ps; + ResultSet rs; + + int numberOfOntologies; + int numberOfErrorOntologies; + int minNumberOfLogicalAxioms; + int maxNumberOfLogicalAxioms; + int avgNumberOfLogicalAxioms; + int minNumberOfTboxAxioms; + int maxNumberOfTboxAxioms; + int avgNumberOfTboxAxioms; + int minNumberOfRboxAxioms; + int maxNumberOfRboxAxioms; + int avgNumberOfRboxAxioms; + int minNumberOfAboxAxioms; + int maxNumberOfAboxAxioms; + int avgNumberOfAboxAxioms; + + //for each repository + for (OntologyRepository repository : repositories) { + try { + //get number of ontologies + ps = conn.prepareStatement("SELECT COUNT(*) FROM Ontology WHERE repository=?"); + ps.setString(1, repository.getName()); + numberOfOntologies = count(ps); + //get number of error causing ontologies + ps = conn.prepareStatement("SELECT COUNT(*) FROM Ontology WHERE repository=? AND iri LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + numberOfErrorOntologies = count(ps); + //get min number of logical axioms + ps = conn.prepareStatement("SELECT MIN(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + minNumberOfLogicalAxioms = count(ps); + //get max number of logical axioms + ps = conn.prepareStatement("SELECT MAX(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + maxNumberOfLogicalAxioms = count(ps); + //get avg number of logical axioms + ps = conn.prepareStatement("SELECT AVG(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + avgNumberOfLogicalAxioms = count(ps); + //get min number of tbox axioms + ps = conn.prepareStatement("SELECT MIN(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + minNumberOfTboxAxioms = count(ps); + //get max number of tbox axioms + ps = conn.prepareStatement("SELECT MAX(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + maxNumberOfTboxAxioms = count(ps); + //get avg number of tbox axioms + ps = conn.prepareStatement("SELECT AVG(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + avgNumberOfTboxAxioms = count(ps); + //get min number of rbox axioms + ps = conn.prepareStatement("SELECT MIN(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + minNumberOfRboxAxioms = count(ps); + //get max number of rbox axioms + ps = conn.prepareStatement("SELECT MAX(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + maxNumberOfRboxAxioms = count(ps); + //get avg number of rbox axioms + ps = conn.prepareStatement("SELECT AVG(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + avgNumberOfRboxAxioms = count(ps); + //get min number of abox axioms + ps = conn.prepareStatement("SELECT MIN(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + minNumberOfAboxAxioms = count(ps); + //get max number of abox axioms + ps = conn.prepareStatement("SELECT MAX(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + maxNumberOfAboxAxioms = count(ps); + //get avg number of abox axioms + ps = conn.prepareStatement("SELECT AVG(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + avgNumberOfAboxAxioms = count(ps); + + latexTable += + repository.getName() + "&" + + numberOfOntologies + "&" + + numberOfErrorOntologies + "&" + + minNumberOfLogicalAxioms + "&" + + avgNumberOfLogicalAxioms + "&" + + maxNumberOfLogicalAxioms + "&" + + minNumberOfTboxAxioms + "&" + + avgNumberOfTboxAxioms + "&" + + maxNumberOfTboxAxioms + "&" + + minNumberOfRboxAxioms + "&" + + avgNumberOfRboxAxioms + "&" + + maxNumberOfRboxAxioms + "&" + + minNumberOfAboxAxioms + "&" + + avgNumberOfAboxAxioms + "&" + + maxNumberOfAboxAxioms + "\\\\\n"; + + } catch (SQLException e) { + e.printStackTrace(); + } + } + latexTable += "\\bottomrule\\end{tabular}"; + try { + new FileOutputStream("repository-statistics.tex").write(latexTable.getBytes()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private String asLatex(String title, Map<OWLAxiom, Pair<Integer, Integer>> topN){ + String latexTable = "\\begin{table}\n"; + latexTable += "\\begin{tabular}{lrr}\n"; + latexTable += "\\toprule\n"; + latexTable += "Pattern & Frequency & \\#Ontologies\\\\\\midrule\n"; + + for (Entry<OWLAxiom, Pair<Integer, Integer>> entry : topN.entrySet()) { + OWLAxiom axiom = entry.getKey(); + Integer frequency = entry.getValue().getKey(); + Integer idf = entry.getValue().getValue(); + + if(axiom != null){ + String axiomColumn = axiomRenderer.render(axiom); + if(fancyLatex){ + axiomColumn = "\\begin{lstlisting}[language=manchester]" + axiomColumn + "\\end{lstlisting}"; + } + latexTable += axiomColumn + " & " + frequency + " & " + idf + "\\\\\n"; + } + } + latexTable += "\\bottomrule\n\\end{tabular}\n"; + latexTable += "\\caption{" + title + "}\n"; + latexTable += "\\end{table}\n"; + return latexTable; + } + + private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(AxiomTypeCategory axiomType, int n){ + Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); + PreparedStatement ps; + ResultSet rs; + try { + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences),COUNT(ontology_id) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, axiomType.name()); + ps.setInt(2, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), new Pair<Integer, Integer>(rs.getInt(2), rs.getInt(3))); + } + } catch(SQLException e){ + e.printStackTrace(); + } + return topN; + } + + private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(OntologyRepository repository, AxiomTypeCategory axiomType, int n){ + Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); + PreparedStatement ps; + ResultSet rs; + try { + //get number of ontologies + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences),COUNT(ontology_id) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, repository.getName()); + ps.setString(2, axiomType.name()); + ps.setInt(3, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), new Pair<Integer, Integer>(rs.getInt(2), rs.getInt(3))); + } + } catch(SQLException e){ + e.printStackTrace(); + } + return topN; + } + + private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNAxiomPatterns(Collection<OntologyRepository> repositories, AxiomTypeCategory axiomType, int n){ + Map<OntologyRepository, Map<OWLAxiom, Integer>> topNByRepository = new LinkedHashMap<OntologyRepository, Map<OWLAxiom,Integer>>(); + PreparedStatement ps; + ResultSet rs; + //for each repository + for (OntologyRepository repository : repositories) { + Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); + try { + //get number of ontologies + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, repository.getName()); + ps.setString(2, "RBox"); + ps.setInt(3, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + } + } catch(SQLException e){ + e.printStackTrace(); + } + topNByRepository.put(repository, topN); + } + return topNByRepository; + } + + private OWLAxiom asOWLAxiom(String functionalSyntaxAxiomString){ + try { + StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org> " + functionalSyntaxAxiomString + ")"); + OWLFunctionalSyntaxOWLParser p = new OWLFunctionalSyntaxOWLParser(); + OWLOntology newOntology = OWLManager.createOWLOntologyManager().createOntology(); + p.parse(s, newOntology); + if(!newOntology.getLogicalAxioms().isEmpty()){ + return newOntology.getLogicalAxioms().iterator().next(); + } + } catch (UnloadableImportException e) { + e.printStackTrace(); + } catch (OWLOntologyCreationException e) { + e.printStackTrace(); + } catch (OWLParserException e) { + System.err.println("Parsing failed for axiom " + functionalSyntaxAxiomString); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private int count(PreparedStatement ps) throws SQLException{ + ResultSet rs = ps.executeQuery(); + rs.next(); + return rs.getInt(1); + } + + public static void main(String[] args) throws Exception { +// ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + boolean analyzeRepositories = true; + if(args.length == 1){ + analyzeRepositories = Boolean.parseBoolean(args[0]); + } + new OWLAxiomPatternDetectionEvaluation().run(analyzeRepositories, Arrays.asList( + new TONESRepository(), new BioPortalRepository(), new OxfordRepository())); + } + + +} Added: trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java (rev 0) +++ trunk/scripts/src/main/java/org/dllearner/scripts/pattern/OWLAxiomPatternUsageEvaluation.java 2013-05-05 14:24:32 UTC (rev 3950) @@ -0,0 +1,418 @@ +package org.dllearner.scripts.pattern; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.SocketTimeoutException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.prefs.Preferences; + +import org.aksw.commons.util.Pair; +import org.apache.log4j.Logger; +import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; +import org.coode.owlapi.turtle.TurtleOntologyFormat; +import org.dllearner.core.EvaluatedAxiom; +import org.dllearner.core.Score; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; +import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtractionDBCache; +import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.kb.sparql.SparqlQuery; +import org.dllearner.learningproblems.AxiomScore; +import org.dllearner.learningproblems.Heuristics; +import org.dllearner.reasoning.SPARQLReasoner; +import org.dllearner.utilities.owl.DLLearnerAxiomConvertVisitor; +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.OWLParserException; +import org.semanticweb.owlapi.io.StringDocumentSource; +import org.semanticweb.owlapi.io.ToStringRenderer; +import org.semanticweb.owlapi.model.AxiomType; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLAnnotationProperty; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLClass; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.semanticweb.owlapi.model.OWLEntity; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.OWLOntologyStorageException; +import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; +import org.semanticweb.owlapi.model.UnloadableImportException; +import org.semanticweb.owlapi.util.OWLObjectDuplicator; + +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; +import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; +import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; + +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.Syntax; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; +import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; + +public class OWLAxiomPatternUsageEvaluation { + + + private static final Logger logger = Logger.getLogger(OWLAxiomPatternUsageEvaluation.AxiomTypeCategory.class + .getName()); + + enum AxiomTypeCategory{ + TBox, RBox, ABox + } + + private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); + private OWLDataFactory df = new OWLDataFactoryImpl(); + private Connection conn; + + private ExtractionDBCache cache = new ExtractionDBCache("pattern-cache"); + private SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia(), cache);//new LocalModelBasedSparqlEndpointKS(model); + private String ns = "http://dbpedia.org/ontology/"; + + private boolean fancyLatex = false; + private DecimalFormat format = new DecimalFormat("00.0%"); + private long waitingTime = TimeUnit.SECONDS.toMillis(3); + private double threshold = 0.6; + private OWLAnnotationProperty confidenceProperty = df.getOWLAnnotationProperty(IRI.create("http://dl-learner.org/pattern/confidence")); + + public OWLAxiomPatternUsageEvaluation() { + initDBConnection(); + } + + private void initDBConnection() { + try { + InputStream is = this.getClass().getClassLoader().getResourceAsStream("db_settings.ini"); + Preferences prefs = new IniPreferences(is); + String dbServer = prefs.node("database").get("server", null); + String dbName = prefs.node("database").get("name", null); + String dbUser = prefs.node("database").get("user", null); + String dbPass = prefs.node("database").get("pass", null); + + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://" + dbServer + "/" + dbName; + conn = DriverManager.getConnection(url, dbUser, dbPass); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (InvalidFileFormatException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void run(){ + SPARQLReasoner reasoner = new SPARQLReasoner(ks, cache); + + OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter(); + + //get the axiom patterns to evaluate + List<OWLAxiom> patterns = getPatternsToEvaluate(); + + //get all classes in KB + Set<NamedClass> classes = reasoner.getTypes(ns); + + //for each pattern + for (OWLAxiom pattern : patterns) { + if(pattern.isOfType(AxiomType.SUBCLASS_OF)){ + logger.info("Processing " + pattern + "..."); + Set<EvaluatedAxiom> evaluatedAxioms = new HashSet<EvaluatedAxiom>(); + Map<OWLAxiom, Score> axioms2Score = new LinkedHashMap<OWLAxiom, Score>(); + OWLClassExpression patternSubClass = ((OWLSubClassOfAxiom)pattern).getSubClass(); + OWLClassExpression superClass = ((OWLSubClassOfAxiom)pattern).getSuperClass(); + //for each class + int i = 1; + for (NamedClass cls : classes) { + logger.info("Processing " + cls + "..."); + //set the subclass as a class from the KB + OWLClass subClass = df.getOWLClass(IRI.create(cls.getName())); + + //1. count number of instances in subclass expression + Query query = QueryFactory.create("SELECT (COUNT(DISTINCT ?x) AS ?cnt) WHERE {" + converter.convert("?x", subClass) + "}",Syntax.syntaxARQ); + int subClassCnt = executeSelectQuery(query).next().getLiteral("cnt").getInt(); + + //2. count number of instances in subclass AND superclass expression + //we have 2 options here to evaluate the whole axiom pattern: + //a) we replace all entities in the signature of the super class expression(except the subclass) with variables + //and GROUP BY them + //b) we replace only 1 entity with a variable, thus we have to try it for several combinations +// for (OWLEntity entity : signature) { +// //replace current entity with variable and for the rest use existing entities in KB +// query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature); +// } + Set<OWLEntity> signature = superClass.getSignature(); + signature.remove(subClass); + query = converter.asQuery("?x", df.getOWLObjectIntersectionOf(subClass, superClass), signature); + query.setLimit(100); + Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping();System.out.println(query); + com.hp.hpl.jena.query.ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + //get the IRIs for each variable + Map<OWLEntity, IRI> entity2IRIMap = new HashMap<OWLEntity, IRI>(); + entity2IRIMap.put(patternSubClass.asOWLClass(), subClass.getIRI()); + for (OWLEntity entity : signature) { + String var = variablesMapping.get(entity); + Resource resource = qs.getResource(var); + entity2IRIMap.put(entity, IRI.create(resource.getURI())); + } + //instantiate the pattern + OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, df); + OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); + int patternInstantiationCnt = qs.getLiteral("cnt").getInt(); + //compute score + Score score; + try { + score = computeScore(subClassCnt, patternInstantiationCnt); + axioms2Score.put(patternInstantiation, score); + logger.info(patternInstantiation + "(" + format.format(score.getAccuracy()) + ")"); + } catch (IllegalArgumentException e) { + //sometimes Virtuosos returns 'wrong' cnt values such that the success number as bigger than the total number of instances + e.printStackTrace(); + } + +// //convert into EvaluatedAxiom such we can serialize it as RDF with accuracy value as annotation +// EvaluatedAxiom evaluatedAxiom = new EvaluatedAxiom(DLLearnerAxiomConvertVisitor.getDLLearnerAxiom(patternInstantiation), score); +// evaluatedAxioms.add(evaluatedAxiom); + + } + //wait some time to avoid flooding of endpoint + try { + Thread.sleep(waitingTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } +// if(i++ == 3) break; + } + save(axioms2Score); + } + } + } + + private void save(Map<OWLAxiom, Score> axioms2Score){ + try { + Set<OWLAxiom> annotatedAxioms = new HashSet<OWLAxiom>(); + for (Entry<OWLAxiom, Score> entry : axioms2Score.entrySet()) { + OWLAxiom axiom = entry.getKey(); + Score score = entry.getValue(); + if(score.getAccuracy() >= threshold){ + annotatedAxioms.add(axiom.getAnnotatedAxiom(Collections.singleton(df.getOWLAnnotation(confidenceProperty, df.getOWLLiteral(score.getAccuracy()))))); + + } + } + OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + OWLOntology ontology = man.createOntology(annotatedAxioms); + man.saveOntology(ontology, new TurtleOntologyFormat(), new FileOutputStream("pattern.ttl")); + } catch (OWLOntologyCreationException e) { + e.printStackTrace(); + } catch (OWLOntologyStorageException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + private void save(Set<EvaluatedAxiom> evaluatedAxioms){ + try { + Set<OWLAxiom> axioms = new HashSet<OWLAxiom>(); + for (EvaluatedAxiom evaluatedAxiom : EvaluatedAxiom.getBestEvaluatedAxioms(evaluatedAxioms, threshold)) { + axioms.addAll(evaluatedAxiom.toRDF("http://dl-learner.org/pattern/").values().iterator().next()); + } + OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + OWLOntology ontology = man.createOntology(axioms); + man.saveOntology(ontology, new TurtleOntologyFormat(), new FileOutputStream("pattern.ttl")); + } catch (OWLOntologyCreationException e) { + e.printStackTrace(); + } catch (OWLOntologyStorageException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + public List<OWLAxiom> getPatternsToEvaluate(){ + List<OWLAxiom> axiomPatterns = new ArrayList<OWLAxiom>(); + + Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(AxiomTypeCategory.TBox, 10); + axiomPatterns.addAll(topNAxiomPatterns.keySet()); + + return axiomPatterns; + } + + + private String asLatex(String title, Map<OWLAxiom, Pair<Integer, Integer>> topN){ + String latexTable = "\\begin{table}\n"; + latexTable += "\\begin{tabular}{lrr}\n"; + latexTable += "\\toprule\n"; + latexTable += "Pattern & Frequency & \\#Ontologies\\\\\\midrule\n"; + + for (Entry<OWLAxiom, Pair<Integer, Integer>> entry : topN.entrySet()) { + OWLAxiom axiom = entry.getKey(); + Integer frequency = entry.getValue().getKey(); + Integer idf = entry.getValue().getValue(); + + if(axiom != null){ + String axiomColumn = axiomRenderer.render(axiom); + if(fancyLatex){ + axiomColumn = "\\begin{lstlisting}[language=manchester]" + axiomColumn + "\\end{lstlisting}"; + } + latexTable += axiomColumn + " & " + frequency + " & " + idf + "\\\\\n"; + } + } + latexTable += "\\bottomrule\n\\end{tabular}\n"; + latexTable += "\\caption{" + title + "}\n"; + latexTable += "\\end{table}\n"; + return latexTable; + } + + private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(AxiomTypeCategory axiomType, int n){ + Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); + PreparedStatement ps; + ResultSet rs; + try { + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences),COUNT(ontology_id) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, axiomType.name()); + ps.setInt(2, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), new Pair<Integer, Integer>(rs.getInt(2), rs.getInt(3))); + } + } catch(SQLException e){ + e.printStackTrace(); + } + return topN; + } + + protected com.hp.hpl.jena.query.ResultSet executeSelectQuery(Query query) { + com.hp.hpl.jena.query.ResultSet rs = null; + if(ks.isRemote()){ + SparqlEndpoint endpoint = ((SparqlEndpointKS) ks).getEndpoint(); + ExtractionDBCache cache = ks.getCache(); + if(cache != null){ + rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query.toString())); + } else { + QueryEngineHTTP queryExecution = new QueryEngineHTTP(endpoint.getURL().toString(), + query); + queryExecution.setDefaultGraphURIs(endpoint.getDefaultGraphURIs()); + queryExecution.setNamedGraphURIs(endpoint.getNamedGraphURIs()); + try { + rs = queryExecution.execSelect(); + return rs; + } catch (QueryExceptionHTTP e) { + if(e.getCause() instanceof SocketTimeoutException){ + logger.warn("Got timeout"); + } else { + logger.error("Exception executing query", e); + } + } + } + + } else { + QueryExecution queryExecution = QueryExecutionFactory.create(query, ((LocalModelBasedSparqlEndpointKS)ks).getModel()); + rs = queryExecution.execSelect(); + } + return rs; + } + + protected com.hp.hpl.jena.query.ResultSet executeSelectQuery(Query query, boolean cached) { + com.hp.hpl.jena.query.ResultSet rs = null; + if(ks.isRemote()){ + SparqlEndpoint endpoint = ((SparqlEndpointKS) ks).getEndpoint(); + ExtractionDBCache cache = ks.getCache(); + if(cache != null && cached){ + rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query.toString())); + } else { + QueryEngineHTTP queryExecution = new QueryEngineHTTP(endpoint.getURL().toString(), + query); + queryExecution.setDefaultGraphURIs(endpoint.getDefaultGraphURIs()); + queryExecution.setNamedGraphURIs(endpoint.getNamedGraphURIs()); + try { + rs = queryExecution.execSelect(); + return rs; + } catch (QueryExceptionHTTP e) { + if(e.getCause() instanceof SocketTimeoutException){ + logger.warn("Got timeout"); + } else { + logger.error("Exception executing query", e); + } + } + } + + } else { + QueryExecution queryExecution = QueryExecutionFactory.create(query, ((LocalModelBasedSparqlEndpointKS)ks).getModel()); + rs = queryExecution.execSelect(); + } + return rs; + } + + private Score computeScore(int total, int success){ + double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(total, success); + + double accuracy = (confidenceInterval[0] + confidenceInterval[1]) / 2; + + double confidence = confidenceInterval[1] - confidenceInterval[0]; + + return new AxiomScore(accuracy, confidence, total, success, total-success); + } + + private OWLAxiom asOWLAxiom(String functionalSyntaxAxiomString){ + try { + StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org> " + functionalSyntaxAxiomString + ")"); + OWLFunctionalSyntaxOWLParser p = new OWLFunctionalSyntaxOWLParser(); + OWLOntology newOntology = OWLManager.createOWLOntologyManager().createOntology(); + p.parse(s, newOntology); + if(!newOntology.getLogicalAxioms().isEmpty()){ + return newOntology.getLogicalAxioms().iterator().next(); + } + } catch (UnloadableImportException e) { + e.printStackTrace(); + } catch (OWLOntologyCreationException e) { + e.printStackTrace(); + } catch (OWLParserException e) { + System.err.println("Parsing failed for axiom " + functionalSyntaxAxiomString); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) throws Exception { + ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); + new OWLAxiomPatternUsageEvaluation().run(); + } + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 11:27:54
|
Revision: 3949 http://sourceforge.net/p/dl-learner/code/3949 Author: lorenz_b Date: 2013-05-04 11:27:51 +0000 (Sat, 04 May 2013) Log Message: ----------- Added Oxford Ontologies repository Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/kb/repository/oxford/ trunk/components-core/src/main/java/org/dllearner/kb/repository/oxford/OxfordRepository.java Added: trunk/components-core/src/main/java/org/dllearner/kb/repository/oxford/OxfordRepository.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/repository/oxford/OxfordRepository.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/kb/repository/oxford/OxfordRepository.java 2013-05-04 11:27:51 UTC (rev 3949) @@ -0,0 +1,125 @@ +package org.dllearner.kb.repository.oxford; + +import java.net.URI; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.apache.log4j.Logger; +import org.dllearner.kb.repository.OntologyRepository; +import org.dllearner.kb.repository.OntologyRepositoryEntry; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.util.OntologyIRIShortFormProvider; + +public class OxfordRepository implements OntologyRepository{ + + private static final Logger log = Logger.getLogger(OxfordRepository.class); + + private final String repositoryName = "Oxford"; + + private final URI repositoryLocation = URI.create("http://www.cs.ox.ac.uk/isg/ontologies/UID/"); + + private List<RepositoryEntry> entries; + + int numberOfEntries = 793; + + DecimalFormat df = new DecimalFormat("00000"); + + + public OxfordRepository() { + entries = new ArrayList<RepositoryEntry>(); + } + + @Override + public void initialize() { + refresh(); + } + + + public String getName() { + return repositoryName; + } + + + public String getLocation() { + return repositoryLocation.toString(); + } + + + public void refresh() { + fillRepository(); + } + + + public Collection<OntologyRepositoryEntry> getEntries() { + List<OntologyRepositoryEntry> ret = new ArrayList<OntologyRepositoryEntry>(); + ret.addAll(entries); + return ret; + } + + + public List<Object> getMetaDataKeys() { + return Collections.emptyList(); + } + + + public void dispose() throws Exception { + } + + ///////////////////////////////////////////////////////////////////////////////////////////////// + // + // Implementation details + + + private void fillRepository() { + entries.clear(); + for(int i = 1; i <= numberOfEntries; i++){ + entries.add(new RepositoryEntry(URI.create(repositoryLocation + df.format(i) + ".owl"))); + } + log.info("Loaded " + entries.size() + " ontology entries from Oxford."); + } + + private class RepositoryEntry implements OntologyRepositoryEntry { + + private String shortName; + + private URI ontologyURI; + + private URI physicalURI; + + public RepositoryEntry(URI ontologyIRI) { + this.ontologyURI = ontologyIRI;System.out.println(ontologyIRI); + OntologyIRIShortFormProvider sfp = new OntologyIRIShortFormProvider(); + shortName = sfp.getShortForm(IRI.create(ontologyIRI)); + physicalURI = ontologyIRI; + } + + + public String getOntologyShortName() { + return shortName; + } + + + public URI getOntologyURI() { + return ontologyURI; + } + + + public URI getPhysicalURI() { + return physicalURI; + } + + + public String getMetaData(Object key) { + return null; + } + + } + + public static void main(String[] args) throws Exception { + new OxfordRepository().fillRepository(); + } + +} Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 11:13:37 UTC (rev 3948) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 11:27:51 UTC (rev 3949) @@ -21,6 +21,7 @@ import org.dllearner.algorithms.pattern.OWLAxiomPatternFinder; import org.dllearner.kb.repository.OntologyRepository; import org.dllearner.kb.repository.bioportal.BioPortalRepository; +import org.dllearner.kb.repository.oxford.OxfordRepository; import org.dllearner.kb.repository.tones.TONESRepository; import org.ini4j.IniPreferences; import org.ini4j.InvalidFileFormatException; @@ -375,7 +376,7 @@ analyzeRepositories = Boolean.parseBoolean(args[0]); } new OWLAxiomPatternDetectionEvaluation().run(analyzeRepositories, Arrays.asList( - new TONESRepository(), new BioPortalRepository())); + new TONESRepository(), new BioPortalRepository(), new OxfordRepository())); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 11:13:40
|
Revision: 3948 http://sourceforge.net/p/dl-learner/code/3948 Author: lorenz_b Date: 2013-05-04 11:13:37 +0000 (Sat, 04 May 2013) Log Message: ----------- Added option to create only statistics. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 11:09:49 UTC (rev 3947) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 11:13:37 UTC (rev 3948) @@ -77,7 +77,9 @@ public void run(boolean analyzeRepositories, Collection<OntologyRepository> repositories){ //analyze repositories - analyze(repositories); + if(analyzeRepositories){ + analyze(repositories); + } //create statistics for the repositories makeRepositoryStatistics(repositories); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 11:09:52
|
Revision: 3947 http://sourceforge.net/p/dl-learner/code/3947 Author: lorenz_b Date: 2013-05-04 11:09:49 +0000 (Sat, 04 May 2013) Log Message: ----------- Improved script Latex output. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 11:07:10 UTC (rev 3946) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 11:09:49 UTC (rev 3947) @@ -75,7 +75,7 @@ } } - public void run(Collection<OntologyRepository> repositories){ + public void run(boolean analyzeRepositories, Collection<OntologyRepository> repositories){ //analyze repositories analyze(repositories); @@ -86,6 +86,10 @@ makePatternStatistics(repositories); } + public void run(Collection<OntologyRepository> repositories){ + run(true, repositories); + } + private void analyze(Collection<OntologyRepository> repositories){ for (OntologyRepository repository : repositories) { repository.initialize(); @@ -364,7 +368,11 @@ public static void main(String[] args) throws Exception { // ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); - new OWLAxiomPatternDetectionEvaluation().run(Arrays.asList( + boolean analyzeRepositories = true; + if(args.length == 1){ + analyzeRepositories = Boolean.parseBoolean(args[0]); + } + new OWLAxiomPatternDetectionEvaluation().run(analyzeRepositories, Arrays.asList( new TONESRepository(), new BioPortalRepository())); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 11:07:14
|
Revision: 3946 http://sourceforge.net/p/dl-learner/code/3946 Author: lorenz_b Date: 2013-05-04 11:07:10 +0000 (Sat, 04 May 2013) Log Message: ----------- Improved script Latex output. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 10:59:43 UTC (rev 3945) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 11:07:10 UTC (rev 3946) @@ -77,7 +77,7 @@ public void run(Collection<OntologyRepository> repositories){ //analyze repositories -// analyze(repositories); + analyze(repositories); //create statistics for the repositories makeRepositoryStatistics(repositories); @@ -295,7 +295,7 @@ "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); ps.setString(1, repository.getName()); - ps.setString(2, "RBox"); + ps.setString(2, axiomType.name()); ps.setInt(3, n); rs = ps.executeQuery(); while(rs.next()){ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 10:59:46
|
Revision: 3945 http://sourceforge.net/p/dl-learner/code/3945 Author: lorenz_b Date: 2013-05-04 10:59:43 +0000 (Sat, 04 May 2013) Log Message: ----------- Improved script Latex output. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java 2013-05-04 10:20:34 UTC (rev 3944) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java 2013-05-04 10:59:43 UTC (rev 3945) @@ -68,6 +68,7 @@ private OWLAxiom renamedAxiom; private boolean normalizeABoxAxioms = true; + private boolean ignoreTrivialAxioms = true;//ignore Thing(a),SubClassOf(A,Thing),SubPropertyOf(A,TopProperty) public OWLAxiomRenamer(OWLDataFactory df) { this.df = df; Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 10:20:34 UTC (rev 3944) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 10:59:43 UTC (rev 3945) @@ -16,6 +16,7 @@ import java.util.Map.Entry; import java.util.prefs.Preferences; +import org.aksw.commons.util.Pair; import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; import org.dllearner.algorithms.pattern.OWLAxiomPatternFinder; import org.dllearner.kb.repository.OntologyRepository; @@ -42,6 +43,8 @@ private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); private Connection conn; + + private boolean fancyLatex = false; public OWLAxiomPatternDetectionEvaluation() { initDBConnection(); @@ -98,14 +101,14 @@ //total pattern statistics for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { - Map<OWLAxiom, Integer> topNAxiomPatterns = getTopNAxiomPatterns(axiomTypeCategory, n); + Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(axiomTypeCategory, n); latex += asLatex("Top " + n + " " + axiomTypeCategory.name() + " axiom patterns.", topNAxiomPatterns) + "\n\n"; } //get top n TBox, RBox and ABox patterns by repository for (OntologyRepository repository : repositories) { for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { - Map<OWLAxiom, Integer> topNAxiomPatterns = getTopNAxiomPatterns(repository, axiomTypeCategory, n); + Map<OWLAxiom, Pair<Integer, Integer>> topNAxiomPatterns = getTopNAxiomPatterns(repository, axiomTypeCategory, n); latex += asLatex("Top " + n + " " + axiomTypeCategory.name() + " axiom patterns for " + repository.getName() + " repository.", topNAxiomPatterns) + "\n\n"; } } @@ -235,32 +238,37 @@ } } - private String asLatex(String title, Map<OWLAxiom, Integer> topN){ + private String asLatex(String title, Map<OWLAxiom, Pair<Integer, Integer>> topN){ String latexTable = "\\begin{table}\n"; - latexTable += "\\begin{tabular}{lr}\n"; + latexTable += "\\begin{tabular}{lrr}\n"; latexTable += "\\toprule\n"; - latexTable += "Pattern & Frequency\\\\\\midrule\n"; + latexTable += "Pattern & Frequency & \\#Ontologies\\\\\\midrule\n"; - for (Entry<OWLAxiom, Integer> entry : topN.entrySet()) { + for (Entry<OWLAxiom, Pair<Integer, Integer>> entry : topN.entrySet()) { OWLAxiom axiom = entry.getKey(); - Integer frequency = entry.getValue(); + Integer frequency = entry.getValue().getKey(); + Integer idf = entry.getValue().getValue(); if(axiom != null){ - latexTable += axiomRenderer.render(axiom) + " & " + frequency + "\\\\\n"; + String axiomColumn = axiomRenderer.render(axiom); + if(fancyLatex){ + axiomColumn = "\\begin{lstlisting}[language=manchester]" + axiomColumn + "\\end{lstlisting}"; + } + latexTable += axiomColumn + " & " + frequency + " & " + idf + "\\\\\n"; } } - latexTable += "\\bottomrule\\end{tabular}\n"; + latexTable += "\\bottomrule\n\\end{tabular}\n"; latexTable += "\\caption{" + title + "}\n"; latexTable += "\\end{table}\n"; return latexTable; } - private Map<OWLAxiom, Integer> getTopNAxiomPatterns(AxiomTypeCategory axiomType, int n){ - Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); + private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(AxiomTypeCategory axiomType, int n){ + Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); PreparedStatement ps; ResultSet rs; try { - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences),COUNT(ontology_id) FROM " + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); @@ -268,7 +276,7 @@ ps.setInt(2, n); rs = ps.executeQuery(); while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + topN.put(asOWLAxiom(rs.getString(1)), new Pair<Integer, Integer>(rs.getInt(2), rs.getInt(3))); } } catch(SQLException e){ e.printStackTrace(); @@ -276,13 +284,13 @@ return topN; } - private Map<OWLAxiom, Integer> getTopNAxiomPatterns(OntologyRepository repository, AxiomTypeCategory axiomType, int n){ + private Map<OWLAxiom, Pair<Integer, Integer>> getTopNAxiomPatterns(OntologyRepository repository, AxiomTypeCategory axiomType, int n){ + Map<OWLAxiom, Pair<Integer, Integer>> topN = new LinkedHashMap<OWLAxiom, Pair<Integer, Integer>>(); PreparedStatement ps; ResultSet rs; - Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); try { //get number of ontologies - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences),COUNT(ontology_id) FROM " + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); @@ -291,7 +299,7 @@ ps.setInt(3, n); rs = ps.executeQuery(); while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + topN.put(asOWLAxiom(rs.getString(1)), new Pair<Integer, Integer>(rs.getInt(2), rs.getInt(3))); } } catch(SQLException e){ e.printStackTrace(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 10:20:37
|
Revision: 3944 http://sourceforge.net/p/dl-learner/code/3944 Author: lorenz_b Date: 2013-05-04 10:20:34 +0000 (Sat, 04 May 2013) Log Message: ----------- Improved script Latex output. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 09:36:21 UTC (rev 3943) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 10:20:34 UTC (rev 3944) @@ -27,17 +27,19 @@ import org.semanticweb.owlapi.io.OWLObjectRenderer; import org.semanticweb.owlapi.io.OWLParserException; import org.semanticweb.owlapi.io.StringDocumentSource; -import org.semanticweb.owlapi.io.ToStringRenderer; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.UnloadableImportException; import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; -import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; public class OWLAxiomPatternDetectionEvaluation { + enum AxiomTypeCategory{ + TBox, RBox, ABox + } + private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); private Connection conn; @@ -91,23 +93,21 @@ private void makePatternStatistics(Collection<OntologyRepository> repositories){ int n = 10; + String latex = ""; - Map<OWLAxiom, Integer> topNTBoxAxiomPatterns = getTopNTBoxAxiomPatterns(n); - latex += asLatex("Total TBox", topNTBoxAxiomPatterns) + "\n\n"; - Map<OWLAxiom, Integer> topNRBoxAxiomPatterns = getTopNRBoxAxiomPatterns(n); - latex += asLatex("Total RBox", topNRBoxAxiomPatterns) + "\n\n"; - Map<OWLAxiom, Integer> topNABoxAxiomPatterns = getTopNABoxAxiomPatterns(n); - latex += asLatex("Total ABox", topNABoxAxiomPatterns) + "\n\n"; + //total pattern statistics + for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { + Map<OWLAxiom, Integer> topNAxiomPatterns = getTopNAxiomPatterns(axiomTypeCategory, n); + latex += asLatex("Top " + n + " " + axiomTypeCategory.name() + " axiom patterns.", topNAxiomPatterns) + "\n\n"; + } + //get top n TBox, RBox and ABox patterns by repository - - Map<OntologyRepository, Map<OWLAxiom, Integer>> topNTBoxAxiomPatternsByRepository = getTopNTBoxAxiomPatterns(repositories, n); - Map<OntologyRepository, Map<OWLAxiom, Integer>> topNRBoxAxiomPatternsByRepository = getTopNRBoxAxiomPatterns(repositories, n); - Map<OntologyRepository, Map<OWLAxiom, Integer>> topNABoxAxiomPatternsByRepository = getTopNABoxAxiomPatterns(repositories, n); for (OntologyRepository repository : repositories) { - latex += asLatex(repository.getName() + " TBox", topNTBoxAxiomPatternsByRepository.get(repository)) + "\n\n"; - latex += asLatex(repository.getName() + " RBox", topNRBoxAxiomPatternsByRepository.get(repository)) + "\n\n"; - latex += asLatex(repository.getName() + " ABox", topNABoxAxiomPatternsByRepository.get(repository)) + "\n\n"; + for (AxiomTypeCategory axiomTypeCategory : AxiomTypeCategory.values()) { + Map<OWLAxiom, Integer> topNAxiomPatterns = getTopNAxiomPatterns(repository, axiomTypeCategory, n); + latex += asLatex("Top " + n + " " + axiomTypeCategory.name() + " axiom patterns for " + repository.getName() + " repository.", topNAxiomPatterns) + "\n\n"; + } } try { new FileOutputStream("pattern-statistics.tex").write(latex.getBytes()); @@ -255,7 +255,7 @@ return latexTable; } - private Map<OWLAxiom, Integer> getTopNTBoxAxiomPatterns(int n){ + private Map<OWLAxiom, Integer> getTopNAxiomPatterns(AxiomTypeCategory axiomType, int n){ Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); PreparedStatement ps; ResultSet rs; @@ -264,7 +264,7 @@ "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, "TBox"); + ps.setString(1, axiomType.name()); ps.setInt(2, n); rs = ps.executeQuery(); while(rs.next()){ @@ -276,45 +276,19 @@ return topN; } - private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNTBoxAxiomPatterns(Collection<OntologyRepository> repositories, int n){ - Map<OntologyRepository, Map<OWLAxiom, Integer>> topNByRepository = new LinkedHashMap<OntologyRepository, Map<OWLAxiom,Integer>>(); + private Map<OWLAxiom, Integer> getTopNAxiomPatterns(OntologyRepository repository, AxiomTypeCategory axiomType, int n){ PreparedStatement ps; ResultSet rs; - //for each repository - for (OntologyRepository repository : repositories) { - Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); - try { - //get number of ontologies - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + - "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + - "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + - "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, repository.getName()); - ps.setString(2, "TBox"); - ps.setInt(3, n); - rs = ps.executeQuery(); - while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); - } - } catch(SQLException e){ - e.printStackTrace(); - } - topNByRepository.put(repository, topN); - } - return topNByRepository; - } - - private Map<OWLAxiom, Integer> getTopNRBoxAxiomPatterns(int n){ Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); - PreparedStatement ps; - ResultSet rs; try { + //get number of ontologies ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + - "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, "RBox"); - ps.setInt(2, n); + ps.setString(1, repository.getName()); + ps.setString(2, "RBox"); + ps.setInt(3, n); rs = ps.executeQuery(); while(rs.next()){ topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); @@ -325,7 +299,7 @@ return topN; } - private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNRBoxAxiomPatterns(Collection<OntologyRepository> repositories, int n){ + private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNAxiomPatterns(Collection<OntologyRepository> repositories, AxiomTypeCategory axiomType, int n){ Map<OntologyRepository, Map<OWLAxiom, Integer>> topNByRepository = new LinkedHashMap<OntologyRepository, Map<OWLAxiom,Integer>>(); PreparedStatement ps; ResultSet rs; @@ -352,56 +326,7 @@ } return topNByRepository; } - - private Map<OWLAxiom, Integer> getTopNABoxAxiomPatterns(int n){ - Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); - PreparedStatement ps; - ResultSet rs; - try { - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + - "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + - "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + - "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, "ABox"); - ps.setInt(2, n); - rs = ps.executeQuery(); - while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); - } - } catch(SQLException e){ - e.printStackTrace(); - } - return topN; - } - private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNABoxAxiomPatterns(Collection<OntologyRepository> repositories, int n){ - Map<OntologyRepository, Map<OWLAxiom, Integer>> topNByRepository = new LinkedHashMap<OntologyRepository, Map<OWLAxiom,Integer>>(); - PreparedStatement ps; - ResultSet rs; - //for each repository - for (OntologyRepository repository : repositories) { - Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); - try { - //get number of ontologies - ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + - "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + - "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + - "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); - ps.setString(1, repository.getName()); - ps.setString(2, "ABox"); - ps.setInt(3, n); - rs = ps.executeQuery(); - while(rs.next()){ - topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); - } - } catch(SQLException e){ - e.printStackTrace(); - } - topNByRepository.put(repository, topN); - } - return topNByRepository; - } - private OWLAxiom asOWLAxiom(String functionalSyntaxAxiomString){ try { StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org> " + functionalSyntaxAxiomString + ")"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 09:36:24
|
Revision: 3943 http://sourceforge.net/p/dl-learner/code/3943 Author: lorenz_b Date: 2013-05-04 09:36:21 +0000 (Sat, 04 May 2013) Log Message: ----------- Ignore illegal axioms? Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java 2013-05-04 06:14:53 UTC (rev 3942) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java 2013-05-04 09:36:21 UTC (rev 3943) @@ -8,6 +8,7 @@ import java.util.Set; import java.util.TreeSet; +import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom; import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom; @@ -66,6 +67,8 @@ private OWLClassExpressionRenamer expressionRenamer; private OWLAxiom renamedAxiom; + private boolean normalizeABoxAxioms = true; + public OWLAxiomRenamer(OWLDataFactory df) { this.df = df; } @@ -163,8 +166,13 @@ @Override public void visit(OWLDifferentIndividualsAxiom axiom) { Set<OWLIndividual> renamedIndividuals = new HashSet<OWLIndividual>(); - for(OWLIndividual ind : axiom.getIndividuals()){ - renamedIndividuals.add(expressionRenamer.rename(ind)); + if(normalizeABoxAxioms){ + renamedIndividuals.add(df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/pattern/a"))); + renamedIndividuals.add(df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/pattern/b"))); + } else { + for(OWLIndividual ind : axiom.getIndividuals()){ + renamedIndividuals.add(expressionRenamer.rename(ind)); + } } renamedAxiom = df.getOWLDifferentIndividualsAxiom(renamedIndividuals); } @@ -333,8 +341,13 @@ @Override public void visit(OWLSameIndividualAxiom axiom) { Set<OWLIndividual> renamedIndividuals = new HashSet<OWLIndividual>(); - for(OWLIndividual ind : axiom.getIndividuals()){ - renamedIndividuals.add(expressionRenamer.rename(ind)); + if(normalizeABoxAxioms){ + renamedIndividuals.add(df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/pattern/a"))); + renamedIndividuals.add(df.getOWLNamedIndividual(IRI.create("http://dl-learner.org/pattern/b"))); + } else { + for(OWLIndividual ind : axiom.getIndividuals()){ + renamedIndividuals.add(expressionRenamer.rename(ind)); + } } renamedAxiom = df.getOWLSameIndividualAxiom(renamedIndividuals); } Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 06:14:53 UTC (rev 3942) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 09:36:21 UTC (rev 3943) @@ -72,7 +72,7 @@ public void run(Collection<OntologyRepository> repositories){ //analyze repositories - analyze(repositories); +// analyze(repositories); //create statistics for the repositories makeRepositoryStatistics(repositories); @@ -245,8 +245,9 @@ OWLAxiom axiom = entry.getKey(); Integer frequency = entry.getValue(); - latexTable += axiomRenderer.render(axiom) + " & " + frequency + "\\\\\n"; - + if(axiom != null){ + latexTable += axiomRenderer.render(axiom) + " & " + frequency + "\\\\\n"; + } } latexTable += "\\bottomrule\\end{tabular}\n"; latexTable += "\\caption{" + title + "}\n"; @@ -403,7 +404,7 @@ private OWLAxiom asOWLAxiom(String functionalSyntaxAxiomString){ try { - StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org>" + functionalSyntaxAxiomString + ")"); + StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org> " + functionalSyntaxAxiomString + ")"); OWLFunctionalSyntaxOWLParser p = new OWLFunctionalSyntaxOWLParser(); OWLOntology newOntology = OWLManager.createOWLOntologyManager().createOntology(); p.parse(s, newOntology); @@ -415,7 +416,7 @@ } catch (OWLOntologyCreationException e) { e.printStackTrace(); } catch (OWLParserException e) { - e.printStackTrace(); + System.err.println("Parsing failed for axiom " + functionalSyntaxAxiomString); } catch (IOException e) { e.printStackTrace(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 06:14:56
|
Revision: 3942 http://sourceforge.net/p/dl-learner/code/3942 Author: lorenz_b Date: 2013-05-04 06:14:53 +0000 (Sat, 04 May 2013) Log Message: ----------- Some modifications in pattern detection script. Modified Paths: -------------- trunk/components-core/pom.xml trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java trunk/scripts/src/main/java/org/dllearner/scripts/OntologyMatching.java Modified: trunk/components-core/pom.xml =================================================================== --- trunk/components-core/pom.xml 2013-05-04 05:50:47 UTC (rev 3941) +++ trunk/components-core/pom.xml 2013-05-04 06:14:53 UTC (rev 3942) @@ -124,7 +124,7 @@ <dependency> <groupId>net.sourceforge.owlapi</groupId> <artifactId>owlapi-distribution</artifactId> - <version>3.4</version> + <type>pom</type> </dependency> <dependency> <groupId>net.sourceforge.owlapi</groupId> 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-05-04 05:50:47 UTC (rev 3941) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java 2013-05-04 06:14:53 UTC (rev 3942) @@ -36,7 +36,6 @@ import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; -import org.semanticweb.owlapi.model.OWLLogicalAxiom; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException; import org.semanticweb.owlapi.model.OWLOntologyCreationException; @@ -46,7 +45,6 @@ import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; -import com.google.common.collect.Multisets; public class OWLAxiomPatternFinder { @@ -363,19 +361,22 @@ Multiset<OWLAxiom> allAxiomPatterns = HashMultiset.create(); int i = 1; for (OntologyRepositoryEntry entry : entries) { - System.out.print(i + ": "); + System.out.print(i++ + ": "); 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); + System.out.print("Loading \"" + entry.getOntologyShortName() + "\" from "+ uri); try { manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntology(IRI.create(uri)); Multiset<OWLAxiom> axiomPatterns = HashMultiset.create(); Set<OWLAxiom> logicalAxioms = new HashSet<OWLAxiom>(); for (AxiomType<?> type : AxiomType.AXIOM_TYPES) { - logicalAxioms.addAll(ontology.getAxioms(type, true)); + if(type.isLogical()){ + logicalAxioms.addAll(ontology.getAxioms(type, true)); + } } + System.out.println(" (" + logicalAxioms.size() + " axioms)"); for (OWLAxiom axiom : logicalAxioms) { OWLAxiom renamedAxiom = renamer.rename(axiom); axiomPatterns.add(renamedAxiom); Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OntologyMatching.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OntologyMatching.java 2013-05-04 05:50:47 UTC (rev 3941) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OntologyMatching.java 2013-05-04 06:14:53 UTC (rev 3942) @@ -315,7 +315,7 @@ logger.info("Done."); if(performCrossValidation){ - org.dllearner.cli.CrossValidation cv = new org.dllearner.cli.CrossValidation(la, lp, rc, 5, false); +// org.dllearner.cli.CrossValidation cv = new org.dllearner.cli.CrossValidation(la, lp, rc, 5, false); } else { //apply the learning algorithm logger.info("Running learning algorithm..."); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-04 05:50:52
|
Revision: 3941 http://sourceforge.net/p/dl-learner/code/3941 Author: lorenz_b Date: 2013-05-04 05:50:47 +0000 (Sat, 04 May 2013) Log Message: ----------- Some modifications in pattern detection script. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java trunk/components-core/src/main/java/org/dllearner/kb/repository/tones/TONESRepository.java trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java trunk/pom.xml trunk/scripts/pom.xml trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.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-05-03 08:49:17 UTC (rev 3940) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java 2013-05-04 05:50:47 UTC (rev 3941) @@ -21,6 +21,7 @@ import java.util.Set; import java.util.prefs.Preferences; +import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; import org.dllearner.kb.dataset.OWLOntologyDataset; import org.dllearner.kb.repository.OntologyRepository; import org.dllearner.kb.repository.OntologyRepositoryEntry; @@ -28,6 +29,7 @@ import org.ini4j.InvalidFileFormatException; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.OWLObjectRenderer; +import org.semanticweb.owlapi.io.StringDocumentSource; import org.semanticweb.owlapi.io.UnparsableOntologyException; import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.IRI; @@ -359,7 +361,9 @@ entries = entryList; } Multiset<OWLAxiom> allAxiomPatterns = HashMultiset.create(); + int i = 1; for (OntologyRepositoryEntry entry : entries) { + System.out.print(i + ": "); URI uri = entry.getPhysicalURI(); // if(uri.toString().startsWith("http://rest.bioontology.org/bioportal/ontologies/download/42764")){ if (!ontologyProcessed(uri)) { @@ -368,15 +372,19 @@ manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntology(IRI.create(uri)); Multiset<OWLAxiom> axiomPatterns = HashMultiset.create(); - for (OWLLogicalAxiom axiom : ontology.getLogicalAxioms()) { + Set<OWLAxiom> logicalAxioms = new HashSet<OWLAxiom>(); + for (AxiomType<?> type : AxiomType.AXIOM_TYPES) { + logicalAxioms.addAll(ontology.getAxioms(type, true)); + } + for (OWLAxiom axiom : logicalAxioms) { OWLAxiom renamedAxiom = renamer.rename(axiom); axiomPatterns.add(renamedAxiom); } allAxiomPatterns.addAll(axiomPatterns); addOntologyPatterns(uri, ontology, axiomPatterns); - for (OWLAxiom owlAxiom : Multisets.copyHighestCountFirst(allAxiomPatterns).elementSet()) { +// for (OWLAxiom owlAxiom : Multisets.copyHighestCountFirst(allAxiomPatterns).elementSet()) { // System.out.println(owlAxiom + ": " + allAxiomPatterns.count(owlAxiom)); - } +// } manager.removeOntology(ontology); } catch (OWLOntologyAlreadyExistsException e) { e.printStackTrace(); @@ -384,6 +392,8 @@ e.printStackTrace(); addOntologyError(uri, e); } + } else { + System.out.println("Already processed."); } } @@ -401,5 +411,11 @@ org.coode.owlapi.functionalrenderer.OWLObjectRenderer r = new org.coode.owlapi.functionalrenderer.OWLObjectRenderer(man, ontology, sw); axiom.accept(r); System.out.println(sw.toString()); + StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org>" + sw.toString() + ")"); + OWLFunctionalSyntaxOWLParser p = new OWLFunctionalSyntaxOWLParser(); + OWLOntology newOntology = man.createOntology(); + p.parse(s, newOntology); + System.out.println(newOntology.getLogicalAxioms()); + } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java 2013-05-03 08:49:17 UTC (rev 3940) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java 2013-05-04 05:50:47 UTC (rev 3941) @@ -341,14 +341,13 @@ @Override public void visit(OWLSubPropertyChainOfAxiom axiom) { + List<OWLObjectPropertyExpression> renamedSubPropertyChain = new ArrayList<OWLObjectPropertyExpression>(); + for (OWLObjectPropertyExpression owlObjectPropertyExpression : axiom.getPropertyChain()) { + renamedSubPropertyChain.add(expressionRenamer.rename(owlObjectPropertyExpression)); + } OWLObjectPropertyExpression superProperty = axiom.getSuperProperty(); superProperty = expressionRenamer.rename(superProperty); - List<OWLObjectPropertyExpression> subPropertyChain = axiom.getPropertyChain(); - List<OWLObjectPropertyExpression> renamedSubPropertyChain = axiom.getPropertyChain(); - for (OWLObjectPropertyExpression owlObjectPropertyExpression : subPropertyChain) { - renamedSubPropertyChain.add(expressionRenamer.rename(owlObjectPropertyExpression)); - } - renamedAxiom = df.getOWLSubPropertyChainOfAxiom(subPropertyChain, superProperty); + renamedAxiom = df.getOWLSubPropertyChainOfAxiom(renamedSubPropertyChain, superProperty); } @Override Modified: trunk/components-core/src/main/java/org/dllearner/kb/repository/tones/TONESRepository.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/repository/tones/TONESRepository.java 2013-05-03 08:49:17 UTC (rev 3940) +++ trunk/components-core/src/main/java/org/dllearner/kb/repository/tones/TONESRepository.java 2013-05-04 05:50:47 UTC (rev 3941) @@ -10,14 +10,18 @@ import java.util.Collections; import java.util.List; +import org.apache.log4j.Logger; import org.dllearner.kb.repository.OntologyRepository; import org.dllearner.kb.repository.OntologyRepositoryEntry; +import org.dllearner.kb.repository.bioportal.BioPortalRepository; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLOntologyIRIMapper; import org.semanticweb.owlapi.util.OntologyIRIShortFormProvider; public class TONESRepository implements OntologyRepository{ + private static final Logger log = Logger.getLogger(TONESRepository.class); + private final String repositoryName = "TONES"; private final URI repositoryLocation = URI.create("http://owl.cs.manchester.ac.uk/repository"); @@ -91,6 +95,7 @@ catch (IOException e) { e.printStackTrace(); } + log.info("Loaded " + entries.size() + " ontology entries from TONES."); } private class RepositoryEntry implements OntologyRepositoryEntry { Modified: trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2013-05-03 08:49:17 UTC (rev 3940) +++ trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2013-05-04 05:50:47 UTC (rev 3941) @@ -61,11 +61,14 @@ import joptsimple.OptionSpec; import org.aksw.commons.jena_owlapi.Conversion; +import org.apache.jena.riot.checker.CheckerLiterals; +import org.apache.jena.riot.system.ErrorHandlerFactory; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat; +import org.coode.owlapi.turtle.TurtleOntologyFormat; import org.dllearner.algorithms.celoe.CELOE; import org.dllearner.algorithms.properties.AsymmetricObjectPropertyAxiomLearner; import org.dllearner.algorithms.properties.DataPropertyDomainAxiomLearner; @@ -127,6 +130,7 @@ import org.dllearner.utilities.datastructures.SortedSetTuple; import org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2; import org.dllearner.utilities.owl.OWLAPIAxiomConvertVisitor; +import org.dllearner.utilities.owl.OWLEntityTypeAdder; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; import org.semanticweb.owlapi.io.SystemOutDocumentTarget; @@ -212,7 +216,7 @@ // restrict tested number of entities per type (only for testing purposes); // should be set to -1 in production mode - int maxEntitiesPerType = -1; + int maxEntitiesPerType = 5; // number of axioms which will be learned/considered (only applies to // some learners) @@ -462,7 +466,7 @@ ksFragment = ksCached; rc = rcCached; } else { - System.out.print("extracting fragment ... "); + System.out.print("extracting fragment ... ");//com.hp.hpl.jena.shared.impl.JenaParameters.enableEagerLiteralValidation = true; startTime = System.currentTimeMillis(); ConciseBoundedDescriptionGenerator cbdGen = new ConciseBoundedDescriptionGeneratorImpl(ks.getEndpoint(), cache, 2); Model model = ModelFactory.createDefaultModel(); @@ -471,6 +475,7 @@ model.add(cbd); } filter(model); + OWLEntityTypeAdder.addEntityTypes(model); runTime = System.currentTimeMillis() - startTime; System.out.println("done (" + model.size()+ " triples found in " + runTime + " ms)"); OWLOntology ontology = asOWLOntology(model); @@ -768,6 +773,13 @@ model.write(fos, "TURTLE", null); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLOntology ontology = man.loadOntologyFromOntologyDocument(new ByteArrayInputStream(baos.toByteArray())); + try { + man.saveOntology(ontology, new TurtleOntologyFormat(), new FileOutputStream("error.owl")); + } catch (OWLOntologyStorageException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } return ontology; } catch (OWLOntologyCreationException e) { e.printStackTrace(); @@ -797,6 +809,7 @@ // fix URIs with spaces Resource newSubject = (Resource) subject; RDFNode newObject = object; + boolean validTriple = true; if (subject.isURIResource()) { String uri = subject.asResource().getURI(); if (uri.contains(" ")) { @@ -814,8 +827,11 @@ if (lit.getDatatype() == null || lit.getDatatype().equals(XSD.STRING)) { newObject = model.createLiteral("shortened", "en"); } + validTriple = CheckerLiterals.checkLiteral(object.asNode(), ErrorHandlerFactory.errorHandlerNoLogging, 1l, 1l); } - statementsToAdd.add(model.createStatement(newSubject, st.getPredicate(), newObject)); + if(validTriple){ + statementsToAdd.add(model.createStatement(newSubject, st.getPredicate(), newObject)); + } statementsToRemove.add(st); } Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2013-05-03 08:49:17 UTC (rev 3940) +++ trunk/pom.xml 2013-05-04 05:50:47 UTC (rev 3941) @@ -123,7 +123,8 @@ <dependency> <groupId>net.sourceforge.owlapi</groupId> <artifactId>owlapi-distribution</artifactId> - <version>3.4.4</version> + <version>3.4.3</version> + <type>pom</type> </dependency> <dependency> <groupId>net.sourceforge.owlapi</groupId> @@ -379,7 +380,7 @@ <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> - <version>5.1.13</version> + <version>5.1.24</version> </dependency> <!--GWT Dependencies --> Modified: trunk/scripts/pom.xml =================================================================== --- trunk/scripts/pom.xml 2013-05-03 08:49:17 UTC (rev 3940) +++ trunk/scripts/pom.xml 2013-05-04 05:50:47 UTC (rev 3941) @@ -116,6 +116,7 @@ <dependency> <groupId>net.sourceforge.owlapi</groupId> <artifactId>owlapi-distribution</artifactId> + <type>pom</type> </dependency> <dependency> <groupId>net.sourceforge.owlapi</groupId> Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-03 08:49:17 UTC (rev 3940) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-04 05:50:47 UTC (rev 3941) @@ -11,17 +11,34 @@ import java.sql.SQLException; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; import java.util.prefs.Preferences; +import org.coode.owlapi.functionalparser.OWLFunctionalSyntaxOWLParser; import org.dllearner.algorithms.pattern.OWLAxiomPatternFinder; import org.dllearner.kb.repository.OntologyRepository; import org.dllearner.kb.repository.bioportal.BioPortalRepository; import org.dllearner.kb.repository.tones.TONESRepository; 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.OWLParserException; +import org.semanticweb.owlapi.io.StringDocumentSource; +import org.semanticweb.owlapi.io.ToStringRenderer; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.UnloadableImportException; +import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; +import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer; + public class OWLAxiomPatternDetectionEvaluation { + private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); private Connection conn; public OWLAxiomPatternDetectionEvaluation() { @@ -59,6 +76,9 @@ //create statistics for the repositories makeRepositoryStatistics(repositories); + + //get top n TBox, RBox and ABox patterns + makePatternStatistics(repositories); } private void analyze(Collection<OntologyRepository> repositories){ @@ -69,6 +89,35 @@ } } + private void makePatternStatistics(Collection<OntologyRepository> repositories){ + int n = 10; + String latex = ""; + Map<OWLAxiom, Integer> topNTBoxAxiomPatterns = getTopNTBoxAxiomPatterns(n); + latex += asLatex("Total TBox", topNTBoxAxiomPatterns) + "\n\n"; + Map<OWLAxiom, Integer> topNRBoxAxiomPatterns = getTopNRBoxAxiomPatterns(n); + latex += asLatex("Total RBox", topNRBoxAxiomPatterns) + "\n\n"; + Map<OWLAxiom, Integer> topNABoxAxiomPatterns = getTopNABoxAxiomPatterns(n); + latex += asLatex("Total ABox", topNABoxAxiomPatterns) + "\n\n"; + + //get top n TBox, RBox and ABox patterns by repository + + Map<OntologyRepository, Map<OWLAxiom, Integer>> topNTBoxAxiomPatternsByRepository = getTopNTBoxAxiomPatterns(repositories, n); + Map<OntologyRepository, Map<OWLAxiom, Integer>> topNRBoxAxiomPatternsByRepository = getTopNRBoxAxiomPatterns(repositories, n); + Map<OntologyRepository, Map<OWLAxiom, Integer>> topNABoxAxiomPatternsByRepository = getTopNABoxAxiomPatterns(repositories, n); + for (OntologyRepository repository : repositories) { + latex += asLatex(repository.getName() + " TBox", topNTBoxAxiomPatternsByRepository.get(repository)) + "\n\n"; + latex += asLatex(repository.getName() + " RBox", topNRBoxAxiomPatternsByRepository.get(repository)) + "\n\n"; + latex += asLatex(repository.getName() + " ABox", topNABoxAxiomPatternsByRepository.get(repository)) + "\n\n"; + } + try { + new FileOutputStream("pattern-statistics.tex").write(latex.getBytes()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + private void makeRepositoryStatistics(Collection<OntologyRepository> repositories){ String latexTable = "\\begin{tabular}{lrr|rrr|rrr|rrr|rrr}"; latexTable += "\\toprule\n"; @@ -147,7 +196,7 @@ ps.setString(1, repository.getName()); minNumberOfAboxAxioms = count(ps); //get max number of abox axioms - ps = conn.prepareStatement("SELECT MAX(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps = conn.prepareStatement("SELECT MAX(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); maxNumberOfAboxAxioms = count(ps); //get avg number of abox axioms @@ -186,6 +235,193 @@ } } + private String asLatex(String title, Map<OWLAxiom, Integer> topN){ + String latexTable = "\\begin{table}\n"; + latexTable += "\\begin{tabular}{lr}\n"; + latexTable += "\\toprule\n"; + latexTable += "Pattern & Frequency\\\\\\midrule\n"; + + for (Entry<OWLAxiom, Integer> entry : topN.entrySet()) { + OWLAxiom axiom = entry.getKey(); + Integer frequency = entry.getValue(); + + latexTable += axiomRenderer.render(axiom) + " & " + frequency + "\\\\\n"; + + } + latexTable += "\\bottomrule\\end{tabular}\n"; + latexTable += "\\caption{" + title + "}\n"; + latexTable += "\\end{table}\n"; + return latexTable; + } + + private Map<OWLAxiom, Integer> getTopNTBoxAxiomPatterns(int n){ + Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); + PreparedStatement ps; + ResultSet rs; + try { + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, "TBox"); + ps.setInt(2, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + } + } catch(SQLException e){ + e.printStackTrace(); + } + return topN; + } + + private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNTBoxAxiomPatterns(Collection<OntologyRepository> repositories, int n){ + Map<OntologyRepository, Map<OWLAxiom, Integer>> topNByRepository = new LinkedHashMap<OntologyRepository, Map<OWLAxiom,Integer>>(); + PreparedStatement ps; + ResultSet rs; + //for each repository + for (OntologyRepository repository : repositories) { + Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); + try { + //get number of ontologies + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, repository.getName()); + ps.setString(2, "TBox"); + ps.setInt(3, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + } + } catch(SQLException e){ + e.printStackTrace(); + } + topNByRepository.put(repository, topN); + } + return topNByRepository; + } + + private Map<OWLAxiom, Integer> getTopNRBoxAxiomPatterns(int n){ + Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); + PreparedStatement ps; + ResultSet rs; + try { + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, "RBox"); + ps.setInt(2, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + } + } catch(SQLException e){ + e.printStackTrace(); + } + return topN; + } + + private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNRBoxAxiomPatterns(Collection<OntologyRepository> repositories, int n){ + Map<OntologyRepository, Map<OWLAxiom, Integer>> topNByRepository = new LinkedHashMap<OntologyRepository, Map<OWLAxiom,Integer>>(); + PreparedStatement ps; + ResultSet rs; + //for each repository + for (OntologyRepository repository : repositories) { + Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); + try { + //get number of ontologies + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, repository.getName()); + ps.setString(2, "RBox"); + ps.setInt(3, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + } + } catch(SQLException e){ + e.printStackTrace(); + } + topNByRepository.put(repository, topN); + } + return topNByRepository; + } + + private Map<OWLAxiom, Integer> getTopNABoxAxiomPatterns(int n){ + Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); + PreparedStatement ps; + ResultSet rs; + try { + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, "ABox"); + ps.setInt(2, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + } + } catch(SQLException e){ + e.printStackTrace(); + } + return topN; + } + + private Map<OntologyRepository, Map<OWLAxiom, Integer>> getTopNABoxAxiomPatterns(Collection<OntologyRepository> repositories, int n){ + Map<OntologyRepository, Map<OWLAxiom, Integer>> topNByRepository = new LinkedHashMap<OntologyRepository, Map<OWLAxiom,Integer>>(); + PreparedStatement ps; + ResultSet rs; + //for each repository + for (OntologyRepository repository : repositories) { + Map<OWLAxiom, Integer> topN = new LinkedHashMap<OWLAxiom, Integer>(); + try { + //get number of ontologies + ps = conn.prepareStatement("SELECT pattern,SUM(occurrences) FROM " + + "Ontology_Pattern OP, Pattern P, Ontology O WHERE " + + "(P.id=OP.pattern_id AND O.id=OP.ontology_id AND O.repository=? AND P.axiom_type=?) " + + "GROUP BY P.id ORDER BY SUM(`OP`.`occurrences`) DESC LIMIT ?"); + ps.setString(1, repository.getName()); + ps.setString(2, "ABox"); + ps.setInt(3, n); + rs = ps.executeQuery(); + while(rs.next()){ + topN.put(asOWLAxiom(rs.getString(1)), rs.getInt(2)); + } + } catch(SQLException e){ + e.printStackTrace(); + } + topNByRepository.put(repository, topN); + } + return topNByRepository; + } + + private OWLAxiom asOWLAxiom(String functionalSyntaxAxiomString){ + try { + StringDocumentSource s = new StringDocumentSource("Ontology(<http://www.pattern.org>" + functionalSyntaxAxiomString + ")"); + OWLFunctionalSyntaxOWLParser p = new OWLFunctionalSyntaxOWLParser(); + OWLOntology newOntology = OWLManager.createOWLOntologyManager().createOntology(); + p.parse(s, newOntology); + if(!newOntology.getLogicalAxioms().isEmpty()){ + return newOntology.getLogicalAxioms().iterator().next(); + } + } catch (UnloadableImportException e) { + e.printStackTrace(); + } catch (OWLOntologyCreationException e) { + e.printStackTrace(); + } catch (OWLParserException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + private int count(PreparedStatement ps) throws SQLException{ ResultSet rs = ps.executeQuery(); rs.next(); @@ -193,6 +429,7 @@ } public static void main(String[] args) throws Exception { +// ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); new OWLAxiomPatternDetectionEvaluation().run(Arrays.asList( new TONESRepository(), new BioPortalRepository())); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-03 08:49:20
|
Revision: 3940 http://sourceforge.net/p/dl-learner/code/3940 Author: lorenz_b Date: 2013-05-03 08:49:17 +0000 (Fri, 03 May 2013) Log Message: ----------- Added axiomtype column. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.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-05-03 08:48:11 UTC (rev 3939) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java 2013-05-03 08:49:17 UTC (rev 3940) @@ -81,7 +81,7 @@ private OWLObjectRenderer axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); - private boolean randomOrder = true; + private boolean randomOrder = false; public OWLAxiomPatternFinder(OWLOntologyDataset dataset) { @@ -113,7 +113,7 @@ ", 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(?,?)"); + insertPatternIdPs = conn.prepareStatement("INSERT INTO Pattern (pattern,pattern_pretty,axiom_type) VALUES(?,?,?)"); insertOntologyPatternPs = conn.prepareStatement("INSERT INTO Ontology_Pattern (ontology_id, pattern_id, occurrences) VALUES(?,?,?)"); } catch (SQLException e) { e.printStackTrace(); @@ -168,6 +168,7 @@ + "id MEDIUMINT NOT NULL AUTO_INCREMENT," + "pattern TEXT NOT NULL," + "pattern_pretty TEXT NOT NULL," + + "axiom_type VARCHAR(15) NOT NULL," + "PRIMARY KEY(id)," + "INDEX(pattern(8000))) DEFAULT CHARSET=utf8"); @@ -216,9 +217,10 @@ try { insertPatternIdPs.setString(1, axiomString); insertPatternIdPs.setString(2, axiomRenderer.render(axiom)); + insertPatternIdPs.setString(3, getAxiomType(axiom)); insertPatternIdPs.execute(); } catch (SQLException e) { - System.out.println(axiomString.length()); + System.err.println("Pattern too long for database?" + axiomString.length()); e.printStackTrace(); } //get the auto generated ID @@ -234,6 +236,22 @@ return -1; } + private String getAxiomType(OWLAxiom axiom){ + AxiomType<?> type = axiom.getAxiomType(); + String s; + if(AxiomType.TBoxAxiomTypes.contains(type)){ + s = "TBox"; + } else if(AxiomType.RBoxAxiomTypes.contains(type)){ + s = "RBox"; + } else if(AxiomType.ABoxAxiomTypes.contains(type)){ + s = "ABox"; + } else {System.out.println(axiom + "-" + type); + //should not happen + s="Non-Logical"; + } + return s; + } + private boolean ontologyProcessed(URI uri){ //check if ontology was already processed try { @@ -287,13 +305,11 @@ 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()])); + Set<OWLAxiom> tbox = ontology.getTBoxAxioms(true); + Set<OWLAxiom> rbox = ontology.getRBoxAxioms(true); + Set<OWLAxiom> abox = ontology.getABoxAxioms(true); - insertOntologyPs.setInt(4, ontology.getLogicalAxiomCount()); + insertOntologyPs.setInt(4, tbox.size() + rbox.size() + abox.size()); insertOntologyPs.setInt(5, tbox.size()); insertOntologyPs.setInt(6, rbox.size()); insertOntologyPs.setInt(7, abox.size()); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java 2013-05-03 08:48:11 UTC (rev 3939) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java 2013-05-03 08:49:17 UTC (rev 3940) @@ -48,6 +48,7 @@ import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLPropertyExpression; import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom; import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom; @@ -86,27 +87,14 @@ } @Override - public void visit(OWLAnnotationAssertionAxiom axiom) { - } - - @Override - public void visit(OWLSubAnnotationPropertyOfAxiom axiom) { - } - - @Override - public void visit(OWLAnnotationPropertyDomainAxiom axiom) { - } - - @Override - public void visit(OWLAnnotationPropertyRangeAxiom axiom) { - } - - @Override - public void visit(OWLDeclarationAxiom axiom) { - } - - @Override public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) { + OWLObjectPropertyExpression property = axiom.getProperty(); + property = expressionRenamer.rename(property); + OWLIndividual subject = axiom.getSubject(); + subject = expressionRenamer.rename(subject); + OWLIndividual object = axiom.getObject(); + object = expressionRenamer.rename(object); + renamedAxiom = df.getOWLNegativeObjectPropertyAssertionAxiom(property, subject, object); } @Override @@ -163,7 +151,13 @@ @Override public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) { -// axiom. + OWLDataPropertyExpression property = axiom.getProperty(); + property = expressionRenamer.rename(property); + OWLIndividual subject = axiom.getSubject(); + subject = expressionRenamer.rename(subject); + OWLLiteral object = axiom.getObject(); + object = expressionRenamer.rename(object); + renamedAxiom = df.getOWLNegativeDataPropertyAssertionAxiom(property, subject, object); } @Override @@ -347,6 +341,14 @@ @Override public void visit(OWLSubPropertyChainOfAxiom axiom) { + OWLObjectPropertyExpression superProperty = axiom.getSuperProperty(); + superProperty = expressionRenamer.rename(superProperty); + List<OWLObjectPropertyExpression> subPropertyChain = axiom.getPropertyChain(); + List<OWLObjectPropertyExpression> renamedSubPropertyChain = axiom.getPropertyChain(); + for (OWLObjectPropertyExpression owlObjectPropertyExpression : subPropertyChain) { + renamedSubPropertyChain.add(expressionRenamer.rename(owlObjectPropertyExpression)); + } + renamedAxiom = df.getOWLSubPropertyChainOfAxiom(subPropertyChain, superProperty); } @Override @@ -360,6 +362,14 @@ @Override public void visit(OWLHasKeyAxiom axiom) { + OWLClassExpression classExpression = axiom.getClassExpression(); + classExpression = expressionRenamer.rename(classExpression); + Set<OWLPropertyExpression<?, ?>> propertyExpressions = axiom.getPropertyExpressions(); + Set<OWLPropertyExpression<?, ?>> renamedPropertyExpressions = new HashSet<OWLPropertyExpression<?,?>>(); + for (OWLPropertyExpression<?, ?> owlPropertyExpression : propertyExpressions) { + renamedPropertyExpressions.add(expressionRenamer.rename(owlPropertyExpression)); + } + renamedAxiom = df.getOWLHasKeyAxiom(classExpression, renamedPropertyExpressions); } @Override @@ -370,6 +380,23 @@ public void visit(SWRLRule axiom) { } - + @Override + public void visit(OWLAnnotationAssertionAxiom axiom) { + } + @Override + public void visit(OWLSubAnnotationPropertyOfAxiom axiom) { + } + + @Override + public void visit(OWLAnnotationPropertyDomainAxiom axiom) { + } + + @Override + public void visit(OWLAnnotationPropertyRangeAxiom axiom) { + } + + @Override + public void visit(OWLDeclarationAxiom axiom) { + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-03 08:48:20
|
Revision: 3939 http://sourceforge.net/p/dl-learner/code/3939 Author: lorenz_b Date: 2013-05-03 08:48:11 +0000 (Fri, 03 May 2013) Log Message: ----------- Continued script. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-03 07:56:49 UTC (rev 3938) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-03 08:48:11 UTC (rev 3939) @@ -71,10 +71,10 @@ private void makeRepositoryStatistics(Collection<OntologyRepository> repositories){ String latexTable = "\\begin{tabular}{lrr|rrr|rrr|rrr|rrr}"; - latexTable += "\\toprule"; - latexTable += "Repository & \\multicolumn{2}{c}{\\#Ontologies} & \\multicolumn{12}{c}{\\#Axioms} \\\\"; - latexTable += "& Total & Error & \\multicolumn{3}{c}{Total} & \\multicolumn{3}{c}{Tbox} & \\multicolumn{3}{c}{RBox} & \\multicolumn{3}{c}{Abox} \\\\\\midrule"; - latexTable += "& & & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max \\\\\\midrule"; + latexTable += "\\toprule\n"; + latexTable += "Repository & \\multicolumn{2}{c}{\\#Ontologies} & \\multicolumn{12}{c}{\\#Axioms} \\\\\n"; + latexTable += "& Total & Error & \\multicolumn{3}{c}{Total} & \\multicolumn{3}{c}{Tbox} & \\multicolumn{3}{c}{RBox} & \\multicolumn{3}{c}{Abox} \\\\\\midrule\n"; + latexTable += "& & & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max \\\\\\midrule\n"; PreparedStatement ps; @@ -170,7 +170,7 @@ maxNumberOfRboxAxioms + "&" + minNumberOfAboxAxioms + "&" + avgNumberOfAboxAxioms + "&" + - maxNumberOfAboxAxioms + "\\\\"; + maxNumberOfAboxAxioms + "\\\\\n"; } catch (SQLException e) { e.printStackTrace(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-03 07:56:55
|
Revision: 3938 http://sourceforge.net/p/dl-learner/code/3938 Author: lorenz_b Date: 2013-05-03 07:56:49 +0000 (Fri, 03 May 2013) Log Message: ----------- Updated script for pattern detection evaluation. Modified Paths: -------------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Modified: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-03 07:32:44 UTC (rev 3937) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-03 07:56:49 UTC (rev 3938) @@ -38,7 +38,7 @@ String dbPass = prefs.node("database").get("pass", null); Class.forName("com.mysql.jdbc.Driver"); - String url = "jdbc:mysql://" + dbServer + "/" + dbName;System.out.println(url); + String url = "jdbc:mysql://" + dbServer + "/" + dbName; conn = DriverManager.getConnection(url, dbUser, dbPass); } catch (ClassNotFoundException e) { e.printStackTrace(); @@ -101,73 +101,59 @@ //get number of ontologies ps = conn.prepareStatement("SELECT COUNT(*) FROM Ontology WHERE repository=?"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - numberOfOntologies = rs.getInt(1); + numberOfOntologies = count(ps); //get number of error causing ontologies ps = conn.prepareStatement("SELECT COUNT(*) FROM Ontology WHERE repository=? AND iri LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - numberOfErrorOntologies = rs.getInt(1); + numberOfErrorOntologies = count(ps); //get min number of logical axioms ps = conn.prepareStatement("SELECT MIN(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - minNumberOfLogicalAxioms = rs.getInt(1); + minNumberOfLogicalAxioms = count(ps); //get max number of logical axioms ps = conn.prepareStatement("SELECT MAX(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - maxNumberOfLogicalAxioms = rs.getInt(1); + maxNumberOfLogicalAxioms = count(ps); //get avg number of logical axioms ps = conn.prepareStatement("SELECT AVG(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - avgNumberOfLogicalAxioms = rs.getInt(1); + avgNumberOfLogicalAxioms = count(ps); //get min number of tbox axioms ps = conn.prepareStatement("SELECT MIN(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - minNumberOfTboxAxioms = rs.getInt(1); + minNumberOfTboxAxioms = count(ps); //get max number of tbox axioms ps = conn.prepareStatement("SELECT MAX(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - maxNumberOfTboxAxioms = rs.getInt(1); + maxNumberOfTboxAxioms = count(ps); //get avg number of tbox axioms ps = conn.prepareStatement("SELECT AVG(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - avgNumberOfTboxAxioms = rs.getInt(1); + avgNumberOfTboxAxioms = count(ps); //get min number of rbox axioms ps = conn.prepareStatement("SELECT MIN(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - minNumberOfRboxAxioms = rs.getInt(1); + minNumberOfRboxAxioms = count(ps); //get max number of rbox axioms ps = conn.prepareStatement("SELECT MAX(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - maxNumberOfRboxAxioms = rs.getInt(1); + maxNumberOfRboxAxioms = count(ps); //get avg number of rbox axioms ps = conn.prepareStatement("SELECT AVG(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - avgNumberOfRboxAxioms = rs.getInt(1); + avgNumberOfRboxAxioms = count(ps); //get min number of abox axioms ps = conn.prepareStatement("SELECT MIN(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - minNumberOfAboxAxioms = rs.getInt(1); + minNumberOfAboxAxioms = count(ps); //get max number of abox axioms ps = conn.prepareStatement("SELECT MAX(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - maxNumberOfAboxAxioms = rs.getInt(1); + maxNumberOfAboxAxioms = count(ps); //get avg number of abox axioms ps = conn.prepareStatement("SELECT AVG(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); ps.setString(1, repository.getName()); - rs = ps.executeQuery(); - avgNumberOfAboxAxioms = rs.getInt(1); + avgNumberOfAboxAxioms = count(ps); latexTable += repository.getName() + "&" + @@ -200,6 +186,12 @@ } } + private int count(PreparedStatement ps) throws SQLException{ + ResultSet rs = ps.executeQuery(); + rs.next(); + return rs.getInt(1); + } + public static void main(String[] args) throws Exception { new OWLAxiomPatternDetectionEvaluation().run(Arrays.asList( new TONESRepository(), new BioPortalRepository())); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-03 07:32:48
|
Revision: 3937 http://sourceforge.net/p/dl-learner/code/3937 Author: lorenz_b Date: 2013-05-03 07:32:44 +0000 (Fri, 03 May 2013) Log Message: ----------- Added script for pattern detection evaluation. Added Paths: ----------- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java Added: trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java (rev 0) +++ trunk/scripts/src/main/java/org/dllearner/scripts/OWLAxiomPatternDetectionEvaluation.java 2013-05-03 07:32:44 UTC (rev 3937) @@ -0,0 +1,209 @@ +package org.dllearner.scripts; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.Collection; +import java.util.prefs.Preferences; + +import org.dllearner.algorithms.pattern.OWLAxiomPatternFinder; +import org.dllearner.kb.repository.OntologyRepository; +import org.dllearner.kb.repository.bioportal.BioPortalRepository; +import org.dllearner.kb.repository.tones.TONESRepository; +import org.ini4j.IniPreferences; +import org.ini4j.InvalidFileFormatException; + +public class OWLAxiomPatternDetectionEvaluation { + + private Connection conn; + + public OWLAxiomPatternDetectionEvaluation() { + initDBConnection(); + } + + private void initDBConnection() { + try { + InputStream is = this.getClass().getClassLoader().getResourceAsStream("db_settings.ini"); + Preferences prefs = new IniPreferences(is); + String dbServer = prefs.node("database").get("server", null); + String dbName = prefs.node("database").get("name", null); + String dbUser = prefs.node("database").get("user", null); + String dbPass = prefs.node("database").get("pass", null); + + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://" + dbServer + "/" + dbName;System.out.println(url); + conn = DriverManager.getConnection(url, dbUser, dbPass); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (InvalidFileFormatException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void run(Collection<OntologyRepository> repositories){ + //analyze repositories + analyze(repositories); + + //create statistics for the repositories + makeRepositoryStatistics(repositories); + } + + private void analyze(Collection<OntologyRepository> repositories){ + for (OntologyRepository repository : repositories) { + repository.initialize(); + OWLAxiomPatternFinder patternFinder = new OWLAxiomPatternFinder(repository, conn); + patternFinder.start(); + } + } + + private void makeRepositoryStatistics(Collection<OntologyRepository> repositories){ + String latexTable = "\\begin{tabular}{lrr|rrr|rrr|rrr|rrr}"; + latexTable += "\\toprule"; + latexTable += "Repository & \\multicolumn{2}{c}{\\#Ontologies} & \\multicolumn{12}{c}{\\#Axioms} \\\\"; + latexTable += "& Total & Error & \\multicolumn{3}{c}{Total} & \\multicolumn{3}{c}{Tbox} & \\multicolumn{3}{c}{RBox} & \\multicolumn{3}{c}{Abox} \\\\\\midrule"; + latexTable += "& & & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max & Min & Avg & Max \\\\\\midrule"; + + + PreparedStatement ps; + ResultSet rs; + + int numberOfOntologies; + int numberOfErrorOntologies; + int minNumberOfLogicalAxioms; + int maxNumberOfLogicalAxioms; + int avgNumberOfLogicalAxioms; + int minNumberOfTboxAxioms; + int maxNumberOfTboxAxioms; + int avgNumberOfTboxAxioms; + int minNumberOfRboxAxioms; + int maxNumberOfRboxAxioms; + int avgNumberOfRboxAxioms; + int minNumberOfAboxAxioms; + int maxNumberOfAboxAxioms; + int avgNumberOfAboxAxioms; + + //for each repository + for (OntologyRepository repository : repositories) { + try { + //get number of ontologies + ps = conn.prepareStatement("SELECT COUNT(*) FROM Ontology WHERE repository=?"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + numberOfOntologies = rs.getInt(1); + //get number of error causing ontologies + ps = conn.prepareStatement("SELECT COUNT(*) FROM Ontology WHERE repository=? AND iri LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + numberOfErrorOntologies = rs.getInt(1); + //get min number of logical axioms + ps = conn.prepareStatement("SELECT MIN(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + minNumberOfLogicalAxioms = rs.getInt(1); + //get max number of logical axioms + ps = conn.prepareStatement("SELECT MAX(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + maxNumberOfLogicalAxioms = rs.getInt(1); + //get avg number of logical axioms + ps = conn.prepareStatement("SELECT AVG(logical_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + avgNumberOfLogicalAxioms = rs.getInt(1); + //get min number of tbox axioms + ps = conn.prepareStatement("SELECT MIN(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + minNumberOfTboxAxioms = rs.getInt(1); + //get max number of tbox axioms + ps = conn.prepareStatement("SELECT MAX(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + maxNumberOfTboxAxioms = rs.getInt(1); + //get avg number of tbox axioms + ps = conn.prepareStatement("SELECT AVG(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + avgNumberOfTboxAxioms = rs.getInt(1); + //get min number of rbox axioms + ps = conn.prepareStatement("SELECT MIN(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + minNumberOfRboxAxioms = rs.getInt(1); + //get max number of rbox axioms + ps = conn.prepareStatement("SELECT MAX(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + maxNumberOfRboxAxioms = rs.getInt(1); + //get avg number of rbox axioms + ps = conn.prepareStatement("SELECT AVG(rbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + avgNumberOfRboxAxioms = rs.getInt(1); + //get min number of abox axioms + ps = conn.prepareStatement("SELECT MIN(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + minNumberOfAboxAxioms = rs.getInt(1); + //get max number of abox axioms + ps = conn.prepareStatement("SELECT MAX(tbox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + maxNumberOfAboxAxioms = rs.getInt(1); + //get avg number of abox axioms + ps = conn.prepareStatement("SELECT AVG(abox_axioms) FROM Ontology WHERE repository=? AND iri NOT LIKE 'ERROR%'"); + ps.setString(1, repository.getName()); + rs = ps.executeQuery(); + avgNumberOfAboxAxioms = rs.getInt(1); + + latexTable += + repository.getName() + "&" + + numberOfOntologies + "&" + + numberOfErrorOntologies + "&" + + minNumberOfLogicalAxioms + "&" + + avgNumberOfLogicalAxioms + "&" + + maxNumberOfLogicalAxioms + "&" + + minNumberOfTboxAxioms + "&" + + avgNumberOfTboxAxioms + "&" + + maxNumberOfTboxAxioms + "&" + + minNumberOfRboxAxioms + "&" + + avgNumberOfRboxAxioms + "&" + + maxNumberOfRboxAxioms + "&" + + minNumberOfAboxAxioms + "&" + + avgNumberOfAboxAxioms + "&" + + maxNumberOfAboxAxioms + "\\\\"; + + } catch (SQLException e) { + e.printStackTrace(); + } + } + latexTable += "\\bottomrule\\end{tabular}"; + try { + new FileOutputStream("repository-statistics.tex").write(latexTable.getBytes()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) throws Exception { + new OWLAxiomPatternDetectionEvaluation().run(Arrays.asList( + new TONESRepository(), new BioPortalRepository())); + } + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2013-05-03 07:31:42
|
Revision: 3936 http://sourceforge.net/p/dl-learner/code/3936 Author: lorenz_b Date: 2013-05-03 07:31:37 +0000 (Fri, 03 May 2013) Log Message: ----------- Commented tests. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java trunk/components-core/src/main/java/org/dllearner/utilities/datastructures/SetManipulation.java trunk/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLPatternDetectionTest.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-05-02 10:57:42 UTC (rev 3935) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java 2013-05-03 07:31:37 UTC (rev 3936) @@ -93,6 +93,19 @@ dataFactory = manager.getOWLDataFactory(); initDBConnection(); + prepare(); + } + + public OWLAxiomPatternFinder(OntologyRepository repository, Connection conn) { + this.repository = repository; + this.conn = conn; + manager = OWLManager.createOWLOntologyManager(); + dataFactory = manager.getOWLDataFactory(); + + prepare(); + } + + private void prepare(){ createTables(); try { selectOntologyIdPs = conn.prepareStatement("SELECT id FROM Ontology WHERE url=?"); Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java 2013-05-02 10:57:42 UTC (rev 3935) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java 2013-05-03 07:31:37 UTC (rev 3936) @@ -45,6 +45,7 @@ import org.dllearner.core.KnowledgeSource; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.owl.Axiom; +import org.dllearner.core.owl.ClassHierarchy; import org.dllearner.core.owl.Constant; import org.dllearner.core.owl.Datatype; import org.dllearner.core.owl.DatatypeProperty; @@ -59,6 +60,7 @@ import org.dllearner.core.owl.Thing; import org.dllearner.core.owl.TypedConstant; import org.dllearner.core.owl.UntypedConstant; +import org.dllearner.kb.OWLAPIOntology; import org.dllearner.kb.OWLOntologyKnowledgeSource; import org.dllearner.utilities.owl.ConceptComparator; import org.dllearner.utilities.owl.DLLearnerDescriptionConvertVisitor; @@ -938,7 +940,11 @@ OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); try { OWLOntology ontology = manager.loadOntologyFromOntologyDocument(IRI.create(iri)); - ontology = manager.loadOntologyFromOntologyDocument(new File("../test/phaenotype/mp-equivalence-axioms-subq.owl")); + KnowledgeSource ks = new OWLAPIOntology(ontology); + OWLAPIReasoner reasoner = new OWLAPIReasoner(ks); + reasoner.init(); + ClassHierarchy classHierarchy = reasoner.getClassHierarchy(); + System.out.println(classHierarchy.toString(false)); // new PelletReasonerFactory().createReasoner(ontology); System.out.println("Reasoner loaded succesfully."); CelReasoner r = new CelReasoner(ontology); Modified: trunk/components-core/src/main/java/org/dllearner/utilities/datastructures/SetManipulation.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/datastructures/SetManipulation.java 2013-05-02 10:57:42 UTC (rev 3935) +++ trunk/components-core/src/main/java/org/dllearner/utilities/datastructures/SetManipulation.java 2013-05-03 07:31:37 UTC (rev 3936) @@ -19,6 +19,7 @@ package org.dllearner.utilities.datastructures; +import java.util.Collection; import java.util.List; import java.util.Random; import java.util.SortedSet; @@ -160,6 +161,14 @@ return ret; } + public static SortedSet<Individual> stringToInd(Collection<String> individualsAsString) { + SortedSet<Individual> ret = new TreeSet<Individual>(); + for (String ind : individualsAsString) { + ret.add(new Individual(ind)); + } + return ret; + } + public static SortedSet<String>indToString(SortedSet<Individual> set) { SortedSet<String> ret = new TreeSet<String>(); for (Individual ind : set) { Modified: trunk/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLPatternDetectionTest.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLPatternDetectionTest.java 2013-05-02 10:57:42 UTC (rev 3935) +++ trunk/components-core/src/test/java/org/dllearner/algorithms/pattern/OWLPatternDetectionTest.java 2013-05-03 07:31:37 UTC (rev 3936) @@ -17,7 +17,7 @@ ToStringRenderer.getInstance().setRenderer(new DLSyntaxObjectRenderer()); } - @Test +// @Test public void testTONESRepository(){ OntologyRepository repository = new TONESRepository(); repository.initialize(); @@ -25,7 +25,7 @@ patternFinder.start(); } - @Test +// @Test public void testBioPortalRepository(){ OntologyRepository repository = new BioPortalRepository(); repository.initialize(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |