Permalink
Browse files

added xquery connector code, still need to debug two queries

  • Loading branch information...
1 parent ebe9775 commit 10a68e7fab6f203efe39ada5b5b8eec9079af3d9 @lidingpku lidingpku committed Apr 4, 2011
View
@@ -37,5 +37,6 @@
<classpathentry kind="lib" path="lib/pellet2.2.2/xsdlib/xsdlib.jar"/>
<classpathentry kind="lib" path="lib/pellet2.2.2/jena/arq-2.8.4.jar"/>
<classpathentry kind="lib" path="lib/sw4j-0.6.jar"/>
+ <classpathentry kind="lib" path="lib/xcc.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
@@ -0,0 +1,12 @@
+#Fri Apr 01 00:41:58 EDT 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
View
Binary file not shown.
@@ -1,6 +1,8 @@
package com.marklogic.sparql2xquery.example;
import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
@@ -12,6 +14,8 @@
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.marklogic.sparql2xquery.translator.S2XTranslator;
+import com.marklogic.xcc.exceptions.RequestException;
+import com.marklogic.xcc.exceptions.XccConfigException;
import sw4j.rdf.load.RDFSYNTAX;
import sw4j.rdf.pellet.ToolPellet;
@@ -46,7 +50,7 @@ public void run(){
executeSparqlQuery();
//execute translated Xquery against triple store and display results
- //TODO
+ executeXquery();
printDebubInfo();
}
@@ -66,9 +70,13 @@ public void printDebubInfo(){
//ToolJena.printModel(m);
if (null!=m_map_query_sparql_result){
- String szResults = m_map_query_sparql_result.get(szQuery).toString();
-
- System.out.println(String.format("[sparql results: %d]", countResults(szResults)));
+ String szResults_sparql = m_map_query_sparql_result.get(szQuery).toString();
+ System.out.println(String.format("[sparql results: %d]", countResults(szResults_sparql)));
+
+ String szResults_xquery = this.m_map_query_xquery_result.get(szQuery);
+ if (null!=szResults_xquery){
+ System.out.println(String.format("[xquery results: %d]", countResults(szResults_xquery)));
+ }
}
System.out.println();
@@ -184,6 +192,9 @@ private String getDataUrl(String szFileName){
String [] querynames = new String[]{
"bakesale-query-31.sparql",
"bakesale-query-32.sparql",
+ "bakesale-query-33.sparql",
+ "bakesale-query-34.sparql",
+ "bakesale-query-36.sparql",
};
for (String szQueryName: querynames){
@@ -250,6 +261,43 @@ private String getDataUrl(String szFileName){
return m_map_query_sparql_result;
}
+ Map<String,String> m_map_query_xquery_result = null;
+ private Map<String,String> executeXquery(){
+ if (null==m_map_query_xquery){
+ return null;
+ }
+
+ if (null==m_map_query_xquery_result){
+ m_map_query_xquery_result = new TreeMap<String,String>();
+
+ for (String szQueryName: m_map_query_xquery.keySet()){
+ String szQueryXquery = m_map_query_xquery.get(szQueryName);
+
+ String connectionUri = "xcc://admin:admin@localhost:8006/bakesale-full";
+
+ ToolMarkLogicQueryRunner cq;
+ try {
+ cq = new ToolMarkLogicQueryRunner(new URI(connectionUri));
+ String results = cq.executeToSingleString(szQueryXquery,"\n");
+
+ m_map_query_xquery_result.put(szQueryName,results);
+
+ } catch (XccConfigException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (URISyntaxException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (RequestException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ return m_map_query_xquery_result;
+ }
+
private int countResults(String results){
Pattern pattern = Pattern.compile("<result>");
Matcher matcher = pattern.matcher(results);
@@ -0,0 +1,28 @@
+package com.marklogic.sparql2xquery.example;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import com.marklogic.xcc.exceptions.RequestException;
+import com.marklogic.xcc.exceptions.XccConfigException;
+
+public class Test {
+ public static void main(String [] args){
+ String connectionUri = "xcc://admin:admin@localhost:8005/bakesale";
+ ToolMarkLogicQueryRunner cq;
+ try {
+ cq = new ToolMarkLogicQueryRunner(new URI(connectionUri));
+ String ret = cq.executeToSingleString("count(/t)","\n");
+ System.out.println(ret);
+ } catch (XccConfigException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (URISyntaxException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (RequestException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+}
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2003-2011 MarkLogic Corporation. All rights reserved.
+ */
+package com.marklogic.sparql2xquery.example;
+
+import java.net.URI;
+
+import com.marklogic.xcc.AdhocQuery;
+import com.marklogic.xcc.ContentSource;
+import com.marklogic.xcc.ContentSourceFactory;
+import com.marklogic.xcc.Request;
+import com.marklogic.xcc.RequestOptions;
+import com.marklogic.xcc.ResultItem;
+import com.marklogic.xcc.ResultSequence;
+import com.marklogic.xcc.Session;
+import com.marklogic.xcc.exceptions.RequestException;
+import com.marklogic.xcc.exceptions.XccConfigException;
+
+/**
+ * <p>
+ * This is a very simple class that will submit an XQuery string to the server and return the
+ * result.
+ * </p>
+ * <p>
+ * Click here for the <a href="doc-files/SimpleQueryRunner.java.txt"> source code for this class</a>
+ * </p>
+ * <p>
+ * The main() method looks for two command-line args, a URL for the server (
+ * {@link ContentSourceFactory#newContentSource(java.net.URI)}) and a filename to read a query from.
+ * It loads the file, submits its contents to the server for evaluation and then prints the result
+ * sequence to stdout, one item per line.
+ * </p>
+ * <p>
+ * The class has methods the could be used to set request options and to obtain the results as an
+ * array of Strings or as a real {@link ResultSequence}.
+ * </p>
+ * <p>
+ * If you want to set external variables for a request, you can call {@link #getRequest()} to obtain
+ * a reference to the internal {@link Request} object and set the variable values on it before
+ * invoking {@link #execute(String)}.
+ * </p>
+ */
+public class ToolMarkLogicQueryRunner {
+ private final Session session;
+ private final AdhocQuery request;
+ private RequestOptions options;
+
+ /**
+ * Construct an instance that will submit query requests to the server represented by the given
+ * URI. Note that the URI will not be validated at this time.
+ *
+ * @param serverUri
+ * A URI that specifies a server per (
+ * {@link ContentSourceFactory#newContentSource(java.net.URI)}).
+ * @throws XccConfigException
+ * If the URI is not a valid XCC server URL.
+ */
+ public ToolMarkLogicQueryRunner(URI serverUri) throws XccConfigException {
+ ContentSource cs = ContentSourceFactory.newContentSource(serverUri);
+
+ session = cs.newSession();
+ request = session.newAdhocQuery(null);
+ }
+
+ /**
+ * Submit the given query string and return a {@link ResultSequence} object.
+ *
+ * @param query
+ * XQuery code as a String, to be evaluated by the server.
+ * @return An instance {@link ResultSequence}, possibly with size zero.
+ * @throws RequestException
+ * If an unrecoverable error occurs when submitting or evaluating the request.
+ */
+ public ResultSequence execute(String query) throws RequestException {
+ request.setQuery(query);
+ request.setOptions(options);
+
+ return session.submitRequest(request);
+ }
+
+ /**
+ * Submit the given query string and return an array of Strings, possibly of length zero, which
+ * contains the String value of each {@link ResultItem} (see
+ * {@link com.marklogic.xcc.ResultItem#asString()})
+ *
+ * @param query
+ * XQuery code as a String, to be evaluated by the server.
+ * @return An array of Strings, one per item in the {@link ResultSequence}.
+ * @throws RequestException
+ * If an unrecoverable error occurs when submitting or evaluating the request.
+ */
+ public String[] executeToStringArray(String query) throws RequestException {
+ ResultSequence rs = execute(query);
+
+ return rs.asStrings();
+ }
+
+ /**
+ * Submit the given query string and return a single String which is the concatenation of all
+ * the {@link ResultItem}s, separated by the given separator string.
+ *
+ * @param query
+ * XQuery code as a String, to be evaluated by the server.
+ * @param separator
+ * A String value which will be inserted in the final string between each item of the
+ * sequence. A value of null is equivalent to the empty string.
+ * @return A String consisting of the {@link com.marklogic.xcc.ResultItem#asString()} value of
+ * each item with the separator string inserted between each instance.
+ * @throws RequestException
+ * If an unrecoverable error occurs when submitting or evaluating the request.
+ */
+ public String executeToSingleString(String query, String separator) throws RequestException {
+ ResultSequence rs = execute(query);
+ String str = rs.asString(separator);
+
+ rs.close();
+
+ return str;
+ }
+
+ /**
+ * Returns the {@link Request} object used internally to submit requests. This object can be
+ * used to set external variables that will be bound to the query when submitted. You should not
+ * set your own {@link RequestOptions} object, use
+ * {@link #setRequestOptions(com.marklogic.xcc.RequestOptions)} instead.
+ *
+ * @return An instance of {@link Request}.
+ */
+ public Request getRequest() {
+ return request;
+ }
+
+ /**
+ * Set (or clear) the {@link RequestOptions} instance to associate with submitted queries.
+ *
+ * @param options
+ * An instance of {@link RequestOptions} or null.
+ */
+ public void setRequestOptions(RequestOptions options) {
+ this.options = options;
+ }
+
+}

0 comments on commit 10a68e7

Please sign in to comment.