📄 example04.java
字号:
// can be used to encode rules as well. To use it, we first initialize an instance
// of the Namespaces class in the same way as in Example 2.
Namespaces namespaces=new Namespaces();
namespaces.registerPrefix("example04","http://kaon2.semanticweb.org/example04#");
// We shall create a yet another rule that is similar to the above rule which says the following:
//
// If
// a person X knows about OWL
// then
// the person X knowls about description logics as well.
//
// In Prolog, this rule would be written like this:
// personKnowsAboutTopic(X,example04:descriptionLogics) :- personKnowsAboutTopic(X,example04:owl)
rule=(Rule)KAON2Manager.factory().axiom(
"[rule ["+ // States that the rule follows. The opening brackes introduces a set of head literals.
" [[oprop example04:personKnowsAboutTopic] X [example04:descriptionLogics]]"+ // This is the specification of one head literal. The individuals must be enclosed in [].
" ] ["+ // The bracket ] terminated the set of head literals. The bracket [ opens the set of body literals.
" [[oprop example04:personKnowsAboutTopic] X [example04:owl]]"+ // This is the one body literal.
" ]"+ // This closes the set of body literals.
"]" // This closes the rule.
,namespaces);
// We add the rule to the chande list.
changes.add(new OntologyChangeEvent(rule,OntologyChangeEvent.ChangeType.ADD));
// We now add some facts. We shall use these facts to query answering later.
Individual boris=KAON2Manager.factory().individual("http://kaon2.semanticweb.org/example04#boris");
// Boris is a student.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().classMember(student,boris),OntologyChangeEvent.ChangeType.ADD));
// DIP is an EU project.
Individual dip=KAON2Manager.factory().individual("http://kaon2.semanticweb.org/example04#dip");
changes.add(new OntologyChangeEvent(KAON2Manager.factory().classMember(euProject,dip),OntologyChangeEvent.ChangeType.ADD));
// DIP is about semantic web.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyMember(projectHasTopic,dip,semanticWeb),OntologyChangeEvent.ChangeType.ADD));
// Boris works on DIP.
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyMember(worksOn,boris,dip),OntologyChangeEvent.ChangeType.ADD));
// We now apply the changes to the ontology. Only after this action is done,
// the axioms created above are added to the ontology.
ontology.applyChanges(changes);
// We now save the ontology by calling the serializer. Observe that the
// location where the ontology is stored does not need to be the same
// as the physical URI. This is deliberate, as this allows you to implement
// 'Save As' operation. The second parameter defines the character encoding used
// in the XML file. we save the ontology into 'c:\temp\example04.xml'.
System.out.println("The ontology will be saved into 'c:\\temp\\example04.xml'.");
System.out.println("Please ensure that 'c:\\temp' directory exists.");
ontology.saveOntology(OntologyFileFormat.OWL_XML,new File("c:\\temp\\example04.xml"),"ISO-8859-1");
System.out.println("The ontology was saved successfully into 'c:\\temp\\example04.xml'.");
// We are now ready to ask some questions. Our goal is to obtain the expertise of all persons. In order words,
// we want to ask the following conjunctive query:
//
// Person(X),personKnowsAboutTopic(X,Y)
//
// The first thing we need to do is create an instance of the reasoner. Reasoners consume resources, so you should keep
// them around only for as long as you need them. However, if you plan to ask several queries, it is *MUCH* more efficient
// to use one and the same reasoner for all queries. One reasoner can be used to run several concurrent queries.
Reasoner reasoner=ontology.createReasoner();
// We now create the query object. A query consists of the following things:
//
// - a list of literals definint the query (they are created as usual)
// - a list of distinguished variables, i.e. the variables that will be returned
Query whatDoPeopleKnowAbout=reasoner.createQuery(new Literal[] {
KAON2Manager.factory().literal(true,person,new Term[] { X }),
KAON2Manager.factory().literal(true,personKnowsAboutTopic,new Term[] { X,Y }),
},new Variable[] { X,Y});
// Creating the query has the effect of compiling it. A single query can be executed then several times.
// A query is executed by invoking the Query.open() method.
System.out.println();
System.out.println("The list of people and things that they know about:");
System.out.println("---------------------------------------------------");
whatDoPeopleKnowAbout.open();
// We now iterate over the query results.
while (!whatDoPeopleKnowAbout.afterLast()) {
// A query result is a set of tuples. The values in each tuple correspond to the distinguished variables.
// In the above example, the distinguished variables are [X,Y]; this means that the first object in
// the tuple is the value for the X variable, and the second one is the value for the Y variable.
Term[] tupleBuffer=whatDoPeopleKnowAbout.tupleBuffer();
System.out.println("Person '"+tupleBuffer[0].toString()+"' knows about '"+tupleBuffer[1].toString()+"'.");
whatDoPeopleKnowAbout.next();
}
System.out.println("---------------------------------------------------");
// If a query has been successfully opened, it should also be closed; otherwise, a resource leak occurs.
whatDoPeopleKnowAbout.close();
// After a query is closed, it may be reopened. The results of the query will reflect the changes to the ontology.
// To see this, we shall add an additional fact that DIP is about OWL.
changes.clear();
changes.add(new OntologyChangeEvent(KAON2Manager.factory().objectPropertyMember(projectHasTopic,dip,owl),OntologyChangeEvent.ChangeType.ADD));
ontology.applyChanges(changes);
System.out.println();
System.out.println("New facts have been successfully added to the ontology.");
System.out.println();
// We now execute the query again. The results reflect the change in the ontology.
System.out.println("The same query executed again:");
System.out.println("---------------------------------------------------");
whatDoPeopleKnowAbout.open();
while (!whatDoPeopleKnowAbout.afterLast()) {
Term[] tupleBuffer=whatDoPeopleKnowAbout.tupleBuffer();
System.out.println("Person '"+tupleBuffer[0].toString()+"' knows about '"+tupleBuffer[1].toString()+"'.");
whatDoPeopleKnowAbout.next();
}
System.out.println("---------------------------------------------------");
whatDoPeopleKnowAbout.close();
// When a query is not needed any more, it should be disposed of. Forgetting to do so will result in
// a fairly serious memory leak.
whatDoPeopleKnowAbout.dispose();
// Reasoners have to be disposed off after they are not used any more. Forgetting to do so will result in
// a fairly serious memory leak!
reasoner.dispose();
// Don't forget to close the connection!
connection.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -