好的,我有一个xml文件,它存储超类"SentenceFromUser"和一些随机问题的子类(如"你做什么"((有限制:
有词上第一一些什么
等。
(什么是个人(
然后我定义了称为OpenQuestion和ClosedQuestion的超类。在此代码中,我想查看我的问题将在这两个超类中的哪一个中排序。在Protege中,我可以启动推理器并立即得到哪种问题。但是对于耶拿,推理者并不能真正处理我自己的(使用此代码(创建的问题。我希望有人可以帮助我...法典:
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;
}
创建具有值函数
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;
}
}
保存模型功能
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();
}
}
我相信这里有两个问题:
hasWordOnFirst some what
存在主义需要一个类表达式而不是一个个体;解析器将推断what
是一个类,对个体what
一语双关。你的意思是使用oneOf {what}
吗?
其次,您正在使用write()
进行OntModel
。你想写出推断的陈述吗?在这种情况下,正如 javadoc forwrite()
所说,您需要writeAll()
.