Search in sources :

Example 6 with InferenceEngine

use of org.apache.rya.rdftriplestore.inference.InferenceEngine in project incubator-rya by apache.

the class StatementPatternStorage method addInferredRanges.

protected void addInferredRanges(String tablePrefix, Job job) throws IOException {
    logger.info("Adding inferences to statement pattern[subject:" + subject_value + ", predicate:" + predicate_value + ", object:" + object_value + "]");
    // inference engine
    AccumuloRyaDAO ryaDAO = new AccumuloRyaDAO();
    InferenceEngine inferenceEngine = new InferenceEngine();
    try {
        AccumuloRdfConfiguration rdfConf = new AccumuloRdfConfiguration(job.getConfiguration());
        rdfConf.setTablePrefix(tablePrefix);
        ryaDAO.setConf(rdfConf);
        try {
            if (!mock) {
                ryaDAO.setConnector(new ZooKeeperInstance(inst, zookeepers).getConnector(user, userP.getBytes(StandardCharsets.UTF_8)));
            } else {
                ryaDAO.setConnector(new MockInstance(inst).getConnector(user, userP.getBytes(StandardCharsets.UTF_8)));
            }
        } catch (Exception e) {
            throw new IOException(e);
        }
        ryaDAO.init();
        inferenceEngine.setConf(rdfConf);
        inferenceEngine.setRyaDAO(ryaDAO);
        inferenceEngine.setSchedule(false);
        inferenceEngine.init();
        // is it subclassof or subpropertyof
        if (RDF.TYPE.equals(predicate_value)) {
            // try subclassof
            Collection<URI> parents = inferenceEngine.findParents(inferenceEngine.getSubClassOfGraph(), (URI) object_value);
            if (parents != null && parents.size() > 0) {
                // add all relationships
                for (URI parent : parents) {
                    Map.Entry<TABLE_LAYOUT, Range> temp = createRange(subject_value, predicate_value, parent);
                    Range range = temp.getValue();
                    if (logger.isDebugEnabled()) {
                        logger.debug("Found subClassOf relationship [type:" + object_value + " is subClassOf:" + parent + "]");
                    }
                    addRange(range);
                }
            }
        } else if (predicate_value != null) {
            // subpropertyof check
            Set<URI> parents = inferenceEngine.findParents(inferenceEngine.getSubPropertyOfGraph(), (URI) predicate_value);
            for (URI parent : parents) {
                Map.Entry<TABLE_LAYOUT, Range> temp = createRange(subject_value, parent, object_value);
                Range range = temp.getValue();
                if (logger.isDebugEnabled()) {
                    logger.debug("Found subPropertyOf relationship [type:" + predicate_value + " is subPropertyOf:" + parent + "]");
                }
                addRange(range);
            }
        }
    } catch (Exception e) {
        logger.error("Exception in adding inferred ranges", e);
        throw new IOException(e);
    } finally {
        if (inferenceEngine != null) {
            try {
                inferenceEngine.destroy();
            } catch (InferenceEngineException e) {
                logger.error("Exception closing InferenceEngine", e);
            }
        }
        if (ryaDAO != null) {
            try {
                ryaDAO.destroy();
            } catch (RyaDAOException e) {
                logger.error("Exception closing ryadao", e);
            }
        }
    }
}
Also used : AccumuloRyaDAO(org.apache.rya.accumulo.AccumuloRyaDAO) Set(java.util.Set) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) IOException(java.io.IOException) Range(org.apache.accumulo.core.data.Range) ByteRange(org.apache.rya.api.query.strategy.ByteRange) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) MalformedQueryException(org.openrdf.query.MalformedQueryException) IOException(java.io.IOException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) ZooKeeperInstance(org.apache.accumulo.core.client.ZooKeeperInstance) TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) InferenceEngine(org.apache.rya.rdftriplestore.inference.InferenceEngine) MockInstance(org.apache.accumulo.core.client.mock.MockInstance) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) Map(java.util.Map)

Example 7 with InferenceEngine

use of org.apache.rya.rdftriplestore.inference.InferenceEngine in project incubator-rya by apache.

the class RyaSailFactory method getRyaSail.

private static Sail getRyaSail(final Configuration config) throws InferenceEngineException, RyaDAOException, AccumuloException, AccumuloSecurityException, SailException {
    final RdfCloudTripleStore store = new RdfCloudTripleStore();
    final RyaDAO<?> dao;
    final RdfCloudTripleStoreConfiguration rdfConfig;
    final String user;
    final String pswd;
    // XXX Should(?) be MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX inside the if below. RYA-135
    final String ryaInstance = config.get(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX);
    Objects.requireNonNull(ryaInstance, "RyaInstance or table prefix is missing from configuration." + RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX);
    if (ConfigUtils.getUseMongo(config)) {
        // Get a reference to a Mongo DB configuration object.
        final MongoDBRdfConfiguration mongoConfig = (config instanceof MongoDBRdfConfiguration) ? (MongoDBRdfConfiguration) config : new MongoDBRdfConfiguration(config);
        // Instantiate a Mongo client and Mongo DAO.
        dao = getMongoDAO(mongoConfig);
        // Then use the DAO's newly-created stateful conf in place of the original
        rdfConfig = dao.getConf();
    } else {
        rdfConfig = new AccumuloRdfConfiguration(config);
        user = rdfConfig.get(ConfigUtils.CLOUDBASE_USER);
        pswd = rdfConfig.get(ConfigUtils.CLOUDBASE_PASSWORD);
        Objects.requireNonNull(user, "Accumulo user name is missing from configuration." + ConfigUtils.CLOUDBASE_USER);
        Objects.requireNonNull(pswd, "Accumulo user password is missing from configuration." + ConfigUtils.CLOUDBASE_PASSWORD);
        rdfConfig.setTableLayoutStrategy(new TablePrefixLayoutStrategy(ryaInstance));
        updateAccumuloConfig((AccumuloRdfConfiguration) rdfConfig, user, pswd, ryaInstance);
        dao = getAccumuloDAO((AccumuloRdfConfiguration) rdfConfig);
    }
    store.setRyaDAO(dao);
    rdfConfig.setTablePrefix(ryaInstance);
    if (rdfConfig.isInfer()) {
        final InferenceEngine inferenceEngine = new InferenceEngine();
        inferenceEngine.setConf(rdfConfig);
        inferenceEngine.setRyaDAO(dao);
        inferenceEngine.init();
        store.setInferenceEngine(inferenceEngine);
    }
    store.initialize();
    return store;
}
Also used : RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) InferenceEngine(org.apache.rya.rdftriplestore.inference.InferenceEngine) TablePrefixLayoutStrategy(org.apache.rya.api.layout.TablePrefixLayoutStrategy) RdfCloudTripleStoreConfiguration(org.apache.rya.api.RdfCloudTripleStoreConfiguration) MongoDBRdfConfiguration(org.apache.rya.mongodb.MongoDBRdfConfiguration) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration)

Example 8 with InferenceEngine

use of org.apache.rya.rdftriplestore.inference.InferenceEngine in project incubator-rya by apache.

the class PropertyChainTest method testGraphConfiguration.

@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"));
}
Also used : RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) InferenceEngine(org.apache.rya.rdftriplestore.inference.InferenceEngine) SailRepository(org.openrdf.repository.sail.SailRepository) InverseURI(org.apache.rya.rdftriplestore.inference.InverseURI) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) Update(org.openrdf.query.Update) URI(org.openrdf.model.URI) InverseURI(org.apache.rya.rdftriplestore.inference.InverseURI) Test(org.junit.Test)

Example 9 with InferenceEngine

use of org.apache.rya.rdftriplestore.inference.InferenceEngine in project incubator-rya by apache.

the class HasValueVisitorTest method testRewriteTypePattern.

