Permalink
Browse files

rfe10988: java api for SPIN functions and magic properties

tests run:   prepush
tests added: spin tests copied from lisp
performance: no impact

<release-notes>
rfe10988: java api for SPIN functions and magic properties

New methods added to AGRepositoryConnection:
putSpinFunction(String, String, String[])
getSpinFunction(String)
deleteSpinFunction(String)
putSpinMagicProperty(String, String, String)
getSpinMagicProperty(String)
deleteSpinMagicProperty(String)

See also rfe10985 for AllegroGraph server.
</release-notes>

Change-Id: I4936f1f114d64e3ac4e00c4e6f2b5b92305ca7be
Reviewed-on: https://gerrit.franz.com:9080/1679
Reviewed-by: Kevin Layer <layer@franz.com>
Tested-by: Kevin Layer <layer@franz.com>
  • Loading branch information...
1 parent 1f13e2a commit 842442dabd4d4c6887e9646c96e3d0e3ba512c95 Mike Hinchey committed with dklayer Oct 26, 2011
View
88 src/com/franz/agraph/http/AGHttpRepoClient.java
@@ -1678,4 +1678,92 @@ public void optimizeIndices(Boolean wait) throws RepositoryException {
throw new RepositoryException(e);
}
}
+
+ private void putSpinX(String x, String uri, String sparqlQuery, String[] arguments) throws RepositoryException {
+ NameValuePair[] params = new NameValuePair[(arguments == null ? 0 : arguments.length) + 1];
+ params[0] = new NameValuePair(AGProtocol.SPIN_QUERY, sparqlQuery);
+ for (int i = 0; arguments != null && i < arguments.length; i++) {
+ params[i+1] = new NameValuePair(AGProtocol.SPIN_ARGUMENTS, arguments[i]);
+ }
+ try {
+ getHTTPClient().put(AGProtocol.spinURL(getRoot(), x, uri), null, params, null);
+ } catch (AGHttpException e) {
+ throw new RepositoryException(e);
+ }
+ }
+
+ public String getSpinFunction(String uri) throws RepositoryException {
+ try {
+ return getHTTPClient().getString(AGProtocol.spinURL(getRoot(), AGProtocol.SPIN_FUNCTION, uri));
+ } catch (AGHttpException e) {
+ throw new RepositoryException(e);
+ }
+ }
+
+ /**
+ * TODO MH: refactor to return a list
+ */
+ public String listSpinFunctions() throws RepositoryException {
+ try {
+ return getHTTPClient().getString(AGProtocol.spinURL(getRoot(), AGProtocol.SPIN_FUNCTION, "*"));
+ } catch (AGHttpException e) {
+ throw new RepositoryException(e);
+ }
+ }
+
+ public void putSpinFunction(String uri, String sparqlQuery, String[] arguments) throws RepositoryException {
+ putSpinX(AGProtocol.SPIN_FUNCTION, uri, sparqlQuery, arguments);
+ }
+
+ public void deleteSpinFunction(String uri) throws RepositoryException {
+ getHTTPClient().delete(AGProtocol.spinURL(getRoot(), AGProtocol.SPIN_FUNCTION, uri), null, null);
+ }
+
+ public void deleteHardSpinFunction(String uri) throws RepositoryException {
+ try {
+ deleteSpinFunction(uri);
+ } catch (RepositoryException e) {
+ if (! e.getMessage().contains(uri + " is not a registered SPIN function")) {
+ throw e;
+ }
+ }
+ }
+
+ public String getSpinMagicProperty(String uri) throws RepositoryException {
+ try {
+ return getHTTPClient().getString(AGProtocol.spinURL(getRoot(), AGProtocol.SPIN_MAGICPROPERTY, uri));
+ } catch (AGHttpException e) {
+ throw new RepositoryException(e);
+ }
+ }
+
+ public void putSpinMagicProperty(String uri, String sparqlQuery, String[] arguments) throws RepositoryException {
+ putSpinX(AGProtocol.SPIN_MAGICPROPERTY, uri, sparqlQuery, arguments);
+ }
+
+ /**
+ * TODO MH: refactor to return a list
+ */
+ public String listSpinMagicProperties() throws RepositoryException {
+ try {
+ return getHTTPClient().getString(AGProtocol.spinURL(getRoot(), AGProtocol.SPIN_MAGICPROPERTY, "*"));
+ } catch (AGHttpException e) {
+ throw new RepositoryException(e);
+ }
+ }
+
+ public void deleteSpinMagicProperty(String uri) throws RepositoryException {
+ getHTTPClient().delete(AGProtocol.spinURL(getRoot(), AGProtocol.SPIN_MAGICPROPERTY, uri), null, null);
+ }
+
+ public void deleteHardSpinMagicProperty(String uri) throws RepositoryException {
+ try {
+ deleteSpinMagicProperty(uri);
+ } catch (RepositoryException e) {
+ if (! e.getMessage().contains(uri + " is not a registered SPIN magic property")) {
+ throw e;
+ }
+ }
+ }
+
}
View
23 src/com/franz/agraph/http/AGProtocol.java
@@ -11,7 +11,10 @@
*/
package com.franz.agraph.http;
+import org.apache.commons.httpclient.URIException;
+import org.apache.commons.httpclient.util.URIUtil;
import org.openrdf.http.protocol.Protocol;
+import org.openrdf.repository.RepositoryException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@@ -415,7 +418,13 @@
* Relative location of the INDICES service.
*/
public static final String INDICES = "indices";
-
+
+ public static final String SPIN = "spin",
+ SPIN_MAGICPROPERTY = "magicproperty",
+ SPIN_FUNCTION = "function",
+ SPIN_QUERY = "query",
+ SPIN_ARGUMENTS = "arguments";
+
/**
* A boolean that defaults to false, indicating whether an error
* should be raised when a SPARQL query selects variables that
@@ -593,4 +602,16 @@ public static String getIndicesURL(String root) {
return root + "/" + INDICES;
}
+ public static String spinURL(String root, String type, String uri) throws RepositoryException {
+ try {
+ if (uri == null) {
+ return root + "/" + SPIN + "/" + type;
+ } else {
+ return root + "/" + SPIN + "/" + type + "/" + URIUtil.encodeAll(uri);
+ }
+ } catch (URIException e) {
+ throw new RepositoryException(e);
+ }
+ }
+
}
View
68 src/com/franz/agraph/repository/AGRepositoryConnection.java
@@ -1447,4 +1447,72 @@ public void optimizeIndices(Boolean wait) throws RepositoryException {
getHttpRepoClient().optimizeIndices(wait);
}
+ /**
+ *
+ * @param uri spin function identifier
+ * @param sparqlQuery spin function query text
+ * @param arguments name of arguments in the sparqlQuery
+ * @see #getSpinFunction(String)
+ * @see #deleteSpinFunction(String)
+ * @see #putSpinMagicProperty(String, String, String)
+ */
+ public void putSpinFunction(String uri, String sparqlQuery, String[] arguments) throws RepositoryException {
+ getHttpRepoClient().putSpinFunction(uri, sparqlQuery, arguments);
+ }
+
+ /**
+ *
+ * @param uri spin function identifier
+ * @return spin function query text
+ * @see #putSpinFunction(String, String, String[])
+ * @see #deleteSpinFunction(String)
+ */
+ public String getSpinFunction(String uri) throws RepositoryException {
+ return getHttpRepoClient().getSpinFunction(uri);
+ }
+
+ /**
+ *
+ * @param uri spin function identifier
+ * @see #putSpinFunction(String, String, String[])
+ * @see #getSpinFunction(String)
+ */
+ public void deleteSpinFunction(String uri) throws RepositoryException {
+ getHttpRepoClient().deleteSpinFunction(uri);
+ }
+
+ /**
+ *
+ * @param uri spin magic property identifier
+ * @param sparqlQuery
+ * @param arguments names of arguments to the sparqlQuery must contain the leading question mark
+ * @see #getSpinMagicProperty(String)
+ * @see #deleteSpinMagicProperty(String)
+ * @see #putSpinFunction(String, String, String[])
+ */
+ public void putSpinMagicProperty(String uri, String sparqlQuery, String[] arguments) throws RepositoryException {
+ getHttpRepoClient().putSpinMagicProperty(uri, sparqlQuery, arguments);
+ }
+
+ /**
+ *
+ * @param uri spin magic property identifier
+ * @return sparqlQuery
+ * @see #putSpinMagicProperty(String, String, String[])
+ * @see #deleteSpinMagicProperty(String)
+ */
+ public String getSpinMagicProperty(String uri) throws RepositoryException {
+ return getHttpRepoClient().getSpinMagicProperty(uri);
+ }
+
+ /**
+ *
+ * @param uri spin magic property identifier
+ * @see #putSpinMagicProperty(String, String, String[])
+ * @see #getSpinMagicProperty(String)
+ */
+ public void deleteSpinMagicProperty(String uri) throws RepositoryException {
+ getHttpRepoClient().deleteSpinMagicProperty(uri);
+ }
+
}
View
143 src/test/SpinTest.java
@@ -0,0 +1,143 @@
+/******************************************************************************
+** Copyright (c) 2008-2011 Franz Inc.
+** All rights reserved. This program and the accompanying materials
+** are made available under the terms of the Eclipse Public License v1.0
+** which accompanies this distribution, and is available at
+** http://www.eclipse.org/legal/epl-v10.html
+******************************************************************************/
+
+package test;
+
+import static test.Stmt.statementSet;
+import static test.Stmt.stmts;
+
+import java.io.File;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.openrdf.model.URI;
+import org.openrdf.model.vocabulary.XMLSchema;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.rio.RDFFormat;
+
+public class SpinTest extends AGAbstractTest {
+
+ private final String baseURI = "http://ex.org#";
+
+ private final String kennedyNamespace = "http://www.franz.com/simple#";
+
+ private final String ageFn = baseURI + "age";
+ private final String parentsMP = baseURI + "parents";
+
+ private URI context;
+
+ @Before
+ public void installScripts() throws Exception {
+ conn.setAutoCommit(true);
+ context = vf.createURI(kennedyNamespace);
+ conn.setNamespace("ex", baseURI);
+ conn.setNamespace("kennedy", kennedyNamespace);
+// conn.registerPredicateMapping(vf.createURI(kennedyNamespace, "birth-year"), XMLSchema.INT);
+ conn.add(new File("src/tutorial/java-kennedy.ntriples"), kennedyNamespace, RDFFormat.NTRIPLES, context);
+ }
+
+ @After
+ public void cleanup0() throws Exception {
+// conn.remove((Resource)null, null, null);
+ }
+
+ @After
+ public void cleanup1() throws Exception {
+// TODO broken: conn.getHttpRepoClient().deleteHardSpinFunction(ageFn);
+ }
+
+ @After
+ public void cleanup2() throws Exception {
+ conn.getHttpRepoClient().deleteHardSpinMagicProperty(parentsMP);
+ }
+
+ /**
+ * rfe10988
+ */
+ @Test
+ @Category(TestSuites.Temp.class)
+ public void spinFunction() throws Exception {
+ try {
+ Assert.fail( conn.getSpinFunction(ageFn) );
+ } catch (Exception e) {
+ if (e.getMessage().contains(ageFn + " is not a registered SPIN function")) {
+ // expected
+ } else {
+ throw e;
+ }
+ }
+ String ageFnSparql = "prefix kennedy: <" + kennedyNamespace + ">\n"
+ + "prefix xs: <http://www.w3.org/2001/XMLSchema#>\n"
+ + "select ( (2011 - xs:int(?birthYear)) as ?age ) { ?who kennedy:birth-year ?birthYear . }";
+ conn.putSpinFunction(ageFn, ageFnSparql, new String[] {"?who"});
+ Assert.assertEquals(ageFnSparql, conn.getSpinFunction(ageFn));
+
+ String queryString = "prefix ex: <" + baseURI + ">\n"
+ + "prefix kennedy: <" + kennedyNamespace + ">\n"
+ + "prefix kennedy: <http://www.franz.com/simple#>\n"
+ + "prefix ex: <http://ex.org#>\n"
+ + "select ?first ?last ?age ?birthYear {\n"
+ + "?person kennedy:first-name ?first .\n"
+ + "?person kennedy:last-name ?last .\n"
+ + "?person kennedy:birth-year ?birthYear .\n"
+ + "bind( ex:age( ?person ) as ?age ) .\n"
+ + "} order by ?age limit 2";
+
+ assertSetsEqual(stmts(new Stmt[] {new Stmt(null, null, vf.createLiteral("39", XMLSchema.INTEGER)),
+ new Stmt(null, null, vf.createLiteral("43", XMLSchema.INTEGER))}),
+ statementSet(conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(), null, null, "age"));
+
+ // TODO MH: Assert.assertEquals("TODO", conn.getHttpRepoClient().listSpinFunctions());
+}
+
+ /**
+ * rfe10988
+ */
+ @Test
+ @Category(TestSuites.Temp.class)
+ public void spinMagicProperty() throws Exception {
+ try {
+ Assert.fail( conn.getSpinMagicProperty(parentsMP) );
+ } catch (Exception e) {
+ if (e.getMessage().contains(parentsMP + " is not a registered SPIN magic property")) {
+ // expected
+ } else {
+ throw e;
+ }
+ }
+ conn.getHttpRepoClient().deleteHardSpinMagicProperty(parentsMP);
+
+ String parentsFnSparql = "prefix kennedy: <" + kennedyNamespace+ ">\n"
+ + "select ?parent { ?parent kennedy:has-child ?child . }";
+ conn.putSpinMagicProperty(parentsMP, parentsFnSparql, new String[] {"?child"});
+ Assert.assertEquals(parentsFnSparql, conn.getSpinMagicProperty(parentsMP));
+
+ String queryString = "prefix ex: <" + baseURI + ">\n"
+ + "prefix kennedy: <" + kennedyNamespace + ">\n"
+ + "select ?person ?parentFirst {\n"
+ + "?person kennedy:first-name 'Joseph' .\n"
+// + "?person kennedy:birth-year '1915'^^<" + XMLSchema.INT + "> .\n"
+ + "?person kennedy:birth-year '1915' .\n"
+ + "?person ex:parents ?parent .\n"
+// + "?parent kennedy:has-child ?person .\n"
+ + "?parent kennedy:first-name ?parentFirst .\n"
+ + "}";
+
+ assertSetsEqual(stmts(new Stmt[] {new Stmt(null, null, vf.createLiteral("Joseph")),
+ new Stmt(null, null, vf.createLiteral("Rose"))}),
+ statementSet(conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(),
+ null, null, "parentFirst"));
+
+ String props = conn.getHttpRepoClient().listSpinMagicProperties();
+ Assert.assertTrue(props, props.contains(parentsFnSparql));
+ }
+
+}
View
3 src/test/TestSuites.java
@@ -44,7 +44,8 @@
JenaTests.class,
StreamingTest.class,
ServerCodeTests.class,
- AGConnPoolSessionTest.class
+ AGConnPoolSessionTest.class,
+ SpinTest.class
})
public static class Temp {}

0 comments on commit 842442d

Please sign in to comment.