Search in sources :

Example 46 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class ProcEval method build.

// ----
public static Procedure build(Node procId, ExprList args, ExecutionContext execCxt) {
    Context context = execCxt.getContext();
    ProcedureRegistry reg = chooseProcedureRegistry(context);
    ProcedureFactory f = reg.get(procId.getURI());
    Procedure proc = f.create(procId.getURI());
    // Allow args to build as well.
    args.prepareExprs(context);
    proc.build(procId, args, execCxt);
    return proc;
}
Also used : Context(org.apache.jena.sparql.util.Context) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) OpProcedure(org.apache.jena.sparql.algebra.op.OpProcedure)

Example 47 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class TestJsonLDWriter method testFrames.

/**
     * Checking frames
     */
@Test
public final void testFrames() throws JsonParseException, IOException {
    Model m = ModelFactory.createDefaultModel();
    String ns = "http://schema.org/";
    Resource person = m.createResource(ns + "Person");
    Resource s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Jane Doe");
    m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
    m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
    m.add(s, RDF.type, person);
    s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Gado Salamatou");
    m.add(s, m.createProperty(ns + "url"), "http://www.salamatou.com");
    m.add(s, RDF.type, person);
    s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Not a person");
    m.add(s, RDF.type, m.createResource(ns + "Event"));
    Context jenaCtx = new Context();
    JsonObject frame = new JsonObject();
    // only output the persons using a frame
    frame.put("@type", ns + "Person");
    jenaCtx.set(JsonLDWriter.JSONLD_FRAME, frame.toString());
    String jsonld = toString(m, RDFFormat.JSONLD_FRAME_PRETTY, jenaCtx);
    Model m2 = parse(jsonld);
    // 2 subjects with a type in m2
    assertTrue(m2.listStatements((Resource) null, RDF.type, (RDFNode) null).toList().size() == 2);
    // 2 persons in m2
    assertTrue(m2.listStatements((Resource) null, RDF.type, person).toList().size() == 2);
    // something we hadn't tested in prettyIsNotFlat
    assertTrue(jsonld.trim().contains("\n"));
    // only output the subjects which have a jobTitle
    frame = new JsonObject();
    frame.put("http://schema.org/jobTitle", new JsonObject());
    jenaCtx.set(JsonLDWriter.JSONLD_FRAME, JsonUtils.fromString(frame.toString()));
    jsonld = toString(m, RDFFormat.JSONLD_FRAME_FLAT, jenaCtx);
    m2 = parse(jsonld);
    // 1 subject with a type in m2
    assertTrue(m2.listStatements((Resource) null, RDF.type, (RDFNode) null).toList().size() == 1);
    // 1 subject with a jobTitle in m2
    assertTrue(m2.listStatements((Resource) null, m.createProperty(ns + "jobTitle"), (RDFNode) null).toList().size() == 1);
    // something we hadn't tested in prettyIsNotFlat
    assertFalse(jsonld.trim().contains("\n"));
}
Also used : Context(org.apache.jena.sparql.util.Context) JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) JsonObject(org.apache.jena.atlas.json.JsonObject) JsonString(org.apache.jena.atlas.json.JsonString) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Example 48 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class TestJsonLDWriter method atVocabJENA1292.

/**
     * setting @vocab and replacing @context
     * not really a test, sample code for JENA-1292 */
@SuppressWarnings("unchecked")
@Test
public final void atVocabJENA1292() throws JsonParseException, JsonLdError, IOException {
    Model m = ModelFactory.createDefaultModel();
    String ns = "http://schema.org/";
    Resource person = m.createResource(ns + "Person");
    Resource s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Jane Doe");
    m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
    m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
    m.add(s, FOAF.nick, "jd");
    m.add(s, RDF.type, person);
    m.setNsPrefix("", ns);
    DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
    PrefixMap pm = RiotLib.prefixMap(g);
    String base = null;
    Context jenaContext = null;
    // the JSON-LD API object. It's a map
    Map<String, Object> map = (Map<String, Object>) JsonLDWriter.toJsonLDJavaAPI((RDFFormat.JSONLDVariant) RDFFormat.JSONLD.getVariant(), g, pm, base, jenaContext);
    // get the @context:
    Map<String, Object> ctx = (Map<String, Object>) map.get("@context");
    // remove from ctx declaration of props in ns
    List<String> remove = new ArrayList<>();
    for (Entry<String, Object> e : ctx.entrySet()) {
        // is it the declaration of a prop in ns?
        Object o = e.getValue();
        if (o instanceof Map) {
            o = ((Map<String, Object>) o).get("@id");
        }
        if ((o != null) && (o instanceof String)) {
            if (((String) o).equals(ns + e.getKey())) {
                remove.add(e.getKey());
            }
        }
    }
    for (String key : remove) {
        ctx.remove(key);
    }
    // add to ctx the "@vocab" key
    ctx.put("@vocab", "http://schema.org/");
// JsonUtils.writePrettyPrint(new PrintWriter(System.out), map) ;
}
Also used : Context(org.apache.jena.sparql.util.Context) JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) PrefixMap(org.apache.jena.riot.system.PrefixMap) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) JsonString(org.apache.jena.atlas.json.JsonString) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Model(org.apache.jena.rdf.model.Model) JsonObject(org.apache.jena.atlas.json.JsonObject) PrefixMap(org.apache.jena.riot.system.PrefixMap) Map(java.util.Map) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Example 49 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class TestJsonLDWriter method testSettingContextAsObjectExpectedByJsonldAPI.

