Java Code Examples for org.eclipse.rdf4j.query.impl.MapBindingSet#addBinding()

The following examples show how to use org.eclipse.rdf4j.query.impl.MapBindingSet#addBinding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatementOutputFormatterTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void unaryResult() {
    // Create the input binding set.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final MapBindingSet bindingSet = new MapBindingSet();
    bindingSet.addBinding("subject", vf.createIRI("urn:Alice"));
    bindingSet.addBinding("predicate", vf.createIRI("urn:age"));
    bindingSet.addBinding("object", vf.createLiteral(34));
    final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a");

    // Mock the processor context that will be invoked.
    final ProcessorContext context = mock(ProcessorContext.class);

    // Run the test.
    final StatementOutputFormatter formatter = new StatementOutputFormatter();
    formatter.init(context);
    formatter.process("key", ProcessorResult.make(new UnaryResult(visBs)));

    // Verify the mock was invoked with the expected output.
    final VisibilityStatement expectedStmt = new VisibilityStatement(vf.createStatement(
            vf.createIRI("urn:Alice"),
            vf.createIRI("urn:age"),
            vf.createLiteral(34)), "a");
    verify(context, times(1)).forward(eq("key"), eq(expectedStmt));
}
 
Example 2
Source File: ZeroLengthPathIterationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Verify that evaluation of a {@link ZeroLengthPathIteration} does not discard input bindings.
 *
 * @see https://github.com/eclipse/rdf4j/issues/689
 */
@Test
public void testRetainInputBindings() {

	MapBindingSet bindings = new MapBindingSet();
	bindings.addBinding("a", RDF.FIRST);

	Var subjectVar = new Var("x");
	Var objVar = new Var("y");
	try (ZeroLengthPathIteration zlp = new ZeroLengthPathIteration(evaluator, subjectVar, objVar, null, null, null,
			bindings)) {
		BindingSet result = zlp.getNextElement();

		assertTrue("zlp evaluation should have retained unrelated input binding", result.hasBinding("a"));
		assertTrue("zlp evaluation should binding for subject var", result.hasBinding("x"));
		assertTrue("zlp evaluation should binding for object var", result.hasBinding("y"));
	}
}
 
Example 3
Source File: KafkaExportIT.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void sum() throws Exception {
    // A query that sums the counts of all of the items that are in the inventory.
    final String sparql =
            "SELECT (sum(?count) as ?itemSum) { " +
                "?item <urn:count> ?count . " +
            "}";

    // Create the Statements that will be loaded into Rya.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Collection<Statement> statements = Sets.newHashSet(
            vf.createStatement(vf.createIRI("urn:apple"), vf.createIRI("urn:count"), vf.createLiteral(5)),
            vf.createStatement(vf.createIRI("urn:gum"), vf.createIRI("urn:count"), vf.createLiteral(7)),
            vf.createStatement(vf.createIRI("urn:sandwich"), vf.createIRI("urn:count"), vf.createLiteral(2)));

    // Create the PCJ in Fluo and load the statements into Rya.
    final String pcjId = loadDataAndCreateQuery(sparql, statements);

    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final MapBindingSet expectedResult = new MapBindingSet();
    expectedResult.addBinding("itemSum", vf.createLiteral("14", XMLSchema.INTEGER));

    // Ensure the last result matches the expected result.
    final VisibilityBindingSet result = readLastResult(pcjId);
    assertEquals(expectedResult, result);
}
 
Example 4
Source File: KafkaExportIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void count() throws Exception {
    // A query that counts the number of unique items that are in the inventory.
    final String sparql =
            "SELECT (count(?item) as ?itemCount) { " +
                "?item <urn:id> ?id . " +
            "}";

    // Create the Statements that will be loaded into Rya.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Collection<Statement> statements = Sets.newHashSet(
            // Three that are part of the count.
            vf.createStatement(vf.createIRI("urn:apple"), vf.createIRI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())),
            vf.createStatement(vf.createIRI("urn:gum"), vf.createIRI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())),
            vf.createStatement(vf.createIRI("urn:sandwich"), vf.createIRI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())),

            // One that is not.
            vf.createStatement(vf.createIRI("urn:sandwich"), vf.createIRI("urn:price"), vf.createLiteral(3.99)));

    // Create the PCJ in Fluo and load the statements into Rya.
    final String pcjId = loadDataAndCreateQuery(sparql, statements);

    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final MapBindingSet expectedResult = new MapBindingSet();
    expectedResult.addBinding("itemCount", vf.createLiteral("3", XMLSchema.INTEGER));

    // Ensure the last result matches the expected result.
    final VisibilityBindingSet result = readLastResult(pcjId);
    assertEquals(expectedResult, result);
}
 
