在jena:我如何加载本体一次?

时间:2017-11-16 08:45:27

标签: sparql jena ontology

我只需要在Jena中加载一次本体。我的包含SPARQL查询的方法也包含

Model model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
model =  FileManager.get().loadModel("c:/jena/ICD.owl");

除非每次执行查询时方法重新加载本体,否则一切正常。 我只需要将本体加载一次,然后将模型加载到模型中。可见的变量'在方法中。如果我将两个模型语句移动到主类中,则该方法无法看到模型'。 Java中没有全局变量。

以下是代码:

 class store {          
        Model model = ModelFactory.createOntologyModel(     OntModelSpec.OWL_MEM );
         public void myQuery() {
                String pattern = "\\#(.*)$";
                model =  FileManager.get().loadModel("c:/jena/ICD.owl");
                String singleQueryWord1="VEnTRICULAR FIBRILLATION";
                String firstQueryWord2="pedestrian";
                String secondQueryWord2="car";
                String queryPattern1="("+singleQueryWord1+"(.*))";
                String queryPattern2="("+firstQueryWord2+"(.|\\n)*"+secondQueryWord2+")|("+secondQueryWord2+"(.|\\n)*"+firstQueryWord2+")";

                Pattern r = Pattern.compile(pattern);


                String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n" + 
                        "PREFIX afn:<http://jena.apache.org/ARQ/function#>\r\n"+
                        "PREFIX owl: <http://www.w3.org/2002/07/owl#>\r\n" + 
                        "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\r\n" + 
                        "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\r\n" + 
                        "PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n" + 
                        "SELECT DISTINCT ?subject ?object\r\n" + 

                        "   WHERE {\r\n"+

                         "?subject dc:title ?object \r\n" +

                        "FILTER regex(?object,\""+ queryPattern1 +"\",\"i\")\r\n" + 
                        //"FILTER regex( ?object,\"( pedestrian (.|\\n)* car )|( car (.|\\n)* pedestrian )\",\"i\")\r\n"+
                        "}"
                         ;

                  Query query = QueryFactory.create(queryString) ;
                  try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
                        ResultSet results = qexec.execSelect() ;
                        if(!results.hasNext()) {
                            System.out.println("No results");
                        }else
                        for ( ; results.hasNext() ; )
                        {

                            QuerySolution soln = results.nextSolution();
                            Resource subj = (Resource) soln.get("?subject");

                            String subStr = subj.toString();
                            Matcher m = r.matcher(subStr);

                                if(m.find()) {
                            Literal obj = (Literal) soln.get("?object");
                            System.out.println(m.group()+" ........ "+obj);
                            }

                            m.reset();

                        }  


                  } catch(Exception e) { 
                        System.out.println(e.getMessage());
                         }
                }                                       
    }

非常感谢任何建议。 菲尔

1 个答案:

答案 0 :(得分:0)

我想我明白了。我只能在main方法中加载一次本体。然后我将模型传递给myQuery方法。我可以使用getter和setter访问myQuery,而不必在每次查询时都加载本体。

public class SPARQLVeriations {
static private String singleQueryWord1="ventricular fibrillation";
static private String firstQueryWord2;
static private String secondQueryWord2;
static private Model model;
SPARQLVeriations sv = new SPARQLVeriations();

public static void main(String[] args) {

     model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
    model = FileManager.get().loadModel("c:/jena/ICD.owl");


     myQuery(model,singleQueryWord1,firstQueryWord2,secondQueryWord2);   


}

private static void myQuery(Model model,String singleQueryWord1,String firstQueryWord2,String secondQueryWord2) {
    String pattern = "\\#(.*)$";

   // String singleQueryWord1="VEnTRICULAR FIBRILLATION";
    //String firstQueryWord2="pedestrian";
    //String secondQueryWord2="car";
    String queryPattern1="("+singleQueryWord1+"(.*))";
    Pattern r = Pattern.compile(pattern);


    String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n" + 
            "PREFIX afn:<http://jena.apache.org/ARQ/function#>\r\n"+
            "PREFIX owl: <http://www.w3.org/2002/07/owl#>\r\n" + 
            "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\r\n" + 
            "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\r\n" + 
            "PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n" + 
            "SELECT DISTINCT ?subject ?object\r\n" + 

            "   WHERE {\r\n"+

             "?subject dc:title ?object \r\n" +

            "FILTER regex(?object,\""+ queryPattern1 +"\",\"i\")\r\n" + 
            //"FILTER regex( ?object,\"( pedestrian (.|\\n)* car )|( car (.|\\n)* pedestrian )\",\"i\")\r\n"+
            "}"
             ;

      Query query = QueryFactory.create(queryString) ;
      try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
            ResultSet results = qexec.execSelect() ;
            if(!results.hasNext()) {
                System.out.println("No results");
            }else
            for ( ; results.hasNext() ; )
            {

                QuerySolution soln = results.nextSolution();
                Resource subj = (Resource) soln.get("?subject");

                String subStr = subj.toString();
                Matcher m = r.matcher(subStr);

                    if(m.find()) {
                Literal obj = (Literal) soln.get("?object");
                System.out.println(m.group()+" ........ "+obj);
                }

                m.reset();

            }  


      } catch(Exception e) { 
            System.out.println(e.getMessage());
             }

}
public void setQuery1() { }

}