From: <lor...@us...> - 2011-08-08 06:00:23
|
Revision: 3010 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3010&view=rev Author: lorenz_b Date: 2011-08-08 06:00:16 +0000 (Mon, 08 Aug 2011) Log Message: ----------- Continued algorithms. Implemented missing toString() methods in some axioms. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexivePropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitivePropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/owl/EquivalentObjectPropertiesAxiom.java trunk/components-core/src/main/java/org/dllearner/core/owl/FunctionalObjectPropertyAxiom.java trunk/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyDomainAxiom.java trunk/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyRangeAxiom.java trunk/components-core/src/main/java/org/dllearner/core/owl/ReflexiveObjectPropertyAxiom.java trunk/components-core/src/main/java/org/dllearner/core/owl/SubObjectPropertyAxiom.java trunk/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQuery.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/kb/sparql/ExtendedQueryEngineHTTP.java trunk/components-core/src/main/java/org/dllearner/kb/sparql/HttpQuery.java Property Changed: ---------------- trunk/components-core/src/main/java/org/dllearner/kb/sparql/ Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentPropertyAxiomLearner.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -23,6 +23,7 @@ import org.dllearner.core.owl.EquivalentObjectPropertiesAxiom; import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.kb.sparql.SparqlQuery; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; @@ -211,7 +212,8 @@ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); - QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { queryExecution.addDefaultGraph(dgu); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/FunctionalPropertyAxiomLearner.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -17,6 +17,7 @@ import org.dllearner.core.owl.FunctionalObjectPropertyAxiom; import org.dllearner.core.owl.ObjectProperty; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; @@ -150,10 +151,14 @@ return result; } + /* + * Executes a SELECT query and returns the result. + */ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); - QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { queryExecution.addDefaultGraph(dgu); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyDomainAxiomLearner.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -30,6 +30,7 @@ import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.ObjectPropertyDomainAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; @@ -221,7 +222,8 @@ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); - QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { queryExecution.addDefaultGraph(dgu); } @@ -232,28 +234,5 @@ return resultSet; } - public static void main(String[] args) throws Exception{ - Map<String, String> propertiesMap = new HashMap<String, String>(); - propertiesMap.put("propertyToDescribe", "http://dbpedia.org/ontology/writer"); - propertiesMap.put("maxExecutionTimeInSeconds", "10"); - propertiesMap.put("maxFetchedRows", "15000"); - - PropertyDomainAxiomLearner l = new PropertyDomainAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia())); - - - Field[] fields = l.getClass().getDeclaredFields(); - for(Field f : fields){ - ConfigOption option = f.getAnnotation(ConfigOption.class); - if(option != null){ - String configValue = propertiesMap.get(option.name()); - PropertyEditor editor = (PropertyEditor) option.propertyEditorClass().newInstance(); - editor.setAsText(configValue); - f.set(l, editor.getValue()); - } - } - l.init(); - l.start(); - System.out.println(l.getCurrentlyBestEvaluatedAxioms(3)); - } } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/PropertyRangeAxiomLearner.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -28,6 +28,7 @@ import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.ObjectPropertyRangeAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; @@ -218,7 +219,8 @@ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); - QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { queryExecution.addDefaultGraph(dgu); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexivePropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexivePropertyAxiomLearner.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ReflexivePropertyAxiomLearner.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -17,6 +17,7 @@ import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.ReflexiveObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; @@ -143,10 +144,14 @@ return result; } + /* + * Executes a SELECT query and returns the result. + */ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); - QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { queryExecution.addDefaultGraph(dgu); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubPropertyOfAxiomLearner.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -10,9 +10,6 @@ import java.util.SortedSet; import java.util.TreeSet; -import org.aksw.commons.sparql.api.core.QueryExecutionFactory; -import org.aksw.commons.sparql.api.http.QueryExecutionFactoryHttp; -import org.aksw.commons.sparql.api.pagination.core.QueryExecutionFactoryPaginated; import org.dllearner.core.AbstractComponent; import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; @@ -26,6 +23,7 @@ import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.SubObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; @@ -215,7 +213,8 @@ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); - QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { queryExecution.addDefaultGraph(dgu); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricPropertyAxiomLearner.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SymmetricPropertyAxiomLearner.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -17,6 +17,7 @@ import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.SymmetricObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; import org.slf4j.Logger; @@ -143,10 +144,14 @@ return result; } + /* + * Executes a SELECT query and returns the result. + */ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); - QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { queryExecution.addDefaultGraph(dgu); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitivePropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitivePropertyAxiomLearner.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/TransitivePropertyAxiomLearner.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -17,6 +17,7 @@ import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.TransitiveObjectPropertyAxiom; import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.ExtendedQueryEngineHTTP; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.learningproblems.AxiomScore; import org.dllearner.reasoning.SPARQLReasoner; @@ -144,10 +145,14 @@ return result; } + /* + * Executes a SELECT query and returns the result. + */ private ResultSet executeQuery(String query){ logger.info("Sending query \n {}", query); - QueryEngineHTTP queryExecution = new QueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + ExtendedQueryEngineHTTP queryExecution = new ExtendedQueryEngineHTTP(ks.getEndpoint().getURL().toString(), query); + queryExecution.setTimeout(maxExecutionTimeInSeconds * 1000); for (String dgu : ks.getEndpoint().getDefaultGraphURIs()) { queryExecution.addDefaultGraph(dgu); } Modified: trunk/components-core/src/main/java/org/dllearner/core/owl/EquivalentObjectPropertiesAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/EquivalentObjectPropertiesAxiom.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/EquivalentObjectPropertiesAxiom.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -50,6 +50,6 @@ */ @Override public String toManchesterSyntaxString(String baseURI, Map<String, String> prefixes) { - return "EquivalentObjectProperties(" + equivRole.toString(baseURI, prefixes) + "," + role.toString(baseURI, prefixes) + ")"; + return equivRole.toString(baseURI, prefixes) + " EquivalentTo: " + role.toString(baseURI, prefixes); } } Modified: trunk/components-core/src/main/java/org/dllearner/core/owl/FunctionalObjectPropertyAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/FunctionalObjectPropertyAxiom.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/FunctionalObjectPropertyAxiom.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -50,6 +50,6 @@ */ @Override public String toManchesterSyntaxString(String baseURI, Map<String, String> prefixes) { - return "FUNCTIONALOBJECTPROPERTYAXIOM NOT IMPLEMENTED"; + return "Functional(" + role.toManchesterSyntaxString(baseURI, prefixes) + ")"; } } Modified: trunk/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyDomainAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyDomainAxiom.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyDomainAxiom.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -76,7 +76,7 @@ */ @Override public String toManchesterSyntaxString(String baseURI, Map<String, String> prefixes) { - return "OBJECTPROPERTYDOMAIN NOT IMPLEMENTED"; + return "Domain(" + getProperty().toManchesterSyntaxString(baseURI, prefixes) + ", " + getDomain().toManchesterSyntaxString(baseURI, prefixes) + ")"; } } Modified: trunk/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyRangeAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyRangeAxiom.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/ObjectPropertyRangeAxiom.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -48,7 +48,7 @@ * @see org.dllearner.core.owl.KBElement#toString(java.lang.String, java.util.Map) */ public String toString(String baseURI, Map<String, String> prefixes) { - return "Domain(" + getProperty() + ", " + getRange() + ")"; + return "Range(" + getProperty() + ", " + getRange() + ")"; } public String toKBSyntaxString(String baseURI, Map<String, String> prefixes) { @@ -80,7 +80,7 @@ */ @Override public String toManchesterSyntaxString(String baseURI, Map<String, String> prefixes) { - return "OBJECTPROPERTYRANGE NOT IMPLEMENTED"; + return "Range(" + getProperty().toManchesterSyntaxString(baseURI, prefixes) + ", " + getRange().toManchesterSyntaxString(baseURI, prefixes) + ")"; } Modified: trunk/components-core/src/main/java/org/dllearner/core/owl/ReflexiveObjectPropertyAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/ReflexiveObjectPropertyAxiom.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/ReflexiveObjectPropertyAxiom.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -44,6 +44,6 @@ */ @Override public String toManchesterSyntaxString(String baseURI, Map<String, String> prefixes) { - return "Transitive(" + role.toString(baseURI, prefixes) + ")"; + return "Reflexive(" + role.toManchesterSyntaxString(baseURI, prefixes) + ")"; } } Modified: trunk/components-core/src/main/java/org/dllearner/core/owl/SubObjectPropertyAxiom.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/owl/SubObjectPropertyAxiom.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/core/owl/SubObjectPropertyAxiom.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -50,6 +50,6 @@ */ @Override public String toManchesterSyntaxString(String baseURI, Map<String, String> prefixes) { - return "Subrole(" + subRole.toString(baseURI, prefixes) + "," + role.toString(baseURI, prefixes) + ")"; + return subRole.toString(baseURI, prefixes) + " SubPropertyOf: " + role.toString(baseURI, prefixes); } } Property changes on: trunk/components-core/src/main/java/org/dllearner/kb/sparql ___________________________________________________________________ Modified: svn:ignore - SparqlQueryDescriptionConvertRDFS.java + SparqlQueryDescriptionConvertRDFS.java Added: trunk/components-core/src/main/java/org/dllearner/kb/sparql/ExtendedQueryEngineHTTP.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/sparql/ExtendedQueryEngineHTTP.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/kb/sparql/ExtendedQueryEngineHTTP.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -0,0 +1,376 @@ +package org.dllearner.kb.sparql; + +import com.hp.hpl.jena.query.*; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.sparql.engine.http.HttpParams; +import com.hp.hpl.jena.sparql.engine.http.Params; +import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; +import com.hp.hpl.jena.sparql.resultset.XMLInput; +import com.hp.hpl.jena.sparql.util.Context; +import com.hp.hpl.jena.sparql.util.graph.GraphFactory; +import com.hp.hpl.jena.util.FileManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Created by Claus Stadler + * Date: Oct 25, 2010 + * Time: 10:15:31 PM + */ +class DisconnectorThread + extends Thread { + private static final Logger logger = LoggerFactory.getLogger(DisconnectorThread.class); + + private HttpQuery connection; + + private long timeOut; + + private boolean canceled = false; + + public DisconnectorThread(HttpQuery connection, long timeOut) { + this.connection = connection; + this.timeOut = timeOut; + } + + public void run() { + synchronized (this) { + + while(!canceled && connection.getConnection() == null) { + //logger.trace("Waiting for connection..."); + + try { + this.wait(500l); + } catch (InterruptedException e) { + } + } + + long startTime = System.currentTimeMillis(); + + long remaining; + while (!canceled && (remaining = (timeOut - (System.currentTimeMillis() - startTime))) > 0) { + logger.trace("Forced disconnect in " + remaining + "ms"); + try { + this.wait(remaining); + } catch (InterruptedException e) { + } + } + + if (!canceled && connection.getConnection() != null) { + logger.warn("Disconnecting Http connection since a sparql query is taking too long"); + connection.getConnection().disconnect(); + canceled = true; + } + } + } + + public void cancel() { + synchronized (this) { + if(!this.canceled) { + logger.trace("Disconnect cancelled"); + } + + this.canceled = true; + this.notify(); + } + } +} + +/** + * A QueryEngineHTTP that is capable of closing connections after a given timeout. + * + * Jena now provides one on its own + */ +public class ExtendedQueryEngineHTTP + implements QueryExecution { + private static Logger log = LoggerFactory.getLogger(QueryEngineHTTP.class); + + public static final String QUERY_MIME_TYPE = "application/sparql-query"; + String queryString; + String service; + Context context = null; + + + long timeOut = 0l; + + public void setTimeOut(long timeOut) { + this.timeOut = timeOut; + } + + public long getTimeOut() { + return timeOut; + } + + + //Params + Params params = null; + + // Protocol + List<String> defaultGraphURIs = new ArrayList<String>(); + List<String> namedGraphURIs = new ArrayList<String>(); + private String user = null; + private char[] password = null; + + // Releasing HTTP input streams is important. We remember this for SELECT, + // and will close when the engine is closed + private InputStream retainedConnection = null; + + public ExtendedQueryEngineHTTP(String serviceURI, Query query) { + this(serviceURI, query.toString()); + } + + public ExtendedQueryEngineHTTP(String serviceURI, String queryString) { + this.queryString = queryString; + service = serviceURI; + // Copy the global context to freeze it. + context = new Context(ARQ.getContext()); + } + +// public void setParams(Params params) +// { this.params = params ; } + + // Meaning-less + + public void setFileManager(FileManager fm) { + throw new UnsupportedOperationException("FileManagers do not apply to remote query execution"); + } + + public void setInitialBinding(QuerySolution binding) { + throw new UnsupportedOperationException("Initial bindings not supported for remote queries"); + } + + public void setInitialBindings(ResultSet table) { + throw new UnsupportedOperationException("Initial bindings not supported for remote queries"); + } + + /** + * @param defaultGraphURIs The defaultGraphURIs to set. + */ + public void setDefaultGraphURIs(List<String> defaultGraphURIs) { + this.defaultGraphURIs = defaultGraphURIs; + } + + /** + * @param namedGraphURIs The namedGraphURIs to set. + */ + public void setNamedGraphURIs(List<String> namedGraphURIs) { + this.namedGraphURIs = namedGraphURIs; + } + + public void addParam(String field, String value) { + if (params == null) + params = new Params(); + params.addParam(field, value); + } + + /** + * @param defaultGraph The defaultGraph to add. + */ + public void addDefaultGraph(String defaultGraph) { + if (defaultGraphURIs == null) + defaultGraphURIs = new ArrayList<String>(); + defaultGraphURIs.add(defaultGraph); + } + + /** + * @param name The URI to add. + */ + public void addNamedGraph(String name) { + if (namedGraphURIs == null) + namedGraphURIs = new ArrayList<String>(); + namedGraphURIs.add(name); + } + + /** + * Set user and password for basic authentication. + * After the request is made (one of the exec calls), the application + * can overwrite the password array to remove details of the secret. + * + * @param user + * @param password + */ + public void setBasicAuthentication(String user, char[] password) { + this.user = user; + this.password = password; + } + + private InputStream doTimedExec(HttpQuery httpQuery) { + DisconnectorThread stopTask = null; + if (timeOut > 0) { + stopTask = new DisconnectorThread(httpQuery, timeOut); +// stopTask.start(); + } + + InputStream in; + try { + in = httpQuery.exec(); + } + finally { + if (stopTask != null) { + stopTask.cancel(); + } + } + + return in; + } + + + public ResultSet execSelect() { + HttpQuery httpQuery = makeHttpQuery(); + // TODO Allow other content types. + httpQuery.setAccept(HttpParams.contentTypeResultsXML); + + InputStream in = doTimedExec(httpQuery); + + + ResultSet rs = ResultSetFactory.fromXML(in); + retainedConnection = in; // This will be closed on close() + return rs; + } + + public Model execConstruct() { + return execConstruct(GraphFactory.makeJenaDefaultModel()); + } + + public Model execConstruct(Model model) { + return execModel(model); + } + + public Model execDescribe() { + return execDescribe(GraphFactory.makeJenaDefaultModel()); + } + + public Model execDescribe(Model model) { + return execModel(model); + } + + private Model execModel(Model model) { + HttpQuery httpQuery = makeHttpQuery(); + httpQuery.setAccept(HttpParams.contentTypeRDFXML); + InputStream in = doTimedExec(httpQuery); + model.read(in, null); + return model; + } + + public boolean execAsk() { + HttpQuery httpQuery = makeHttpQuery(); + httpQuery.setAccept(HttpParams.contentTypeResultsXML); + InputStream in = doTimedExec(httpQuery); + boolean result = XMLInput.booleanFromXML(in); + // Ensure connection is released + try { + in.close(); + } + catch (java.io.IOException e) { + log.warn("Failed to close connection", e); + } + return result; + } + + public Context getContext() { + return context; + } + + private HttpQuery makeHttpQuery() { + HttpQuery httpQuery = new HttpQuery(service); + httpQuery.setTimeOut((int)timeOut); + httpQuery.addParam(HttpParams.pQuery, queryString); + + for (Iterator<String> iter = defaultGraphURIs.iterator(); iter.hasNext();) { + String dft = iter.next(); + httpQuery.addParam(HttpParams.pDefaultGraph, dft); + } + for (Iterator<String> iter = namedGraphURIs.iterator(); iter.hasNext();) { + String name = iter.next(); + httpQuery.addParam(HttpParams.pNamedGraph, name); + } + + if (params != null) + httpQuery.merge(params); + + httpQuery.setBasicAuthentication(user, password); + return httpQuery; + } + + public void abort() { + } + + public void close() { + if (retainedConnection != null) { + try { + retainedConnection.close(); + } + catch (java.io.IOException e) { + log.warn("Failed to close connection", e); + } + finally { + retainedConnection = null; + } + } + } + + @Override + public void setTimeout(long timeout, TimeUnit timeoutUnits) { + //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public void setTimeout(long timeout) { + this.timeOut = timeout; + } + + @Override + public void setTimeout(long timeout1, TimeUnit timeUnit1, long timeout2, TimeUnit timeUnit2) { + //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public void setTimeout(long timeout1, long timeout2) { + //To change body of implemented methods use File | Settings | File Templates. + } + +// public boolean isActive() { return false ; } + + @Override + public String toString() { + HttpQuery httpQuery = makeHttpQuery(); + return "GET " + httpQuery.toString(); + } + + public Dataset getDataset() { + return null; + } +} + +/* + * (c) Copyright 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + Property changes on: trunk/components-core/src/main/java/org/dllearner/kb/sparql/ExtendedQueryEngineHTTP.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/kb/sparql/HttpQuery.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/sparql/HttpQuery.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/kb/sparql/HttpQuery.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -0,0 +1,401 @@ +package org.dllearner.kb.sparql; + +/* + * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP + * [See end of file] + */ + + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.openjena.atlas.lib.Base64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.hp.hpl.jena.query.ARQ; +import com.hp.hpl.jena.sdb.util.Pair; +import com.hp.hpl.jena.shared.JenaException; +import com.hp.hpl.jena.sparql.ARQInternalErrorException; +import com.hp.hpl.jena.sparql.engine.http.HttpParams; +import com.hp.hpl.jena.sparql.engine.http.Params; +import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP; +import com.hp.hpl.jena.sparql.util.Convert; +import com.hp.hpl.jena.util.FileUtils; + +/** Create an execution object for performing a query on a model + * over HTTP. This is the main protocol engine for HTTP query. + * There are higher level classes for doing a query and presenting + * the results in an API fashion. + * + * If the query string is large, then HTTP POST is used. */ +public class HttpQuery extends Params +{ + static final Logger log = LoggerFactory.getLogger(HttpQuery.class.getName()) ; + + /** The definition of "large" queries */ + // Not final so that other code can change it. + static public /*final*/ int urlLimit = 2*1024 ; + + String serviceURL ; + + String contentTypeResult = HttpParams.contentTypeResultsXML ; + HttpURLConnection httpConnection = null ; + + // An object indicate no value associated with parameter name + final static Object noValue = new Object() ; + String user = null ; + char[] password = null ; + + int responseCode = 0; + String responseMessage = null ; + boolean forcePOST = false ; + String queryString = null ; + + private int timeOut = 0; + + public void setTimeOut(int timeOut){ + this.timeOut = timeOut; + } + + //static final String ENC_UTF8 = "UTF-8" ; + + /** Create a execution object for a whole model GET + * @param serviceURL The model + */ + + public HttpQuery(String serviceURL) + { + init(serviceURL) ; + } + + + /** Create a execution object for a whole model GET + * @param url The model + */ + + public HttpQuery(URL url) + { + init(url.toString()) ; + } + + + private void init(String serviceURL) + { + if ( log.isTraceEnabled()) + log.trace("URL: "+serviceURL) ; + + if ( serviceURL.indexOf('?') >= 0 ) + throw new QueryExceptionHTTP(-1, "URL already has a query string ("+serviceURL+")") ; + + this.serviceURL = serviceURL ; + } + + private String getQueryString() + { + if ( queryString == null ) + queryString = super.httpString() ; + return queryString ; + } + + public HttpURLConnection getConnection() { return httpConnection ; } + + /** Set the content type (Accept header) for the results + */ + + public void setAccept(String contentType) + { + contentTypeResult = contentType ; + } + + public void setBasicAuthentication(String user, char[] password) + { + this.user = user ; + this.password = password ; + } + + /** Return whether this request will go by GET or POST + * @return boolean + */ + public boolean usesPOST() + { + if ( forcePOST ) + return true ; + String s = getQueryString() ; + + return serviceURL.length()+s.length() >= urlLimit ; + } + + /** Force the use of HTTP POST for the query operation + */ + + public void setForcePOST() + { + forcePOST = true ; + } + + /** Execute the operation + * @return Model The resulting model + * @throws QueryExceptionHTTP + */ + public InputStream exec() throws QueryExceptionHTTP + { + try { + if (usesPOST()) + return execPost(); + return execGet(); + } catch (QueryExceptionHTTP httpEx) + { + log.trace("Exception in exec", httpEx); + throw httpEx; + } + catch (JenaException jEx) + { + log.trace("JenaException in exec", jEx); + throw jEx ; + } + } + + private InputStream execGet() throws QueryExceptionHTTP + { + URL target = null ; + String qs = getQueryString() ; + + ARQ.getHttpRequestLogger().trace(qs) ; + + try { + if ( count() == 0 ) + target = new URL(serviceURL) ; + else + target = new URL(serviceURL+"?"+qs) ; + } + catch (MalformedURLException malEx) + { throw new QueryExceptionHTTP(0, "Malformed URL: "+malEx) ; } + log.trace("GET "+target.toExternalForm()) ; + + try + { + httpConnection = (HttpURLConnection) target.openConnection(); + httpConnection.setRequestProperty("Accept", contentTypeResult) ; + httpConnection.setReadTimeout(timeOut); + int x = httpConnection.getReadTimeout() ; + + // By default, following 3xx redirects is true + //conn.setFollowRedirects(true) ; + basicAuthentication(httpConnection) ; + + httpConnection.setDoInput(true); + httpConnection.connect(); + try + { + return execCommon(); + } + catch (QueryExceptionHTTP qEx) + { + // Back-off and try POST if something complain about long URIs + // Broken + if (qEx.getResponseCode() == 414 /*HttpServletResponse.SC_REQUEST_URI_TOO_LONG*/ ) + return execPost(); + throw qEx; + } + } + catch (java.net.ConnectException connEx) + { throw new QueryExceptionHTTP(QueryExceptionHTTP.NoServer, "Failed to connect to remote server"); } + catch (IOException ioEx) + { throw new QueryExceptionHTTP(ioEx); } + } + + private InputStream execPost() throws QueryExceptionHTTP + { + URL target = null; + try { target = new URL(serviceURL); } + catch (MalformedURLException malEx) + { throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx); } + log.trace("POST "+target.toExternalForm()) ; + + ARQ.getHttpRequestLogger().trace(target.toExternalForm()) ; + + try + { + httpConnection = (HttpURLConnection) target.openConnection(); + httpConnection.setRequestMethod("POST") ; + httpConnection.setRequestProperty("Accept", contentTypeResult) ; + httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") ; + basicAuthentication(httpConnection) ; + httpConnection.setDoOutput(true) ; + + boolean first = true ; + OutputStream out = httpConnection.getOutputStream() ; + for ( Iterator<Pair> iter = pairs().listIterator() ; iter.hasNext() ; ) + { + if ( ! first ) + out.write('&') ; + first = false ; + Pair p = iter.next() ; + out.write(p.getName().getBytes()) ; + out.write('=') ; + String x = p.getValue() ; + x = Convert.encWWWForm(x) ; + out.write(x.getBytes()) ; + ARQ.getHttpRequestLogger().trace("Param: "+x) ; + } + out.flush() ; + httpConnection.connect() ; + return execCommon() ; + } + catch (java.net.ConnectException connEx) + { throw new QueryExceptionHTTP(-1, "Failed to connect to remote server"); } + catch (IOException ioEx) + { throw new QueryExceptionHTTP(ioEx); } + } + + private void basicAuthentication(HttpURLConnection httpConnection2) + { + // Do basic authentication : do directly, not via an Authenticator, because it + // avoids an extra round trip (Java normally does the request without authetication, + // then reties with) + + if ( user != null || password != null) + { + try + { + if ( user == null || password == null ) + log.warn("Only one of user/password is set") ; + // We want: "Basic user:password" except user:password is base 64 encoded. + // Build string, get as UTF-8, bytes, translate to base 64. + StringBuffer x = new StringBuffer() ; + byte b[] = x.append(user).append(":").append(password).toString().getBytes("UTF-8") ; + String y = Base64.encodeBytes(b) ; + httpConnection.setRequestProperty("Authorization", "Basic "+y) ; + // Overwrite any password details we copied. + // Still leaves the copy in the HTTP connection. But this only basic auth. + for ( int i = 0 ; i < x.length() ; i++ ) x.setCharAt(i, '*') ; + for ( int i = 0 ; i < b.length ; i++ ) b[i] = (byte)0 ; + } catch (UnsupportedEncodingException ex) + { + // Can't happen - UTF-8 is required of all Java platforms. + throw new ARQInternalErrorException("UTF-8 is broken on this platform", ex) ; + } + } + } + + + private InputStream execCommon() throws QueryExceptionHTTP + { + try { + responseCode = httpConnection.getResponseCode() ; + responseMessage = Convert.decWWWForm(httpConnection.getResponseMessage()) ; + + // 1xx: Informational + // 2xx: Success + // 3xx: Redirection + // 4xx: Client Error + // 5xx: Server Error + + if ( 300 <= responseCode && responseCode < 400 ) + throw new QueryExceptionHTTP(responseCode, responseMessage) ; + + // Other 400 and 500 - errors + + if ( responseCode >= 400 ) + throw new QueryExceptionHTTP(responseCode, responseMessage) ; + + // Request suceeded + //httpConnection.setReadTimeout(10) ; + InputStream in = httpConnection.getInputStream() ; + + if ( false ) + { + // Dump the header + Map<String,List<String>> map = httpConnection.getHeaderFields() ; + for ( Iterator<String> iter = map.keySet().iterator() ; iter.hasNext() ; ) + { + String k = iter.next(); + List<String> v = map.get(k) ; + System.out.println(k+" = "+v) ; + } + } + + // Dump response body + if ( false ) + { + StringBuffer b = new StringBuffer(1000) ; + byte[] chars = new byte[1000] ; + while(true) + { + int x = in.read(chars) ; + if ( x < 0 ) break ; + b.append(new String(chars, 0, x, FileUtils.encodingUTF8)) ; + } + System.out.println(b.toString()) ; + System.out.flush() ; + // Reset + in = new ByteArrayInputStream(b.toString().getBytes(FileUtils.encodingUTF8)) ; + } + + + // +++ WORKAROUND for badly behaved apps. + // Apps sometimes call QueryExecution.close straight after .execSelect. + // that results in some resuls being seen, not all of them => XMl parse errors. +// byte[] bytes = IO.readWholeFile(in) ; +// in = new ByteArrayInputStream(bytes) ; + // +++ + + return in ; + } + catch (IOException ioEx) + { + throw new QueryExceptionHTTP(ioEx) ; + } + catch (JenaException rdfEx) + { + throw new QueryExceptionHTTP(rdfEx) ; + } + } + + @Override + public String toString() + { + String s = httpString() ; + if ( s != null && s.length() > 0 ) + return serviceURL+"?"+s ; + return serviceURL ; + } +} + +/* + * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + Property changes on: trunk/components-core/src/main/java/org/dllearner/kb/sparql/HttpQuery.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: trunk/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQuery.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQuery.java 2011-08-05 13:47:08 UTC (rev 3009) +++ trunk/components-core/src/main/java/org/dllearner/kb/sparql/SparqlQuery.java 2011-08-08 06:00:16 UTC (rev 3010) @@ -219,6 +219,7 @@ */ private static void writeToSparqlLog(String s) { File f = new File(sparqlLog); + f.mkdirs(); if(!f.canWrite() ){ logger.info("could not write SPARQL log to : " + f.getAbsolutePath()); return ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |