krssconverter.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 902 行 · 第 1/4 页

JAVA
902
字号
                    	   this.addEntity(ontology, new URI(r.getName()), OBJECT_PROP);
                    	   this.setPropAttribute(ontology, new URI(r.getName()), OBJECT_PROP, TRANS, true);
                       }
                       
                       else if(str.equalsIgnoreCase("DISJOINT")) {
                           List args = parseExprList(in, ontology);
                           this.addAxiom(ontology, DISJ_CLASS, args);

//                          for(int i = 0; i < list.size() - 1; i++) {
//                              OWLDescription c1 = (OWLDescription) list.get(i);
//                              for(int j = i + 1; j < list.size(); j++) {
//                                      OWLDescription c2 = (OWLDescription) list.get(j);
////                                kb.addClass(c2);
////                                kb.addDisjointClass(c1, c2);
//                                      this.addAxiom(ontology, DISJ_CLASS, args);
//                                  if(DEBUG) System.out.println("DISJOINT " + c1 + " " + c2);
//                              }
//                          }
                       }
                       else
                               throw new RuntimeException("Unknown command " + str);
                       parseToken(in, ')');

                       token = in.nextToken();
               }
               return ontology;
       }

       private void setPropAttribute(OWLOntology ontology, URI uri, int type, int attrib, boolean value) {
               try {
                       OWLProperty prop = null;
                       if (type == DATA_PROP) prop = ontology.getDataProperty(uri);
                       else prop = ontology.getObjectProperty(uri);

                       OntologyChange change = null;
                       switch (attrib) {
                               case FUNC:
                                       change = new SetFunctional(ontology, prop, value, null);
                                       break;
                               case INV_FUNC:
                                       change = new SetInverseFunctional(ontology, (OWLObjectProperty) prop, value, null);
                                       break;
                               case SYMM:
                                       change = new SetSymmetric(ontology, (OWLObjectProperty) prop, value, null);
                                       break;
                               case TRANS:
                                       change = new SetTransitive(ontology, (OWLObjectProperty) prop, value, null);
                                       break;
                       }
                       change.accept((ChangeVisitor) ontology);
               }
               catch (Exception ex) {
                       ex.printStackTrace();
               }
       }

       private void addAxiom(OWLOntology ontology, int type, List args) {
           OntologyChange oc = null;
               try {
                       switch (type) {
                               case SAME_CLASS:
                                   OWLDescription desc1 = (OWLDescription) args.get(0);
                                   OWLDescription desc2 = (OWLDescription) args.get(1);
                                   if( desc1 instanceof OWLClass ) {
                                       oc = new AddEquivalentClass( ontology, (OWLClass) desc1, desc2, null );
                                   }
                                   else {
                                               OWLEquivalentClassesAxiom equ = ontology.getOWLDataFactory().getOWLEquivalentClassesAxiom(new HashSet(args));
                                               oc = new AddClassAxiom(ontology, equ, null);
                                   }
                                       oc.accept((ChangeVisitor) ontology);
                                       break;

                               case DISJ_CLASS:
                                       OWLDisjointClassesAxiom disj = ontology.getOWLDataFactory().getOWLDisjointClassesAxiom(new HashSet(args));
                                       oc = new AddClassAxiom(ontology, disj, null);
                                       oc.accept((ChangeVisitor) ontology);
                                       break;

                               case SUB_CLASS:
                                       OWLDescription sub = (OWLDescription) args.get(0);
                                       OWLDescription sup = (OWLDescription) args.get(1);
                                       if( sub instanceof OWLClass ) {
                                           oc = new AddSuperClass( ontology, (OWLClass) sub, sup, null );
                                       }
                                       else {
                                           OWLSubClassAxiom ax = ontology.getOWLDataFactory().getOWLSubClassAxiom(sub, sup);
                                               oc = new AddClassAxiom(ontology, ax, null);
                                       }
                                       oc.accept((ChangeVisitor) ontology);
                                       break;

                               case SUB_OPROP:
                                       OWLObjectProperty p = ontology.getObjectProperty(new URI(args.get(0).toString()));
                                       OWLObjectProperty q = ontology.getObjectProperty(new URI(args.get(1).toString()));
                                       AddSuperProperty as = new AddSuperProperty(ontology, p, q, null);
                                       as.accept((ChangeVisitor) ontology);
                                       break;

                               case OBJ_RANGE:
                                       p = ontology.getObjectProperty(new URI(args.get(0).toString()));
                                       OWLDescription desc = (OWLDescription) args.get(1);
                                       AddObjectPropertyRange aopr = new AddObjectPropertyRange(ontology, p, desc, null);
                                       aopr.accept((ChangeVisitor) ontology);
                                       break;

                               case OBJ_DOMAIN:
                                       p = ontology.getObjectProperty(new URI(args.get(0).toString()));
                                       desc = (OWLDescription) args.get(1);
                                       AddDomain ad = new AddDomain(ontology, p, desc, null);
                                       ad.accept((ChangeVisitor) ontology);
                                       break;

                               case INV_PROP:
                                       p = ontology.getObjectProperty(new URI(args.get(0).toString()));
                                       q = ontology.getObjectProperty(new URI(args.get(1).toString()));
                                       AddInverse ai = new AddInverse(ontology, p, q, null);
                                       ai.accept((ChangeVisitor) ontology);
                                       break;
                       }
               }
               catch (Exception ex) {
                       ex.printStackTrace();
                       try {
               oc.accept((ChangeVisitor) ontology);
           }
           catch( OWLException e ) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
               }
       }

       private OWLEntity addEntity(OWLOntology ontology, URI uri, int type) {

               OWLEntity newEntity = null;
               try {
                       OWLDataFactory df = ontology.getOWLDataFactory();
                       switch (type) {
                               case CLASS:
                                       newEntity = df.getOWLClass(uri);
                                       break;
                               case DATA_PROP:
                                       newEntity = df.getOWLDataProperty(uri);
                                       break;
                               case OBJECT_PROP:
                                       newEntity = df.getOWLObjectProperty(uri);
                                       break;
                               case INDIVIDUAL:
                                       newEntity = df.getOWLIndividual(uri);
                                       break;
                       }
                       AddEntity ae = new AddEntity(ontology, newEntity, null);
                       ae.accept((ChangeVisitor) ontology);
               }
               catch (Exception ex) {
                       ex.printStackTrace();
               }
               return newEntity;
       }

//      public void readABox(StreamTokenizer in) throws Exception {
//              int token = in.nextToken();
//              while(token != StreamTokenizer.TT_EOF && token != ')') {
//                      assertTrue(token == '(');
//
//                      String str = getToken(in);
//                      if(str.equalsIgnoreCase("INSTANCE")) {
//                              ATermAppl x = getTerm(in);
//                              ATermAppl c = parseExpr(in);
//
//                              kb.addIndividual(x);
//                              kb.addType(x, c);
//                              if(DEBUG) System.out.println("INSTANCE " + x + " " + c);
//                      }
//                      else if(str.equalsIgnoreCase("RELATED")) {
//                              ATermAppl x = getTerm(in);
//                              ATermAppl y = getTerm(in);
//                              ATermAppl r = getTerm(in);
//
//                              kb.addIndividual(x);
//                              kb.addIndividual(y);
//                              kb.addPropertyValue(r, x, y);
//
//                              if(DEBUG) System.out.println("RELATED " + x + " - " + r + " -> " + y);
//                      }
//                      else
//                              throw new RuntimeException("Unknown command " + str);
//
//                      parseToken(in, ')');
//
//                      token = in.nextToken();
//              }
//      }

   public final static void main(String[] args)throws Exception  {
       String loc = "C:/Documents and Settings/UMD/My Documents/Semantic Web/Pellet-Main/test_data/dl-benchmark/tbox/";
       File dir = new File( loc );
       File[] files = dir.listFiles( new FilenameFilter() {
           public boolean accept(File dir, String name) {
               return dir != null && name.endsWith(".tkb")
               && name.indexOf( "-roles" ) == -1 && name.indexOf( "-cd" ) == -1
               && name.indexOf("people") == -1 && name.indexOf("veda-all") == -1
               && name.indexOf("pdwq") == -1;
           }
       });
       Arrays.sort( files, AlphaNumericComparator.CASE_INSENSITIVE );
       for( int i = 0; i < files.length; i++ ) {
           File file = files[i];
           String outFile = file.getAbsolutePath() + ".owl";

           System.out.println( "Converting " + file.getName() );

           KRSSConverter converter = new KRSSConverter();
           OWLOntology ont = converter.readTBox( file.getAbsolutePath() );

           FileWriter writer = new FileWriter( outFile );
           CorrectedRDFRenderer rdfRender = new CorrectedRDFRenderer();
           rdfRender.renderOntology(ont, writer);
           writer.close();
       }

   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?