Search in sources :

Example 81 with QuerySolution

use of org.apache.jena.query.QuerySolution in project rdf2neo by Rothamsted.

the class CypherHandlersIT method testRelations.

/**
 * Tests {@link CyRelationLoadingHandler} to see if relations are mapped from RDF and loaded into Neo4J.
 */
@Test
public void testRelations() throws Exception {
    try (Driver neoDriver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "test"));
        CyRelationLoadingHandler handler = new CyRelationLoadingHandler();
        RdfDataManager rdfMgr = new RdfDataManager(RdfDataManagerTest.TDB_PATH);
        Neo4jDataManager neoMgr = new Neo4jDataManager(neoDriver)) {
        handler.setRdfDataManager(rdfMgr);
        handler.setNeo4jDataManager(neoMgr);
        handler.setRelationTypesSparql(RdfDataManagerTest.SPARQL_REL_TYPES);
        handler.setRelationPropsSparql(RdfDataManagerTest.SPARQL_REL_PROPS);
        Set<QuerySolution> relSparqlRows = new HashSet<>();
        Dataset dataSet = rdfMgr.getDataSet();
        Txn.executeRead(dataSet, () -> SparqlUtils.select(RdfDataManagerTest.SPARQL_REL_TYPES, rdfMgr.getDataSet().getDefaultModel()).forEachRemaining(row -> relSparqlRows.add(row)));
        handler.accept(relSparqlRows);
        Session session = neoDriver.session(AccessMode.READ);
        StatementResult cursor = session.run("MATCH ()-[r]->() RETURN COUNT ( r ) AS ct");
        Assert.assertEquals("Wrong count for relations", 3, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH p = (:TestNode{ iri:$iri1 })-[:relatedTo]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) AS ct", parameters("iri1", iri("ex:1"), "iri2", iri("ex:2")));
        Assert.assertEquals("Wrong count for {1 relatedTo 2}!", 1, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH p = (:SuperTestNode{ iri:$iri1 })-[:derivedFrom]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) AS ct", parameters("iri1", iri("ex:3"), "iri2", iri("ex:1")));
        Assert.assertEquals("Wrong count for {3 derivedFrom 1}!", 1, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH (:TestNode{ iri:$iri1 })-[r:relatedTo]->(:AdditionalLabel{ iri:$iri2 }) RETURN r.note AS note", parameters("iri1", iri("ex:2"), "iri2", iri("ex:3")));
        assertTrue("{2 relatedTo 3} not found!", cursor.hasNext());
        Set<String> values = cursor.next().get("note").asList().stream().map(v -> (String) v).collect(Collectors.toSet());
        Set<String> refValues = new HashSet<>(Arrays.asList(new String[] { "Reified Relation", "Another Note" }));
        assertTrue("reified relation, wrong property value for 'note'!", Sets.difference(values, refValues).isEmpty());
    }
}
Also used : SparqlUtils(info.marcobrandizi.rdfutils.jena.SparqlUtils) Arrays(java.util.Arrays) Driver(org.neo4j.driver.v1.Driver) BeforeClass(org.junit.BeforeClass) AccessMode(org.neo4j.driver.v1.AccessMode) LoggerFactory(org.slf4j.LoggerFactory) HashSet(java.util.HashSet) QuerySolution(org.apache.jena.query.QuerySolution) Session(org.neo4j.driver.v1.Session) Txn(org.apache.jena.system.Txn) Values.parameters(org.neo4j.driver.v1.Values.parameters) Resource(org.apache.jena.rdf.model.Resource) Map(java.util.Map) GraphDatabase(org.neo4j.driver.v1.GraphDatabase) NamespaceUtils.iri(info.marcobrandizi.rdfutils.namespaces.NamespaceUtils.iri) Dataset(org.apache.jena.query.Dataset) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) AuthTokens(org.neo4j.driver.v1.AuthTokens) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Stream(java.util.stream.Stream) StatementResult(org.neo4j.driver.v1.StatementResult) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) StatementResult(org.neo4j.driver.v1.StatementResult) Dataset(org.apache.jena.query.Dataset) Driver(org.neo4j.driver.v1.Driver) QuerySolution(org.apache.jena.query.QuerySolution) HashSet(java.util.HashSet) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 82 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class EastPFTest method testExecEvaluated.

/**
 * Test of execEvaluated method, of class EastPF.
 */
@Test
public void testExecEvaluated() {
    Dataset dataset = SpatialIndexTestData.createTestDataset();
    String query = "PREFIX spatial: <http://jena.apache.org/spatial#>\n" + "\n" + "SELECT ?subj\n" + "WHERE{\n" + "    ?subj spatial:east(48.857487 2.373047) .\n" + "}ORDER by ?subj";
    List<Resource> result = new ArrayList<>();
    try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
        ResultSet rs = qe.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            Resource feature = qs.getResource("subj");
            result.add(feature);
        }
    }
    List<Resource> expResult = Arrays.asList(SpatialIndexTestData.AUCKLAND_FEATURE, SpatialIndexTestData.PERTH_FEATURE);
    assertEquals(expResult, result);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 83 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class NorthPFTest method testExecEvaluated.

/**
 * Test of execEvaluated method, of class NorthPF.
 */
@Test
public void testExecEvaluated() {
    Dataset dataset = SpatialIndexTestData.createTestDataset();
    String query = "PREFIX spatial: <http://jena.apache.org/spatial#>\n" + "\n" + "SELECT ?subj\n" + "WHERE{\n" + "    ?subj spatial:north(48.857487 2.373047) .\n" + "}ORDER by ?subj";
    List<Resource> result = new ArrayList<>();
    try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
        ResultSet rs = qe.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            Resource feature = qs.getResource("subj");
            result.add(feature);
        }
    }
    List<Resource> expResult = Arrays.asList(SpatialIndexTestData.LONDON_FEATURE);
    assertEquals(expResult, result);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 84 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class NearbyPFTest method testExecEvaluated_fail.

/**
 * Test of execEvaluated method, of class NearbyPF.<br>
 * Close enough for first filter but rejected by second filter.
 */
@Test
public void testExecEvaluated_fail() {
    Dataset dataset = SpatialIndexTestData.createTestDataset();
    String query = "PREFIX spatial: <http://jena.apache.org/spatial#>\n" + "\n" + "SELECT ?subj\n" + "WHERE{\n" + "    ?subj spatial:nearby(48.857487 2.373047 340) .\n" + "}ORDER by ?subj";
    List<Resource> result = new ArrayList<>();
    try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
        ResultSet rs = qe.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            Resource feature = qs.getResource("subj");
            result.add(feature);
        }
    }
    List<Resource> expResult = new ArrayList<>();
    assertEquals(expResult, result);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 85 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class AngleFFTest method testExec_query.

/**
 * Test of exec method, of class AngleFF.
 */
@Test
public void testExec_query() {
    Dataset dataset = SpatialIndexTestData.createTestDataset();
    String query = "PREFIX spatialF: <http://jena.apache.org/function/spatial#>\n" + "\n" + "SELECT ?rads\n" + "WHERE{\n" + "    BIND( spatialF:angle(25, 45, 75, 100) AS ?rads) \n" + "}ORDER by ?rads";
    List<Literal> results = new ArrayList<>();
    try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
        ResultSet rs = qe.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            Literal result = qs.getLiteral("rads");
            results.add(result);
        }
    }
    List<Literal> expResults = Arrays.asList(ResourceFactory.createTypedLiteral("0.7378150601204649e0", XSDDatatype.XSDdouble));
    // 
    // 
    assertEquals(expResults, results);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Aggregations

QuerySolution (org.apache.jena.query.QuerySolution)125 ResultSet (org.apache.jena.query.ResultSet)103 QueryExecution (org.apache.jena.query.QueryExecution)96 ArrayList (java.util.ArrayList)70 Test (org.junit.Test)69 Dataset (org.apache.jena.query.Dataset)44 Query (org.apache.jena.query.Query)44 Resource (org.apache.jena.rdf.model.Resource)43 Literal (org.apache.jena.rdf.model.Literal)21 RDFNode (org.apache.jena.rdf.model.RDFNode)20 Model (org.apache.jena.rdf.model.Model)12 HashMap (java.util.HashMap)7 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)6 Extractor (infoeval.main.WikiData.Extractor)4 Node (org.apache.jena.graph.Node)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 IOException (java.io.IOException)3 Date (java.sql.Date)3 SimpleDateFormat (java.text.SimpleDateFormat)3 LinkedList (java.util.LinkedList)3