0

OK、スーパークラス「SentenceFromUser」をいくつかのランダムな質問(「何をしますか」など)のサブクラスとともに保存するxmlファイルがあります(制限付きで行われます:

hasWordOnFirstいくつ

個人とは)

次に、OpenQuestion および ClosedQuestion というスーパークラスを定義しました。このコードでは、私の質問がこれら 2 つのスーパークラスのどちらに分類されるかを確認したいと思います。しかし、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();
    }
}
4

1 に答える 1