use of org.openrdf.model.URI in project incubator-rya by apache.
the class ForwardChainTest method testTransitiveChain.
/**
* MultipleOutputs support is minimal, so we have to check each map/reduce
* step explicitly
*/
@Test
public void testTransitiveChain() throws Exception {
int max = 8;
int n = 4;
URI prop = TestUtils.uri("subOrganizationOf");
Map<Integer, Map<Integer, Pair<Fact, NullWritable>>> connections = new HashMap<>();
for (int i = 0; i <= max; i++) {
connections.put(i, new HashMap<Integer, Pair<Fact, NullWritable>>());
}
// Initial input: make a chain from org0 to org8
for (int i = 0; i < max; i++) {
URI orgI = TestUtils.uri("org" + i);
URI orgJ = TestUtils.uri("org" + (i + 1));
Fact triple = new Fact(orgI, prop, orgJ);
connections.get(i).put(i + 1, new Pair<>(triple, NullWritable.get()));
}
for (int i = 1; i <= n; i++) {
// Map:
MapDriver<Fact, NullWritable, ResourceWritable, Fact> mDriver = new MapDriver<>();
mDriver.getConfiguration().setInt(MRReasoningUtils.STEP_PROP, i);
mDriver.setMapper(new ForwardChain.FileMapper(schema));
for (int j : connections.keySet()) {
for (int k : connections.get(j).keySet()) {
mDriver.addInput(connections.get(j).get(k));
}
}
List<Pair<ResourceWritable, Fact>> mapped = mDriver.run();
// Convert data for reduce phase:
ReduceFeeder<ResourceWritable, Fact> feeder = new ReduceFeeder<>(mDriver.getConfiguration());
List<KeyValueReuseList<ResourceWritable, Fact>> intermediate = feeder.sortAndGroup(mapped, new ResourceWritable.SecondaryComparator(), new ResourceWritable.PrimaryComparator());
// Reduce, and compare to expected output:
ReduceDriver<ResourceWritable, Fact, Fact, NullWritable> rDriver = new ReduceDriver<>();
rDriver.getConfiguration().setInt(MRReasoningUtils.STEP_PROP, i);
rDriver.setReducer(new ForwardChain.ReasoningReducer(schema));
rDriver.addAllElements(intermediate);
int maxSpan = (int) Math.pow(2, i);
int minSpan = (maxSpan / 2) + 1;
// For each j, build all paths starting with j:
for (int j = 0; j < max; j++) {
// This includes any path of length k for appropriate k:
for (int k = minSpan; k <= maxSpan && j + k <= max; k++) {
int middle = j + minSpan - 1;
URI left = TestUtils.uri("org" + j);
URI right = TestUtils.uri("org" + (j + k));
Fact triple = new Fact(left, prop, right, i, OwlRule.PRP_TRP, TestUtils.uri("org" + middle));
triple.addSource(connections.get(j).get(middle).getFirst());
triple.addSource(connections.get(middle).get(j + k).getFirst());
Pair<Fact, NullWritable> expected = new Pair<>(triple, NullWritable.get());
connections.get(j).put(j + k, expected);
rDriver.addMultiOutput("intermediate", expected);
}
}
rDriver.runTest();
}
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class PipelineQueryIT method testFilterQuery.
@Test
public void testFilterQuery() throws Exception {
// Insert data
URI alice = VF.createURI("urn:Alice");
URI bob = VF.createURI("urn:Bob");
URI eve = VF.createURI("urn:Eve");
URI relatedTo = VF.createURI("urn:relatedTo");
insert(alice, FOAF.KNOWS, bob);
insert(alice, FOAF.KNOWS, alice);
insert(alice, FOAF.KNOWS, eve);
insert(alice, relatedTo, bob);
insert(bob, FOAF.KNOWS, eve);
insert(bob, relatedTo, bob);
dao.flush();
// Define query 1 and expected results
final String query1 = "SELECT * WHERE {\n" + " ?x <" + FOAF.KNOWS.stringValue() + "> ?y1 .\n" + " ?x <" + relatedTo.stringValue() + "> ?y2 .\n" + " FILTER (?y1 != ?y2) .\n" + "}";
final List<String> varNames = Arrays.asList("x", "y1", "y2");
final Multiset<BindingSet> expected1 = HashMultiset.create();
expected1.add(new ListBindingSet(varNames, alice, alice, bob));
expected1.add(new ListBindingSet(varNames, alice, eve, bob));
expected1.add(new ListBindingSet(varNames, bob, eve, bob));
// Define query 2 and expected results
final String query2 = "SELECT * WHERE {\n" + " ?x <" + FOAF.KNOWS.stringValue() + "> ?y1 .\n" + " ?x <" + relatedTo.stringValue() + "> ?y2 .\n" + " FILTER (?y1 = ?y2) .\n" + "}";
final Multiset<BindingSet> expected2 = HashMultiset.create();
expected2.add(new ListBindingSet(varNames, alice, bob, bob));
// Execute and verify results
testPipelineQuery(query1, expected1);
testPipelineQuery(query2, expected2);
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class TemporalTupleSet method evaluate.
/**
* Returns an iterator over the result set associated with contained IndexingExpr.
* <p>
* Should be thread-safe (concurrent invocation {@link OfflineIterable} this
* method can be expected with some query evaluators.
*/
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final BindingSet bindings) throws QueryEvaluationException {
final URI funcURI = filterInfo.getFunction();
final SearchFunction searchFunction = new TemporalSearchFunctionFactory(conf, temporalIndexer).getSearchFunction(funcURI);
if (filterInfo.getArguments().length > 1) {
throw new IllegalArgumentException("Index functions do not support more than two arguments.");
}
final String queryText = ((Value) filterInfo.getArguments()[0]).stringValue();
return IteratorFactory.getIterator(filterInfo.getSpConstraint(), bindings, queryText, searchFunction);
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class GeoIndexerTest method testAntiMeridianSearch.
// @Test
public void testAntiMeridianSearch() throws Exception {
// verify that a search works if the bounding box crosses the anti meridian
try (final GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) {
f.setConf(conf);
final ValueFactory vf = new ValueFactoryImpl();
final Resource context = vf.createURI("foo:context");
final Resource subjectEast = vf.createURI("foo:subj:east");
final URI predicateEast = GeoConstants.GEO_AS_WKT;
final Value objectEast = vf.createLiteral("Point(179 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
final Statement statementEast = new ContextStatementImpl(subjectEast, predicateEast, objectEast, context);
f.storeStatement(convertStatement(statementEast));
final Resource subjectWest = vf.createURI("foo:subj:west");
final URI predicateWest = GeoConstants.GEO_AS_WKT;
final Value objectWest = vf.createLiteral("Point(-179 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
final Statement statementWest = new ContextStatementImpl(subjectWest, predicateWest, objectWest, context);
f.storeStatement(convertStatement(statementWest));
f.flush();
final double[] ONE = { 178.1, 1, -178, 1, -178, -1, 178.1, -1, 178.1, 1 };
final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(ONE, 2));
final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {});
Assert.assertEquals(Sets.newHashSet(statementEast, statementWest), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS)));
}
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class GeoIndexerTest method testRestrictPredicatesSearch.
@Test
public void testRestrictPredicatesSearch() throws Exception {
conf.setStrings(ConfigUtils.GEO_PREDICATES_LIST, "pred:1,pred:2");
try (final GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) {
f.setConf(conf);
final ValueFactory vf = new ValueFactoryImpl();
final Point point = gf.createPoint(new Coordinate(10, 10));
final Value pointValue = vf.createLiteral("Point(10 10)", GeoConstants.XMLSCHEMA_OGC_WKT);
final URI invalidPredicate = GeoConstants.GEO_AS_WKT;
// These should not be stored because they are not in the predicate list
f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj1"), invalidPredicate, pointValue)));
f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj2"), invalidPredicate, pointValue)));
final URI pred1 = vf.createURI("pred:1");
final URI pred2 = vf.createURI("pred:2");
// These should be stored because they are in the predicate list
final Statement s3 = new StatementImpl(vf.createURI("foo:subj3"), pred1, pointValue);
final Statement s4 = new StatementImpl(vf.createURI("foo:subj4"), pred2, pointValue);
f.storeStatement(convertStatement(s3));
f.storeStatement(convertStatement(s4));
// This should not be stored because the object is not valid wkt
f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj5"), pred1, vf.createLiteral("soint(10 10)"))));
// This should not be stored because the object is not a literal
f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj6"), pred1, vf.createURI("p:Point(10 10)"))));
f.flush();
final Set<Statement> actual = getSet(f.queryEquals(point, EMPTY_CONSTRAINTS));
Assert.assertEquals(2, actual.size());
Assert.assertTrue(actual.contains(s3));
Assert.assertTrue(actual.contains(s4));
}
}
Aggregations