From: <lor...@us...> - 2011-12-13 11:57:39
|
Revision: 3501 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3501&view=rev Author: lorenz_b Date: 2011-12-13 11:57:29 +0000 (Tue, 13 Dec 2011) Log Message: ----------- Updated chunk of algorithms to work with SPARQL endpoints, which not support COUNT queries. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.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-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -350,7 +350,7 @@ SortedSet<Description> mostGeneralClasses = reasoner.getClassHierarchy().getMostGeneralClasses(); } for(NamedClass cls : completeDisjointclasses){ - if(useClassPopularity){ + if(useClassPopularity && ks.supportsSPARQL_1_1()){ int popularity = reasoner.getIndividualsCount(cls); //we skip classes with no instances if(popularity == 0) continue; Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java 2011-12-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -42,6 +42,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="disjoint dataproperty axiom learner", shortName="dpldisjoint", version=0.1) public class DisjointDataPropertyAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -51,6 +53,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=DataPropertyEditor.class) private DatatypeProperty propertyToDescribe; + private Set<DatatypeProperty> allDataProperties; + public DisjointDataPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; } @@ -73,45 +77,94 @@ //TODO //at first get all existing dataproperties in knowledgebase - Set<DatatypeProperty> dataProperties = new SPARQLTasks(ks.getEndpoint()).getAllDataProperties(); + allDataProperties = new SPARQLTasks(ks.getEndpoint()).getAllDataProperties(); + allDataProperties.remove(propertyToDescribe); + 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 ?p ?o.} WHERE {?s <%s> ?o. ?s ?p ?o.} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>(); + while(!terminationCriteriaSatisfied() && newModel.size() != 0){ + model.add(newModel); + query = "SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o.} GROUP BY ?p"; + + DatatypeProperty prop; + Integer oldCnt; + ResultSet rs = executeSelectQuery(query, model); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + prop = new DatatypeProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); + } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result, allDataProperties); + } + + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } + + } + + private void runSPARQL1_1_Mode() { //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." + - "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + - "}"; - String query; - Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>(); - DatatypeProperty prop; - Integer oldCnt; - boolean repeat = true; - - ResultSet rs = null; - while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, propertyToDescribe, limit, offset); - rs = executeSelectQuery(query); - QuerySolution qs; - repeat = false; - while(rs.hasNext()){ - qs = rs.next(); - prop = new DatatypeProperty(qs.getResource("p").getURI()); - int newCnt = qs.getLiteral("count").getInt(); - oldCnt = result.get(prop); - if(oldCnt == null){ - oldCnt = Integer.valueOf(newCnt); - } - result.put(prop, oldCnt); - qs.getLiteral("count").getInt(); - repeat = true; - } - if(!result.isEmpty()){ - currentlyBestAxioms = buildAxioms(result, dataProperties); - offset += 1000; - } + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?p (COUNT(?s) as ?count) WHERE {?s ?p ?o." + + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + + "}"; + String query; + Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>(); + DatatypeProperty prop; + Integer oldCnt; + boolean repeat = true; + + ResultSet rs = null; + while(!terminationCriteriaSatisfied() && repeat){ + query = String.format(queryTemplate, propertyToDescribe, limit, offset); + rs = executeSelectQuery(query); + QuerySolution qs; + repeat = false; + while(rs.hasNext()){ + qs = rs.next(); + prop = new DatatypeProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + repeat = true; + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result, allDataProperties); + offset += 1000; + } + } - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } @Override Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java 2011-12-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -36,6 +36,7 @@ import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.ObjectPropertyEditor; import org.dllearner.core.owl.DisjointObjectPropertyAxiom; +import org.dllearner.core.owl.FunctionalObjectPropertyAxiom; import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SPARQLTasks; @@ -47,6 +48,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="disjoint objectproperty axiom learner", shortName="opldisjoint", version=0.1) public class DisjointObjectPropertyAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -56,6 +59,8 @@ @ConfigOption(name="propertyToDescribe", description="", propertyEditorClass=ObjectPropertyEditor.class) private ObjectProperty propertyToDescribe; + private Set<ObjectProperty> allObjectProperties; + private boolean usePropertyPopularity = true; public DisjointObjectPropertyAxiomLearner(SparqlEndpointKS ks){ @@ -77,48 +82,96 @@ fetchedRows = 0; currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); - //TODO + //TODO detect existing axioms //at first get all existing objectproperties in knowledgebase - Set<ObjectProperty> objectProperties = new SPARQLTasks(ks.getEndpoint()).getAllObjectProperties(); - objectProperties.remove(propertyToDescribe); + allObjectProperties = new SPARQLTasks(ks.getEndpoint()).getAllObjectProperties(); + allObjectProperties.remove(propertyToDescribe); + 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 ?p ?o.} WHERE {?s <%s> ?o. ?s ?p ?o.} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); + while(!terminationCriteriaSatisfied() && newModel.size() != 0){ + model.add(newModel); + query = "SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o.} GROUP BY ?p"; + + ObjectProperty prop; + Integer oldCnt; + ResultSet rs = executeSelectQuery(query, model); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + prop = new ObjectProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); + } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result, allObjectProperties); + } + + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } + + } + + private void runSPARQL1_1_Mode() { //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." + - "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + - "}"; - String query; - Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); - ObjectProperty prop; - Integer oldCnt; - boolean repeat = true; - - while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, propertyToDescribe, limit, offset); - ResultSet rs = executeSelectQuery(query); - QuerySolution qs; - repeat = false; - while(rs.hasNext()){ - qs = rs.next(); - prop = new ObjectProperty(qs.getResource("p").getURI()); - int newCnt = qs.getLiteral("count").getInt(); - oldCnt = result.get(prop); - if(oldCnt == null){ - oldCnt = Integer.valueOf(newCnt); - } - result.put(prop, oldCnt); - qs.getLiteral("count").getInt(); - repeat = true; - } - if(!result.isEmpty()){ - currentlyBestAxioms = buildAxioms(result, objectProperties); - offset += 1000; - } + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + + "}"; + String query; + Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); + ObjectProperty prop; + Integer oldCnt; + boolean repeat = true; + + while(!terminationCriteriaSatisfied() && repeat){ + query = String.format(queryTemplate, propertyToDescribe, limit, offset); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + repeat = false; + while(rs.hasNext()){ + qs = rs.next(); + prop = new ObjectProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + repeat = true; + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result, allObjectProperties); + offset += 1000; + } + } - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } private List<EvaluatedAxiom> buildAxioms(Map<ObjectProperty, Integer> property2Count, Set<ObjectProperty> allProperties){ Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java 2011-12-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -41,6 +41,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="equivalent dataproperty axiom learner", shortName="dplequiv", version=0.1) public class EquivalentDataPropertyAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -72,43 +74,91 @@ SortedSet<DatatypeProperty> existingSuperProperties = reasoner.getSuperProperties(propertyToDescribe); logger.debug("Existing super properties: " + existingSuperProperties); - //get subjects with types + 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 queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + - "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + - "}"; + String baseQuery = "CONSTRUCT {?s ?p ?o.} WHERE {?s <%s> ?o. ?s ?p ?o.} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); + Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>(); + while(!terminationCriteriaSatisfied() && newModel.size() != 0){ + model.add(newModel); + query = "SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o.} GROUP BY ?p"; + + DatatypeProperty prop; + Integer oldCnt; + ResultSet rs = executeSelectQuery(query, model); + QuerySolution qs; + while(rs.hasNext()){ + qs = rs.next(); + prop = new DatatypeProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); + } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result); + } + + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); + } + + } + + private void runSPARQL1_1_Mode() { + // get subjects with types + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + "}"; String query; Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>(); DatatypeProperty prop; Integer oldCnt; boolean repeat = true; - - while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, propertyToDescribe, limit, offset); + + while (!terminationCriteriaSatisfied() && repeat) { + query = String.format(queryTemplate, propertyToDescribe, limit, + offset); ResultSet rs = executeSelectQuery(query); QuerySolution qs; repeat = false; - while(rs.hasNext()){ + while (rs.hasNext()) { qs = rs.next(); prop = new DatatypeProperty(qs.getResource("p").getURI()); int newCnt = qs.getLiteral("count").getInt(); oldCnt = result.get(prop); - if(oldCnt == null){ + if (oldCnt == null) { oldCnt = Integer.valueOf(newCnt); } result.put(prop, oldCnt); qs.getLiteral("count").getInt(); repeat = true; } - if(!result.isEmpty()){ + if (!result.isEmpty()) { currentlyBestAxioms = buildAxioms(result); offset += 1000; } - + } - - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); + } 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-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -40,12 +40,13 @@ import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; -import org.dllearner.learningproblems.AxiomScore; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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="equivalent objectproperty axiom learner", shortName="oplequiv", version=0.1) public class EquivalentObjectPropertyAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -77,23 +78,31 @@ SortedSet<ObjectProperty> existingSuperProperties = reasoner.getSuperProperties(propertyToDescribe); logger.debug("Existing super properties: " + existingSuperProperties); - //get subjects with types + 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 queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + - "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + - "}"; - String query; + String baseQuery = "CONSTRUCT {?s ?p ?o.} WHERE {?s <%s> ?o. ?s ?p ?o.} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); - ObjectProperty prop; - Integer oldCnt; - boolean repeat = true; - - while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, propertyToDescribe, limit, offset); - ResultSet rs = executeSelectQuery(query); + while(!terminationCriteriaSatisfied() && newModel.size() != 0){ + model.add(newModel); + query = "SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o.} GROUP BY ?p"; + + ObjectProperty prop; + Integer oldCnt; + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; - repeat = false; while(rs.hasNext()){ qs = rs.next(); prop = new ObjectProperty(qs.getResource("p").getURI()); @@ -104,18 +113,58 @@ } result.put(prop, oldCnt); qs.getLiteral("count").getInt(); - repeat = true; } if(!result.isEmpty()){ currentlyBestAxioms = buildAxioms(result); - offset += 1000; } + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); } - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } + private void runSPARQL1_1_Mode() { + //get subjects with types + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + + "}"; + String query; + Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); + ObjectProperty prop; + Integer oldCnt; + boolean repeat = true; + + while(!terminationCriteriaSatisfied() && repeat){ + query = String.format(queryTemplate, propertyToDescribe, limit, offset); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + repeat = false; + while(rs.hasNext()){ + qs = rs.next(); + prop = new ObjectProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); + } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + repeat = true; + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result); + offset += 1000; + } + + } + + } + private List<EvaluatedAxiom> buildAxioms(Map<ObjectProperty, Integer> property2Count){ List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); Integer all = property2Count.get(propertyToDescribe); @@ -129,7 +178,7 @@ properties.add(entry.getKey()); 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); + 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); 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-12-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -40,6 +40,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="data subPropertyOf axiom learner", shortName="dplsubprop", version=0.1) public class SubDataPropertyOfAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -71,23 +73,31 @@ SortedSet<DatatypeProperty> existingSuperProperties = reasoner.getSuperProperties(propertyToDescribe); logger.debug("Existing super properties: " + existingSuperProperties); - //get properties and how often they occur + 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 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; + String baseQuery = "CONSTRUCT {?s ?p ?o.} WHERE {?s <%s> ?o. ?s ?p ?o.} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>(); - DatatypeProperty prop; - Integer oldCnt; - boolean repeat = true; - - while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, propertyToDescribe, limit, offset); - ResultSet rs = executeSelectQuery(query); + while(!terminationCriteriaSatisfied() && newModel.size() != 0){ + model.add(newModel); + query = "SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o.} GROUP BY ?p"; + + DatatypeProperty prop; + Integer oldCnt; + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; - repeat = false; while(rs.hasNext()){ qs = rs.next(); prop = new DatatypeProperty(qs.getResource("p").getURI()); @@ -98,18 +108,58 @@ } result.put(prop, oldCnt); qs.getLiteral("count").getInt(); - repeat = true; } - if(!result.isEmpty()){ currentlyBestAxioms = buildAxioms(result); - offset += 1000; } + + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); } - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } + private void runSPARQL1_1_Mode() { + //get subjects with types + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + + "}"; + String query; + Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>(); + DatatypeProperty prop; + Integer oldCnt; + boolean repeat = true; + + while(!terminationCriteriaSatisfied() && repeat){ + query = String.format(queryTemplate, propertyToDescribe, limit, offset); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + repeat = false; + while(rs.hasNext()){ + qs = rs.next(); + prop = new DatatypeProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); + } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + repeat = true; + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result); + offset += 1000; + } + + } + + } + private List<EvaluatedAxiom> buildAxioms(Map<DatatypeProperty, Integer> property2Count){ List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); Integer total = property2Count.get(propertyToDescribe); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java 2011-12-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -40,6 +40,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="object subPropertyOf axiom learner", shortName="oplsubprop", version=0.1) public class SubObjectPropertyOfAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -71,23 +73,31 @@ SortedSet<ObjectProperty> existingSuperProperties = reasoner.getSuperProperties(propertyToDescribe); logger.debug("Existing super properties: " + existingSuperProperties); - //get subjects with types + 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 queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + - "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + - "}"; - String query; + String baseQuery = "CONSTRUCT {?s ?p ?o.} WHERE {?s <%s> ?o. ?s ?p ?o.} LIMIT %d OFFSET %d"; + String query = String.format(baseQuery, propertyToDescribe.getName(), limit, offset); + Model newModel = executeConstructQuery(query); Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); - ObjectProperty prop; - Integer oldCnt; - boolean repeat = true; - - while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, propertyToDescribe, limit, offset); - ResultSet rs = executeSelectQuery(query); + while(!terminationCriteriaSatisfied() && newModel.size() != 0){ + model.add(newModel); + query = "SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o.} GROUP BY ?p"; + + ObjectProperty prop; + Integer oldCnt; + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; - repeat = false; while(rs.hasNext()){ qs = rs.next(); prop = new ObjectProperty(qs.getResource("p").getURI()); @@ -98,17 +108,58 @@ } result.put(prop, oldCnt); qs.getLiteral("count").getInt(); - repeat = true; } if(!result.isEmpty()){ currentlyBestAxioms = buildAxioms(result); - offset += 1000; } + + + offset += limit; + query = String.format(baseQuery, propertyToDescribe.getName(), propertyToDescribe.getName(), limit, offset); + newModel = executeConstructQuery(query); } - logger.info("...finished in {}ms.", (System.currentTimeMillis()-startTime)); } + private void runSPARQL1_1_Mode() { + //get subjects with types + int limit = 1000; + int offset = 0; + String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + + "}"; + String query; + Map<ObjectProperty, Integer> result = new HashMap<ObjectProperty, Integer>(); + ObjectProperty prop; + Integer oldCnt; + boolean repeat = true; + + while(!terminationCriteriaSatisfied() && repeat){ + query = String.format(queryTemplate, propertyToDescribe, limit, offset); + ResultSet rs = executeSelectQuery(query); + QuerySolution qs; + repeat = false; + while(rs.hasNext()){ + qs = rs.next(); + prop = new ObjectProperty(qs.getResource("p").getURI()); + int newCnt = qs.getLiteral("count").getInt(); + oldCnt = result.get(prop); + if(oldCnt == null){ + oldCnt = Integer.valueOf(newCnt); + } + result.put(prop, oldCnt); + qs.getLiteral("count").getInt(); + repeat = true; + } + if(!result.isEmpty()){ + currentlyBestAxioms = buildAxioms(result); + offset += 1000; + } + + } + + } + private List<EvaluatedAxiom> buildAxioms(Map<ObjectProperty, Integer> property2Count){ List<EvaluatedAxiom> axioms = new ArrayList<EvaluatedAxiom>(); Integer total = property2Count.get(propertyToDescribe); 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-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -191,7 +191,7 @@ queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); queryExecution.setDefaultGraphURIs(ks.getEndpoint().getDefaultGraphURIs()); queryExecution.setNamedGraphURIs(ks.getEndpoint().getNamedGraphURIs()); - System.out.println(query); + return queryExecution.execConstruct(); } Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2011-12-12 15:40:07 UTC (rev 3500) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2011-12-13 11:57:29 UTC (rev 3501) @@ -595,7 +595,7 @@ public SortedSet<ObjectProperty> getInverseObjectProperties(ObjectProperty property){ SortedSet<ObjectProperty> inverseObjectProperties = new TreeSet<ObjectProperty>(); String query = "SELECT ?p WHERE {" + - "{<%p> <%ax> ?p.} UNION {?p <%ax> <%p>}}".replace("%p", property.getName()).replace("%ax", OWL.inverseOf.getURI()); + "{<%p> <%ax> ?p.} UNION {?p <%ax> <%p>}}".replace("%p", property.getName()).replace("%ax", OWL.inverseOf.getURI());System.out.println(query); ResultSet rs = executeSelectQuery(query); QuerySolution qs; while(rs.hasNext()){ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |