Skip to content

Example

Robin Keskisärkkä edited this page Nov 7, 2016 · 1 revision

Using the TemplateManager

// Create a template
TemplateManager tm = TemplateManager.get();
Template template = tm.createTemplate("http://org.example/template/1",
          "PREFIX : <http://example.org#> "
        + "REGISTER STREAM <http://s> AS "
        + "SELECT * "
        + "FROM NAMED WINDOW :w ON :s [RANGE PT1H STEP PT10M] "
        + "WHERE { "
        + "   WINDOW :w { ?s :distance ?distance } "
        + "   FILTER(?distance > ?limit) "
        + "}");

tm.addArgumentConstraint("limit", XSD.integer, ResourceFactory.createTypedLiteral("2", XSDDatatype.XSDinteger),
        true, template);

// Get template from model
Template t = tm.getTemplate("http://org.example/template/1");
QuerySolutionMap bindings = new QuerySolutionMap();

// Default value
tm.check(template, bindings);
System.err.println(tm.getQuery(t, bindings));

// Custom value
bindings.add("limit", ResourceFactory.createTypedLiteral("123", XSDDatatype.XSDinteger));
tm.check(template, bindings);
System.err.println(tm.getQuery(t, bindings));

// Invalid parameter, throws an error
bindings.add("limit", ResourceFactory.createTypedLiteral(">2", XSDDatatype.XSDstring));
bindings.add("outputStream", ResourceFactory.createTypedLiteral(">2", XSDDatatype.XSDstring));
tm.check(template, bindings);
System.err.println(tm.getQuery(t, bindings));
Clone this wiki locally