0

Java owl-api でペレットを使用して swrl を使用しようとしています。私は見てきました: owl2 と swrl チュートリアル ペレットswrl 定義の組み込み swrl のテスト

A(?x) -> B(?x) のような単純な推論にswrlを使用することができました

しかし、「組み込み」のswrl述語(equal、greaterThan、lesserThanなど)を使用する必要がある場合に行き詰まります。テストプログラムのどこかに問題があると思います(おそらく名前空間にあります)が、できませんどこを見つける。

予想される出力は次のとおりです。

<http://org.katk/Motifs#me> -> owl:Thing
<http://org.katk/Motifs#me> -> <http://org.katk/Motifs#A>
prop(me, 50)
A(?x) ← prop(?x, ?y) ⋀ equal(?y, 50)

しかし、実際の出力は次のとおりです。

<http://org.katk/Motifs#me> -> owl:Thing
prop(me, 50)
A(?x) ← prop(?x, ?y) ⋀ equal(?y, 50)

A クラスが見つかりません。 ここに私のプログラムのコード:

package org.katk;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.OWLObjectRenderer;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataPropertyExpression;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyChange;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.SWRLAtom;
import org.semanticweb.owlapi.model.SWRLVariable;
import org.semanticweb.owlapi.reasoner.NodeSet;
import uk.ac.manchester.cs.owlapi.dlsyntax.DLSyntaxObjectRenderer;

import com.clarkparsia.pellet.owlapiv3.PelletReasoner;
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;

public class Motifs {

    public static <T> IRI iri(final Class<T> clazz, final String name) {
        return IRI.create("http://" + clazz.getPackage().getName() + "/" + clazz.getSimpleName() + "#" + name);
    }

    final static IRI base = iri(Motifs.class, "ontology");
    final OWLDataFactory factory;
    final OWLOntology ontology;
    final OWLOntologyManager manager;

    public Motifs() throws OWLOntologyCreationException {
        manager = OWLManager.createOWLOntologyManager();
        factory = manager.getOWLDataFactory();
        ontology = manager.createOntology(base);
    }

    public OWLNamedIndividual declareIndividual(final IRI iri)
    {
        final OWLNamedIndividual baby = factory.getOWLNamedIndividual(iri);
        manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(baby));
        return baby;
    }

    public List<OWLOntologyChange> addAxiom(final OWLAxiom axiom)
    {
        return manager.addAxiom(ontology, axiom);
    }

    public void applyAll() {
    final OWLClass clsA = factory.getOWLClass(iri(Motifs.class, "A"));

    final OWLDataPropertyExpression prop = factory.getOWLDataProperty(iri(Motifs.class, "prop"));
    final OWLDataPropertyExpression equal = factory.getOWLDataProperty(IRI.create("http://www.w3.org/2003/11/swrlb#equal"));

    final SWRLVariable varX = factory.getSWRLVariable(iri(Motifs.class, "x"));
    final SWRLVariable varY = factory.getSWRLVariable(iri(Motifs.class, "y"));
    final OWLLiteral literal = factory.getOWLLiteral(50);
    final OWLNamedIndividual ind = declareIndividual(iri(Motifs.class, "me"));
    addAxiom(factory.getOWLDataPropertyAssertionAxiom(prop, ind, literal));

    final Set<SWRLAtom> antecedent = new HashSet<>();
    antecedent.add(factory.getSWRLDataPropertyAtom(prop, varX, varY));
    antecedent.add(factory.getSWRLDataPropertyAtom(equal, varY, factory.getSWRLLiteralArgument(literal)));

    final Set<SWRLAtom> consequences = new HashSet<>();
    consequences.add(factory.getSWRLClassAtom(clsA, varX));

    addAxiom(factory.getSWRLRule(antecedent, consequences));

    final PelletReasoner pellet = PelletReasonerFactory.getInstance().createReasoner(ontology);
    final NodeSet<OWLClass> x = pellet.getTypes(ind, false);
    x.getNodes().forEach(node -> System.out.println(ind + " -> " + node.getRepresentativeElement()));

    System.out.println("------------------------------------");
    if (!pellet.isConsistent()) System.out.println("Ontology isn't consistent");
    final OWLObjectRenderer renderer = new DLSyntaxObjectRenderer();
    for (final OWLAxiom r : ontology.getAxioms())
        System.out.println(renderer.render(r));
    }

    public static void main(String[] argv)
    {
        try
        {
            (new Motifs()).applyAll();
        }
        catch(final Exception e)
        {
            e.printStackTrace();
        }
    }
}

これを使用して実行されるテストプログラム:

groupId:net.sourceforge.owlapi, artifact:owlapi-distribution, version:3.4.10
groupId:com.github.ansell.pellet, artifcat:pellet-owlapiv3, version:2.3.6-ansell

誰かが私を修正するのを手伝ってもらえますか?

4

1 に答える 1

1

答えは: owl-api/pellet の実装では、「applyAll()」メソッドの次の修正のように、特別なアトム「getSWRLBuiltInAtom」を使用してビルトインを使用する必要があります。このアトムは、組み込みの swrl 述語で特別な評価動作を有効にします。

public void applyAll() {
    final OWLClass clsA = factory.getOWLClass(iri(Motifs.class, "A"));

    final OWLDataPropertyExpression prop = factory.getOWLDataProperty(Utils.iri(Motifs.class, "prop"));
    //final OWLDataPropertyExpression equal = factory.getOWLDataProperty(IRI.create("http://www.w3.org/2003/11/swrlb#equal"));

    final SWRLVariable varX = factory.getSWRLVariable(iri(Motifs.class, "x"));
    final SWRLVariable varY = factory.getSWRLVariable(iri(Motifs.class, "y"));
    final OWLLiteral literal = factory.getOWLLiteral(50);
    final OWLNamedIndividual ind = declareIndividual(iri(Motifs.class, "me"));
    addAxiom(factory.getOWLDataPropertyAssertionAxiom(prop, ind, literal));

    final Set<SWRLAtom> antecedent = new HashSet<>();
    antecedent.add(factory.getSWRLDataPropertyAtom(prop, varX, varY));
    // antecedent.add(factory.getSWRLDataPropertyAtom(equal, varY, factory.getSWRLLiteralArgument(literal)));
    final List<SWRLDArgument> args = new ArrayList<>(2);
    args.add(varY);
    args.add(factory.getSWRLLiteralArgument(literal));
    antecedent.add(factory.getSWRLBuiltInAtom(IRI.create("http://www.w3.org/2003/11/swrlb#equal"), args));

    final Set<SWRLAtom> consequences = new HashSet<>();
    consequences.add(factory.getSWRLClassAtom(clsA, varX));

    addAxiom(factory.getSWRLRule(antecedent, consequences));

    final PelletReasoner pellet = PelletReasonerFactory.getInstance().createReasoner(ontology);
    final NodeSet<OWLClass> x = pellet.getTypes(ind, false);
    x.getNodes().forEach(node -> System.out.println(ind + " -> " + node.getRepresentativeElement()));

    System.out.println("------------------------------------");
    if (!pellet.isConsistent()) System.out.println("Ontology isn't consistent");
    final OWLObjectRenderer renderer = new DLSyntaxObjectRenderer();
    for (final OWLAxiom r : ontology.getAxioms())
        System.out.println(renderer.render(r));
}
于 2015-07-20T11:53:24.940 に答える