From: <lor...@us...> - 2011-09-16 20:17:29
|
Revision: 3276 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3276&view=rev Author: lorenz_b Date: 2011-09-16 20:17:22 +0000 (Fri, 16 Sep 2011) Log Message: ----------- Added inference to subClassOf learner. Result of learning algorithm now prefers the more specific resources. Added method to check if endpoint supports SPARQL 1.1. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.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/kb/SparqlEndpointKS.java trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java 2011-09-16 11:04:43 UTC (rev 3275) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java 2011-09-16 20:17:22 UTC (rev 3276) @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Map.Entry; import java.util.SortedSet; import java.util.TreeSet; @@ -45,6 +46,7 @@ import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.learningproblems.AxiomScore; +import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -131,7 +133,7 @@ } - Map<Individual, SortedSet<NamedClass>> ind2Types = new HashMap<Individual, SortedSet<NamedClass>>(); + Map<Individual, SortedSet<Description>> ind2Types = new HashMap<Individual, SortedSet<Description>>(); int limit = 1000; boolean repeat = true; while(!terminationCriteriaSatisfied() && repeat){ @@ -160,37 +162,46 @@ this.maxFetchedRows = maxFetchedRows; } - private boolean addIndividualsWithTypes(Map<Individual, SortedSet<NamedClass>> ind2Types, int limit, int offset){ + private boolean addIndividualsWithTypes(Map<Individual, SortedSet<Description>> ind2Types, int limit, int offset){ // String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a <%s>. ?ind a ?type} LIMIT %d OFFSET %d", classToDescribe.getName(), limit, offset); boolean notEmpty = false; String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind a <%s>} LIMIT %d OFFSET %d}}", classToDescribe.getName(), limit, offset); // String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a <%s>. ?ind a ?type} LIMIT %d OFFSET %d", classToDescribe.getName(), limit, offset); ResultSet rs = executeSelectQuery(query); Individual ind; - NamedClass newType; + Description newType; QuerySolution qs; - SortedSet<NamedClass> types; + SortedSet<Description> types; while(rs.hasNext()){ qs = rs.next(); ind = new Individual(qs.getResource("ind").getURI()); 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); + } + + } + notEmpty = true; } return notEmpty; } - private void createEvaluatedDescriptions(Map<Individual, SortedSet<NamedClass>> individual2Types){ + private void createEvaluatedDescriptions(Map<Individual, SortedSet<Description>> individual2Types){ currentlyBestEvaluatedDescriptions.clear(); - 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); @@ -207,7 +218,7 @@ EvaluatedDescription evalDesc; int total = individual2Types.keySet().size(); - for(Entry<NamedClass, Integer> entry : sortByValues(result)){ + for(Entry<Description, Integer> entry : sortByValues(result, true)){ evalDesc = new EvaluatedDescription(entry.getKey(), computeScore(total, entry.getValue())); currentlyBestEvaluatedDescriptions.add(evalDesc); @@ -223,7 +234,14 @@ public static void main(String[] args) throws Exception{ - SimpleSubclassLearner l = new SimpleSubclassLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW())); + SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveOpenLink()); + + SPARQLReasoner reasoner = new SPARQLReasoner(ks); + reasoner.prepareSubsumptionHierarchy(); + + SimpleSubclassLearner l = new SimpleSubclassLearner(ks); + l.setReasoner(reasoner); + ConfigHelper.configure(l, "maxExecutionTimeInSeconds", 10); l.setClassToDescribe(new NamedClass("http://dbpedia.org/ontology/Bridge")); l.init(); 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-16 11:04:43 UTC (rev 3275) +++ trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2011-09-16 20:17:22 UTC (rev 3276) @@ -29,6 +29,8 @@ import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.IntegerEditor; import org.dllearner.core.owl.Axiom; +import org.dllearner.core.owl.ClassHierarchy; +import org.dllearner.core.owl.Description; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.learningproblems.AxiomScore; @@ -163,6 +165,38 @@ return entries; } + protected List<Entry<Description, Integer>> sortByValues(Map<Description, Integer> map, final boolean useHierachy){ + List<Entry<Description, Integer>> entries = new ArrayList<Entry<Description, Integer>>(map.entrySet()); + final ClassHierarchy hierarchy = reasoner.getClassHierarchy(); + Collections.sort(entries, new Comparator<Entry<Description, Integer>>() { + + @Override + public int compare(Entry<Description, Integer> o1, Entry<Description, Integer> o2) { + int ret = o2.getValue().compareTo(o1.getValue()); + //if the score is the same, than we optionally also take into account the subsumption hierarchy + if(ret == 0 && useHierachy){ + if(hierarchy != null){ + if(hierarchy.contains(o1.getKey()) && hierarchy.contains(o2.getKey())){ + if(hierarchy.isSubclassOf(o1.getKey(), o2.getKey())){ + ret = -1; + } else if(hierarchy.isSubclassOf(o2.getKey(), o1.getKey())){ + ret = 1; + } else { + //we use the depth in the class hierarchy as third ranking property +// int depth1 = hierarchy.getDepth2Root(o1.getKey()); +// int depth2 = hierarchy.getDepth2Root(o2.getKey()); +// ret = depth1 - depth2; + } + } + } + } + + return ret; + } + }); + return entries; + } + protected Score computeScore(int total, int success){ double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(total, success); 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-16 11:04:43 UTC (rev 3275) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/ClassHierarchy.java 2011-09-16 20:17:22 UTC (rev 3276) @@ -184,6 +184,44 @@ return false; } } + + /** + * Implements a subsumption check using the hierarchy (no further reasoning + * checks are used). + * + * @param subClass + * The (supposedly) more special class. + * @param superClass + * The (supposedly) more general class. + * @return True if <code>subClass</code> is a subclass of + * <code>superclass</code>. + */ + public boolean isSubclassOf(Description subClass, Description superClass) { + if (subClass.equals(superClass)) { + return true; + } else { + SortedSet<Description> superClasses = subsumptionHierarchyUp.get(subClass); + if(superClasses != null){ + for (Description moreGeneralClass : superClasses) { + + // search the upper classes of the subclass + if (moreGeneralClass instanceof NamedClass) { + if (isSubclassOf(moreGeneralClass, superClass)) { + return true; + } + // we reached top, so we can return false (if top is a + // direct upper + // class, then no other upper classes can exist) + } else { + return false; + } + } + } + // we cannot reach the class via any of the upper classes, + // so it is not a super class + return false; + } + } @Override public String toString() { @@ -306,4 +344,16 @@ public boolean contains(Description description){ return subsumptionHierarchyUp.containsKey(description); } + + public int getDepth2Root(Description description){ + SortedSet<Description> superClasses = subsumptionHierarchyUp.get(description); + int depth = 0; + if(superClasses != null){ + depth = 1; + for(Description superClass : superClasses){ + depth += getDepth2Root(superClass); + } + } + return depth; + } } Modified: trunk/components-core/src/main/java/org/dllearner/kb/SparqlEndpointKS.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/SparqlEndpointKS.java 2011-09-16 11:04:43 UTC (rev 3275) +++ trunk/components-core/src/main/java/org/dllearner/kb/SparqlEndpointKS.java 2011-09-16 20:17:22 UTC (rev 3276) @@ -43,6 +43,7 @@ public class SparqlEndpointKS implements KnowledgeSource { private SparqlEndpoint endpoint; + private boolean supportsSPARQL_1_1 = false; // TODO: turn those into config options @@ -96,6 +97,14 @@ public void setNamedGraphURIs(List<String> namedGraphURIs) { this.namedGraphURIs = namedGraphURIs; + } + + public boolean supportsSPARQL_1_1() { + return supportsSPARQL_1_1; + } + + public void setSupportsSPARQL_1_1(boolean supportsSPARQL_1_1) { + this.supportsSPARQL_1_1 = supportsSPARQL_1_1; } } Modified: trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java 2011-09-16 11:04:43 UTC (rev 3275) +++ trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java 2011-09-16 20:17:22 UTC (rev 3276) @@ -713,6 +713,18 @@ return classes; } + public boolean supportsSPARQL_1_1(){ + String query = "SELECT * WHERE {?s ?p ?o. {SELECT * WHERE {?s ?p ?o.} LIMIT 1} } LIMIT 1"; + SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); + try { + sq.send(); + return true; + } catch (Exception e) { + System.out.println("Endpoint doesn't seem to support SPARQL 1.1 ."); + } + return false; + } + } /* This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |