好的我有一个存储超类" SentenceFromUser"的xml文件。与一些随机问题的子类(如"你做什么")(完成限制:
hasWordOnFirst 一些什么
等
( 是个人)
然后我有定义的超类,称为OpenQuestion和ClosedQuestion。在这段代码中,我想看看我的问题将在哪两个超类中排序。在Protege中,我可以启动Reasoner并立即获得哪种问题。但是对Jena而言,Reasoner并没有真正使用我自己(使用此代码)创建的问题。我希望有人可以帮助我......代码:
public class main {
public static void main (String[] args) {
String state = "learn";
while(0 == 0) {
OntModel m1 = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
m1.read("src/test/new.xml", "RDF/XML");
m1.setStrictMode(false);
String uri = "http://www.semanticweb.org/ontologies/2017/7/untitled-ontology-3#";
Scanner scan = new Scanner(System.in);
System.out.println("Enter a question:");
String input = scan.nextLine();
if (input.equals("exit")) {
break;
}
String[] words = input.split("\\s+");
for (int i = 0; i < words.length; i++) {
words[i] = words[i].replaceAll("[^\\w]", "");
}
//create new question
OntClass sentenceFromUser = m1.getOntClass(uri + "SentenceFromUser");
String questionName = "Question" + ThreadLocalRandom.current().nextInt(1, 100000);
Resource newResQuestion = m1.createResource(uri + questionName);
sentenceFromUser.addSubClass(newResQuestion);
OntClass savedQuestion = m1.getOntClass(uri + questionName);
savedQuestion.addSuperClass(createHasValue(uri, "hasWordOnFirst", words[0], m1));
for (int counter = 1; counter < words.length; counter++) {
OntClass newSuperClass = createHasValue(uri, "hasWord", words[counter], m1);
if(newSuperClass != null) {
savedQuestion.addSuperClass(newSuperClass);
}
else {
if(state.equals("learn")) {
OntClass word = m1.getOntClass(uri + "Word");
ExtendedIterator<OntClass> subClasses = word.listSubClasses();
while(subClasses.hasNext()) {
OntClass sc = subClasses.next();
String subClass = sc.getURI().toString().replaceAll(uri, "");
System.out.print(subClass + "\n");
ExtendedIterator<OntClass> subClassesOfSC = sc.listSubClasses();
while(subClassesOfSC.hasNext()) {
OntClass scosc = subClassesOfSC.next();
String subClassOfSubClass = scosc.getURI().toString().replaceAll(uri, "");
System.out.print("-" + subClassOfSubClass + "\n");
}
}
scan = new Scanner(System.in);
System.out.println("In which Category would you put '" + words[counter] + "'");
input = scan.nextLine();
OntClass category = m1.getOntClass(uri + input);
Individual newWord = m1.createIndividual( uri + words[counter], category );
counter--;
}
}
}
saveModel(m1, "src/test/new.xml", "RDF/XML");
OntModel m2 = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
m2.read("src/test/new.xml", "RDF/XML");
Reasoner r = ReasonerRegistry.getOWLReasoner();
r = r.bindSchema(m2);
OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM;
ontModelSpec.setReasoner(r);
OntModel model = ModelFactory.createOntologyModel(ontModelSpec, m2);
OntClass question = model.getOntClass(uri + questionName);
ExtendedIterator<OntClass> superClasses = question.listSuperClasses();
output(superClasses, uri);
//delete question
OntClass oldResQuestion = m2.getOntClass(uri + questionName);
if(oldResQuestion != null) {
oldResQuestion.remove();
}
saveModel(m2, "src/test/new.xml", "RDF/XML");
}
}
输出功能
public static void output(ExtendedIterator<OntClass> superClasses, String uri) {
while(superClasses.hasNext()) {
OntResource sc = (OntResource)superClasses.next();
if (sc.getURI() != null) {
String superClass = sc.getURI().toString().replaceAll(uri, "");
System.out.print(superClass);
//String firstSuperClass = question.getSuperClass().getURI().toString().replaceAll(uri, "");
switch (superClass) {
case "OpenQuestion":
System.out.print("open question (W-)\n");
return;
case "ClosedQuestion":
System.out.print("closed question (Yes/No)\n");
return;
default:
break;
}
}
}
return;
}
createHasValue 功能
public static OntClass createHasValue(String uri, String propertyName, String individualName, OntModel m) {
Property p = m.getProperty(uri + propertyName);
try {
Individual i = m.getIndividual(uri + individualName.toLowerCase());
return(m.createHasValueRestriction(null, p, i));
}
catch (Exception e) {
return null;
}
}
saveModel 功能
public static void saveModel(OntModel m, String file, String type) {
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(file);
m. write(outputStream, type);
} catch (FileNotFoundException e) {
e.printStackTrace(System.out);
} finally {
m.close();
}
}
答案 0 :(得分:0)
我相信这里有两个问题:
hasWordOnFirst some what
存在主义需要一个阶级表达而不是个人;解析器会将what
推断为一个类,对个人what
进行惩罚。您的意思是使用oneOf {what}
吗?
其次,您使用write()
作为OntModel
。你想写出推断的陈述吗?在这种情况下,正如write()
的javadoc所说,您需要writeAll()
。