0

I have created new custom built-ins inside my Java program. They work fine and if I execute them into a ".txt" file making a rule I get the result I expect to . The problem is that I can use this built-ins only where I have implemented them (in the same file).

For example, I have a java class file which is called "RulesOntology_DiffYear.java" where I have created a built-in called "myDiffDate" which does the difference between two dates and it return the number of years of difference.

If I try to write the rule (using the built-in "myDiffDate") and I try to execute it inside the file "RulesOntology_DiffYear.java", the rule recognises the new built-in and it runs it without problems.

If I try to write the same rule in another file ".java", the built-in I have created in "RulesOntology_DiffYear.java", it is not recognised, of course.

How can I use my custom built-ins in another files ? Like the default built-ins ? Need I to create a library or there is something else to do ?

I have found this Create a library for new built-ins Jena but I do not really know how it works (even if I tried it) and I do not know if it is the easiest solution for my purpose.

In my file "DiffDateLib.java" I have defined my custom built-in "myDiffDateYear".

I have created another file named "MyReasonerFactory.java" with the following code :

public class MyReasonerFactory implements ReasonerFactory {

    /** The single global static registry */
    public static BuiltinRegistry theRegistry;

    private static final String RULE_LOC = "????";

    static {
        BuiltinRegistry.theRegistry.register(new DiffDateLib());
    }

    @Override
    public Reasoner create(Resource r) {
        final GenericRuleReasoner reasoner = new GenericRuleReasoner(this, r);
        reasoner.setRules(FBRuleReasoner.loadRules(RULE_LOC));
        return reasoner;
    }

    @Override
    public Model getCapabilities() {
        return GenericRuleReasonerFactory.theInstance().getCapabilities();
    }

    @Override
    public String getURI() {
        return RULE_LOC;
    }

}

What can I write into the variable "RULE_LOC" ? Which filepath ?

I have made also another class called "tryNoBuiltin.java" which contains the main of the program :

public class tryNoBuiltin {

    public static void main(String[] args) throws OWLOntologyStorageException,
    OWLOntologyCreationException, IOException  {

        String percorsoFile ="./prova_rules_M_rdf.owl";
        String ruleFile= "./prova_rules_M_rdf_7_diffDate.txt";

        Model rawModel = ModelFactory.createDefaultModel();

        MyReasonerFactory MyReas = new MyReasonerFactory();

        //create a resource (empty model)
        Resource configuration = rawModel.createResource();

        // set engine mode
        configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
        // set the rules file
        configuration.addProperty(ReasonerVocabulary.PROPruleSet, ruleFile);

        List<Rule> rules = Rule.rulesFromURL(ruleFile);

        GenericRuleReasoner reasonerRULE = (GenericRuleReasoner) GenericRuleReasonerFactory.theInstance().create(configuration);
        reasonerRULE.setRules(rules);

        Model modelRULE= FileManager.get().loadModel(percorsoFile);
        //create the inference model
        InfModel infModelRULE = ModelFactory.createInfModel(reasonerRULE, modelRULE);
        //force starting the rule execution
        infModelRULE.prepare();

        //write down the result in RDFXML form
        infModelRULE.write(System.out);

    }

}

In the main I can see the output so, the new built-in (which I have used into the rule file "prova_rules_M_rdf_7_diffDate.txt") is recognised . But I think I did not use correctly the class "MyReasonerFactory". Can you provide me an example ? Where can I use its methods ?

Thank you all!

Community
  • 1
  • 1
user3563844
  • 113
  • 1
  • 8
  • 1
    The answer to your earlier question that you linked to shows how to define and use built-ins. If you're having a problem with an implementation of built ins, then you need to provide enough code for us to reproduce the problem. – Joshua Taylor Jun 19 '14 at 04:05
  • For example, which file should I link here ? private static final String RULE_LOC = "/some/directory/in/my/jar/filename.extensiondoesntmatter"; – user3563844 Jun 19 '14 at 08:11
  • 1
    You should create a complete minimal example and paste it here. It should be *minimal* in that is doesn't include anything unnecessary to reproduce the problem. Yes, that might mean recreating the problem from scratch. It should be *complete* in that it's a file or two that anyone else should be able to copy&paste/download and get the same results that you do. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Oftentimes, while putting together a Minimal, Complete, and Verifiable example, you'll find the solution anyhow. – Joshua Taylor Jun 19 '14 at 10:33

1 Answers1

1

This should probably work for you. The idea is that the MyReasonerFactory is acting as a way to retrieve a reasoner for your domain. Not only does it register the builtins, but it also gets/sets the rules.

public class MyReasonerFactory implements ReasonerFactory {

    private static final String RULE_LOC = "./prova_rules_M_rdf_7_diffDate.txt";

    static {
        BuiltinRegistry.theRegistry.register(new DiffDateLib());
    }

    @Override
    public Reasoner create(Resource r) {
        final GenericRuleReasoner reasoner = new GenericRuleReasoner(this, r);
        reasoner.setRules(Rule.rulesFromURL(RULE_LOC));
        return reasoner;
    }

    @Override
    public Model getCapabilities() {
        return GenericRuleReasonerFactory.theInstance().getCapabilities();
    }

    @Override
    public String getURI() {
        // TODO NOTE this is just a suggestion
        return "urn:ex:provaRuleReasoner";
    }

}

You did a few redundant things while loading the rules. ie: you set it in the config as well as manually retrieved them and parsed them. I removed the extra stuff.

public class tryNoBuiltin {

    public static void main(String[] args) throws OWLOntologyStorageException,
    OWLOntologyCreationException, IOException  {

        //Create a configuration model
        Resource configuration = ModelFactory.createDefaultModel().createResource();
        configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");

        MyReasonerFactory MyReas = new MyReasonerFactory();
        GenericRuleReasoner reasonerRULE = (GenericRuleReasoner)MyReas.create(configuration);

        Model modelRULE= FileManager.get().loadModel("./prova_rules_M_rdf.owl");
        InfModel infModelRULE = ModelFactory.createInfModel(reasonerRULE, modelRULE);
        infModelRULE.prepare();

        //write down the result in RDFXML form
        infModelRULE.write(System.out);
    }

}
Rob Hall
  • 2,693
  • 16
  • 22