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;
}
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"));
}
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) ;
}
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));
}
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;
}
Aggregations