Example 5
Source File: BindingSetStringConverterTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void fromString_Boolean() throws BindingSetConversionException {
    // Setup the String that will be converted.
    final String bindingSetString = "true<<~>>http://www.w3.org/2001/XMLSchema#boolean";

    // Convert it to a BindingSet
    final BindingSetConverter<String> converter = new BindingSetStringConverter();
    final BindingSet bindingSet = converter.convert(bindingSetString, new VariableOrder("x"));

    // Ensure it converted to the expected result.
    final MapBindingSet expected = new MapBindingSet();
    expected.addBinding("x", VF.createLiteral((true)));

    assertEquals(expected, bindingSet);
}
 
Example 6
Source File: ProjectionProcessorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void showProjectionFunctionIsCalled() throws Exception {
    // A query whose projection does not change anything.
    final Projection projection = RdfTestUtil.getProjection(
            "SELECT (?person AS ?p) (?employee AS ?e) ?business " +
            "WHERE { " +
                "?person <urn:talksTo> ?employee . " +
                "?employee <urn:worksAt> ?business . " +
            "}");

    // Create a Binding Set that contains the result of the WHERE clause.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final MapBindingSet inputBs = new MapBindingSet();
    inputBs.addBinding("person", vf.createIRI("urn:Alice"));
    inputBs.addBinding("employee", vf.createIRI("urn:Bob"));
    inputBs.addBinding("business", vf.createIRI("urn:TacoJoint"));
    final VisibilityBindingSet inputVisBs = new VisibilityBindingSet(inputBs, "a");

    // The expected binding set changes the "person" binding name to "p" and "employee" to "e".
    final MapBindingSet expectedBs = new MapBindingSet();
    expectedBs.addBinding("p", vf.createIRI("urn:Alice"));
    expectedBs.addBinding("e", vf.createIRI("urn:Bob"));
    expectedBs.addBinding("business", vf.createIRI("urn:TacoJoint"));
    final VisibilityBindingSet expectedVisBs = new VisibilityBindingSet(expectedBs, "a");

    // Show it resulted in the correct output BindingSet.
    final ProcessorContext context = mock(ProcessorContext.class);
    final ProjectionProcessor processor = new ProjectionProcessor(
            ProjectionEvaluator.make(projection),
            result -> ProcessorResult.make(new UnaryResult(result)));
    processor.init(context);

    processor.process("key", ProcessorResult.make(new UnaryResult(inputVisBs)));

    // Verify the expected binding set was emitted.
    final ProcessorResult expected = ProcessorResult.make(new UnaryResult(expectedVisBs));
    verify(context, times(1)).forward(eq("key"), eq(expected));
}
 
Example 7
Source File: VisibilityBindingSetKafkaIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void readAndWrite() throws Exception {
    // Create the object that will be written to the topic.
    final ValueFactory vf = SimpleValueFactory.getInstance();

    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("urn:name", vf.createIRI("urn:alice"));
    bs.addBinding("urn:age", vf.createLiteral(32));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b|c");

    // Write a VisibilityBindingSet to the test topic.
    try(Producer<String, VisibilityBindingSet> producer = KafkaTestUtil.makeProducer(
            kafka, StringSerializer.class, VisibilityBindingSetSerializer.class)) {
        producer.send( new ProducerRecord<String, VisibilityBindingSet>(kafka.getKafkaTopicName(), original) );
    }

    // Read a VisibilityBindingSet from the test topic.
    try(Consumer<String, VisibilityBindingSet> consumer = KafkaTestUtil.fromStartConsumer(
            kafka, StringDeserializer.class, VisibilityBindingSetDeserializer.class)) {
        // Register the topic.
        consumer.subscribe(Arrays.asList(kafka.getKafkaTopicName()));

        // Poll for the result.
        final List<VisibilityBindingSet> results = KafkaTestUtil.pollForResults(500, 6, 1, consumer);

        // Show the written statement matches the read one.
        final VisibilityBindingSet read = results.iterator().next();
        assertEquals(original, read);
    }
}
 
Example 8
Source File: GeoRelationQuerySpecTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testReplaceQueryPatternsWithNonEmptyResults() {
	final String expectedQueryPlan = "Projection\n" +
			"   ProjectionElemList\n" +
			"      ProjectionElem \"matchUri\"\n" +
			"      ProjectionElem \"match\"\n" +
			"   BindingSetAssignment ([[toUri=urn:subject4;to=\"POLYGON ((2.3294 48.8726, 2.2719 48.8643, 2.3370 48.8398, 2.3294 48.8726))\"^^<http://www.opengis.net/ont/geosparql#wktLiteral>]])\n";
	final ParsedQuery query = parseQuery(QUERY);

	final List<SearchQueryEvaluator> queries = new ArrayList<>();

	new GeoRelationQuerySpecBuilder(new SearchIndexImpl())
			.process(query.getTupleExpr(), EmptyBindingSet.getInstance(), queries);
	assertEquals(1, queries.size());

	final GeoRelationQuerySpec querySpec = (GeoRelationQuerySpec) queries.get(0);

	final MapBindingSet bindingSet = new MapBindingSet();
	bindingSet.addBinding("toUri", VF.createIRI("urn:subject4"));
	bindingSet.addBinding("to", VF.createLiteral(
			"POLYGON ((2.3294 48.8726, 2.2719 48.8643, 2.3370 48.8398, 2.3294 48.8726))", GEO.WKT_LITERAL));

	BindingSetAssignment bsa = new BindingSetAssignment();
	bsa.setBindingSets(Collections.singletonList(bindingSet));

	querySpec.replaceQueryPatternsWithResults(bsa);
	String result = querySpec.getParentQueryModelNode().getParentNode().toString().replaceAll("\r\n|\r", "\n");
	assertEquals(expectedQueryPlan, result);
}
 
Example 9
Source File: MongoGeoTemporalIndexIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void constantSubjQuery_Test() throws Exception {
    final Sail sail = GeoRyaSailFactory.getInstance(conf);
    final SailRepositoryConnection conn = new SailRepository(sail).getConnection();

    try {
        addStatements(conn);

        final String query =
                "PREFIX time: <http://www.w3.org/2006/time#> \n"
                        + "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"
                        + "PREFIX geo: <http://www.opengis.net/ont/geosparql#>"
                        + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>"
                        + "SELECT * "
                        + "WHERE { "
                        + "  <urn:event1> time:atTime ?time . "
                        + "  <urn:event1> geo:asWKT ?point . "
                        + "  FILTER(geof:sfWithin(?point, \"POLYGON((-3 -2, -3 2, 1 2, 1 -2, -3 -2))\"^^geo:wktLiteral)) "
                        + "  FILTER(tempo:equals(?time, \"2015-12-30T12:00:00Z\")) "
                        + "}";

        final TupleQueryResult rez = conn.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate();
        final Set<BindingSet> results = new HashSet<>();
        while(rez.hasNext()) {
            final BindingSet bs = rez.next();
            results.add(bs);
        }
        final MapBindingSet expected = new MapBindingSet();
        expected.addBinding("point", VF.createLiteral("POINT (0 0)"));
        expected.addBinding("time", VF.createLiteral("2015-12-30T12:00:00Z"));

        assertEquals(1, results.size());
        assertEquals(expected, results.iterator().next());
    } finally {
        conn.close();
        sail.shutDown();
    }
}
 
Example 10
Source File: BindingSetStringConverterTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void fromString_Decimal() throws BindingSetConversionException {
    // Setup the String that will be converted.
    final String bindingSetString = "2.5<<~>>http://www.w3.org/2001/XMLSchema#decimal";

    // Convert it to a BindingSet
    final BindingSetConverter<String> converter = new BindingSetStringConverter();
    final BindingSet bindingSet = converter.convert(bindingSetString, new VariableOrder("x"));

    // Ensure it converted to the expected result.
    final MapBindingSet expected = new MapBindingSet();
    expected.addBinding("x", VF.createLiteral((new BigDecimal(2.5))));

    assertEquals(expected, bindingSet);
}
 
Example 11
Source File: NaturalJoinTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void newLeftResult_noRightMatches() {
    final IterativeJoin naturalJoin = new NaturalJoin();

    // There is a new left result.
    final MapBindingSet newLeftResult = new MapBindingSet();
    newLeftResult.addBinding("name", VF.createLiteral("Bob"));

    // There are no right results that join with the left result.
    final Iterator<VisibilityBindingSet> rightResults= new ArrayList<VisibilityBindingSet>().iterator();

    // Therefore, the left result is a new join result.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = naturalJoin.newLeftResult(new VisibilityBindingSet(newLeftResult), rightResults);
    assertFalse( newJoinResultsIt.hasNext() );
}
 
Example 12
Source File: LeftOuterJoinTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void newRightResult_noLeftMatches() {
    final IterativeJoin leftOuterJoin = new LeftOuterJoin();

    // There are no left results.
    final Iterator<VisibilityBindingSet> leftResults= new ArrayList<VisibilityBindingSet>().iterator();

    // There is a new right result.
    final MapBindingSet newRightResult = new MapBindingSet();
    newRightResult.addBinding("name", VF.createLiteral("Bob"));

    // Therefore, there are no new join results.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = leftOuterJoin.newRightResult(leftResults, new VisibilityBindingSet(newRightResult));
    assertFalse( newJoinResultsIt.hasNext() );
}
 
Example 13
Source File: AverageFunction.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) {
    checkArgument(aggregation.getAggregationType() == AggregationType.AVERAGE, "The AverageFunction only accepts AVERAGE AggregationElements.");
    requireNonNull(state);
    requireNonNull(childBindingSet);

    // Only update the average if the child contains the binding that we are averaging.
    final String aggregatedName = aggregation.getAggregatedBindingName();
    if(childBindingSet.hasBinding(aggregatedName)) {
        final MapBindingSet result = state.getBindingSet();
        final String resultName = aggregation.getResultBindingName();
        final boolean newBinding = !result.hasBinding(resultName);

        // Get the state of the average.
        final Map<String, AverageState> averageStates = state.getAverageStates();
        AverageState averageState = newBinding ? new AverageState() : averageStates.get(resultName);

        // Update the state of the average.
        final Value childValue = childBindingSet.getValue(aggregatedName);
        if(childValue instanceof Literal) {
            final Literal childLiteral = (Literal) childValue;
            if (childLiteral.getDatatype() != null && XMLDatatypeUtil.isNumericDatatype(childLiteral.getDatatype())) {
                try {
                    // Update the sum.
                    final Literal oldSum = VF.createLiteral(averageState.getSum());
                    final BigDecimal sum = MathUtil.compute(oldSum, childLiteral, MathOp.PLUS).decimalValue();

                    // Update the count.
                    final BigInteger count = averageState.getCount().add( BigInteger.ONE );

                    // Update the BindingSet to include the new average.
                    final Literal sumLiteral = VF.createLiteral(sum);
                    final Literal countLiteral = VF.createLiteral(count);
                    final Literal average = MathUtil.compute(sumLiteral, countLiteral, MathOp.DIVIDE);
                    result.addBinding(resultName, average);

                    // Update the average state that is stored.
                    averageState = new AverageState(sum, count);
                    averageStates.put(resultName, averageState);
                } catch (final ValueExprEvaluationException e) {
                    log.error("A problem was encountered while updating an Average Aggregation. This binding set will be ignored: " + childBindingSet);
                    return;
                }
            }
        }
    }
}
 
Example 14
Source File: QueryIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void dateTimeWithin() throws Exception {

    final ValueFactory vf = SimpleValueFactory.getInstance();
    final DatatypeFactory dtf = DatatypeFactory.newInstance();
    FunctionRegistry.getInstance().add(new DateTimeWithinPeriod());

    final String sparql = "PREFIX fn: <" + FN.NAMESPACE +">"
            + "SELECT ?event ?startTime ?endTime WHERE { ?event <uri:startTime> ?startTime; <uri:endTime> ?endTime. "
            + "FILTER(fn:dateTimeWithin(?startTime, ?endTime, 2,<" + OWLTime.HOURS_URI + "> ))}";

    final ZonedDateTime zTime = ZonedDateTime.now();
    final String time = zTime.format(DateTimeFormatter.ISO_INSTANT);

    final ZonedDateTime zTime1 = zTime.minusHours(1);
    final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);

    final ZonedDateTime zTime2 = zTime.minusHours(2);
    final String time2 = zTime2.format(DateTimeFormatter.ISO_INSTANT);

    final Literal lit = vf.createLiteral(dtf.newXMLGregorianCalendar(time));
    final Literal lit1 = vf.createLiteral(dtf.newXMLGregorianCalendar(time1));
    final Literal lit2 = vf.createLiteral(dtf.newXMLGregorianCalendar(time2));

    // Create the Statements that will be loaded into Rya.
    final Collection<Statement> statements = Sets.newHashSet(
            vf.createStatement(vf.createIRI("uri:event1"), vf.createIRI("uri:startTime"), lit),
            vf.createStatement(vf.createIRI("uri:event1"), vf.createIRI("uri:endTime"), lit1),
            vf.createStatement(vf.createIRI("uri:event2"), vf.createIRI("uri:startTime"), lit),
            vf.createStatement(vf.createIRI("uri:event2"), vf.createIRI("uri:endTime"), lit2)
           );

    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final Set<BindingSet> expectedResults = new HashSet<>();

    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("event", vf.createIRI("uri:event1"));
    bs.addBinding("startTime", lit);
    bs.addBinding("endTime", lit1);
    expectedResults.add(bs);

    // Verify the end results of the query match the expected results.
    runTest(sparql, statements, expectedResults, ExportStrategy.RYA);
}
 
Example 15
Source File: LeftOuterJoinTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void newRightResult_joinsWithLeftResults() {
    final IterativeJoin leftOuterJoin = new LeftOuterJoin();

    // There are a few left results that join with the new right result.
    final MapBindingSet nameAge = new MapBindingSet();
    nameAge.addBinding("name", VF.createLiteral("Bob"));
    nameAge.addBinding("age", VF.createLiteral(BigInteger.valueOf(56)));

    final MapBindingSet nameHair = new MapBindingSet();
    nameHair.addBinding("name", VF.createLiteral("Bob"));
    nameHair.addBinding("hairColor", VF.createLiteral("Brown"));

    final Iterator<VisibilityBindingSet> leftResults = Lists.newArrayList(
            new VisibilityBindingSet(nameAge),
            new VisibilityBindingSet(nameHair)).iterator();

    // There is a new right result.
    final MapBindingSet newRightResult = new MapBindingSet();
    newRightResult.addBinding("name", VF.createLiteral("Bob"));
    newRightResult.addBinding("height", VF.createLiteral("5'9\""));

    // Therefore, there are a few new join results that mix the two together.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = leftOuterJoin.newRightResult(leftResults, new VisibilityBindingSet(newRightResult));

    final Set<BindingSet> newJoinResults = new HashSet<>();
    while(newJoinResultsIt.hasNext()) {
        newJoinResults.add( newJoinResultsIt.next() );
    }

    final Set<BindingSet> expected = Sets.newHashSet();
    final MapBindingSet nameHeightAge = new MapBindingSet();
    nameHeightAge.addBinding("name", VF.createLiteral("Bob"));
    nameHeightAge.addBinding("height", VF.createLiteral("5'9\""));
    nameHeightAge.addBinding("age", VF.createLiteral(BigInteger.valueOf(56)));
    expected.add(new VisibilityBindingSet(nameHeightAge));

    final MapBindingSet nameHeightHair = new MapBindingSet();
    nameHeightHair.addBinding("name", VF.createLiteral("Bob"));
    nameHeightHair.addBinding("height", VF.createLiteral("5'9\""));
    nameHeightHair.addBinding("hairColor", VF.createLiteral("Brown"));
    expected.add(new VisibilityBindingSet(nameHeightHair));

    assertEquals(expected, newJoinResults);
}
 
Example 16
Source File: GeoFunctionsIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void withGeoFilters() throws Exception {
    final String sparql =
            "PREFIX geo: <http://www.opengis.net/ont/geosparql#> " +
            "PREFIX ryageo: <tag:rya.apache.org,2017:function/geo#> " +
            "PREFIX geof: <http://www.opengis.net/def/function/geosparql/> " +
            "SELECT ?feature ?point ?wkt {" +
                " ?feature a geo:Feature . " +
                " ?feature geo:hasGeometry ?point . " +
                " ?point a geo:Point . " +
                " ?point geo:asWKT ?wkt . " +
                " FILTER(ryageo:ehContains(?wkt, \"POLYGON((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " +
            "}";

    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Set<Statement> statements = Sets.newHashSet(
            vf.createStatement(vf.createIRI("tag:rya.apache.org,2017:ex#feature"), vf.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), vf.createIRI("http://www.opengis.net/ont/geosparql#Feature")),
            vf.createStatement(vf.createIRI("tag:rya.apache.org,2017:ex#feature"), vf.createIRI("http://www.opengis.net/ont/geosparql#hasGeometry"), vf.createIRI("tag:rya.apache.org,2017:ex#test_point")),
            vf.createStatement(vf.createIRI("tag:rya.apache.org,2017:ex#test_point"), vf.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), vf.createIRI("http://www.opengis.net/ont/geosparql#Point")),
            vf.createStatement(vf.createIRI("tag:rya.apache.org,2017:ex#test_point"), vf.createIRI("http://www.opengis.net/ont/geosparql#asWKT"), vf.createLiteral("Point(-77.03524 38.889468)", vf.createIRI("http://www.opengis.net/ont/geosparql#wktLiteral"))));

    // Create a Geo function.
    final Function geoFunction = new Function() {
        @Override
        public String getURI() {
            return "tag:rya.apache.org,2017:function/geo#ehContains";
        }

        @Override
        public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException {
            if (args.length != 2) {
                throw new ValueExprEvaluationException(getURI() + " requires exactly 3 arguments, got " + args.length);
            }
            return valueFactory.createLiteral(true);
        }
    };

    // Add our new function to the registry
    FunctionRegistry.getInstance().add(geoFunction);

    // The expected results of the SPARQL query once the PCJ has been computed.
    final Set<BindingSet> expectedResults = new HashSet<>();
    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("wkt", vf.createLiteral("Point(-77.03524 38.889468)", vf.createIRI("http://www.opengis.net/ont/geosparql#wktLiteral")));
    bs.addBinding("feature", vf.createIRI("tag:rya.apache.org,2017:ex#feature"));
    bs.addBinding("point", vf.createIRI("tag:rya.apache.org,2017:ex#test_point"));
    expectedResults.add(bs);

    runTest(sparql, statements, expectedResults);
}
 
Example 17
Source File: PcjTablesIT.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure when results are already stored in Rya, that we are able to populate
 * the PCJ table for a new SPARQL query using those results.
 * <p>
 * The method being tested is: {@link PcjTables#populatePcj(Connector, String, RepositoryConnection)}
 */
@Test
public void populatePcj() throws RepositoryException, PcjException, TableNotFoundException, BindingSetConversionException, AccumuloException, AccumuloSecurityException {
    // Load some Triples into Rya.
    final Set<Statement> triples = new HashSet<>();
    triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(14))) );
    triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(16))) );
    triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(12))) );
    triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
    triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(43))) );
    triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );

    for(final Statement triple : triples) {
        ryaConn.add(triple);
    }

    // Create a PCJ table that will include those triples in its results.
    final String sparql =
            "SELECT ?name ?age " +
            "{" +
              "FILTER(?age < 30) ." +
              "?name <http://hasAge> ?age." +
              "?name <http://playsSport> \"Soccer\" " +
            "}";

    final Connector accumuloConn = cluster.getConnector();

    final String pcjTableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), "testPcj");
    final Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
    final PcjTables pcjs = new PcjTables();
    pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);

    // Populate the PCJ table using a Rya connection.
    pcjs.populatePcj(accumuloConn, pcjTableName, ryaConn);

    // Scan Accumulo for the stored results.
    final Multimap<String, BindingSet> fetchedResults = loadPcjResults(accumuloConn, pcjTableName);

    // Make sure the cardinality was updated.
    final PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
    assertEquals(3, metadata.getCardinality());

    // Ensure the expected results match those that were stored.
    final MapBindingSet alice = new MapBindingSet();
    alice.addBinding("name", VF.createIRI("http://Alice"));
    alice.addBinding("age", VF.createLiteral(BigInteger.valueOf(14)));

    final MapBindingSet bob = new MapBindingSet();
    bob.addBinding("name", VF.createIRI("http://Bob"));
    bob.addBinding("age", VF.createLiteral(BigInteger.valueOf(16)));

    final MapBindingSet charlie = new MapBindingSet();
    charlie.addBinding("name", VF.createIRI("http://Charlie"));
    charlie.addBinding("age", VF.createLiteral(BigInteger.valueOf(12)));

    final Set<BindingSet> results = Sets.newHashSet(alice, bob, charlie);

    final Multimap<String, BindingSet> expectedResults = HashMultimap.create();
    expectedResults.putAll("name;age", results);
    expectedResults.putAll("age;name", results);
    assertEquals(expectedResults, fetchedResults);
}
 
Example 18
Source File: PcjDocumentsIntegrationTest.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure the method that creates a new PCJ table, scans Rya for matches, and
 * stores them in the PCJ table works.
 * <p>
 * The method being tested is: {@link PcjTables#createAndPopulatePcj(RepositoryConnection, Connector, String, String, String[], Optional)}
 */
@Test
public void createAndPopulatePcj() throws Exception {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    dao.setConf(new StatefulMongoDBRdfConfiguration(conf, getMongoClient()));
    dao.init();
    final RdfCloudTripleStore<StatefulMongoDBRdfConfiguration> ryaStore = new RdfCloudTripleStore<>();
    ryaStore.setRyaDAO(dao);
    ryaStore.initialize();
    final SailRepositoryConnection ryaConn = new RyaSailRepository(ryaStore).getConnection();
    ryaConn.begin();

    try {
        // Load some Triples into Rya.
        final Set<Statement> triples = new HashSet<>();
        triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(14))) );
        triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
        triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(16))) );
        triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
        triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(12))) );
        triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );
        triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(43))) );
        triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) );

        for(final Statement triple : triples) {
            ryaConn.add(triple);
        }

        // Create a PCJ table that will include those triples in its results.
        final String sparql =
                "SELECT ?name ?age " +
                        "{" +
                        "FILTER(?age < 30) ." +
                        "?name <http://hasAge> ?age." +
                        "?name <http://playsSport> \"Soccer\" " +
                        "}";

        final String pcjTableName = "testPcj";

        // Create and populate the PCJ table.
        final MongoPcjDocuments pcjs = new MongoPcjDocuments(getMongoClient(), conf.getRyaInstanceName());
        pcjs.createAndPopulatePcj(ryaConn, pcjTableName, sparql);

        // Make sure the cardinality was updated.
        final PcjMetadata metadata = pcjs.getPcjMetadata(pcjTableName);
        assertEquals(3, metadata.getCardinality());

        // Scan Accumulo for the stored results.
        final Collection<BindingSet> fetchedResults = loadPcjResults(pcjTableName);

        // Ensure the expected results match those that were stored.
        final MapBindingSet alice = new MapBindingSet();
        alice.addBinding("name", VF.createIRI("http://Alice"));
        alice.addBinding("age", VF.createLiteral(BigInteger.valueOf(14)));

        final MapBindingSet bob = new MapBindingSet();
        bob.addBinding("name", VF.createIRI("http://Bob"));
        bob.addBinding("age", VF.createLiteral(BigInteger.valueOf(16)));

        final MapBindingSet charlie = new MapBindingSet();
        charlie.addBinding("name", VF.createIRI("http://Charlie"));
        charlie.addBinding("age", VF.createLiteral(BigInteger.valueOf(12)));

        final Set<BindingSet> expected = Sets.<BindingSet>newHashSet(alice, bob, charlie);

        assertEquals(expected, fetchedResults);
    } finally {
        ryaConn.close();
        ryaStore.shutDown();
    }
}
 
Example 19
Source File: AggregationResultUpdater.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the results of an Aggregation node where its child has emitted a new Binding Set.
 *
 * @param tx - The transaction all Fluo queries will use. (not null)
 * @param childBindingSet - The Binding Set that was omitted by the Aggregation Node's child. (not null)
 * @param aggregationMetadata - The metadata of the Aggregation node whose results will be updated. (not null)
 * @throws Exception The update could not be successfully performed.
 */
