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);
}
}
}
}
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;
}
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"));
}
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)));
}
Aggregations