Извлечение правил из RDF-файла с использованием Sparql - PullRequest
1 голос
/ 05 марта 2019

Это правило, которое я хочу извлечь в формате RDF / XML :

<rdf:Description rdf:about="http://www.semanticweb.org/myCompany/ontologies#x">
    <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#Variable"/>
</rdf:Description>
<rdf:Description>
    <swrla:isRuleEnabled rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</swrla:isRuleEnabled>
    <rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string">testing a rule</rdfs:comment>
    <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">TEST</rdfs:label>
    <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#Imp"/>
    <swrl:body>
        <rdf:Description>
            <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
            <rdf:first>
                <rdf:Description>
                    <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
                    <swrl:classPredicate rdf:resource="http://www.semanticweb.org/myCompany/ontologies#depth"/>
                    <swrl:argument1 rdf:resource="http://www.semanticweb.org/myCompany/ontologies#x"/>
                </rdf:Description>
            </rdf:first>
            <rdf:rest>
                <rdf:Description>
                    <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
                    <rdf:first>
                        <rdf:Description>
                            <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#BuiltinAtom"/>
                            <swrl:builtin rdf:resource="http://www.w3.org/2003/11/swrlb#greaterThan"/>
                            <swrl:arguments>
                                <rdf:Description>
                                    <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#List"/>
                                    <rdf:first rdf:datatype="http://www.w3.org/2001/XMLSchema#int">2</rdf:first>
                                    <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
                                </rdf:Description>
                            </swrl:arguments>
                        </rdf:Description>
                    </rdf:first>
                    <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
                </rdf:Description>
            </rdf:rest>
        </rdf:Description>
    </swrl:body>
    <swrl:head>
        <rdf:Description>
            <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
            <rdf:first>
                <rdf:Description>
                    <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
                    <swrl:classPredicate rdf:resource="http://www.semanticweb.org/myCompany/ontologies#profondeur"/>
                    <swrl:argument1 rdf:resource="http://www.semanticweb.org/myCompany/ontologies#Bad"/>
                </rdf:Description>
            </rdf:first>
            <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
        </rdf:Description>
    </swrl:head>
</rdf:Description>

Мне интересно, могу ли я извлечь правило с синтаксисом, например:

depth(?x) ^ swrlb:greaterThan(2) -> profondeur(Bad)

Кроме того, я не хочу использовать свое правило внутри protégé или другого программного обеспечения, я ищу внешний разъем, такой как Jena или Pellet

Жду ваших ответов,

Aloïs, пользователь Sparql.

1 Ответ

0 голосов
/ 06 марта 2019

Спасибо за вашу помощь, мне наконец-то удалось написать код с OWL API

        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        try {
            OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File(ONTOLOGY_FILE_NAME));
            Set<SWRLRule> rules = ontology.getAxioms(AxiomType.SWRL_RULE);
            for (SWRLRule r : rules) {
                // body
                for (SWRLAtom a : r.getBody()) {
                    System.out.println(a.getPredicate().toString());
                    for (SWRLArgument ar : a.getAllArguments()) {
                        System.out.println(ar.toString());
                    }
                }

                // head
                for (SWRLAtom a : r.getHead()) {
                    System.out.println(a.getPredicate().toString());
                    for (SWRLArgument ar : a.getAllArguments()) {
                        System.out.println(ar.toString());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
...