我尝试在java owl-api中使用swrl和pellet。我看过: owl2 and swrl tutorial test of builtin swrl in pellet 和swrl definition
我设法使用swrl进行简单的推理,例如: A(?x) - > B(?x)的
但是当我需要使用“builtin”swrl谓词时,我就会陷入困境(相等,更大,更小,等等......)我觉得某个地方有一个问题是我的测试程序(可能在命名空间中)但是我无法找到。
预期输出为:
package Sockets;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class UDPEchoClient {
public static void main(String[] args) throws SocketException{
DatagramSocket socket = new DatagramSocket();
try {
DatagramPacket packet = new DatagramPacket(
"Hello server".getBytes(),
"Hello server".getBytes().length,
InetAddress.getByName("127.0.0.1"),
1234);
socket.send(packet);
byte[] byteArray = new byte[256];
DatagramPacket packet2 = new DatagramPacket(byteArray, byteArray.length);
socket.receive(packet2);
String myString = new String(byteArray);
System.out.println("Client received: " + myString);
} catch (UnknownHostException e) {
System.err.println("UnknownHostException!");
System.exit(1);
} catch (IOException e){
System.err.println("IOException!");
System.exit(1);
}
socket.close();
}
}
但实际输出为:
<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)
找不到A类。 这是我的程序代码:
<http://org.katk/Motifs#me> -> owl:Thing
prop(me, 50)
A(?x) ← prop(?x, ?y) ⋀ equal(?y, 50)
测试程序使用以下命令运行:
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();
}
}
}
有人可以帮我解决问题吗?
答案 0 :(得分:1)
答案是:在owl-api / pellet实现中,必须使用特殊原子“getSWRLBuiltInAtom”juste来使用内置函数,就像在'applyAll()'方法的下面更正中一样。这个原子能够特别评估内置swrl谓词的特殊评估。
[000000 -> 2015-07-10 14:29:15, 111111 -> 2015-07-11 14:29:15]