?? example04.java
字號:
package ex04;
import java.io.*;
import java.util.*;
import org.semanticweb.kaon2.api.*;
import org.semanticweb.kaon2.api.logic.*;
import org.semanticweb.kaon2.api.owl.elements.*;
import org.semanticweb.kaon2.api.formatting.*;
import org.semanticweb.kaon2.api.reasoner.*;
/**
* This example shows how to create a simple ontology containing rules. Also, it shows how to run sample
* queries. You should familiarize yourself with examples 1 and 2 before you go through this example.
*/
public class Example04 {
public static void main(String[] args) throws Exception {
// To create an ontology, we again start by creating a connection.
// We again need to register a resolver that will provide a physical URI
// for the ontology. In this example, the physical URI is relative to the current directory.
KAON2Connection connection=KAON2Manager.newConnection();
DefaultOntologyResolver resolver=new DefaultOntologyResolver();
resolver.registerReplacement("http://kaon2.semanticweb.org/example04","file:example04.xml");
connection.setOntologyResolver(resolver);
// We create an ontology by specifying its logical URI. The resolver provides the physical URI.
// Up until now this example is the same as Example 2.
Ontology ontology=connection.createOntology("http://kaon2.semanticweb.org/example04",new HashMap<String,Object>());
// We now create a sample ontology describing relationships among objects in a domain.
OWLClass person=KAON2Manager.factory().owlClass("http://kaon2.semanticweb.org/example04#person");
OWLClass student=KAON2Manager.factory().owlClass("http://kaon2.semanticweb.org/example04#student");
OWLClass professor=KAON2Manager.factory().owlClass("http://kaon2.semanticweb.org/example04#professor");
OWLClass project=KAON2Manager.factory().owlClass("http://kaon2.semanticweb.org/example04#project");
OWLClass euProject=KAON2Manager.factory().owlClass("http://kaon2.semanticweb.org/example04#euProject");
OWLClass dfgProject=KAON2Manager.factory().owlClass("http://kaon2.semanticweb.org/example04#dfgProject");
OWLClass topic=KAON2Manager.factory().owlClass("http://kaon2.semanticweb.org/example04#topic");
ObjectProperty worksOn=KAON2Manager.factory().objectProperty("http://kaon2.semanticweb.org/example04#worksOn");
ObjectProperty projectHasTopic=KAON2Manager.factory().objectProperty("http://kaon2.semanticweb.org/example04#projectHasTopic");
ObjectProperty personKnowsAboutTopic=KAON2Manager.factory().objectProperty("http://kaon2.semanticweb.org/example04#personKnowsAboutTopic");
Individual semanticWeb=KAON2Manager.factory().individual("http://kaon2.semanticweb.org/example04#semanticWeb");
Individual descriptionLogics=KAON2Manager.factory().individual("http://kaon2.semanticweb.org/example04#descriptionLogics");
Individual owl=KAON2Manager.factory().individual("http://kaon2.semanticweb.org/example04#owl");
// We perform updates as in Example 2, by adding a sequence of axioms to the ontology.
List<OntologyChangeEvent> changes=new ArrayList<OntologyChangeEvent>();
// We now add describe the domain of the ontology.
// All students are persons.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().subClassOf(student,person),OntologyChangeEvent.ChangeType.ADD));
// All professors are persons.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().subClassOf(professor,person),OntologyChangeEvent.ChangeType.ADD));
// EU projects are projects
changes.add(new OntologyChangeEvent(KAON2Manager.factory().subClassOf(euProject,project),OntologyChangeEvent.ChangeType.ADD));
// DFG projects are projects
changes.add(new OntologyChangeEvent(KAON2Manager.factory().subClassOf(dfgProject,project),OntologyChangeEvent.ChangeType.ADD));
// Persons work on projects.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyDomain(worksOn,person),OntologyChangeEvent.ChangeType.ADD));
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyRange(worksOn,project),OntologyChangeEvent.ChangeType.ADD));
// Projects have topics.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyDomain(projectHasTopic,project),OntologyChangeEvent.ChangeType.ADD));
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyRange(projectHasTopic,topic),OntologyChangeEvent.ChangeType.ADD));
// Persons know about topics.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyDomain(personKnowsAboutTopic,person),OntologyChangeEvent.ChangeType.ADD));
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyRange(personKnowsAboutTopic,topic),OntologyChangeEvent.ChangeType.ADD));
// Semantic Web, description logics and OWL are topics.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().classMember(topic,semanticWeb),OntologyChangeEvent.ChangeType.ADD));
changes.add(new OntologyChangeEvent(KAON2Manager.factory().classMember(topic,descriptionLogics),OntologyChangeEvent.ChangeType.ADD));
changes.add(new OntologyChangeEvent(KAON2Manager.factory().classMember(topic,owl),OntologyChangeEvent.ChangeType.ADD));
// We now create a rule that axiomatizes the following relationship:
//
// If
// a person X works on a project Y, and
// the project Y is about a topic Z,
// then
// the person X knows about topic Z.
//
// In Prolog, this rule would be written like this:
// personKnowsAboutTopic(X,Z) :- worksOn(X,Y), projectHasTopic(Y,Z).
//
// Although the practice often disputes this rule, we shall pretend that we live in a perfect
// world where only competent people are woking on interesting projects. (sigh!)
//
// The above rule is directly converted into an object strucutre. We first create the variables X, Y and Z:
Variable X=KAON2Manager.factory().variable("X");
Variable Y=KAON2Manager.factory().variable("Y");
Variable Z=KAON2Manager.factory().variable("Z");
// We now create the literals (notice that all of them are positive):
Literal personKnowsAboutTopic_X_Z=KAON2Manager.factory().literal(true,personKnowsAboutTopic,new Term[] { X,Z });
Literal worksOn_X_Y=KAON2Manager.factory().literal(true,worksOn,new Term[] { X,Y });
Literal projectHasTopic_Y_Z=KAON2Manager.factory().literal(true,projectHasTopic,new Term[] { Y,Z });
// We now create the rule.
Rule rule=KAON2Manager.factory().rule(
personKnowsAboutTopic_X_Z, // this is the rule head, i.e. the consequent of the rule
new Literal[] { worksOn_X_Y,projectHasTopic_Y_Z } // this is the rule body, i.e. the condition of the rule
);
// Rule is a kind of axiom, so it can be added to the ontology in the same way as
// any axiom is added, i.e. by an OntologyChangeEvent.
changes.add(new OntologyChangeEvent(rule,OntologyChangeEvent.ChangeType.ADD));
// We shall create another rule which says:
//
// If
// a project X is about Semantic Web
// then
// the project X is about description logic.
//
// In Prolog, this rule would be written like this:
// projectHasTopic(X,example04:descriptionLogics) :- projectHasTopic(X,example04:semanticWeb)
//
// The above rule is the dream of every logician ("you shall be assimilated, resistence is futile",
// if you know what I mean :-)
//
// We do not to create new variable X; we simply reuse the already created object.
// We now create the literals inline:
rule=KAON2Manager.factory().rule(
KAON2Manager.factory().literal(true,projectHasTopic,new Term[] { X,descriptionLogics }),
new Literal[] { KAON2Manager.factory().literal(true,projectHasTopic,new Term[] { X,semanticWeb }) }
);
// We add the rule to the chande list.
changes.add(new OntologyChangeEvent(rule,OntologyChangeEvent.ChangeType.ADD));
// Creating rules in the above way can be tedious. Therefore, the LISP-like syntax
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -