From: <lor...@us...> - 2012-05-03 21:20:30
|
Revision: 3689 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3689&view=rev Author: lorenz_b Date: 2012-05-03 21:20:22 +0000 (Thu, 03 May 2012) Log Message: ----------- Fallback to SPARQL 1.0 after 2 attempts. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.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/reasoning/SPARQLReasoner.java 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 2012-05-03 20:59:33 UTC (rev 3688) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/AsymmetricObjectPropertyAxiomLearner.java 2012-05-03 21:20:22 UTC (rev 3689) @@ -99,7 +99,7 @@ // get number of instances of s with <s p o> query = "SELECT (COUNT(*) AS ?total) WHERE {?s <%s> ?o.}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - ResultSet rs = executeSelectQuery(query); + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; int total = 0; while(rs.hasNext()){ @@ -108,7 +108,7 @@ } query = "SELECT (COUNT(*) AS ?symmetric) WHERE {?s <%s> ?o. ?o <%s> ?s.}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeSelectQuery(query); + rs = executeSelectQuery(query, model); int symmetric = 0; while(rs.hasNext()){ qs = rs.next(); 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 2012-05-03 20:59:33 UTC (rev 3688) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalDataPropertyAxiomLearner.java 2012-05-03 21:20:22 UTC (rev 3689) @@ -97,7 +97,7 @@ query = String.format( "SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", propertyToDescribe.getName()); - ResultSet rs = executeSelectQuery(query); + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; int all = 1; while (rs.hasNext()) { @@ -107,7 +107,7 @@ // get number of instances of s with <s p o> <s p o1> where o != o1 query = "SELECT (COUNT(DISTINCT ?s) AS ?functional) WHERE {?s <%s> ?o1. FILTER NOT EXISTS {?s <%s> ?o2. FILTER(?o1 != ?o1)} }"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeSelectQuery(query); + rs = executeSelectQuery(query, model); int functional = 1; while (rs.hasNext()) { qs = rs.next(); @@ -129,29 +129,25 @@ 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()) { - qs = rs.next(); - all = qs.getLiteral("all").getInt(); + int numberOfSubjects = reasoner.getSubjectCountForProperty(propertyToDescribe, getRemainingRuntimeInMilliSeconds()); + if(numberOfSubjects == -1){ + logger.warn("Early termination: Got timeout while counting number of distinct subjects for given property."); + return; } // get number of instances of s with <s p o> <s p o1> where o != o1 - query = "SELECT (COUNT(DISTINCT ?s) AS ?functional) WHERE {?s <%s> ?o1. FILTER NOT EXISTS {?s <%s> ?o2. FILTER(?o1 != ?o1)} }"; + String query = "SELECT (COUNT(DISTINCT ?s) AS ?functional) WHERE {?s <%s> ?o1. FILTER NOT EXISTS {?s <%s> ?o2. FILTER(?o1 != ?o1)} }"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeSelectQuery(query); + ResultSet rs = executeSelectQuery(query); int functional = 1; + QuerySolution qs; while (rs.hasNext()) { qs = rs.next(); functional = qs.getLiteral("functional").getInt(); } - if (all > 0) { + if (numberOfSubjects > 0) { currentlyBestAxioms.add(new EvaluatedAxiom( new FunctionalDatatypePropertyAxiom(propertyToDescribe), - computeScore(all, functional), + computeScore(numberOfSubjects, functional), declaredAsFunctional)); } } 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 2012-05-03 20:59:33 UTC (rev 3688) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/InverseFunctionalObjectPropertyAxiomLearner.java 2012-05-03 21:20:22 UTC (rev 3689) @@ -102,7 +102,7 @@ query = String.format( "SELECT (COUNT(DISTINCT ?o) AS ?all) WHERE {?s <%s> ?o.}", propertyToDescribe.getName()); - ResultSet rs = executeSelectQuery(query); + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; int all = 1; while (rs.hasNext()) { @@ -110,13 +110,13 @@ 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 ?o) AS ?noninversefunctional) WHERE {?s1 <%s> ?o. ?s2 <%s> ?o. FILTER(?s1 != ?s2) }"; + query = "SELECT (COUNT(DISTINCT ?o) AS ?inversefunctional) WHERE {?s1 <%s> ?o. FILTER NOT EXISTS {?s2 <%s> ?o. FILTER(?s1 != ?s2)}}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeSelectQuery(query); - int notInverseFunctional = 1; + rs = executeSelectQuery(query, model); + int inverseFunctional = 1; while (rs.hasNext()) { qs = rs.next(); - notInverseFunctional = qs.getLiteral("noninversefunctional") + inverseFunctional = qs.getLiteral("inversefunctional") .getInt(); } if (all > 0) { @@ -124,8 +124,7 @@ currentlyBestAxioms .add(new EvaluatedAxiom( new InverseFunctionalObjectPropertyAxiom( - propertyToDescribe), computeScore(all, all - - notInverseFunctional), + propertyToDescribe), computeScore(all, inverseFunctional), declaredAsInverseFunctional)); } @@ -137,32 +136,27 @@ 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()) { - qs = rs.next(); - all = qs.getLiteral("all").getInt(); + int numberOfObjects = reasoner.getObjectCountForProperty(propertyToDescribe, getRemainingRuntimeInMilliSeconds()); + if(numberOfObjects == -1){ + logger.warn("Early termination: Got timeout while counting number of distinct objects for given property."); + return; } // get number of instances of s with <s p o> <s p o1> where o != o1 - query = "SELECT (COUNT(DISTINCT ?o) AS ?noninversefunctional) WHERE {?s1 <%s> ?o. ?s2 <%s> ?o. FILTER(?s1 != ?s2) }"; + String query = "SELECT (COUNT(DISTINCT ?o) AS ?inversefunctional) WHERE {?s1 <%s> ?o. FILTER NOT EXISTS {?s2 <%s> ?o. FILTER(?s1 != ?s2)}}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeSelectQuery(query); - int notInverseFunctional = 1; + ResultSet rs = executeSelectQuery(query); + int inverseFunctional = 1; + QuerySolution qs; while (rs.hasNext()) { qs = rs.next(); - notInverseFunctional = qs.getLiteral("noninversefunctional") + inverseFunctional = qs.getLiteral("inversefunctional") .getInt(); } - if (all > 0) { + if (numberOfObjects > 0) { currentlyBestAxioms .add(new EvaluatedAxiom( new InverseFunctionalObjectPropertyAxiom( - propertyToDescribe), computeScore(all, all - - notInverseFunctional), + propertyToDescribe), computeScore(numberOfObjects, inverseFunctional), declaredAsInverseFunctional)); } } @@ -173,6 +167,7 @@ l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/profession")); l.setMaxExecutionTimeInSeconds(10); l.init(); + l.setForceSPARQL_1_0_Mode(true); l.start(); System.out.println(l.getCurrentlyBestEvaluatedAxioms(1)); } 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 2012-05-03 20:59:33 UTC (rev 3688) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/IrreflexiveObjectPropertyAxiomLearner.java 2012-05-03 21:20:22 UTC (rev 3689) @@ -101,7 +101,7 @@ query = String.format( "SELECT (COUNT(DISTINCT ?s) AS ?all) WHERE {?s <%s> ?o.}", propertyToDescribe); - ResultSet rs = executeSelectQuery(query); + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; int all = 0; while (rs.hasNext()) { @@ -113,7 +113,7 @@ // 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); + rs = executeSelectQuery(query, model); int irreflexive = 0; while (rs.hasNext()) { qs = rs.next(); 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 2012-05-03 20:59:33 UTC (rev 3688) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexiveObjectPropertyAxiomLearner.java 2012-05-03 21:20:22 UTC (rev 3689) @@ -99,7 +99,7 @@ // get fraction of instances s with <s p s> query = "SELECT (COUNT(DISTINCT ?s) AS ?total) WHERE {?s <%s> ?o.}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - ResultSet rs = executeSelectQuery(query); + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; int total = 0; while (rs.hasNext()) { @@ -108,7 +108,7 @@ } query = "SELECT (COUNT(DISTINCT ?s) AS ?reflexive) WHERE {?s <%s> ?s.}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeSelectQuery(query); + rs = executeSelectQuery(query, model); int reflexive = 0; while (rs.hasNext()) { qs = rs.next(); 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 2012-05-03 20:59:33 UTC (rev 3688) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricObjectPropertyAxiomLearner.java 2012-05-03 21:20:22 UTC (rev 3689) @@ -98,7 +98,7 @@ // get number of instances of s with <s p o> query = "SELECT (COUNT(*) AS ?total) WHERE {?s <%s> ?o.}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - ResultSet rs = executeSelectQuery(query); + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; int total = 0; while(rs.hasNext()){ @@ -107,7 +107,7 @@ } query = "SELECT (COUNT(*) AS ?symmetric) WHERE {?s <%s> ?o. ?o <%s> ?s}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeSelectQuery(query); + rs = executeSelectQuery(query, model); int symmetric = 0; while(rs.hasNext()){ qs = rs.next(); 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 2012-05-03 20:59:33 UTC (rev 3688) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitiveObjectPropertyAxiomLearner.java 2012-05-03 21:20:22 UTC (rev 3689) @@ -100,7 +100,7 @@ // get number of instances of s with <s p o> query = "SELECT (COUNT(*) AS ?total) WHERE {?s <%s> ?o. ?o <%s> ?o1.}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - ResultSet rs = executeSelectQuery(query); + ResultSet rs = executeSelectQuery(query, model); QuerySolution qs; int total = 0; while(rs.hasNext()){ @@ -109,7 +109,7 @@ } query = "SELECT (COUNT(*) AS ?transitive) WHERE {?s <%s> ?o. ?o <%s> ?o1. ?s <%s> ?o1.}"; query = query.replace("%s", propertyToDescribe.getURI().toString()); - rs = executeSelectQuery(query); + rs = executeSelectQuery(query, model); int transitive = 0; while(rs.hasNext()){ qs = rs.next(); Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2012-05-03 20:59:33 UTC (rev 3688) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2012-05-03 21:20:22 UTC (rev 3689) @@ -53,6 +53,7 @@ import org.dllearner.core.owl.Nothing; import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.ObjectPropertyHierarchy; +import org.dllearner.core.owl.Property; import org.dllearner.core.owl.Thing; import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; import org.dllearner.kb.SparqlEndpointKS; @@ -169,7 +170,7 @@ } } - public int getSubjectCountForProperty(ObjectProperty p, long timeout){ + public int getSubjectCountForProperty(Property p, long timeout){ int cnt = -1; String query = String.format( "SELECT (COUNT(DISTINCT ?s) AS ?cnt) WHERE {?s <%s> ?o.}", @@ -182,6 +183,19 @@ return cnt; } + public int getObjectCountForProperty(ObjectProperty p, long timeout){ + int cnt = -1; + String query = String.format( + "SELECT (COUNT(DISTINCT ?o) AS ?cnt) WHERE {?s <%s> ?o.}", + p.getName()); + ResultSet rs = executeSelectQuery(query, timeout); + if(rs.hasNext()){ + cnt = rs.next().getLiteral("cnt").getInt(); + } + + return cnt; + } + public int getPopularity(NamedClass nc){ if(classPopularityMap.containsKey(nc)){ return classPopularityMap.get(nc); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |