User-defined Rule Reasoning in Java

Can we load a data file wich contains a rule by using Java ?
I try it with Jena and SNARL but it seems impossible.


There should be no issues loading turtle files with rules in them via whatever method. What is the error you are seeing?

My ttl file is:

@prefix db: http://data/# .
@prefix fx: http://fx/# .
@prefix rdfs: .

db:Jean a fx:male ,
rdfs:label “Jean” .

IF {
?x a fx:male, fx:parent .
?x a fx:father
db:Louis a fx:male .
db:Louis a fx:parent .

the error is:

Exception in thread “main” org.apache.jena.riot.RiotException: [line: 9, col: 1 ] Out of place: [KEYWORD:IF]
at org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.fatal(
at org.apache.jena.riot.lang.LangEngine.raiseException(
at org.apache.jena.riot.lang.LangEngine.exceptionDirect(
at org.apache.jena.riot.lang.LangEngine.exception(
at org.apache.jena.riot.lang.LangTurtleBase.triplesSameSubject(
at org.apache.jena.riot.lang.LangTurtle.oneTopLevelElement(
at org.apache.jena.riot.lang.LangTurtleBase.runParser(
at org.apache.jena.riot.lang.LangBase.parse(
at org.apache.jena.riot.RDFParserRegistry$
at org.apache.jena.riot.RDFDataMgr.process(
at triplestores.StardogJena.insertWithRule(
at controllers.JenaControllerSD.insertModelWithRulesOnStardog(
at main.Triples.main(

I apologize, I should have been more clear… Uploading a ttl file with inline rules like this will NOT work in Sesame/Jena/RDF4J because it’s non-standard Turtle. Via SNARL, however, I am able to load the rule in and query it just fine:

public static void main(String... args) {
		try (Connection conn = Utils.getDefaultConnection("support")) {
			conn.add().io().file(new File("/Users/stephen/support/community/rules.ttl").toPath());

			try (TupleQueryResult res ="SELECT * WHERE { ?f a <http://fx/#father> }").reasoning(true).execute()) {
				while (res.hasNext()) {

Just a couple of gotchas to watch out for:

  1. It might be Discourse eating the formatting, but be sure that your prefixes are surrounded with <>
  2. This also might be Discourse, but your “Jean” label looks to have ‘smart quotes’ instead of standard " characters, making the turtle invalid
1 Like

Thank you very much it seems to work correctly

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.