/**
     * one may pass the object expected by the JSON-LD java AP as context
     * (otherwise, same thing as testSettingContextAsJsonString)
     */
@Test
public void testSettingContextAsObjectExpectedByJsonldAPI() {
    String ns = "http://www.a.com/foo/";
    Model m = simpleModel(ns);
    m.setNsPrefix("ex", ns);
    String s1 = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, null);
    // there's a prefix in m, and we find it in the output
    String prefixStringInResult = "\"ex\":\"" + ns + "\"";
    assertTrue(s1.contains(prefixStringInResult));
    Model m1 = parse(s1);
    // the context used in this case, as it would automatically be created as none is set
    // it includes one prefix
    Object ctx = JsonLDWriter.createJsonldContext(m.getGraph());
    // remove the prefix from m
    m.removeNsPrefix("ex");
    String s2 = toString(m, RDFFormat.JSONLD_COMPACT_PRETTY, null);
    // model wo prefix -> no more prefix string in result:
    assertFalse(s2.contains(prefixStringInResult));
    // the model wo prefix, output as jsonld using a context that defines the prefix
    Context jenaCtx = new Context();
    jenaCtx.set(JsonLDWriter.JSONLD_CONTEXT, ctx);
    String s3 = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
    assertTrue(s3.length() == s1.length());
    assertTrue(s3.contains(prefixStringInResult));
    Model m3 = parse(s3);
    assertTrue(m3.isIsomorphicWith(m));
    assertTrue(m3.isIsomorphicWith(m1));
}
Also used : Context(org.apache.jena.sparql.util.Context) JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) JsonObject(org.apache.jena.atlas.json.JsonObject) JsonString(org.apache.jena.atlas.json.JsonString) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Example 50 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class EnvTDB method processProperties.

public static Context processProperties(Properties properties) {
    Context context = new Context();
    Set<Object> keys = properties.keySet();
    for (Object key : keys) {
        if (key instanceof String) {
            String keyStr = (String) key;
            if (keyStr.startsWith(prefix))
                keyStr = SystemTDB.symbolNamespace + keyStr.substring(prefix.length());
            if (!keyStr.startsWith(SystemTDB.symbolNamespace))
                continue;
            Object value = properties.get(key);
            Symbol symbol = Symbol.create(keyStr);
            context.set(symbol, value);
        }
    }
    return context;
}
Also used : Context(org.apache.jena.sparql.util.Context) Symbol(org.apache.jena.sparql.util.Symbol)

Aggregations

Context (org.apache.jena.sparql.util.Context)52 Test (org.junit.Test)36 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)15 Map (java.util.Map)11 BaseTest (org.apache.jena.atlas.junit.BaseTest)11 SerializationContext (org.apache.jena.sparql.serializer.SerializationContext)11 HashMap (java.util.HashMap)10 QueryIterSort (org.apache.jena.sparql.engine.iterator.QueryIterSort)9 HttpClient (org.apache.http.client.HttpClient)7 QueryEngineHTTP (org.apache.jena.sparql.engine.http.QueryEngineHTTP)7 Query (org.apache.jena.query.Query)5 QueryCancelledException (org.apache.jena.query.QueryCancelledException)4 SocketException (java.net.SocketException)3 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)3 HttpContext (org.apache.http.protocol.HttpContext)3 JsonObject (org.apache.jena.atlas.json.JsonObject)3 JsonString (org.apache.jena.atlas.json.JsonString)3 Node (org.apache.jena.graph.Node)3 Model (org.apache.jena.rdf.model.Model)3 JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)3