From: <lor...@us...> - 2011-09-15 07:57:54
|
Revision: 3266 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3266&view=rev Author: lorenz_b Date: 2011-09-15 07:57:48 +0000 (Thu, 15 Sep 2011) Log Message: ----------- Added inference usage for object property domain axiom learner. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java trunk/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java 2011-09-14 19:19:58 UTC (rev 3265) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyRangeAxiomLearner.java 2011-09-15 07:57:48 UTC (rev 3266) @@ -143,7 +143,7 @@ private int addIndividualsWithTypes(Map<Individual, SortedSet<Datatype>> ind2Datatypes, int limit, int offset){ - String query = String.format("SELECT ?ind, (DATATYPE(?val) AS ?datatype) WHERE {?ind <%s> ?val.} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); + String query = String.format("SELECT ?ind (DATATYPE(?val) AS ?datatype) WHERE {?ind <%s> ?val.} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); ResultSet rs = executeSelectQuery(query); Individual ind; Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java 2011-09-14 19:19:58 UTC (rev 3265) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java 2011-09-15 07:57:48 UTC (rev 3266) @@ -88,7 +88,7 @@ } //get number of instances of s with <s p o> - query = String.format("SELECT (COUNT(DISTINCT ?s)) AS ?all WHERE {?s <%s> ?o.}", propertyToDescribe.getName()); + query = String.format("SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", propertyToDescribe.getName()); ResultSet rs = executeSelectQuery(query); QuerySolution qs; int all = 1; @@ -97,7 +97,7 @@ all = qs.getLiteral("all").getInt(); } //get number of instances of s with <s p o> <s p o1> where o != o1 - query = "SELECT (COUNT(DISTINCT ?s)) AS ?notfunctional WHERE {?s <%s> ?o. ?s <%s> ?o1. FILTER(?o != ?o1) }"; + query = "SELECT (COUNT(DISTINCT ?s) AS ?notfunctional) WHERE {?s <%s> ?o. ?s <%s> ?o1. FILTER(?o != ?o1) }"; query = query.replace("%s", propertyToDescribe.getURI().toString()); rs = executeSelectQuery(query); int notFunctional = 1; Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java 2011-09-14 19:19:58 UTC (rev 3265) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java 2011-09-15 07:57:48 UTC (rev 3266) @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -41,6 +42,7 @@ import org.dllearner.core.owl.Thing; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -92,7 +94,7 @@ logger.info("Existing domain: " + existingDomain); //get subjects with types - Map<Individual, SortedSet<NamedClass>> individual2Types = new HashMap<Individual, SortedSet<NamedClass>>(); + Map<Individual, SortedSet<Description>> individual2Types = new HashMap<Individual, SortedSet<Description>>(); boolean repeat = true; int limit = 1000; while(!terminationCriteriaSatisfied() && repeat){ @@ -115,11 +117,11 @@ return timeLimitExceeded || resultLimitExceeded; } - private List<EvaluatedAxiom> buildEvaluatedAxioms(Map<Individual, SortedSet<NamedClass>> individual2Types){ + private List<EvaluatedAxiom> buildEvaluatedAxioms(Map<Individual, SortedSet<Description>> individual2Types){ List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); - Map<NamedClass, Integer> result = new HashMap<NamedClass, Integer>(); - for(Entry<Individual, SortedSet<NamedClass>> entry : individual2Types.entrySet()){ - for(NamedClass nc : entry.getValue()){ + Map<Description, Integer> result = new HashMap<Description, Integer>(); + for(Entry<Individual, SortedSet<Description>> entry : individual2Types.entrySet()){ + for(Description nc : entry.getValue()){ Integer cnt = result.get(nc); if(cnt == null){ cnt = Integer.valueOf(1); @@ -135,7 +137,7 @@ EvaluatedAxiom evalAxiom; int total = individual2Types.keySet().size(); - for(Entry<NamedClass, Integer> entry : sortByValues(result)){ + for(Entry<Description, Integer> entry : sortByValues(result)){ evalAxiom = new EvaluatedAxiom(new ObjectPropertyDomainAxiom(propertyToDescribe, entry.getKey()), computeScore(total, entry.getValue())); axioms.add(evalAxiom); @@ -144,16 +146,16 @@ return axioms; } - private int addIndividualsWithTypes(Map<Individual, SortedSet<NamedClass>> ind2Types, int limit, int offset){ + private int addIndividualsWithTypes(Map<Individual, SortedSet<Description>> ind2Types, int limit, int offset){ String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind <%s> ?o. ?ind a ?type} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); // String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind <%s> ?o.} LIMIT %d OFFSET %d}}", propertyToDescribe.getName(), limit, offset); ResultSet rs = executeSelectQuery(query); Individual ind; - NamedClass newType; + Description newType; QuerySolution qs; - SortedSet<NamedClass> types; + SortedSet<Description> types; int cnt = 0; while(rs.hasNext()){ cnt++; @@ -162,20 +164,35 @@ newType = new NamedClass(qs.getResource("type").getURI()); types = ind2Types.get(ind); if(types == null){ - types = new TreeSet<NamedClass>(); + types = new TreeSet<Description>(); ind2Types.put(ind, types); } types.add(newType); + Set<Description> superClasses; + if(reasoner.isPrepared()){ + if(reasoner.getClassHierarchy().contains(newType)){ + superClasses = reasoner.getClassHierarchy().getSuperClasses(newType); + types.addAll(superClasses); + } + + } } return cnt; } public static void main(String[] args) throws Exception{ - ObjectPropertyDomainAxiomLearner l = new ObjectPropertyDomainAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW())); + SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW()); + + SPARQLReasoner reasoner = new SPARQLReasoner(ks); + reasoner.prepareSubsumptionHierarchy(); + + ObjectPropertyDomainAxiomLearner l = new ObjectPropertyDomainAxiomLearner(ks); + l.setReasoner(reasoner); l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/hometown")); l.setMaxExecutionTimeInSeconds(10); l.init(); l.start(); + System.out.println(l.getCurrentlyBestEvaluatedAxioms(5)); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java 2011-09-14 19:19:58 UTC (rev 3265) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java 2011-09-15 07:57:48 UTC (rev 3266) @@ -172,7 +172,7 @@ public static void main(String[] args) throws Exception{ ObjectPropertyRangeAxiomLearner l = new ObjectPropertyRangeAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia())); - l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/academicAdvisor")); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/vicePrimeMinister")); l.setMaxExecutionTimeInSeconds(10); l.init(); l.start(); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java 2011-09-14 19:19:58 UTC (rev 3265) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java 2011-09-15 07:57:48 UTC (rev 3266) @@ -90,7 +90,7 @@ //get properties and how often they occur int limit = 1000; int offset = 0; - String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?p a <http://www.w3.org/2002/07/owl#DatatypeProperty>. ?s ?p ?o. " + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + "}"; String query; @@ -159,8 +159,8 @@ public static void main(String[] args) throws Exception{ SubDataPropertyOfAxiomLearner l = new SubDataPropertyOfAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia())); - l.setPropertyToDescribe(new DatatypeProperty("http://dbpedia.org/ontology/number")); - l.setMaxExecutionTimeInSeconds(1000000); + l.setPropertyToDescribe(new DatatypeProperty("http://dbpedia.org/ontology/purpose")); + l.setMaxExecutionTimeInSeconds(10); l.init(); l.start(); System.out.println(l.getCurrentlyBestEvaluatedAxioms(5)); Modified: trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2011-09-14 19:19:58 UTC (rev 3265) +++ trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2011-09-15 07:57:48 UTC (rev 3266) @@ -78,7 +78,9 @@ @Override public void init() throws ComponentInitException { - reasoner = new SPARQLReasoner(ks); + if(reasoner == null){ + reasoner = new SPARQLReasoner(ks); + } } @Override Modified: trunk/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java 2011-09-14 19:19:58 UTC (rev 3265) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java 2011-09-15 07:57:48 UTC (rev 3266) @@ -297,4 +297,13 @@ return new ClassHierarchy(subsumptionHierarchyUpNew, subsumptionHierarchyDownNew); } + + /** + * Checks whether the description is contained in the hierarchy. + * @param description + * @return + */ + public boolean contains(Description description){ + return subsumptionHierarchyUp.containsKey(description); + } } Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2011-09-14 19:19:58 UTC (rev 3265) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2011-09-15 07:57:48 UTC (rev 3266) @@ -19,7 +19,6 @@ package org.dllearner.reasoning; -import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -30,9 +29,6 @@ import java.util.TreeMap; import java.util.TreeSet; -import org.aksw.commons.sparql.api.cache.core.QueryExecutionFactoryCache; -import org.aksw.commons.sparql.api.cache.extra.CacheCoreH2; -import org.aksw.commons.sparql.api.cache.extra.CacheImpl; import org.aksw.commons.sparql.api.core.QueryExecutionFactory; import org.aksw.commons.sparql.api.http.QueryExecutionFactoryHttp; import org.aksw.commons.sparql.api.pagination.core.QueryExecutionFactoryPaginated; @@ -64,7 +60,6 @@ import org.dllearner.utilities.owl.ConceptComparator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.propertyeditors.StringTrimmerEditor; import com.clarkparsia.owlapiv3.XSD; import com.hp.hpl.jena.query.QuerySolution; @@ -72,7 +67,6 @@ 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.rdf.model.Statement; import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; import com.hp.hpl.jena.vocabulary.OWL; import com.hp.hpl.jena.vocabulary.OWL2; @@ -632,8 +626,7 @@ @Override public ClassHierarchy getClassHierarchy() { - // TODO Auto-generated method stub - return null; + return hierarchy; } @Override @@ -812,6 +805,14 @@ return resultset; } + /** + * Returns TRUE if the class hierarchy was computed before. + * @return + */ + public boolean isPrepared(){ + return hierarchy != null; + } + private boolean executeAskQuery(String query){ QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { @@ -841,10 +842,13 @@ SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW()); SPARQLReasoner r = new SPARQLReasoner(ks); long startTime = System.currentTimeMillis(); - Model schema = r.loadSchema(); - for(Statement st : schema.listStatements().toList()){ - System.out.println(st); - } + ClassHierarchy h = r.prepareSubsumptionHierarchy(); + System.out.println(h.getSuperClasses(new NamedClass("http://dbpedia.org/ontology/PoloLeague"))); + System.out.println(h.toString(false)); +// Model schema = r.loadSchema(); +// for(Statement st : schema.listStatements().toList()){ +// System.out.println(st); +// } System.out.println("Time needed: " + (System.currentTimeMillis()-startTime) + "ms"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |