Java Code Examples for org.eclipse.rdf4j.model.ValueFactory#createStatement()

The following examples show how to use org.eclipse.rdf4j.model.ValueFactory#createStatement() . 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: StatementPatternMatcherTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void doesNotMatchSubject() throws Exception {
    // Create the matcher against a pattern that matches a specific subject.
    final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp(
            "SELECT * WHERE {" +
                "<urn:Alice> ?p ?o ." +
            "}"));

    // Create a statement that does not match the pattern.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Statement statement = vf.createStatement(vf.createIRI("urn:Charlie"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"), vf.createIRI("urn:testGraph"));

    // Show the statement did not match.
    final Optional<BindingSet> bs = matcher.match(statement);
    assertFalse(bs.isPresent());
}
 
Example 2
Source File: StatementPatternMatcherTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void variableContext_contextFreeStatement() throws Exception {
    // Create a matcher against a pattern that matches a variable context.
    final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp(
            "SELECT * WHERE {" +
                "GRAPH ?c {" +
                    "?s ?p ?o ." +
                "}" +
            "}"));

    // Create a statement that does not have a context value.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Statement statement = vf.createStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"));

    // Show the statement did not match.
    final Optional<BindingSet> bs = matcher.match(statement);
    assertFalse(bs.isPresent());
}
 
Example 3
Source File: VisibilityStatementSerdeTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeAndDeserialize() {
    // Create the object that will be serialized.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Statement statement = vf.createStatement(
            vf.createIRI("urn:person1"),
            vf.createIRI("urn:hasName"),
            vf.createLiteral("alice"),
            vf.createIRI("urn:testContext"));

    final VisibilityStatement original = new VisibilityStatement(statement, "a|b|c");

    // Serialize it.
    try(final Serde<VisibilityStatement> serde = new VisibilityStatementSerde()) {
        final byte[] bytes = serde.serializer().serialize("topic", original);

        // Deserialize it.
        final VisibilityStatement deserialized = serde.deserializer().deserialize("topic", bytes);

        // Show the deserialized value matches the original.
        assertEquals(original, deserialized);
    }
}
 
Example 4
Source File: StatementSerializerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleStatementObjectUri() throws Exception {
    ValueFactory vf = SimpleValueFactory.getInstance();
    Statement s;

    s = vf.createStatement(vf.createIRI("foo:subject"), vf.createIRI("foo:predicate"), vf.createIRI("foo:object"));
    Assert.assertEquals(s, StatementSerializer.readStatement(StatementSerializer.writeStatement(s)));

    s = vf.createStatement(vf.createIRI("foo:subject"), vf.createIRI("foo:predicate"), vf.createIRI("foo:object"),
            vf.createIRI("foo:context"));
    Assert.assertEquals(s, StatementSerializer.readStatement(StatementSerializer.writeStatement(s)));
}
 
Example 5
Source File: AccumuloTemporalIndexerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testStoreStatementsSameTime() throws IOException, NoSuchAlgorithmException, AccumuloException, AccumuloSecurityException, TableNotFoundException, TableExistsException {
    ValueFactory vf = SimpleValueFactory.getInstance();
    IRI pred1_atTime = vf.createIRI(URI_PROPERTY_AT_TIME);
    IRI pred2_circa = vf.createIRI(URI_PROPERTY_CIRCA);

    // These are the same datetime instant but from different time
    // zones.
    // This is an arbitrary zone, BRST=Brazil, better if not local.
    final String ZONETestDateInBRST = "2014-12-31T23:59:59-02:00";
    final String ZONETestDateInZulu = "2015-01-01T01:59:59Z";
    final String ZONETestDateInET = "2014-12-31T20:59:59-05:00";

    // These all should be stored because they are in the predicate list.
    // BUT they will get converted to the same exact datetime in UTC.
    // So we have to make the key distinct! Good luck indexer!
    Statement s1 = vf.createStatement(vf.createIRI("foo:subj1"), pred2_circa, vf.createLiteral(ZONETestDateInET));
    Statement s2 = vf.createStatement(vf.createIRI("foo:subj2"), pred1_atTime, vf.createLiteral(ZONETestDateInZulu));
    Statement s3 = vf.createStatement(vf.createIRI("foo:subj3"), pred1_atTime, vf.createLiteral(ZONETestDateInBRST));
    int rowsStoredExpected = 0;
    tIndexer.storeStatement(convertStatement(s1));
    rowsStoredExpected++;
    tIndexer.storeStatement(convertStatement(s2));
    rowsStoredExpected++;
    tIndexer.storeStatement(convertStatement(s3));
    rowsStoredExpected++;
    int rowsStoredActual = printTables("junit testing: Duplicate times stored", null /*System.out*/, null);
    Assert.assertEquals("Number of Duplicate times stored, 1 means duplicates not handled correctly.", rowsStoredExpected*4, rowsStoredActual);
}
 
Example 6
Source File: MongoTemporalIndexerIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelete() throws IOException, MongoException, TableNotFoundException, TableExistsException, NoSuchAlgorithmException {
    try(MongoTemporalIndexer tIndexer = new MongoTemporalIndexer()) {
        tIndexer.setConf(conf);
        tIndexer.init();

        final ValueFactory vf = SimpleValueFactory.getInstance();

        final IRI pred1_atTime = vf.createIRI(URI_PROPERTY_AT_TIME);
        final IRI pred2_circa = vf.createIRI(URI_PROPERTY_CIRCA);

        final String testDate2014InBRST = "2014-12-31T23:59:59-02:00";
        final String testDate2016InET = "2016-12-31T20:59:59-05:00";

        // These should be stored because they are in the predicate list.
        // BUT they will get converted to the same exact datetime in UTC.
        final Statement s1 = vf.createStatement(vf.createIRI("foo:subj3"), pred1_atTime, vf.createLiteral(testDate2014InBRST));
        final Statement s2 = vf.createStatement(vf.createIRI("foo:subj4"), pred2_circa, vf.createLiteral(testDate2016InET));
        tIndexer.storeStatement(convertStatement(s1));
        tIndexer.storeStatement(convertStatement(s2));

        final String dbName = conf.getMongoDBName();
        final MongoDatabase db = super.getMongoClient().getDatabase(dbName);
        final MongoCollection<Document> collection = db.getCollection(conf.get(MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX, "rya") + tIndexer.getCollectionName());

        printTables(tIndexer, "junit testing: Temporal entities stored in testDelete before delete");
        assertEquals("Number of rows stored.", 2, collection.countDocuments()); // 4 index entries per statement

        tIndexer.deleteStatement(convertStatement(s1));
        tIndexer.deleteStatement(convertStatement(s2));

        printTables(tIndexer, "junit testing: Temporal entities stored in testDelete after delete");
        assertEquals("Number of rows stored after delete.", 0, collection.countDocuments());
    }
}
 
Example 7
Source File: MongoGeoIndexerIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteSearch() throws Exception {
    // test a ring around dc
    try (final MongoGeoIndexer f = new MongoGeoIndexer()) {
        f.setConf(conf);
        f.init();

        final ValueFactory vf = SimpleValueFactory.getInstance();
        final Resource subject = vf.createIRI("foo:subj");
        final IRI predicate = GeoConstants.GEO_AS_WKT;
        final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Resource context = vf.createIRI("foo:context");

        final Statement statement = vf.createStatement(subject, predicate, object, context);
        f.storeStatement(convertStatement(statement));
        f.flush();

        f.deleteStatement(convertStatement(statement));

        // test a ring that the point would be inside of if not deleted
        final double[] in = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 };
        final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(in, 2));
        final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {});
        assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS)));

        // test a ring that the point would be outside of if not deleted
        final double[] out = { -77, 39, -76, 39, -76, 38, -77, 38, -77, 39 };
        final LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(out, 2));
        final Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {});
        assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS)));

        // test a ring for the whole world and make sure the point is gone
        // Geomesa is a little sensitive around lon 180, so we only go to 179
        final double[] world = { -180, 90, 179, 90, 179, -90, -180, -90, -180, 90 };
        final LinearRing rWorld = gf.createLinearRing(new PackedCoordinateSequence.Double(world, 2));
        final Polygon pWorld = gf.createPolygon(rWorld, new LinearRing[] {});
        assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pWorld, EMPTY_CONSTRAINTS)));
    }
}
 
Example 8
Source File: GeoWaveIndexerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testRestrictPredicatesSearch() throws Exception {
    conf.setStrings(ConfigUtils.GEO_PREDICATES_LIST, "pred:1,pred:2");
    try (final GeoWaveGeoIndexer f = new GeoWaveGeoIndexer()) {
        f.setConf(conf);
        f.purge(conf);

        final ValueFactory vf = SimpleValueFactory.getInstance();

        final Point point = gf.createPoint(new Coordinate(10, 10));
        final Value pointValue = vf.createLiteral("Point(10 10)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final IRI invalidPredicate = GeoConstants.GEO_AS_WKT;

        // These should not be stored because they are not in the predicate list
        f.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj1"), invalidPredicate, pointValue)));
        f.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj2"), invalidPredicate, pointValue)));

        final IRI pred1 = vf.createIRI("pred:1");
        final IRI pred2 = vf.createIRI("pred:2");

        // These should be stored because they are in the predicate list
        final Statement s3 = vf.createStatement(vf.createIRI("foo:subj3"), pred1, pointValue);
        final Statement s4 = vf.createStatement(vf.createIRI("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(vf.createStatement(vf.createIRI("foo:subj5"), pred1, vf.createLiteral("soint(10 10)"))));

        // This should not be stored because the object is not a literal
        f.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj6"), pred1, vf.createIRI("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));
    }
}
 
Example 9
Source File: MongoGeoIndexerIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testRestrictPredicatesSearch() throws Exception {
    try (final MongoGeoIndexer f = new MongoGeoIndexer()) {
        conf.setStrings(ConfigUtils.GEO_PREDICATES_LIST, "pred:1,pred:2");
        f.setConf(conf);
        f.init();

        final ValueFactory vf = SimpleValueFactory.getInstance();

        final Point point = gf.createPoint(new Coordinate(10, 10));
        final Value pointValue = vf.createLiteral("Point(10 10)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final IRI invalidPredicate = GeoConstants.GEO_AS_WKT;

        // These should not be stored because they are not in the predicate list
        f.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj1"), invalidPredicate, pointValue)));
        f.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj2"), invalidPredicate, pointValue)));

        final IRI pred1 = vf.createIRI("pred:1");
        final IRI pred2 = vf.createIRI("pred:2");

        // These should be stored because they are in the predicate list
        final Statement s3 = vf.createStatement(vf.createIRI("foo:subj3"), pred1, pointValue);
        final Statement s4 = vf.createStatement(vf.createIRI("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(vf.createStatement(vf.createIRI("foo:subj5"), pred1, vf.createLiteral("soint(10 10)"))));

        // This should not be stored because the object is not a literal
        f.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj6"), pred1, vf.createIRI("p:Point(10 10)"))));

        f.flush();

        final Set<Statement> actual = getSet(f.queryEquals(point, EMPTY_CONSTRAINTS));
        assertEquals(2, actual.size());
        assertTrue(actual.contains(s3));
        assertTrue(actual.contains(s4));
    }
}
 
Example 10
Source File: MongoGeoIndexerIT.java    From rya with Apache License 2.0 5 votes vote down vote up
public void testAntiMeridianSearch() throws Exception {
    // verify that a search works if the bounding box crosses the anti meridian
    try (final MongoGeoIndexer f = new MongoGeoIndexer()) {
        f.setConf(conf);
        f.init();

        final ValueFactory vf = SimpleValueFactory.getInstance();
        final Resource context = vf.createIRI("foo:context");

        final Resource subjectEast = vf.createIRI("foo:subj:east");
        final IRI predicateEast = GeoConstants.GEO_AS_WKT;
        final Value objectEast = vf.createLiteral("Point(179 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Statement statementEast = vf.createStatement(subjectEast, predicateEast, objectEast, context);
        f.storeStatement(convertStatement(statementEast));

        final Resource subjectWest = vf.createIRI("foo:subj:west");
        final IRI predicateWest = GeoConstants.GEO_AS_WKT;
        final Value objectWest = vf.createLiteral("Point(-179 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Statement statementWest = vf.createStatement(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[] {});

        assertEquals(Sets.newHashSet(statementEast, statementWest), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS)));
    }
}
 
Example 11
Source File: GeoWaveIndexerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
public void testAntiMeridianSearch() throws Exception {
    // verify that a search works if the bounding box crosses the anti meridian
    try (final GeoWaveGeoIndexer f = new GeoWaveGeoIndexer()) {
        f.setConf(conf);
        f.purge(conf);

        final ValueFactory vf = SimpleValueFactory.getInstance();
        final Resource context = vf.createIRI("foo:context");

        final Resource subjectEast = vf.createIRI("foo:subj:east");
        final IRI predicateEast = GeoConstants.GEO_AS_WKT;
        final Value objectEast = vf.createLiteral("Point(179 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Statement statementEast = vf.createStatement(subjectEast, predicateEast, objectEast, context);
        f.storeStatement(convertStatement(statementEast));

        final Resource subjectWest = vf.createIRI("foo:subj:west");
        final IRI predicateWest = GeoConstants.GEO_AS_WKT;
        final Value objectWest = vf.createLiteral("Point(-179 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Statement statementWest = vf.createStatement(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)));
    }
}
 
Example 12
Source File: GeoWaveIndexerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testDcSearchWithPredicate() throws Exception {
    // test a ring around dc
    try (final GeoWaveGeoIndexer f = new GeoWaveGeoIndexer()) {
        f.setConf(conf);
        f.purge(conf);

        final ValueFactory vf = SimpleValueFactory.getInstance();
        final Resource subject = vf.createIRI("foo:subj");
        final IRI predicate = GeoConstants.GEO_AS_WKT;
        final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Resource context = vf.createIRI("foo:context");

        final Statement statement = vf.createStatement(subject, predicate, object, context);
        f.storeStatement(convertStatement(statement));
        f.flush();

        final double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 };
        final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2));
        final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {});

        // query with correct Predicate
        Assert.assertEquals(Sets.newHashSet(statement),
                getSet(f.queryWithin(p1, new StatementConstraints().setPredicates(Collections.singleton(predicate)))));

        // query with wrong predicate
        Assert.assertEquals(Sets.newHashSet(),
                getSet(f.queryWithin(p1, new StatementConstraints().setPredicates(Collections.singleton(vf.createIRI("other:pred"))))));
    }
}
 
Example 13
Source File: GeoIndexerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testDcSearchWithSubjectAndContext() throws Exception {
    // test a ring around dc
    try (final GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) {
        f.setConf(conf);

        final ValueFactory vf = SimpleValueFactory.getInstance();
        final Resource subject = vf.createIRI("foo:subj");
        final IRI predicate = GeoConstants.GEO_AS_WKT;
        final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Resource context = vf.createIRI("foo:context");

        final Statement statement = vf.createStatement(subject, predicate, object, context);
        f.storeStatement(convertStatement(statement));
        f.flush();

        final double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 };
        final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2));
        final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {});

        // query with correct context subject
        Assert.assertEquals(Sets.newHashSet(statement),
                getSet(f.queryWithin(p1, new StatementConstraints().setContext(context).setSubject(subject))));

        // query with wrong context
        Assert.assertEquals(Sets.newHashSet(),
                getSet(f.queryWithin(p1, new StatementConstraints().setContext(vf.createIRI("foo:context2")))));

        // query with wrong subject
        Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p1, new StatementConstraints().setSubject(vf.createIRI("foo:subj2")))));
    }
}
 
Example 14
Source File: GeoIndexerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
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 = SimpleValueFactory.getInstance();
        final Resource context = vf.createIRI("foo:context");

        final Resource subjectEast = vf.createIRI("foo:subj:east");
        final IRI predicateEast = GeoConstants.GEO_AS_WKT;
        final Value objectEast = vf.createLiteral("Point(179 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Statement statementEast = vf.createStatement(subjectEast, predicateEast, objectEast, context);
        f.storeStatement(convertStatement(statementEast));

        final Resource subjectWest = vf.createIRI("foo:subj:west");
        final IRI predicateWest = GeoConstants.GEO_AS_WKT;
        final Value objectWest = vf.createLiteral("Point(-179 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Statement statementWest = vf.createStatement(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)));
    }
}
 
Example 15
Source File: SailGraphQuery.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public GraphQueryResult evaluate() throws QueryEvaluationException {
	TupleExpr tupleExpr = getParsedQuery().getTupleExpr();

	CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter1 = null;
	CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter2 = null;
	CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter3 = null;
	CloseableIteration<Statement, QueryEvaluationException> stIter = null;
	IteratingGraphQueryResult result = null;
	boolean allGood = false;
	try {

		SailConnection sailCon = getConnection().getSailConnection();
		bindingsIter1 = sailCon.evaluate(tupleExpr, getActiveDataset(), getBindings(), getIncludeInferred());

		// Filters out all partial and invalid matches
		bindingsIter2 = new FilterIteration<BindingSet, QueryEvaluationException>(bindingsIter1) {

			@Override
			protected boolean accept(BindingSet bindingSet) {
				Value context = bindingSet.getValue("context");

				return bindingSet.getValue("subject") instanceof Resource
						&& bindingSet.getValue("predicate") instanceof IRI
						&& bindingSet.getValue("object") instanceof Value
						&& (context == null || context instanceof Resource);
			}
		};

		bindingsIter3 = enforceMaxQueryTime(bindingsIter2);

		// Convert the BindingSet objects to actual RDF statements
		final ValueFactory vf = getConnection().getRepository().getValueFactory();
		stIter = new ConvertingIteration<BindingSet, Statement, QueryEvaluationException>(bindingsIter3) {

			@Override
			protected Statement convert(BindingSet bindingSet) {
				Resource subject = (Resource) bindingSet.getValue("subject");
				IRI predicate = (IRI) bindingSet.getValue("predicate");
				Value object = bindingSet.getValue("object");
				Resource context = (Resource) bindingSet.getValue("context");

				if (context == null) {
					return vf.createStatement(subject, predicate, object);
				} else {
					return vf.createStatement(subject, predicate, object, context);
				}
			}
		};

		result = new IteratingGraphQueryResult(getParsedQuery().getQueryNamespaces(), stIter);
		allGood = true;
		return result;
	} catch (SailException e) {
		throw new QueryEvaluationException(e.getMessage(), e);
	} finally {
		if (!allGood) {
			try {
				if (result != null) {
					result.close();
				}
			} finally {
				try {
					if (stIter != null) {
						stIter.close();
					}
				} finally {
					try {
						if (bindingsIter3 != null) {
							bindingsIter3.close();
						}
					} finally {
						try {
							if (bindingsIter2 != null) {
								bindingsIter2.close();
							}
						} finally {
							if (bindingsIter1 != null) {
								bindingsIter1.close();
							}
						}
					}
				}
			}
		}
	}
}
 
Example 16
Source File: WriteTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected Statement simpleStatement() {
	ValueFactory vf = SimpleValueFactory.getInstance();
	IRI subject = vf.createIRI("http://example.org/person1");
	return vf.createStatement(subject, RDF.TYPE, FOAF.PERSON);
}
 
Example 17
Source File: KBInstance.java    From inception with Apache License 2.0 4 votes vote down vote up
public void write(RepositoryConnection aConn, KnowledgeBase kb)
{
    ValueFactory vf = aConn.getValueFactory();
    IRI subject = vf.createIRI(identifier);

    originalStatements.clear();

    Statement typeStmt = vf.createStatement(subject, kb.getTypeIri(),
            vf.createIRI(type.toString()));
    originalStatements.add(typeStmt);
    aConn.add(typeStmt);

    if (isNotBlank(name)) {
        Literal nameLiteral;
        if (language != null) {
            nameLiteral = vf.createLiteral(name, language);
        }
        else if (kb.getDefaultLanguage() != null) {
            nameLiteral = vf.createLiteral(name, kb.getDefaultLanguage());
        }
        else {
            nameLiteral = vf.createLiteral(name);
        }
        Statement nameStmt = vf.createStatement(subject, kb.getLabelIri(), nameLiteral);
        originalStatements.add(nameStmt);
        aConn.add(nameStmt);
    }

    if (isNotBlank(description)) {
        Literal descriptionLiteral;
        if (language == null) {
            descriptionLiteral = vf.createLiteral(description);
        }
        else {
            descriptionLiteral = vf.createLiteral(description, language);
        }
        Statement descStmt = vf
            .createStatement(subject, kb.getDescriptionIri(), descriptionLiteral);
        originalStatements.add(descStmt);
        aConn.add(descStmt);
    }
}
 
Example 18
Source File: AccumuloTemporalIndexerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
     * Test method for {@link AccumuloTemporalIndexer#storeStatement(convertStatement(org.eclipse.rdf4j.model.Statement)}
     *
     * @throws NoSuchAlgorithmException
     */
    @Test
    public void testStoreStatement() throws IOException, AccumuloException, AccumuloSecurityException, TableNotFoundException, TableExistsException, NoSuchAlgorithmException {
        // count rows expected to store:
        int rowsStoredExpected = 0;

        ValueFactory vf = SimpleValueFactory.getInstance();

        IRI pred1_atTime = vf.createIRI(URI_PROPERTY_AT_TIME);
        IRI pred2_circa = vf.createIRI(URI_PROPERTY_CIRCA);

        // Should not be stored because they are not in the predicate list
        String validDateStringWithThirteens = "1313-12-13T13:13:13Z";
        tIndexer.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj1"), RDFS.LABEL, vf.createLiteral(validDateStringWithThirteens))));

        // Test: Should not store an improper date, and log a warning (log warning not tested).
        final String invalidDateString = "ThisIsAnInvalidDate";
//        // Silently logs a warning for bad dates.  Old: Set true when we catch the error:
//        boolean catchErrorThrownCorrectly = false;
//        try {
            tIndexer.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj2"), pred1_atTime, vf.createLiteral(invalidDateString))));
//        } catch (IllegalArgumentException e) {
//            catchErrorThrownCorrectly = true;
//            Assert.assertTrue(
//                    "Invalid date parse error should include the invalid string. message=" + e.getMessage(),
//                    e.getMessage().contains(invalidDateString));
//        }
//        Assert.assertTrue("Invalid date parse error should be thrown for this bad date=" + invalidDateString, catchErrorThrownCorrectly);

        // These are different datetimes instant but from different time zones.
        // This is an arbitrary zone, BRST=Brazil, better if not local.
        // same as "2015-01-01T01:59:59Z"
        final String testDate2014InBRST = "2014-12-31T23:59:59-02:00";
        // next year, same as "2017-01-01T01:59:59Z"
        final String testDate2016InET = "2016-12-31T20:59:59-05:00";

        // These should be stored because they are in the predicate list.
        // BUT they will get converted to the same exact datetime in UTC.
        Statement s3 = vf.createStatement(vf.createIRI("foo:subj3"), pred1_atTime, vf.createLiteral(testDate2014InBRST));
        Statement s4 = vf.createStatement(vf.createIRI("foo:subj4"), pred2_circa, vf.createLiteral(testDate2016InET));
        tIndexer.storeStatement(convertStatement(s3));
        rowsStoredExpected++;
        tIndexer.storeStatement(convertStatement(s4));
        rowsStoredExpected++;

        // This should not be stored because the object is not a literal
        tIndexer.storeStatement(convertStatement(vf.createStatement(vf.createIRI("foo:subj5"), pred1_atTime, vf.createIRI("in:valid"))));

        tIndexer.flush();

        int rowsStoredActual = printTables("junit testing: Temporal entities stored in testStoreStatement", null, null);
        assertEquals("Number of rows stored.", rowsStoredExpected*4, rowsStoredActual); // 4 index entries per statement

    }
 
Example 19
Source File: GeoWaveIndexerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testPrimeMeridianSearch() throws Exception {
    try (final GeoWaveGeoIndexer f = new GeoWaveGeoIndexer()) {
        f.setConf(conf);
        f.purge(conf);

        final ValueFactory vf = SimpleValueFactory.getInstance();
        final Resource subject = vf.createIRI("foo:subj");
        final IRI predicate = GeoConstants.GEO_AS_WKT;
        final Value object = vf.createLiteral("Point(0 0)", GeoConstants.XMLSCHEMA_OGC_WKT);
        final Resource context = vf.createIRI("foo:context");

        final Statement statement = vf.createStatement(subject, predicate, object, context);
        f.storeStatement(convertStatement(statement));
        f.flush();

        final double[] ONE = { 1, 1, -1, 1, -1, -1, 1, -1, 1, 1 };
        final double[] TWO = { 2, 2, -2, 2, -2, -2, 2, -2, 2, 2 };
        final double[] THREE = { 3, 3, -3, 3, -3, -3, 3, -3, 3, 3 };

        final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(ONE, 2));
        final LinearRing r2 = gf.createLinearRing(new PackedCoordinateSequence.Double(TWO, 2));
        final LinearRing r3 = gf.createLinearRing(new PackedCoordinateSequence.Double(THREE, 2));

        final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {});
        final Polygon p2 = gf.createPolygon(r2, new LinearRing[] {});
        final Polygon p3 = gf.createPolygon(r3, new LinearRing[] {});

        Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS)));
        Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p2, EMPTY_CONSTRAINTS)));
        Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p3, EMPTY_CONSTRAINTS)));

        // Test a ring with a hole in it
        final Polygon p3m2 = gf.createPolygon(r3, new LinearRing[] { r2 });
        Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p3m2, EMPTY_CONSTRAINTS)));

        // test a ring outside the point
        final double[] OUT = { 3, 3, 1, 3, 1, 1, 3, 1, 3, 3 };
        final LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(OUT, 2));
        final Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {});
        Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS)));
    }
}
 