@Test
public void testRewriteTypePattern() throws Exception {
    // Configure a mock instance engine with an ontology:
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    Map<URI, Set<Value>> vertebrateValues = new HashMap<>();
    vertebrateValues.put(hasCharacteristic, new HashSet<>());
    vertebrateValues.put(belongsTo, new HashSet<>());
    vertebrateValues.get(hasCharacteristic).add(notochord);
    vertebrateValues.get(hasCharacteristic).add(skull);
    vertebrateValues.get(belongsTo).add(chordata);
    when(inferenceEngine.getHasValueByType(vertebrate)).thenReturn(vertebrateValues);
    // Query for a specific type and rewrite using the visitor:
    final Projection query = new Projection(new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", vertebrate)), new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new HasValueVisitor(conf, inferenceEngine));
    // Expected structure: two nested unions whose members are (in some order) the original
    // statement pattern and two joins, one for each unique property involved in a relevant
    // restriction. Each join should be between a StatementPattern for the property and a
    // FixedStatementPattern providing the value(s).
    // Collect the arguments to the unions, ignoring nesting order:
    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union1 = (Union) query.getArg();
    final Set<TupleExpr> unionArgs = new HashSet<>();
    if (union1.getLeftArg() instanceof Union) {
        unionArgs.add(((Union) union1.getLeftArg()).getLeftArg());
        unionArgs.add(((Union) union1.getLeftArg()).getRightArg());
        unionArgs.add(union1.getRightArg());
    } else {
        Assert.assertTrue(union1.getRightArg() instanceof Union);
        unionArgs.add(union1.getLeftArg());
        unionArgs.add(((Union) union1.getRightArg()).getLeftArg());
        unionArgs.add(((Union) union1.getRightArg()).getRightArg());
    }
    // There should be one StatementPattern and two joins with structure Join(FSP, SP):
    final StatementPattern directSP = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", vertebrate));
    StatementPattern actualSP = null;
    FixedStatementPattern hasCharacteristicFSP = null;
    FixedStatementPattern belongsToFSP = null;
    for (TupleExpr arg : unionArgs) {
        if (arg instanceof StatementPattern) {
            actualSP = (StatementPattern) arg;
        } else {
            Assert.assertTrue(arg instanceof Join);
            final Join join = (Join) arg;
            Assert.assertTrue(join.getLeftArg() instanceof FixedStatementPattern);
            Assert.assertTrue(join.getRightArg() instanceof StatementPattern);
            final FixedStatementPattern fsp = (FixedStatementPattern) join.getLeftArg();
            final StatementPattern sp = (StatementPattern) join.getRightArg();
            // Should join FSP([unused], property, ?value) with SP(subject, property, ?value)
            Assert.assertEquals(directSP.getSubjectVar(), sp.getSubjectVar());
            Assert.assertEquals(fsp.getPredicateVar(), sp.getPredicateVar());
            Assert.assertEquals(fsp.getObjectVar(), sp.getObjectVar());
            if (hasCharacteristic.equals(fsp.getPredicateVar().getValue())) {
                hasCharacteristicFSP = fsp;
            } else if (belongsTo.equals(fsp.getPredicateVar().getValue())) {
                belongsToFSP = fsp;
            } else {
                Assert.fail("Unexpected property variable in rewritten query: " + fsp.getPredicateVar());
            }
        }
    }
    Assert.assertEquals(directSP, actualSP);
    Assert.assertNotNull(hasCharacteristicFSP);
    Assert.assertNotNull(belongsToFSP);
    // Verify the expected FSPs for the appropriate properties:
    Assert.assertEquals(2, hasCharacteristicFSP.statements.size());
    Assert.assertTrue(hasCharacteristicFSP.statements.contains(vf.createStatement(vertebrate, hasCharacteristic, skull)));
    Assert.assertTrue(hasCharacteristicFSP.statements.contains(vf.createStatement(vertebrate, hasCharacteristic, notochord)));
    Assert.assertEquals(1, belongsToFSP.statements.size());
    Assert.assertTrue(belongsToFSP.statements.contains(vf.createStatement(vertebrate, belongsTo, chordata)));
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) HashSet(java.util.HashSet) Set(java.util.Set) HasValueVisitor(org.apache.rya.rdftriplestore.inference.HasValueVisitor) HashMap(java.util.HashMap) Var(org.openrdf.query.algebra.Var) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) URI(org.openrdf.model.URI) Union(org.openrdf.query.algebra.Union) TupleExpr(org.openrdf.query.algebra.TupleExpr) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) InferenceEngine(org.apache.rya.rdftriplestore.inference.InferenceEngine) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

InferenceEngine (org.apache.rya.rdftriplestore.inference.InferenceEngine)9 RdfCloudTripleStore (org.apache.rya.rdftriplestore.RdfCloudTripleStore)5 URI (org.openrdf.model.URI)5 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)4 Test (org.junit.Test)4 Set (java.util.Set)3 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)3 StatementPattern (org.openrdf.query.algebra.StatementPattern)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ZooKeeperInstance (org.apache.accumulo.core.client.ZooKeeperInstance)2 AccumuloRyaDAO (org.apache.rya.accumulo.AccumuloRyaDAO)2 RdfCloudTripleStoreConfiguration (org.apache.rya.api.RdfCloudTripleStoreConfiguration)2 TablePrefixLayoutStrategy (org.apache.rya.api.layout.TablePrefixLayoutStrategy)2 MongoDBRdfConfiguration (org.apache.rya.mongodb.MongoDBRdfConfiguration)2 StatefulMongoDBRdfConfiguration (org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration)2 HasValueVisitor (org.apache.rya.rdftriplestore.inference.HasValueVisitor)2 Resource (org.openrdf.model.Resource)2 Statement (org.openrdf.model.Statement)2