From: <lor...@us...> - 2011-12-12 14:37:07
|
Revision: 3498 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3498&view=rev Author: lorenz_b Date: 2011-12-12 14:36:59 +0000 (Mon, 12 Dec 2011) Log Message: ----------- Updated chunk of algorithms to work with SPARQL endpoints, which not support COUNT queries. Fixed problem in hierarchy, which occurs when triples of type [<cls> <rdfs:subClassOf> <cls>] occur in the knowledge base. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.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 trunk/components-core/src/main/java/org/dllearner/utilities/WordnetSimilarity.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/utilities/ICFinder.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -77,6 +77,8 @@ private boolean suggestMostGeneralClasses = true; private boolean useClassPopularity = true; + private Set<NamedClass> allClasses; + public DisjointClassesLearner(SparqlEndpointKS ks){ this.ks = ks; } @@ -115,8 +117,8 @@ //TODO //at first get all existing classes in knowledgebase - Set<NamedClass> classes = new SPARQLTasks(ks.getEndpoint()).getAllClasses(); - classes.remove(classToDescribe); + allClasses = new SPARQLTasks(ks.getEndpoint()).getAllClasses(); + allClasses.remove(classToDescribe); //get the subclasses if(reasoner.isPrepared()){ @@ -125,46 +127,97 @@ subClasses = reasoner.getSubClasses(classToDescribe, true); } + if(ks.supportsSPARQL_1_1()){ + runSPARQL1_1_Mode(); + } else { + runSPARQL1_0_Mode(); + } + //get classes and how often they occur - int limit = 1000; - int offset = 0; - String queryTemplate = "SELECT ?type COUNT(?s) AS ?count WHERE {?s a ?type." + - "{SELECT ?s WHERE {?s a <%s>.} LIMIT %d OFFSET %d}" + - "}"; - String query; - Map<NamedClass, Integer> result = new HashMap<NamedClass, Integer>(); - NamedClass cls; - Integer oldCnt; - boolean repeat = true; - while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, classToDescribe, limit, offset); - ResultSet rs = executeSelectQuery(query); - QuerySolution qs; - repeat = false; - while(rs.hasNext()){ - qs = rs.next(); - cls = new NamedClass(qs.getResource("type").getURI()); - int newCnt = qs.getLiteral("count").getInt(); - oldCnt = result.get(cls); - if(oldCnt == null){ - oldCnt = Integer.valueOf(newCnt); - } else { - oldCnt += newCnt; - } - - result.put(cls, oldCnt); - qs.getLiteral("count").getInt(); - repeat = true; - } - if(!result.isEmpty()){ - currentlyBestEvaluatedDescriptions = buildEvaluatedClassDescriptions(result, classes); - offset += 1000; - } - } logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } + + private void runSPARQL1_0_Mode(){ + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?s ?type WHERE {?s a <%s>. ?s a ?type.} LIMIT %d OFFSET %d"; + String query; + Map<NamedClass, Integer> result = new HashMap<NamedClass, Integer>(); + NamedClass cls; + Integer oldCnt; + boolean repeat = true; + + int total = 0; + + String resource = ""; + while(!terminationCriteriaSatisfied() && repeat){ + query = String.format(queryTemplate, classToDescribe, limit, offset); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + repeat = false; + while(rs.hasNext()){ + qs = rs.next(); + String newResource = qs.getResource("?s").getURI(); + if(newResource != resource){ + total++; + resource = newResource; + } + cls = new NamedClass(qs.getResource("type").getURI()); + oldCnt = result.get(cls); + if(oldCnt == null){ + oldCnt = Integer.valueOf(0); + } + int newCnt = oldCnt + 1; + + result.put(cls, newCnt); + repeat = true; + } + if(!result.isEmpty()){ + currentlyBestEvaluatedDescriptions = buildEvaluatedClassDescriptions(result, total); + offset += 1000; + } + } + } + + private void runSPARQL1_1_Mode(){ + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?type COUNT(?s) AS ?count WHERE {?s a ?type." + + "{SELECT ?s WHERE {?s a <%s>.} LIMIT %d OFFSET %d}" + + "}"; + String query; + Map<NamedClass, Integer> result = new HashMap<NamedClass, Integer>(); + NamedClass cls; + Integer oldCnt; + boolean repeat = true; + + while(!terminationCriteriaSatisfied() && repeat){ + query = String.format(queryTemplate, classToDescribe, limit, offset); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + repeat = false; + while(rs.hasNext()){ + qs = rs.next(); + cls = new NamedClass(qs.getResource("type").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(cls); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); + } else { + oldCnt += newCnt; + } + + result.put(cls, oldCnt); + repeat = true; + } + if(!result.isEmpty()){ + currentlyBestEvaluatedDescriptions = buildEvaluatedClassDescriptions(result, allClasses); + offset += 1000; + } + } + } @Override public List<Description> getCurrentlyBestDescriptions(int nrOfDescriptions) { @@ -269,6 +322,63 @@ return evalDescs; } + private List<EvaluatedDescription> buildEvaluatedClassDescriptions(Map<NamedClass, Integer> class2Count, int total){ + List<EvaluatedDescription> evalDescs = new ArrayList<EvaluatedDescription>(); + + //Remove temporarily classToDescribe but keep track of their count + class2Count.remove(classToDescribe); + + //get complete disjoint classes + Set<NamedClass> completeDisjointclasses = new TreeSet<NamedClass>(allClasses); + completeDisjointclasses.removeAll(class2Count.keySet()); + + //drop all classes which have a super class in this set + if(suggestMostGeneralClasses && reasoner.isPrepared()){ + keepMostGeneralClasses(completeDisjointclasses); + } + + //we remove the asserted subclasses here + completeDisjointclasses.removeAll(subClasses); + for(Description subClass : subClasses){ + class2Count.remove(subClass); + } + + + EvaluatedDescription evalDesc; + //firstly, create disjoint classexpressions which not occur and give score of 1 + if(reasoner.isPrepared()){ + SortedSet<Description> mostGeneralClasses = reasoner.getClassHierarchy().getMostGeneralClasses(); + } + for(NamedClass cls : completeDisjointclasses){ + if(useClassPopularity){ + int popularity = reasoner.getIndividualsCount(cls); + //we skip classes with no instances + if(popularity == 0) continue; + double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(popularity, 0); + double accuracy = (confidenceInterval[0] + confidenceInterval[1]) / 2; + evalDesc = new EvaluatedDescription(cls, new AxiomScore(1- accuracy)); + } else { + evalDesc = new EvaluatedDescription(cls, new AxiomScore(1)); + } + + evalDescs.add(evalDesc); + } + + //secondly, create disjoint classexpressions with score 1 - (#occurence/#all) + for(Entry<NamedClass, Integer> entry : sortByValues(class2Count)){ +// evalDesc = new EvaluatedDescription(entry.getKey(), +// new AxiomScore(1 - (entry.getValue() / (double)all))); + double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(total, entry.getValue()); + double accuracy = (confidenceInterval[0] + confidenceInterval[1]) / 2; + evalDesc = new EvaluatedDescription(entry.getKey(), + new AxiomScore(1 - accuracy)); + evalDescs.add(evalDesc); + } + + class2Count.put(classToDescribe, total); + return evalDescs; + } + private void keepMostGeneralClasses(Set<NamedClass> classes){ ClassHierarchy h = reasoner.getClassHierarchy(); for(NamedClass nc : new HashSet<NamedClass>(classes)){ Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -129,7 +129,6 @@ logger.info("Existing super classes: " + existingSuperClasses); } - Map<Individual, SortedSet<Description>> ind2Types = new HashMap<Individual, SortedSet<Description>>(); int limit = 1000; boolean repeat = true; @@ -142,7 +141,7 @@ logger.info("...finished in {}ms. (Got {} rows)", (System.currentTimeMillis()-startTime), fetchedRows); } - + public NamedClass getClassToDescribe() { return classToDescribe; } @@ -152,10 +151,13 @@ } 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); + String query; + if(ks.supportsSPARQL_1_1()){ + query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind a <%s>} LIMIT %d OFFSET %d}}", classToDescribe.getName(), limit, offset); + } else { + 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; Description newType; Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -21,18 +21,14 @@ import java.net.URL; import java.util.ArrayList; -import java.util.Collections; -import org.aksw.commons.collections.multimaps.BiHashMultimap; import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.ObjectPropertyEditor; import org.dllearner.core.owl.AsymmetricObjectPropertyAxiom; -import org.dllearner.core.owl.Individual; import org.dllearner.core.owl.ObjectProperty; -import org.dllearner.core.owl.SymmetricObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; import org.slf4j.Logger; @@ -40,6 +36,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL2; @ComponentAnn(name="asymmetric objectproperty axiom learner", shortName="oplasymm", version=0.1) @@ -49,6 +47,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; + + private boolean declaredAsymmetric; public AsymmetricObjectPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; @@ -71,7 +71,7 @@ //check if property is already declared as asymmetric in knowledge base String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL2.AsymmetricProperty.getURI()); - boolean declaredAsymmetric = executeAskQuery(query); + declaredAsymmetric = executeAskQuery(query); if(declaredAsymmetric) { existingAxioms.add(new AsymmetricObjectPropertyAxiom(propertyToDescribe)); logger.info("Property is already declared as symmetric in knowledge base."); @@ -87,36 +87,42 @@ } private void runSPARQL1_0_Mode(){ - BiHashMultimap<Individual, Individual> individualsMap = new BiHashMultimap<Individual, Individual>(); - boolean repeat = true; + Model model = ModelFactory.createDefaultModel(); int limit = 1000; - while(!terminationCriteriaSatisfied() && repeat){ - String query = String.format("SELECT DISTINCT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d", propertyToDescribe.getURI().toString(), limit, fetchedRows); + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // get number of instances of s with <s p o> + query = "SELECT (COUNT(?s) AS ?total) WHERE {?s <%s> ?o.}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); ResultSet rs = executeSelectQuery(query); QuerySolution qs; - Individual s; - Individual o; - int cnt = 0; + int total = 0; while(rs.hasNext()){ qs = rs.next(); - s = new Individual(qs.getResource("s").getURI()); - o = new Individual(qs.getResource("o").getURI()); - individualsMap.put(s, o); - cnt++; + total = qs.getLiteral("total").getInt(); } - int total = individualsMap.size(); - int asymmetric = 0; + query = "SELECT (COUNT(?s) AS ?symmetric) WHERE {?s <%s> ?o. ?o <%s> ?s.}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeSelectQuery(query); + int symmetric = 0; + while(rs.hasNext()){ + qs = rs.next(); + symmetric = qs.getLiteral("symmetric").getInt(); + } + int asymmetric = total - symmetric; - for(java.util.Map.Entry<Individual, Individual> e : individualsMap.entries()){ - if(!individualsMap.getInverse().containsEntry(e.getKey(), e.getValue())){ - asymmetric++; - } + if(total > 0){ + currentlyBestAxioms.clear(); + currentlyBestAxioms.add(new EvaluatedAxiom(new AsymmetricObjectPropertyAxiom(propertyToDescribe), + computeScore(total, asymmetric), declaredAsymmetric)); } - - currentlyBestAxioms = Collections.singletonList(new EvaluatedAxiom(new AsymmetricObjectPropertyAxiom(propertyToDescribe), - computeScore(total, asymmetric))); - fetchedRows += limit; - repeat = (cnt == limit); + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); } } @@ -142,7 +148,7 @@ if(total > 0){ currentlyBestAxioms.add(new EvaluatedAxiom(new AsymmetricObjectPropertyAxiom(propertyToDescribe), - computeScore(total, asymmetric))); + computeScore(total, asymmetric), declaredAsymmetric)); } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -33,6 +33,7 @@ import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; import org.dllearner.core.EvaluatedAxiom; +import org.dllearner.core.Score; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.ObjectPropertyEditor; import org.dllearner.core.owl.EquivalentObjectPropertiesAxiom; @@ -126,8 +127,11 @@ properties = new HashSet<ObjectProperty>(); properties.add(propertyToDescribe); properties.add(entry.getKey()); - evalAxiom = new EvaluatedAxiom(new EquivalentObjectPropertiesAxiom(properties), - new AxiomScore(entry.getValue() / (double)all)); + int popularity = reasoner.getPropertyCount(entry.getKey()); + int total = popularity;//Math.max(popularity, all); + int success = entry.getValue();System.out.println(entry.getKey());System.out.println(total);System.out.println(success); + Score score = computeScore(total, success); + evalAxiom = new EvaluatedAxiom(new EquivalentObjectPropertiesAxiom(properties),score); axioms.add(evalAxiom); } @@ -138,7 +142,7 @@ public static void main(String[] args) throws Exception{ EquivalentObjectPropertyAxiomLearner l = new EquivalentObjectPropertyAxiomLearner(new SparqlEndpointKS(new SparqlEndpoint( new URL("http://dbpedia.aksw.org:8902/sparql"), Collections.singletonList("http://dbpedia.org"), Collections.<String>emptyList())));//.getEndpointDBpediaLiveAKSW())); - l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/country")); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/thirdDriverCountry")); l.setMaxExecutionTimeInSeconds(10); l.init(); l.start(); 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-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -34,6 +34,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL; @ComponentAnn(name="functional dataproperty axiom learner", shortName="dplfunc", version=0.1) @@ -43,6 +45,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=DataPropertyEditor.class) private DatatypeProperty propertyToDescribe; + + private boolean declaredAsFunctional; public FunctionalDataPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; @@ -65,35 +69,90 @@ //check if property is already declared as functional in knowledge base String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL.FunctionalProperty.getURI()); - boolean declaredAsFunctional = executeAskQuery(query); + declaredAsFunctional = executeAskQuery(query); if(declaredAsFunctional) { existingAxioms.add(new FunctionalDatatypePropertyAxiom(propertyToDescribe)); logger.info("Property is already declared as functional in knowledge base."); } - //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()); + if(ks.supportsSPARQL_1_1()){ + runSPARQL1_1_Mode(); + } else { + runSPARQL1_0_Mode(); + } + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); + } + + private void runSPARQL1_0_Mode() { + Model model = ModelFactory.createDefaultModel(); + int limit = 1000; + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // 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()); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + int all = 1; + while (rs.hasNext()) { + qs = rs.next(); + 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 = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeSelectQuery(query); + int notFunctional = 1; + while (rs.hasNext()) { + qs = rs.next(); + notFunctional = qs.getLiteral("notfunctional").getInt(); + } + if (all > 0) { + currentlyBestAxioms.clear(); + currentlyBestAxioms.add(new EvaluatedAxiom( + new FunctionalDatatypePropertyAxiom(propertyToDescribe), + computeScore(all, all - notFunctional), + declaredAsFunctional)); + } + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } + } + + private void runSPARQL1_1_Mode() { + // get number of instances of s with <s p o> + String query = String.format( + "SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", + propertyToDescribe.getName()); ResultSet rs = executeSelectQuery(query); QuerySolution qs; int all = 1; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); all = qs.getLiteral("all").getInt(); } - //get number of instances of s with <s p o> <s p o1> where o != o1 + // 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 = query.replace("%s", propertyToDescribe.getURI().toString()); rs = executeSelectQuery(query); int notFunctional = 1; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); notFunctional = qs.getLiteral("notfunctional").getInt(); } - if(all > 0){ - currentlyBestAxioms.add(new EvaluatedAxiom(new FunctionalDatatypePropertyAxiom(propertyToDescribe), - computeScore(all, all - notFunctional), declaredAsFunctional)); + if (all > 0) { + currentlyBestAxioms.add(new EvaluatedAxiom( + new FunctionalDatatypePropertyAxiom(propertyToDescribe), + computeScore(all, all - notFunctional), + declaredAsFunctional)); } - - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -35,6 +35,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL; @ComponentAnn(name="functional objectproperty axiom learner", shortName="oplfunc", version=0.1) @@ -44,6 +46,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; + + private boolean declaredAsFunctional; public FunctionalObjectPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; @@ -66,40 +70,94 @@ //check if property is already declared as symmetric in knowledge base String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL.FunctionalProperty.getURI()); - boolean declaredAsFunctional = executeAskQuery(query); + declaredAsFunctional = executeAskQuery(query); if(declaredAsFunctional) { existingAxioms.add(new FunctionalObjectPropertyAxiom(propertyToDescribe)); logger.info("Property is already declared as functional in knowledge base."); } - //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()); + if(ks.supportsSPARQL_1_1()){ + runSPARQL1_1_Mode(); + } else { + runSPARQL1_0_Mode(); + } + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); + } + + private void runSPARQL1_0_Mode() { + Model model = ModelFactory.createDefaultModel(); + int limit = 1000; + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // 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()); + ResultSet rs = executeSelectQuery(query, model); + QuerySolution qs; + int all = 1; + while (rs.hasNext()) { + qs = rs.next(); + 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 = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeSelectQuery(query, model); + int notFunctional = 1; + while (rs.hasNext()) { + qs = rs.next(); + notFunctional = qs.getLiteral("notfunctional").getInt(); + } + if (all > 0) { + currentlyBestAxioms.clear(); + currentlyBestAxioms.add(new EvaluatedAxiom( + new FunctionalObjectPropertyAxiom(propertyToDescribe), + computeScore(all, all - notFunctional), + declaredAsFunctional)); + } + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } + } + + private void runSPARQL1_1_Mode() { + // get number of instances of s with <s p o> + String query = String.format( + "SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", + propertyToDescribe.getName()); ResultSet rs = executeSelectQuery(query); QuerySolution qs; int all = 1; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); all = qs.getLiteral("all").getInt(); } - //get number of instances of s with <s p o> <s p o1> where o != o1 + // 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 = query.replace("%s", propertyToDescribe.getURI().toString()); rs = executeSelectQuery(query); int notFunctional = 1; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); notFunctional = qs.getLiteral("notfunctional").getInt(); } - if(all > 0){ - currentlyBestAxioms.add(new EvaluatedAxiom(new FunctionalObjectPropertyAxiom(propertyToDescribe), - computeScore(all, all - notFunctional), declaredAsFunctional)); + if (all > 0) { + currentlyBestAxioms.add(new EvaluatedAxiom( + new FunctionalObjectPropertyAxiom(propertyToDescribe), + computeScore(all, all - notFunctional), + declaredAsFunctional)); } - - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } public static void main(String[] args) throws Exception{ - FunctionalObjectPropertyAxiomLearner l = new FunctionalObjectPropertyAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveAKSW())); + FunctionalObjectPropertyAxiomLearner l = new FunctionalObjectPropertyAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia())); l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/league")); l.setMaxExecutionTimeInSeconds(10); l.init(); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -35,6 +35,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL; @ComponentAnn(name="inversefunctional objectproperty axiom learner", shortName="oplinvfunc", version=0.1) @@ -44,6 +46,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; + + private boolean declaredAsInverseFunctional; public InverseFunctionalObjectPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; @@ -66,36 +70,99 @@ //check if property is already declared as symmetric in knowledge base String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL.InverseFunctionalProperty.getURI()); - boolean declaredAsInverseFunctional = executeAskQuery(query); + declaredAsInverseFunctional = executeAskQuery(query); if(declaredAsInverseFunctional) { existingAxioms.add(new InverseFunctionalObjectPropertyAxiom(propertyToDescribe)); logger.info("Property is already declared as functional in knowledge base."); } - //get number of instances of s with <s p o> - query = String.format("SELECT (COUNT(DISTINCT ?o) AS ?all) WHERE {?s <%s> ?o.}", propertyToDescribe.getName()); + if(ks.supportsSPARQL_1_1()){ + runSPARQL1_1_Mode(); + } else { + runSPARQL1_0_Mode(); + } + + + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); + } + + private void runSPARQL1_0_Mode() { + Model model = ModelFactory.createDefaultModel(); + int limit = 1000; + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // get number of instances of s with <s p o> + query = String.format( + "SELECT (COUNT(DISTINCT ?o) AS ?all) WHERE {?s <%s> ?o.}", + propertyToDescribe.getName()); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + int all = 1; + while (rs.hasNext()) { + qs = rs.next(); + 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 ?s1) AS ?noninversefunctional) WHERE {?s1 <%s> ?o. ?s2 <%s> ?o. FILTER(?s1 != ?s2) }"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeSelectQuery(query); + int notInverseFunctional = 1; + while (rs.hasNext()) { + qs = rs.next(); + notInverseFunctional = qs.getLiteral("noninversefunctional") + .getInt(); + } + if (all > 0) { + currentlyBestAxioms.clear(); + currentlyBestAxioms + .add(new EvaluatedAxiom( + new InverseFunctionalObjectPropertyAxiom( + propertyToDescribe), computeScore(all, all + - notInverseFunctional), + declaredAsInverseFunctional)); + } + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } + } + + private void runSPARQL1_1_Mode() { + // get number of instances of s with <s p o> + String query = String.format( + "SELECT (COUNT(DISTINCT ?o) AS ?all) WHERE {?s <%s> ?o.}", + propertyToDescribe.getName()); ResultSet rs = executeSelectQuery(query); QuerySolution qs; int all = 1; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); all = qs.getLiteral("all").getInt(); } - //get number of instances of s with <s p o> <s p o1> where o != o1 + // get number of instances of s with <s p o> <s p o1> where o != o1 query = "SELECT (COUNT(DISTINCT ?s1) AS ?noninversefunctional) WHERE {?s1 <%s> ?o. ?s2 <%s> ?o. FILTER(?s1 != ?s2) }"; query = query.replace("%s", propertyToDescribe.getURI().toString()); rs = executeSelectQuery(query); int notInverseFunctional = 1; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); - notInverseFunctional = qs.getLiteral("noninversefunctional").getInt(); + notInverseFunctional = qs.getLiteral("noninversefunctional") + .getInt(); } - if(all > 0){ - currentlyBestAxioms.add(new EvaluatedAxiom(new InverseFunctionalObjectPropertyAxiom(propertyToDescribe), - computeScore(all, all - notInverseFunctional), declaredAsInverseFunctional)); + if (all > 0) { + currentlyBestAxioms + .add(new EvaluatedAxiom( + new InverseFunctionalObjectPropertyAxiom( + propertyToDescribe), computeScore(all, all + - notInverseFunctional), + declaredAsInverseFunctional)); } - - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } public static void main(String[] args) throws Exception{ Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -21,22 +21,15 @@ import java.net.URL; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; import java.util.SortedSet; -import org.aksw.commons.collections.multimaps.BiHashMultimap; import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.ObjectPropertyEditor; -import org.dllearner.core.owl.Individual; import org.dllearner.core.owl.InverseObjectPropertyAxiom; import org.dllearner.core.owl.ObjectProperty; -import org.dllearner.core.owl.SymmetricObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.reasoning.SPARQLReasoner; @@ -45,6 +38,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; @ComponentAnn(name="inverse objectproperty domain axiom learner", shortName="oplinv", version=0.1) public class InverseObjectPropertyAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -91,35 +86,36 @@ } private void runSPARQL1_0_Mode(){ - Map<ObjectProperty, Integer> prop2CountMap = new HashMap<ObjectProperty, Integer>(); - boolean repeat = true; + Model model = ModelFactory.createDefaultModel(); int limit = 1000; - int total = 0; - while(!terminationCriteriaSatisfied() && repeat){ - String query = String.format("SELECT ?s ?p WHERE {?s <%s> ?o. OPTIONAL{?o ?p ?s.}} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, fetchedRows); + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // get number of instances of s with <s p o> + query = "SELECT (COUNT(?s) AS ?total) WHERE {?s <%s> ?o.}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); ResultSet rs = executeSelectQuery(query); QuerySolution qs; - ObjectProperty p; - int cnt = 0; + int total = 0; while(rs.hasNext()){ qs = rs.next(); - if(qs.getResource("p") != null){ - p = new ObjectProperty(qs.getResource("p").getURI()); - Integer oldCnt = prop2CountMap.get(p); - if(oldCnt == null){ - oldCnt = Integer.valueOf(0); - } - prop2CountMap.put(p, Integer.valueOf(oldCnt + 1)); - } - cnt++; + total = qs.getLiteral("total").getInt(); } - total += cnt; - for(Entry<ObjectProperty, Integer> entry : prop2CountMap.entrySet()){ - currentlyBestAxioms = Collections.singletonList(new EvaluatedAxiom(new InverseObjectPropertyAxiom(entry.getKey(), propertyToDescribe), - computeScore(total, entry.getValue()))); + + query = String.format("SELECT ?p (COUNT(?s) AS ?cnt) WHERE {?s <%s> ?o. ?o ?p ?s.} GROUP BY ?p", propertyToDescribe.getName()); + rs = executeSelectQuery(query); + while(rs.hasNext()){ + qs = rs.next(); + currentlyBestAxioms.add(new EvaluatedAxiom( + new InverseObjectPropertyAxiom(new ObjectProperty(qs.getResource("p").getURI()), propertyToDescribe), + computeScore(total, qs.getLiteral("cnt").getInt()))); } - fetchedRows += limit; - repeat = (cnt == limit); + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -19,7 +19,6 @@ package org.dllearner.algorithms.properties; -import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -37,6 +36,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL2; @ComponentAnn(name="irreflexive objectproperty axiom learner", shortName="oplirrefl", version=0.1) @@ -46,6 +47,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; + + private boolean declaredAsIrreflexive; public IrreflexiveObjectPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; @@ -68,39 +71,97 @@ //check if property is already declared as irreflexive in knowledge base String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL2.IrreflexiveProperty.getURI()); - boolean declaredAsIrreflexive = executeAskQuery(query); + declaredAsIrreflexive = executeAskQuery(query); if(declaredAsIrreflexive) { existingAxioms.add(new IrreflexiveObjectPropertyAxiom(propertyToDescribe)); logger.info("Property is already declared as irreflexive in knowledge base."); } + + if(ks.supportsSPARQL_1_1()){ + runSPARQL1_1_Mode(); + } else { + runSPARQL1_0_Mode(); + } - //get all instance s with <s p o> - query = String.format("SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", propertyToDescribe); + + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); + } + + private void runSPARQL1_0_Mode() { + Model model = ModelFactory.createDefaultModel(); + int limit = 1000; + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // get all instance s with <s p o> + query = String.format( + "SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", + propertyToDescribe); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + int all = 0; + while (rs.hasNext()) { + qs = rs.next(); + all = qs.getLiteral("all").getInt(); + + } + + // get number of instances s where not exists <s p s> + query = "SELECT (COUNT(DISTINCT ?s) AS ?irreflexive) WHERE {?s <%s> ?o. FILTER(?s != ?o)}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeSelectQuery(query); + int irreflexive = 0; + while (rs.hasNext()) { + qs = rs.next(); + irreflexive = qs.getLiteral("irreflexive").getInt(); + } + + if (all > 0) { + currentlyBestAxioms.clear(); + currentlyBestAxioms.add(new EvaluatedAxiom( + new IrreflexiveObjectPropertyAxiom(propertyToDescribe), + computeScore(all, irreflexive), declaredAsIrreflexive)); + } + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } + } + + private void runSPARQL1_1_Mode() { + // get all instance s with <s p o> + String query = String.format( + "SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", + propertyToDescribe); ResultSet rs = executeSelectQuery(query); QuerySolution qs; int all = 0; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); all = qs.getLiteral("all").getInt(); - + } - - //get number of instances s where not exists <s p s> + + // get number of instances s where not exists <s p s> query = "SELECT (COUNT(DISTINCT ?s) AS ?irreflexive) WHERE {?s <%s> ?o. FILTER(?s != ?o)}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); rs = executeSelectQuery(query); int irreflexive = 0; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); irreflexive = qs.getLiteral("irreflexive").getInt(); } - - if(all > 0){ - currentlyBestAxioms.add(new EvaluatedAxiom(new IrreflexiveObjectPropertyAxiom(propertyToDescribe), + + if (all > 0) { + currentlyBestAxioms.add(new EvaluatedAxiom( + new IrreflexiveObjectPropertyAxiom(propertyToDescribe), computeScore(all, irreflexive), declaredAsIrreflexive)); } - - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } public static void main(String[] args) throws Exception { Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -35,6 +35,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL2; @ComponentAnn(name="reflexive objectproperty axiom learner", shortName="oplrefl", version=0.1) @@ -44,6 +46,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; + + private boolean declaredAsReflexive; public ReflexiveObjectPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; @@ -66,19 +70,72 @@ //check if property is already declared as reflexive in knowledge base String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL2.ReflexiveProperty.getURI()); - boolean declaredAsReflexive = executeAskQuery(query); + declaredAsReflexive = executeAskQuery(query); if(declaredAsReflexive) { existingAxioms.add(new ReflexiveObjectPropertyAxiom(propertyToDescribe)); logger.info("Property is already declared as reflexive in knowledge base."); } - //get fraction of instances s with <s p s> - query = "SELECT (COUNT(?s) AS ?total) WHERE {?s <%s> ?o.}"; + if(ks.supportsSPARQL_1_1()){ + runSPARQL1_1_Mode(); + } else { + runSPARQL1_0_Mode(); + } + + + + logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); + } + + private void runSPARQL1_0_Mode() { + Model model = ModelFactory.createDefaultModel(); + int limit = 1000; + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // get fraction of instances s with <s p s> + query = "SELECT (COUNT(?s) AS ?total) WHERE {?s <%s> ?o.}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + int total = 0; + while (rs.hasNext()) { + qs = rs.next(); + total = qs.getLiteral("total").getInt(); + } + query = "SELECT (COUNT(?s) AS ?reflexive) WHERE {?s <%s> ?s.}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeSelectQuery(query); + int reflexive = 0; + while (rs.hasNext()) { + qs = rs.next(); + reflexive = qs.getLiteral("reflexive").getInt(); + + } + if (total > 0) { + currentlyBestAxioms.clear(); + currentlyBestAxioms.add(new EvaluatedAxiom( + new ReflexiveObjectPropertyAxiom(propertyToDescribe), + computeScore(total, reflexive), declaredAsReflexive)); + } + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } + } + + private void runSPARQL1_1_Mode() { + // get fraction of instances s with <s p s> + String query = "SELECT (COUNT(?s) AS ?total) WHERE {?s <%s> ?o.}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); ResultSet rs = executeSelectQuery(query); QuerySolution qs; int total = 0; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); total = qs.getLiteral("total").getInt(); } @@ -86,17 +143,16 @@ query = query.replace("%s", propertyToDescribe.getURI().toString()); rs = executeSelectQuery(query); int reflexive = 0; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); reflexive = qs.getLiteral("reflexive").getInt(); - + } - if(total > 0){ - currentlyBestAxioms.add(new EvaluatedAxiom(new ReflexiveObjectPropertyAxiom(propertyToDescribe), + if (total > 0) { + currentlyBestAxioms.add(new EvaluatedAxiom( + new ReflexiveObjectPropertyAxiom(propertyToDescribe), computeScore(total, reflexive), declaredAsReflexive)); } - - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } public static void main(String[] args) throws Exception{ Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -21,15 +21,12 @@ import java.net.URL; import java.util.ArrayList; -import java.util.Collections; -import org.aksw.commons.collections.multimaps.BiHashMultimap; import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.ObjectPropertyEditor; -import org.dllearner.core.owl.Individual; import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.SymmetricObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; @@ -39,6 +36,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL2; @ComponentAnn(name="symmetric objectproperty axiom learner", shortName="oplsymm", version=0.1) @@ -48,6 +47,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; + + private boolean declaredAsSymmetric; public SymmetricObjectPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; @@ -70,7 +71,7 @@ //check if property is already declared as symmetric in knowledge base String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL2.SymmetricProperty.getURI()); - boolean declaredAsSymmetric = executeAskQuery(query); + declaredAsSymmetric = executeAskQuery(query); if(declaredAsSymmetric) { existingAxioms.add(new SymmetricObjectPropertyAxiom(propertyToDescribe)); logger.info("Property is already declared as symmetric in knowledge base."); @@ -86,36 +87,42 @@ } private void runSPARQL1_0_Mode(){ - BiHashMultimap<Individual, Individual> individualsMap = new BiHashMultimap<Individual, Individual>(); - boolean repeat = true; + Model model = ModelFactory.createDefaultModel(); int limit = 1000; - while(!terminationCriteriaSatisfied() && repeat){ - String query = String.format("SELECT DISTINCT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d", propertyToDescribe.getURI().toString(), limit, fetchedRows); + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // get number of instances of s with <s p o> + query = "SELECT (COUNT(?s) AS ?total) WHERE {?s <%s> ?o.}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); ResultSet rs = executeSelectQuery(query); QuerySolution qs; - Individual s; - Individual o; - int cnt = 0; + int total = 0; while(rs.hasNext()){ qs = rs.next(); - s = new Individual(qs.getResource("s").getURI()); - o = new Individual(qs.getResource("o").getURI()); - individualsMap.put(s, o); - cnt++; + total = qs.getLiteral("total").getInt(); } - int total = individualsMap.size(); + query = "SELECT (COUNT(?s) AS ?symmetric) WHERE {?s <%s> ?o. ?o <%s> ?s}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeSelectQuery(query); int symmetric = 0; + while(rs.hasNext()){ + qs = rs.next(); + symmetric = qs.getLiteral("symmetric").getInt(); + } - for(java.util.Map.Entry<Individual, Individual> e : individualsMap.entries()){ - if(individualsMap.getInverse().containsEntry(e.getKey(), e.getValue())){ - symmetric++; - } + + if(total > 0){ + currentlyBestAxioms.clear(); + currentlyBestAxioms.add(new EvaluatedAxiom(new SymmetricObjectPropertyAxiom(propertyToDescribe), + computeScore(total, symmetric), declaredAsSymmetric)); } - - currentlyBestAxioms = Collections.singletonList(new EvaluatedAxiom(new SymmetricObjectPropertyAxiom(propertyToDescribe), - computeScore(total, symmetric))); - fetchedRows += limit; - repeat = (cnt == limit); + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); } } @@ -141,7 +148,7 @@ if(total > 0){ currentlyBestAxioms.add(new EvaluatedAxiom(new SymmetricObjectPropertyAxiom(propertyToDescribe), - computeScore(total, symmetric))); + computeScore(total, symmetric), declaredAsSymmetric)); } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -20,7 +20,6 @@ package org.dllearner.algorithms.properties; import java.util.ArrayList; -import java.util.Collections; import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; @@ -36,6 +35,8 @@ import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.vocabulary.OWL; @ComponentAnn(name="transitive objectproperty axiom learner", shortName="opltrans", version=0.1) @@ -45,6 +46,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; + + private boolean declaredAsTransitive; public TransitiveObjectPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; @@ -67,13 +70,12 @@ //check if property is already declared as transitive in knowledge base String query = String.format("ASK {<%s> a <%s>}", propertyToDescribe, OWL.TransitiveProperty.getURI()); - boolean declaredAsTransitive = executeAskQuery(query); + declaredAsTransitive = executeAskQuery(query); if(declaredAsTransitive) { existingAxioms.add(new TransitiveObjectPropertyAxiom(propertyToDescribe)); logger.info("Property is already declared as transitive in knowledge base."); } - if(ks.supportsSPARQL_1_1()){ runSPARQL1_1_Mode(); } else { @@ -85,7 +87,42 @@ private void runSPARQL1_0_Mode(){ - currentlyBestAxioms = Collections.emptyList(); + Model model = ModelFactory.createDefaultModel(); + int limit = 1000; + int offset = 0; + String baseQuery = "CONSTRUCT {?s <%s> ?o.} WHERE {?s <%s> ?o} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + while(newModel.size() != 0){ + model.add(newModel); + // get number of instances of s with <s p o> + query = "SELECT (COUNT(?o) AS ?total) WHERE {?s <%s> ?o. ?o <%s> ?o1.}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + int total = 0; + while(rs.hasNext()){ + qs = rs.next(); + total = qs.getLiteral("total").getInt(); + } + query = "SELECT (COUNT(?o) AS ?transitive) WHERE {?s <%s> ?o. ?o <%s> ?o1. ?s <%s> ?o1.}"; + query = query.replace("%s", propertyToDescribe.getURI().toString()); + rs = executeSelectQuery(query); + int transitive = 0; + while(rs.hasNext()){ + qs = rs.next(); + transitive = qs.getLiteral("transitive").getInt(); + } + + if(total > 0){ + currentlyBestAxioms.clear(); + currentlyBestAxioms.add(new EvaluatedAxiom(new TransitiveObjectPropertyAxiom(propertyToDescribe), + computeScore(total, transitive), declaredAsTransitive)); + } + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } } private void runSPARQL1_1_Mode(){ @@ -109,7 +146,7 @@ if(total > 0){ currentlyBestAxioms.add(new EvaluatedAxiom(new TransitiveObjectPropertyAxiom(propertyToDescribe), - computeScore(total, transitive))); + computeScore(total, transitive), declaredAsTransitive)); } } Modified: trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2011-12-11 11:37:29 UTC (rev 3497) +++ trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2011-12-12 14:36:59 UTC (rev 3498) @@ -28,7 +28,6 @@ import java.util.SortedSet; import java.util.TreeSet; -import org.aksw.commons.jena.CollectionResultSet; import org.dllearner.core.config.BooleanEditor; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.IntegerEditor; @@ -45,8 +44,10 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; /** @@ -183,6 +184,17 @@ return returnList; } + protected Model executeConstructQuery(String query) { + logger.info("Sending query\n{} ...", query); + queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), + query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); + queryExecution.setDefaultGraphURIs(ks.getEndpoint().getDefaultGraphURIs()); + queryExecution.setNamedGraphURIs(ks.getEndpoint().getNamedGraphURIs()); + System.out.println(query); + return queryExecution.execConstruct(); + } + protected ResultSet executeSelectQuery(String query) { logger.info("Sending query\n{} ...", query); queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), @@ -203,6 +215,15 @@ return resultSet; } + protected ResultSet executeSelectQuery(String query, Model model) { + logger.info("Sending query\n{} ...", query); + QueryExecution qexec = QueryExecutionFactory.create(query, model); + ResultSet rs = qexec.execSelect();; + + + return rs; + } + protected v... [truncated message content] |