View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
konstantin baierer > RDF-TrineX-RuleEngine-Jena-0.001 > RDF::TrineX::RuleEngine::Jena



Annotate this POD

View/Report Bugs
Module Version: 0.001   Source  


RDF::TrineX::RuleEngine::Jena - Wrapper around Jena's rule engine for reasoning over RDF


    use RDF::Trine::Namespace qw(rdf rdfs);

    my $one_triple = "<test/classA> <${rdfs}domain> <test/ClassB> .";

    my $reasoner = RDF::TrineX::RuleEngine::Jena->new;
    my $model_inferred = $reasoner->apply_rules(
        input => \ $one_triple,
        rules => 'rdfs-fb',
        purge_schemas => ':all',

    print $model_inferred->size;    
    # 7

    my $serializer = RDF::Trine::Serializer->new('turtle' , namespaces => { rdf => $rdf, rdfs => $rdfs });
    print $serializer->serialize_model_to_string( $model_inferred );

    # <test/ClassB> rdfs:subClassOf rdfs:Resource, <test/ClassB> ;
    #     a rdfs:Class .
    # <test/classA> rdfs:domain <test/ClassB> ;
    #     a rdf:Property, rdfs:Resource .


This module is a convenience wrapper around a call to Jena's jena.RuleMap command line rule-engine interface. It transparently handles serialization and creation of temporary files, but it relies on a working Java installation and knowledge of the location of the Jena framework.

Finding Jena

When building this module, the Jena framework can be downloaded or a path to an existing Jena installation can be specified. This path is stored in a shared file. If you can't or don't want to specify it at build time, you can set the JENAROOT environment variable to the location of the extracted Jena download. Finally you can pass the path to it at runtime to the constructor.

RDF::Trine vs. Jena Format names

    Trine    | Jena
    ntriples | N-TRIPLE
    turtle   | TURTLE
    rdxml    | RDF/XML, RDF/XML-ABBREV
    n3       | N3-PP, N3-PLAIN, N3-TRIPLE



A Path::Class::Dir object of the Jena directory.


The Version of Jena used, determined from the jena-X.X.X-sources.jar file.


Archive::Zip object for the jena-X.X.X-sources.jar file. Contains the predefined rulesets.


Array reference holding the paths to all the <jar> files required for Jena to run.



Returns a new RDF::TrineX::RuleEngine::Jena object. Before

The optional JENAROOT argument holds the path to the extracted Jena source. If not set, JENAROOT is determined as described in "JENAROOT".


Applies a set of Jena rules to RDF input and adds the inferred statements to the output model.

input => $input_data


$input_data is serialized, written to a temporary file and fed to "exec_jena_rulemap" as the filename_input argument. Currently, the following data types are handled:

  • RDF::Trine::Model.
        my $model = RDF::Trine::Model->temporary_model;
            input => $model,
            rules => ...,
  • String: Treated as the path to a file containing a serialized RDF graph.
            input => 'my_file.nt',
            rules => ...,
  • Scalar reference: Treated as a reference to a serialized RDF graph.
        my $input_ttl = <'EOF';
        @prefix rdfs: .
        <Tiny> rdfs:subClassOf <Small> .
            input => \ $input_ttl,
            input_format => 'TURTLE',
            rules => ...,
rules => $rules_data


$rules_data can be any of the following:

  • String matching one of the "available_rulesets": The appropriate rules file is loaded from "JENA_SOURCES_JAR".
            input => ...,
            rules => 'rdfs',
  • Scalar reference: The dereferenced value is treated as a string of rules.
        my $rules = "[dummy: (?a ?b ?c) -> (?a rdfs:label "This is stupid") ]";
            input => ...,
            rules => \ $rules,
  • Any other string: Treat $rules_data as a filename and load rules from there.
            input => ...,
            rules => '/path/to/my/ruleset.rules',
output => ($model|":fh"|":filename"|":string"|$string)

If specified, inferred statements are written to this model, otherwise a temporary model is created. If you set output to the same value as input, inferred statements are added to the original model.

  • $model: The statements are added to this RDF::Trine::Model. Setting this to the same model as in input will cause all rule-based statement removals to be ignored since there currently is no way of tracking which statements were by applying the rules.
  • ":fh": If this special string (case-insensitive) is supplied, a readable filehandle to the raw output of jena.RuleMap is returned. purge_schemas is ignored.
        my $fh = $reasoner->apply_rules(
            input => ...,
            rules => ...,
            output => ':FH',
        while (<$fh>) {
            my ($s, $p, $o ) = $_ =~ m/^\s*<([^>]+>\s+<([^>]+>\s+<([^>]+>\s*.$/;
  • ":filename": If this special string (case-insensitive) is supplied, the filename of the temporary file containing the raw output of jena.rulemap is returned . purge_schemas is ignored.
        use File::Slurp;
        my $fname = $reasoner->apply_rules(
            input => ...,
            rules => ...,
            output => ':filename',
        my $contents = read_file $fname;
  • ":string": If this special string (case-insensitive) is supplied, the complete raw output of jena.RuleMap is returned. purge_schemas is ignored.
        my $serialized = $reasoner->apply_rules(
            input => ...,
            rules => ...,
            output => ':sTRing',
  • $string: Any other string is treated as a filename to write the raw output of jena.RuleMap to. purge_schemas is ignored.
        my $serialized = $reasoner->apply_rules(
            input => 'data.nt',
            rules => ...,
            output => 'data_inferred.nt',
purge_schemas => (\@list_of_schemanames|":all")

Jena's rule engine adds lots and lots of schema statements about rdf, rdfs, owl, xsd plus some internals. You can tell RDF::TrineX::RuleEngine::Jena to purge those statements by supplying an array ref of schema names to purge_schemas.

Specifying :all removes all schema statements, RDF::TrineX::RuleEngine::Jena knows about.

        input => ...,
        rules => ...,
        purge_schemas => ':all',

is equivalent to

        input => ...,
        rules => ...,
        purge_schemas => [qw( rdf rdfs daml xsd owl jena )],


Sets and resets CLASSPATH and runs java jena.RuleMap ... using a system call. This is all this function does, capturing STDIN and STDERR and parsing/serializing happens in apply_rules.



Filename of the .rules file


File name of the file containing the assertions.

input format

The format of the input file, in Jena notation (i.e. 'N-TRIPLE', 'TURTLE', 'RDF/XML'...)


Format of the result printed to STDOUT, again in Jena notation.


When this flag is set, Jena will only return deduced and schema statements, as opposed to the original model with added and removed statements when the flag is not set.


Given two models A and B, remove all statements from A that are also in B.


Remove all statements of the form X owl:equivalentProperty X.


Lists the available predefined rulesets shipped with Jena that aren't broken. Currently, these are:


Get the filename of a predefined ruleset within "JENA_SOURCES_JAR".


Konstantin Baierer <>


"/ " in http:
syntax highlighting: