Search in sources :

Example 36 with URI

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();
    }
}
Also used : HashMap(java.util.HashMap) ReduceFeeder(org.apache.hadoop.mrunit.mapreduce.ReduceFeeder) URI(org.openrdf.model.URI) ReduceDriver(org.apache.hadoop.mrunit.mapreduce.ReduceDriver) Pair(org.apache.hadoop.mrunit.types.Pair) MapDriver(org.apache.hadoop.mrunit.mapreduce.MapDriver) KeyValueReuseList(org.apache.hadoop.mrunit.types.KeyValueReuseList) Fact(org.apache.rya.reasoning.Fact) NullWritable(org.apache.hadoop.io.NullWritable) HashMap(java.util.HashMap) Map(java.util.Map) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with URI

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);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 38 with URI

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);
}
Also used : Value(org.openrdf.model.Value) URI(org.openrdf.model.URI)

Example 39 with URI

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)));
    }
}
Also used : ContextStatementImpl(org.openrdf.model.impl.ContextStatementImpl) Statement(org.openrdf.model.Statement) RdfToRyaConversions.convertStatement(org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Resource(org.openrdf.model.Resource) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI) Value(org.openrdf.model.Value) LinearRing(com.vividsolutions.jts.geom.LinearRing) Polygon(com.vividsolutions.jts.geom.Polygon) PackedCoordinateSequence(com.vividsolutions.jts.geom.impl.PackedCoordinateSequence)

Example 40 with URI

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));
    }
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) Statement(org.openrdf.model.Statement) RdfToRyaConversions.convertStatement(org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement) StatementImpl(org.openrdf.model.impl.StatementImpl) ContextStatementImpl(org.openrdf.model.impl.ContextStatementImpl) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Value(org.openrdf.model.Value) ValueFactory(org.openrdf.model.ValueFactory) Point(com.vividsolutions.jts.geom.Point) URI(org.openrdf.model.URI) Test(org.junit.Test)

Aggregations

URI (org.openrdf.model.URI)368 Test (org.junit.Test)138 Value (org.openrdf.model.Value)122 Resource (org.openrdf.model.Resource)110 Statement (org.openrdf.model.Statement)96 ValueFactory (org.openrdf.model.ValueFactory)83 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)67 URIImpl (org.openrdf.model.impl.URIImpl)58 HashSet (java.util.HashSet)36 RyaURI (org.apache.rya.api.domain.RyaURI)34 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)34 Literal (org.openrdf.model.Literal)34 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)34 StatementImpl (org.openrdf.model.impl.StatementImpl)32 LiteralImpl (org.openrdf.model.impl.LiteralImpl)29 Var (org.openrdf.query.algebra.Var)28 ArrayList (java.util.ArrayList)25 SailConnection (org.openrdf.sail.SailConnection)25 LinearRing (com.vividsolutions.jts.geom.LinearRing)24 Polygon (com.vividsolutions.jts.geom.Polygon)24