RYA-168 adding test and support for alternate representation

Project: http://git-wip-us.apache.org/repos/asf/incubator-rya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-rya/commit/5edfd65e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-rya/tree/5edfd65e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-rya/diff/5edfd65e

Branch: refs/heads/master
Commit: 5edfd65e58f97f1ff6188a69c1d719f9701a94e4
Parents: da8a2e6
Author: pujav65 <[email protected]>
Authored: Mon Sep 26 12:30:05 2016 -0400
Committer: pujav65 <[email protected]>
Committed: Tue Sep 27 11:25:20 2016 -0400

----------------------------------------------------------------------
 .../mvm/rya/sail/config/RyaSailFactory.java     |   5 +-
 .../src/main/java/MongoRyaDirectExample.java    |  51 +++++++++
 .../src/main/java/RyaDirectExample.java         |  22 ++++
 .../inference/InferenceEngine.java              |  57 ++++++++++
 .../rdftriplestore/inference/InverseURI.java    |  46 ++++++++
 .../inference/PropertyChainVisitor.java         |   9 +-
 .../inference/PropertyChainTest.java            | 106 +++++++++++++++++++
 7 files changed, 290 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5edfd65e/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java
----------------------------------------------------------------------
diff --git 
a/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java 
b/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java
index afffb68..b0a719b 100644
--- a/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java
+++ b/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java
@@ -89,9 +89,8 @@ public class RyaSailFactory {
                 final MongoRyaInstanceDetailsRepository ryaDetailsRepo = new 
MongoRyaInstanceDetailsRepository(client, mongoConfig.getCollectionName());
                 
RyaDetailsToConfiguration.addRyaDetailsToConfiguration(ryaDetailsRepo.getRyaInstanceDetails(),
 mongoConfig);
             } catch (final RyaDetailsRepositoryException e) {
-                LOG.info("Instance does not have a rya details collection, 
skipping.");
-            }
-            dao = getMongoDAO((MongoDBRdfConfiguration)rdfConfig, client);
+               LOG.info("Instance does not have a rya details collection, 
skipping.");
+           }            dao = getMongoDAO((MongoDBRdfConfiguration)rdfConfig, 
client);
         } else {
             rdfConfig = new AccumuloRdfConfiguration(config);
             user = rdfConfig.get(ConfigUtils.CLOUDBASE_USER);

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5edfd65e/extras/indexingExample/src/main/java/MongoRyaDirectExample.java
----------------------------------------------------------------------
diff --git a/extras/indexingExample/src/main/java/MongoRyaDirectExample.java 
b/extras/indexingExample/src/main/java/MongoRyaDirectExample.java
index 2bc0924..8a2bac5 100644
--- a/extras/indexingExample/src/main/java/MongoRyaDirectExample.java
+++ b/extras/indexingExample/src/main/java/MongoRyaDirectExample.java
@@ -89,6 +89,7 @@ public class MongoRyaDirectExample {
             if (USE_INFER){
                testInfer(conn, sail);
                testPropertyChainInference(conn, sail);
+               testPropertyChainInferenceAltRepresentation(conn, sail);
             }
 
             log.info("TIME: " + (System.currentTimeMillis() - start) / 1000.);
@@ -324,6 +325,55 @@ public class MongoRyaDirectExample {
        Validate.isTrue(resultHandler.getCount() == 0);
     }
 
+    
+    public static void 
testPropertyChainInferenceAltRepresentation(SailRepositoryConnection conn, Sail 
sail) throws MalformedQueryException, RepositoryException,
+    UpdateExecutionException, QueryEvaluationException, 
TupleQueryResultHandlerException, InferenceEngineException {
+
+       // Add data
+       String query = "INSERT DATA\n"//
+                       + "{ GRAPH <http://updated/test> {\n"//
+                       + "  <urn:jenGreatGranMother> <urn:Motherof> 
<urn:jenGranMother> . "
+                       + "  <urn:jenGranMother> <urn:isChildOf> 
<urn:jenGreatGranMother> . "
+                       + "  <urn:jenGranMother> <urn:Motherof> <urn:jenMother> 
. " 
+                       + "  <urn:jenMother> <urn:isChildOf> 
<urn:jenGranMother> . "
+                       + " <urn:jenMother> <urn:Motherof> <urn:jen> . "
+                       + "  <urn:jen> <urn:isChildOf> <urn:jenMother> . "
+                       + " <urn:jen> <urn:Motherof> <urn:jenDaughter> .  }}";
+
+       log.info("Performing Query");
+
+       Update update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
+       update.execute();
+
+       query = "select ?p { GRAPH <http://updated/test> {?s 
<urn:Motherof>/<urn:Motherof> ?p}}";
+       CountingResultHandler resultHandler = new CountingResultHandler();
+       TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, 
query);
+       tupleQuery.evaluate(resultHandler);
+       log.info("Result count : " + resultHandler.getCount());
+
+
+       // try adding a property chain and querying for it
+       query = "INSERT DATA\n"//
+                       + "{ GRAPH <http://updated/test> {\n"//
+                       + "  <urn:greatMother> owl:propertyChainAxiom 
<urn:12342>  . " + 
+                       " <urn:12342> 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:node1atjakcvbx15023 . " + 
+                       " _:node1atjakcvbx15023 
<http://www.w3.org/2002/07/owl#inverseOf> <urn:isChildOf> . " + 
+                       " <urn:12342> 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:node1atjakcvbx15123 . " + 
+                               " _:node1atjakcvbx15123 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . " + 
+                       " _:node1atjakcvbx15123 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <urn:MotherOf> .  }}";
+       update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
+       update.execute();
+       ((RdfCloudTripleStore) sail).getInferenceEngine().refreshGraph();
+
+       resultHandler.resetCount();
+       query = "select ?x { GRAPH <http://updated/test> 
{<urn:jenGreatGranMother> <urn:greatMother> ?x}}";
+       resultHandler = new CountingResultHandler();
+       tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
+       tupleQuery.evaluate(resultHandler);
+       log.info("Result count : " + resultHandler.getCount());
+
+    }
+    
     public static void testPropertyChainInference(SailRepositoryConnection 
conn, Sail sail) throws MalformedQueryException, RepositoryException,
     UpdateExecutionException, QueryEvaluationException, 
