Skip to content

Instantly share code, notes, and snippets.

@mhgrove
Created April 10, 2015 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhgrove/92f8c87dcd3bafdc5d9b to your computer and use it in GitHub Desktop.
Save mhgrove/92f8c87dcd3bafdc5d9b to your computer and use it in GitHub Desktop.
Demonstrates the use of Adder#graph correctly adds a Rule to the database
@Test
public void ruleExample() throws Exception {
final String aRule = "PREFIX :<urn:test:>\n" +
" IF {\n" +
" ?r a :Rectangle ;\n" +
" :width ?w ;\n" +
" :height ?h\n" +
" BIND (?w * ?h AS ?area)\n" +
" }\n" +
" THEN {\n" +
" ?r :area ?area\n" +
" }";
final String aData = "PREFIX rule: <tag:stardog:api:rule:>\n" +
"PREFIX : <urn:test:>\n" +
"\n" +
":c a :Circle ;\n" +
" :radius 10 .\n" +
"\n" +
":t a :Triangle ;\n" +
" :base 4 ;\n" +
" :height 10 .\n" +
"\n" +
":r a :Rectangle ;\n" +
" :width 5 ;\n" +
" :height 8 .\n" +
"\n" +
":s a :Rectangle ;\n" +
" :width 10 ;\n" +
" :height 10 .";
Connection aConnection = ConnectionConfiguration.to("mem")
.credentials("admin", "admin")
.reasoning(true)
.connect();
final String aQuery = "select ?x ?area where { ?x <urn:test:area> ?area }";
try {
aConnection.begin();
aConnection.add().io()
.format(RDFFormat.TURTLE)
.stream(new ByteArrayInputStream(aData.getBytes(Charsets.UTF_8)));
aConnection.commit();
assertEquals(0, AdunaIterations.size(aConnection.select(aQuery).execute()));
aConnection.begin();
aConnection.add().graph(ExpressionFactory.rule(aRule).graph());
aConnection.commit();
assertEquals(2, AdunaIterations.size(aConnection.select(aQuery).execute()));
}
finally {
aConnection.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment