Ok I have a xml file that stores the superclass "SentenceFromUser" with the subclasses of some random questions (like "what do you do") (done with restrictions:
hasWordOnFirst some what
etc.
(what is an individual)
Then I have defined superclasses called OpenQuestion and ClosedQuestion. In this code I want to see in which of those two superclasses my question would get sorted in. In Protege I can start the Reasoner and instantly get which is which kind of Question. But with Jena the Reasoner doesn't really work with my own (with this code) created questions. I hope somebody can help me... Code:
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");
}
}
output function
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 function
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 function
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();
}
}