public void updateAggregateResults(
        final TransactionBase tx,
        final VisibilityBindingSet childBindingSet,
        final AggregationMetadata aggregationMetadata) throws Exception {
    requireNonNull(tx);
    requireNonNull(childBindingSet);
    requireNonNull(aggregationMetadata);

    log.trace(
            "Transaction ID: " + tx.getStartTimestamp() + "\n" +
            "Child Binding Set:\n" + childBindingSet + "\n");

    // The Row ID for the Aggregation State that needs to be updated is defined by the Group By variables.
    final String aggregationNodeId = aggregationMetadata.getNodeId();
    final VariableOrder groupByVars = aggregationMetadata.getGroupByVariableOrder();
    final Bytes rowId = makeRowKey(aggregationNodeId, groupByVars, childBindingSet);

    // Load the old state from the bytes if one was found; otherwise initialize the state.
    final Optional<Bytes> stateBytes = Optional.ofNullable( tx.get(rowId, FluoQueryColumns.AGGREGATION_BINDING_SET) );

    final AggregationState state;
    if(stateBytes.isPresent()) {
        // Deserialize the old state
        final byte[] bytes = stateBytes.get().toArray();
        state = AGG_STATE_SERDE.deserialize(bytes);
    } else {
        // Initialize a new state.
        state = new AggregationState();

        // If we have group by bindings, their values need to be added to the state's binding set.
        final MapBindingSet bindingSet = state.getBindingSet();
        for(final String variable : aggregationMetadata.getGroupByVariableOrder()) {
            bindingSet.addBinding( childBindingSet.getBinding(variable) );
        }
    }

    log.trace(
            "Transaction ID: " + tx.getStartTimestamp() + "\n" +
            "Before Update: " + LogUtils.clean(state.getBindingSet().toString()) + "\n");

    // Update the visibilities of the result binding set based on the child's visibilities.
    final String oldVisibility = state.getVisibility();
    final String updateVisibilities = VisibilitySimplifier.unionAndSimplify(oldVisibility, childBindingSet.getVisibility());
    state.setVisibility(updateVisibilities);

    // Update the Aggregation State with each Aggregation function included within this group.
    for(final AggregationElement aggregation : aggregationMetadata.getAggregations()) {
        final AggregationType type = aggregation.getAggregationType();
        final AggregationFunction function = FUNCTIONS.get(type);
        if(function == null) {
            throw new RuntimeException("Unrecognized aggregation function: " + type);
        }

        function.update(aggregation, state, childBindingSet);
    }

    log.trace(
            "Transaction ID: " + tx.getStartTimestamp() + "\n" +
            "After Update:" + LogUtils.clean(state.getBindingSet().toString()) + "\n" );

    // Store the updated state. This will write on top of any old state that was present for the Group By values.
    tx.set(rowId, FluoQueryColumns.AGGREGATION_BINDING_SET, Bytes.of(AGG_STATE_SERDE.serialize(state)));
}
 
Example 20
Source File: AbstractQueryResultIOTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
protected TupleQueryResult createTupleSingleVarMultipleBindingSets() {
	List<String> bindingNames = Arrays.asList("a");

	MapBindingSet solution1 = new MapBindingSet(bindingNames.size());
	solution1.addBinding("a", vf.createIRI("foo:bar"));

	MapBindingSet solution2 = new MapBindingSet(bindingNames.size());
	solution2.addBinding("a", vf.createLiteral("2.0", XMLSchema.DOUBLE));

	MapBindingSet solution3 = new MapBindingSet(bindingNames.size());
	solution3.addBinding("a", vf.createBNode("bnode3"));

	MapBindingSet solution4 = new MapBindingSet(bindingNames.size());
	solution4.addBinding("a", vf.createLiteral("''single-quoted string", XMLSchema.STRING));

	MapBindingSet solution5 = new MapBindingSet(bindingNames.size());
	solution5.addBinding("a", vf.createLiteral("\"\"double-quoted string", XMLSchema.STRING));

	MapBindingSet solution6 = new MapBindingSet(bindingNames.size());
	solution6.addBinding("a", vf.createLiteral("space at the end         ", XMLSchema.STRING));

	MapBindingSet solution7 = new MapBindingSet(bindingNames.size());
	solution7.addBinding("a", vf.createLiteral("space at the end         ", XMLSchema.STRING));

	MapBindingSet solution8 = new MapBindingSet(bindingNames.size());
	solution8.addBinding("a", vf.createLiteral("\"\"double-quoted string with no datatype"));

	MapBindingSet solution9 = new MapBindingSet(bindingNames.size());
	solution9.addBinding("a", vf.createLiteral("newline at the end \n", XMLSchema.STRING));

	MapBindingSet solution10 = new MapBindingSet(bindingNames.size());
	solution10.addBinding("a", vf.createTriple(vf.createIRI("urn:a"), RDF.TYPE, vf.createIRI("urn:b")));

	List<? extends BindingSet> bindingSetList = Arrays.asList(solution1, solution2, solution3, solution4, solution5,
			solution6, solution7, solution8, solution9, solution10);

	IteratingTupleQueryResult result = new IteratingTupleQueryResult(bindingNames, bindingSetList);

	return result;
}