TupleQueryResultHandlerException, InferenceEngineException {
 
@@ -480,6 +530,7 @@ public class MongoRyaDirectExample {
         @Override
         public void handleSolution(BindingSet arg0) throws 
TupleQueryResultHandlerException {
             count++;
+            System.out.println(arg0);
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5edfd65e/extras/indexingExample/src/main/java/RyaDirectExample.java
----------------------------------------------------------------------
diff --git a/extras/indexingExample/src/main/java/RyaDirectExample.java 
b/extras/indexingExample/src/main/java/RyaDirectExample.java
index 03712ba..acc5617 100644
--- a/extras/indexingExample/src/main/java/RyaDirectExample.java
+++ b/extras/indexingExample/src/main/java/RyaDirectExample.java
@@ -17,7 +17,10 @@
  * under the License.
  */
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.net.UnknownHostException;
+import java.util.Collection;
 import java.util.List;
 
 import org.apache.accumulo.core.client.AccumuloException;
@@ -33,6 +36,7 @@ import org.apache.log4j.Logger;
 import org.apache.rya.indexing.pcj.storage.PcjException;
 import org.apache.rya.indexing.pcj.storage.accumulo.PcjTables;
 import org.apache.rya.indexing.pcj.storage.accumulo.PcjVarOrderFactory;
+import org.openrdf.model.Statement;
 import org.openrdf.model.URI;
 import org.openrdf.model.ValueFactory;
 import org.openrdf.model.impl.LiteralImpl;
@@ -52,6 +56,10 @@ import org.openrdf.query.UpdateExecutionException;
 import org.openrdf.repository.RepositoryException;
 import org.openrdf.repository.sail.SailRepository;
 import org.openrdf.repository.sail.SailRepositoryConnection;
+import org.openrdf.rio.RDFFormat;
+import org.openrdf.rio.RDFParser;
+import org.openrdf.rio.Rio;
+import org.openrdf.rio.helpers.StatementCollector;
 import org.openrdf.sail.Sail;
 import org.openrdf.sail.SailException;
 
@@ -81,6 +89,20 @@ public class RyaDirectExample {
        private static final String AUTHS = "U";
 
        public static void main(final String[] args) throws Exception {
+               
+               File inputFile = new File("C:\\SPARTADEV\\propChain.ttl");
+               RDFParser parser = Rio.createParser(RDFFormat.TURTLE);
+               StatementCollector coll = new StatementCollector();
+               parser.setRDFHandler(coll);
+               parser.parse(new FileInputStream(inputFile), "");
+               Collection<Statement> statements = coll.getStatements();
+               System.out.println("Start");
+               for (Statement statement : statements){
+                       System.out.println(statement);
+               }
+               System.out.println("End");
+               System.exit(-1);
+               
                final Configuration conf = getConf();
                conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
 

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5edfd65e/sail/src/main/java/mvm/rya/rdftriplestore/inference/InferenceEngine.java
----------------------------------------------------------------------
diff --git 
a/sail/src/main/java/mvm/rya/rdftriplestore/inference/InferenceEngine.java 
b/sail/src/main/java/mvm/rya/rdftriplestore/inference/InferenceEngine.java
index c323892..b7288b2 100644
--- a/sail/src/main/java/mvm/rya/rdftriplestore/inference/InferenceEngine.java
+++ b/sail/src/main/java/mvm/rya/rdftriplestore/inference/InferenceEngine.java
@@ -303,6 +303,63 @@ public class InferenceEngine {
                }
                propertyChainPropertyToChain.put(propertyChainProperty, 
properties);
             }
+            
+            // could also be represented as a list of properties (some of 
which may be blank nodes)
+            for (URI propertyChainProperty : 
propertyChainPropertiesToBNodes.keySet()){
+               List<URI> existingChain = 
propertyChainPropertyToChain.get(propertyChainProperty);
+               // if we didn't get a chain, try to get it through following 
the collection
+               if ((existingChain == null) || existingChain.isEmpty()) {
+                       
+                         CloseableIteration<Statement, 
QueryEvaluationException>  iter2 = RyaDAOHelper.query(ryaDAO, 
propertyChainPropertiesToBNodes.get(propertyChainProperty), RDF.FIRST,
+                               null, conf);
+                         List<URI> properties = new ArrayList<URI>();
+                         URI previousBNode = 
propertyChainPropertiesToBNodes.get(propertyChainProperty);
+                 if (iter2.hasNext()) {
+                         Statement iter2Statement = iter2.next();
+                         Value currentPropValue = iter2Statement.getObject();
+                         while ((currentPropValue != null) && 
(!currentPropValue.stringValue().equalsIgnoreCase(RDF.NIL.stringValue()))){
+                                 if (currentPropValue instanceof URI){
+                                 iter2 = RyaDAOHelper.query(ryaDAO, 
vf.createURI(currentPropValue.stringValue()), RDF.FIRST,
+                                               null, conf);
+                                         if (iter2.hasNext()){
+                                                 iter2Statement = iter2.next();
+                                                 if 
(iter2Statement.getObject() instanceof URI){
+                                                         
properties.add((URI)iter2Statement.getObject());
+                                                 }
+                                         }
+                                         // otherwise see if there is an 
inverse declaration
+                                         else {
+                                                 iter2 = 
RyaDAOHelper.query(ryaDAO, vf.createURI(currentPropValue.stringValue()), 
OWL.INVERSEOF,
+                                                       null, conf);
+                                                 if (iter2.hasNext()){
+                                                 iter2Statement = iter2.next();
+                                                 if 
(iter2Statement.getObject() instanceof URI){
+                                                         properties.add(new 
InverseURI((URI)iter2Statement.getObject()));
+                                                 }
+                                         }
+                                         }
+                                         // get the next prop pointer
+                                         iter2 = RyaDAOHelper.query(ryaDAO, 
previousBNode, RDF.REST,
+                                               null, conf);
+                                         if (iter2.hasNext()){
+                                                 iter2Statement = iter2.next();
+                                                 previousBNode = 
(URI)currentPropValue;
+                                                 currentPropValue = 
iter2Statement.getObject();
+                                         }
+                                         else {
+                                                 currentPropValue = null;
+                                         }
+                                 }
+                                 else {
+                                   currentPropValue = null;
+                                 }
+                                 
+                         }
+                       propertyChainPropertyToChain.put(propertyChainProperty, 
properties);
+                 }
+               }
+            }
+            
         } catch (QueryEvaluationException e) {
             throw new InferenceEngineException(e);
         }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5edfd65e/sail/src/main/java/mvm/rya/rdftriplestore/inference/InverseURI.java
----------------------------------------------------------------------
diff --git 
a/sail/src/main/java/mvm/rya/rdftriplestore/inference/InverseURI.java 
b/sail/src/main/java/mvm/rya/rdftriplestore/inference/InverseURI.java
new file mode 100644
index 0000000..c182fb5
--- /dev/null
+++ b/sail/src/main/java/mvm/rya/rdftriplestore/inference/InverseURI.java
@@ -0,0 +1,46 @@
+package mvm.rya.rdftriplestore.inference;
+
+import org.openrdf.model.URI;
+import org.openrdf.model.impl.URIImpl;
+
+public class InverseURI implements URI {
+
+       private URI impl;
+       
+       public InverseURI(URI uri) {
+               this.impl = uri;
+       }
+
+       @Override
+       public int hashCode() {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + ((impl == null) ? 0 : 
impl.hashCode());
+               return result;
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (obj instanceof InverseURI){
+                       return impl.equals(((InverseURI) obj).impl);
+               }
+               return false;
+       }
+
+       @Override
+       public String stringValue() {
+               return impl.stringValue();
+       }
+
+       @Override
+       public String getNamespace() {
+               return impl.getNamespace();
+       }
+
+       @Override
+       public String getLocalName() {
+               return impl.getLocalName();
+       }
+       
+       
+}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5edfd65e/sail/src/main/java/mvm/rya/rdftriplestore/inference/PropertyChainVisitor.java
----------------------------------------------------------------------
diff --git 
a/sail/src/main/java/mvm/rya/rdftriplestore/inference/PropertyChainVisitor.java 
b/sail/src/main/java/mvm/rya/rdftriplestore/inference/PropertyChainVisitor.java
index 06d557f..6e487b2 100644
--- 
a/sail/src/main/java/mvm/rya/rdftriplestore/inference/PropertyChainVisitor.java
+++ 
b/sail/src/main/java/mvm/rya/rdftriplestore/inference/PropertyChainVisitor.java
@@ -82,10 +82,14 @@ public class PropertyChainVisitor extends 
AbstractInferVisitor {
                for (URI chainElement : chain ){
                        String s = UUID.randomUUID().toString();
                     Var currentObj = new Var("c-" + s);
-                       StatementPattern statementPattern = new 
StatementPattern(nextSubj, new Var(chainElement.stringValue()), currentObj, 
sp.getContextVar());
+                    StatementPattern statementPattern = new 
StatementPattern(nextSubj, new Var(chainElement.stringValue()), currentObj, 
sp.getContextVar());
+                    if (chainElement instanceof InverseURI){
+                       statementPattern = new StatementPattern(currentObj, new 
Var(chainElement.stringValue()), nextSubj, sp.getContextVar());
+                    }
                        expandedPatterns.add(statementPattern);
                        lastStatementPatternAdded = statementPattern;
-                       nextSubj = statementPattern.getObjectVar();
+                    nextSubj = currentObj;
+                       
                }
                lastStatementPatternAdded.setObjectVar(originalObj);
                
@@ -99,7 +103,6 @@ public class PropertyChainVisitor extends 
AbstractInferVisitor {
                                Join newJoin = new Join(pattern, lastRight);
                                lastRight = newJoin;
                        }
-                       System.out.println(pattern);
                }
                if (lastRight != null){
                        node.replaceWith(lastRight);    

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5edfd65e/sail/src/test/java/mvm/rya/triplestore/inference/PropertyChainTest.java
----------------------------------------------------------------------
diff --git 
a/sail/src/test/java/mvm/rya/triplestore/inference/PropertyChainTest.java 
b/sail/src/test/java/mvm/rya/triplestore/inference/PropertyChainTest.java
new file mode 100644
index 0000000..8592e0a
--- /dev/null
+++ b/sail/src/test/java/mvm/rya/triplestore/inference/PropertyChainTest.java
@@ -0,0 +1,106 @@
+package mvm.rya.triplestore.inference;
+
+import java.util.List;
+
+import org.apache.accumulo.core.Constants;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.admin.SecurityOperations;
+import org.apache.accumulo.core.client.mock.MockInstance;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.accumulo.core.security.TablePermission;
+import org.junit.Assert;
+import org.junit.Test;
+import org.openrdf.model.URI;
+import org.openrdf.model.ValueFactory;
+import org.openrdf.model.impl.ValueFactoryImpl;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.query.Update;
+import org.openrdf.repository.sail.SailRepository;
+import org.openrdf.repository.sail.SailRepositoryConnection;
+
+import junit.framework.TestCase;
+import mvm.rya.accumulo.AccumuloRdfConfiguration;
+import mvm.rya.accumulo.AccumuloRyaDAO;
+import mvm.rya.api.RdfCloudTripleStoreConstants;
+import mvm.rya.rdftriplestore.RdfCloudTripleStore;
+import mvm.rya.rdftriplestore.inference.InferenceEngine;
+import mvm.rya.rdftriplestore.inference.InverseURI;
+
+public class PropertyChainTest extends TestCase {
+    private String user = "user";
+    private String pwd = "pwd";
+    private String instance = "myinstance";
+    private String tablePrefix = "t_";
+    private Authorizations auths = Constants.NO_AUTHS;
+    private Connector connector;
+    private AccumuloRyaDAO ryaDAO;
+    private ValueFactory vf = new ValueFactoryImpl();
+    private String namespace = "urn:test#";
+    private AccumuloRdfConfiguration conf;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        connector = new MockInstance(instance).getConnector(user, 
pwd.getBytes());
+        connector.tableOperations().create(tablePrefix + 
RdfCloudTripleStoreConstants.TBL_SPO_SUFFIX);
+        connector.tableOperations().create(tablePrefix + 
RdfCloudTripleStoreConstants.TBL_PO_SUFFIX);
+        connector.tableOperations().create(tablePrefix + 
RdfCloudTripleStoreConstants.TBL_OSP_SUFFIX);
+        connector.tableOperations().create(tablePrefix + 
RdfCloudTripleStoreConstants.TBL_NS_SUFFIX);
+        SecurityOperations secOps = connector.securityOperations();
+        secOps.createUser(user, pwd.getBytes(), auths);
+        secOps.grantTablePermission(user, tablePrefix + 
RdfCloudTripleStoreConstants.TBL_SPO_SUFFIX, TablePermission.READ);
+        secOps.grantTablePermission(user, tablePrefix + 
RdfCloudTripleStoreConstants.TBL_PO_SUFFIX, TablePermission.READ);
+        secOps.grantTablePermission(user, tablePrefix + 
RdfCloudTripleStoreConstants.TBL_OSP_SUFFIX, TablePermission.READ);
+        secOps.grantTablePermission(user, tablePrefix + 
RdfCloudTripleStoreConstants.TBL_NS_SUFFIX, TablePermission.READ);
+
+        conf = new AccumuloRdfConfiguration();
+        ryaDAO = new AccumuloRyaDAO();
+        ryaDAO.setConnector(connector);
+        conf.setTablePrefix(tablePrefix);
+        ryaDAO.setConf(conf);
+        ryaDAO.init();
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+        connector.tableOperations().delete(tablePrefix + 
RdfCloudTripleStoreConstants.TBL_SPO_SUFFIX);
+        connector.tableOperations().delete(tablePrefix + 
RdfCloudTripleStoreConstants.TBL_PO_SUFFIX);
+        connector.tableOperations().delete(tablePrefix + 
RdfCloudTripleStoreConstants.TBL_OSP_SUFFIX);
+        connector.tableOperations().delete(tablePrefix + 
RdfCloudTripleStoreConstants.TBL_NS_SUFFIX);
+    }
+
+    @Test
+    public void testGraphConfiguration() throws Exception {
+        // build a connection
+        RdfCloudTripleStore store = new RdfCloudTripleStore();
+        store.setConf(conf);
+        store.setRyaDAO(ryaDAO);
+        InferenceEngine inferenceEngine = new InferenceEngine();
+        inferenceEngine.setRyaDAO(ryaDAO);
+        store.setInferenceEngine(inferenceEngine);
+        inferenceEngine.refreshGraph();
+        store.initialize();
+        SailRepository repository = new SailRepository(store);
+        SailRepositoryConnection conn = repository.getConnection();
+        
+
+        
+       String query = "INSERT DATA\n"//
+                       + "{ GRAPH <http://updated/test> {\n"//
+                       + "  <urn:greatMother> owl:propertyChainAxiom 
<urn:12342>  . " + 
+                       " <urn:12342> 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:node1atjakcvbx15023 . " + 
+                       " _:node1atjakcvbx15023 
<http://www.w3.org/2002/07/owl#inverseOf> <urn:isChildOf> . " + 
+                       " <urn:12342> 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:node1atjakcvbx15123 . " + 
+                               " _:node1atjakcvbx15123 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . " + 
+                       " _:node1atjakcvbx15123 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <urn:MotherOf> .  }}";
+       Update update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
+       update.execute();
+        inferenceEngine.refreshGraph();
+       List<URI> chain = 
inferenceEngine.getPropertyChain(vf.createURI("urn:greatMother"));
+       Assert.assertEquals(chain.size(), 2);
+       Assert.assertEquals(chain.get(0), new 
InverseURI(vf.createURI("urn:isChildOf")));
+       Assert.assertEquals(chain.get(1), vf.createURI("urn:MotherOf"));
+ 
+    }
+}

Reply via email to