Example 20
Source File: MongoFreeTextIndexerIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testDelete() throws Exception {
    try (MongoFreeTextIndexer f = new MongoFreeTextIndexer()) {
        f.setConf(conf);
        f.init();

        final ValueFactory vf = SimpleValueFactory.getInstance();

        final IRI subject1 = vf.createIRI("foo:subj");
        final IRI predicate1 = RDFS.LABEL;
        final Value object1 = vf.createLiteral("this is a new hat");

        final IRI context1 = vf.createIRI("foo:context");

        final Statement statement1 = vf.createStatement(subject1, predicate1, object1, context1);
        f.storeStatement(RdfToRyaConversions.convertStatement(statement1));

        final IRI subject2 = vf.createIRI("foo:subject");
        final IRI predicate2 = RDFS.LABEL;
        final Value object2 = vf.createLiteral("Do you like my new hat?");

        final IRI context2 = vf.createIRI("foo:context");

        final Statement statement2 = vf.createStatement(subject2, predicate2, object2, context2);
        f.storeStatement(RdfToRyaConversions.convertStatement(statement2));

        f.flush();


        f.deleteStatement(RdfToRyaConversions.convertStatement(statement1));
        assertEquals(Sets.newHashSet(statement2), getSet(f.queryText("Do you like my new hat?", EMPTY_CONSTRAINTS)));

        // Check that "new" didn't get deleted from the term table after "this is a new hat"
        // was deleted since "new" is still in "Do you like my new hat?"
        assertEquals(Sets.newHashSet(statement2), getSet(f.queryText("new", EMPTY_CONSTRAINTS)));

        f.deleteStatement(RdfToRyaConversions.convertStatement(statement2));
        assertEquals(Sets.newHashSet(), getSet(f.queryText("this is a new hat", EMPTY_CONSTRAINTS)));
        assertEquals(Sets.newHashSet(), getSet(f.queryText("Do you like my new hat?", EMPTY_CONSTRAINTS)));
    }
}