Search in sources :

Example 11 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class SesameGraph method select.

public List<BoundVariables> select(String sparqlSelect) {
    List<BoundVariables> results = new LinkedList<>();
    TupleQueryResult tupleQueryResult = getTupleQueryResult(sparqlSelect);
    try {
        while (tupleQueryResult.hasNext()) {
            results.add(new SesameBoundVariables(tupleQueryResult.getBindingNames(), tupleQueryResult.next()));
        }
    } catch (QueryEvaluationException e) {
        throw new ShineRuntimeException(e);
    }
    return results;
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) TupleQueryResult(org.openrdf.query.TupleQueryResult) LinkedList(java.util.LinkedList)

Example 12 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class StagesQuery method selectForSingleStage.

private <T> List<T> selectForSingleStage(String sparql, StageIdentifier stageIdentifier, RdfResultMapper<T> mapper) {
    StagesQueryCache.CacheKey key = new StagesQueryCache.CacheKey(sparql, stageIdentifier);
    List<T> mappedResults = (List<T>) cache.get(key);
    if (mappedResults != null) {
        return mappedResults;
    }
    synchronized (key.getKey().intern()) {
        mappedResults = (List<T>) cache.get(key);
        if (mappedResults != null) {
            return mappedResults;
        }
        Graph graph = stageGraphLoader.load(stageIdentifier);
        List<BoundVariables> boundVariableses = graph.select(sparql);
        mappedResults = new ArrayList<>();
        for (BoundVariables bv : boundVariableses) {
            mappedResults.add(mapper.map(bv));
        }
        cache.put(mappedResults, key);
        return mappedResults;
    }
}
Also used : Graph(com.thoughtworks.studios.shine.semweb.Graph) BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class SesameBoundVariablesTest method checkGetBoolean.

@Test
public void checkGetBoolean() {
    String turtle = "@prefix ex: <http://example.com/ontology#> . " + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . " + "<http://example.com/1> ex:is \"true\"^^xsd:boolean . " + "<http://example.com/2> ex:is \"false\"^^xsd:boolean . ";
    graph.addTriplesFromTurtle(turtle);
    String sparqlSelect = "PREFIX ex: <http://example.com/ontology#> " + "SELECT ?one ?two ?three WHERE { " + "<http://example.com/1> ex:is ?one . " + "<http://example.com/2> ex:is ?two . " + "OPTIONAL { <http://example.com/3> ex:is ?three } " + "}";
    BoundVariables bv = graph.selectFirst(sparqlSelect);
    assertTrue(bv.getBoolean("one"));
    assertFalse(bv.getBoolean("two"));
    assertNull(bv.getBoolean("three"));
    try {
        bv.getBoolean("baz");
        fail("Illegal argument exception expected.");
    } catch (IllegalArgumentException e) {
        assertEquals("boundName 'baz' is not in the list of possible values ('one', 'two', 'three')", e.getMessage());
    }
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) Test(org.junit.Test)

Example 14 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class SesameBoundVariablesTest method checkGetBoundVariableNames.

@Test
public void checkGetBoundVariableNames() {
    String turtle = "@prefix ex: <http://example.com/ontology#> . " + "<http://example.com/1> ex:is <http://hello> . ";
    graph.addTriplesFromTurtle(turtle);
    String sparqlSelect = "PREFIX ex: <http://example.com/ontology#> " + "SELECT ?one ?two WHERE { " + "<http://example.com/1> ex:is ?one . " + "OPTIONAL { [] ex:nope ?two } " + "}";
    BoundVariables bv = graph.selectFirst(sparqlSelect);
    assertEquals("one", bv.getBoundVariableNames().get(0));
    assertEquals("two", bv.getBoundVariableNames().get(1));
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) Test(org.junit.Test)

Example 15 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class StagesQueryTest method testQueryResultWillBeGetFromCacheIfCacheIsHit.

@Test
public void testQueryResultWillBeGetFromCacheIfCacheIsHit() {
    InMemoryCache cache = new InMemoryCache();
    StagesQuery stagesQuery = new StagesQuery(graphLoader, cache);
    String sparql = "PREFIX cruise:<" + GoOntology.URI + ">" + "SELECT ?job WHERE {" + "  ?job a cruise:Job . " + "}";
    StageIdentifier stageIdentifier = new StageIdentifier("p", 1, "s", "2");
    cache.put(Arrays.<BoundVariables>asList(new BoundVariablesStub("job", "http://job/1")), new StagesQueryCache.CacheKey(sparql, stageIdentifier));
    List<BoundVariables> bvs = stagesQuery.select(sparql, Arrays.asList(stageIdentifier), new RdfResultMapper<BoundVariables>() {

        public BoundVariables map(BoundVariables aRow) {
            return aRow;
        }
    });
    assertEquals(1, bvs.size());
    assertEquals("http://job/1", bvs.get(0).getAsString("job"));
}
Also used : StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) Test(org.junit.Test)

Aggregations

BoundVariables (com.thoughtworks.studios.shine.semweb.BoundVariables)16 Test (org.junit.Test)10 StageIdentifier (com.thoughtworks.go.domain.StageIdentifier)3 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)3 Graph (com.thoughtworks.studios.shine.semweb.Graph)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)2 TupleQueryResult (org.openrdf.query.TupleQueryResult)2 MoreThanOneResultFoundException (com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException)1 InMemoryTempGraphFactory (com.thoughtworks.studios.shine.semweb.sesame.InMemoryTempGraphFactory)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 LinkedList (java.util.LinkedList)1