2013-07-03 5 views
2

나는 온톨로지 창조의 한가운데에 머물러있다. 내 목표는 Java를 사용하여 OWL 파일을 만들고 싶습니다. 나는 계층 적 클러스터를 만들었지 만 이제는 온톨로지 생성에서 이들 클러스터를 사용해야합니다.JENA를 사용하여 올빼미 파일을 만드는 방법은 무엇입니까?

미리 감사드립니다.

나는 이것을 지금까지 시도했다. 나는 디킨슨 포인트를 얻었다. 이제 이것은 내 코드이며 다음과 같은 예외가 발생했습니다 스레드 "main"의 예외 com.hp.hpl.jena.shared.BadURIException : 형식이 올바른 절대 URIrefs 만 RDF/XML 출력에 포함될 수 있습니다. 코드 : 57/SCHEME의 REQUIRED_COMPONENT_MISSING : 구성표에 필요한 구성 요소가 누락되었습니다.

JenaOwl.java

public class JenaOwl { 

    static OntModel jenaModel = null; 

    public static void main(String[] args) throws IOException { 
     JenaOwl jo = new JenaOwl(); 
     FileWriter fw = null; 
     try { 
      jenaModel = createModel(); 
     } catch (Exception ex) { 
      Logger.getLogger(JenaOwl.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     OutputStream output = null; 
     try { 
      fw = new FileWriter("D:/mymodel.owl"); 
      jenaModel.write(fw, "RDF/XML-ABBREV"); 
      fw.close(); 
    // OR Turtle format - compact and more readable 
    // use this variant if you're not sure which to use! 
      fw = new FileWriter("D:/mymodel.ttl"); 
      jenaModel.write(fw, "Turtle"); 
//   output = new FileOutputStream(new File("D:/Sample.owl")); 
//   jenaModel.write(output); 
      //jo.write(output); 
     } finally { 
     if (fw != null) { 
       try { 
        fw.close(); 
       } 
      catch (IOException ignore) { 
     } 
     } 


} 
    } 
     //jenaModel.write(output)} 

    public static OntModel createModel() throws Exception { 
     jenaModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); 
     jenaModel.setNsPrefix("ot", OT.NS); 
     jenaModel.setNsPrefix("owl", OWL.NS); 
     jenaModel.setNsPrefix("dc", DC.NS); 
//  OT ot = new OT(); 
//  OT.OTClass otc; 
     System.out.println("jenaModel.getOntClass(OT.OTClass.Dataset.getNS()) : "+jenaModel.getOntClass(OT.OTClass.Dataset.getNS())); 
     Individual dataset = jenaModel.createIndividual("Dataset URI", jenaModel.getOntClass(OT.OTClass.Dataset.getNS())); 

     OT.OTClass.Dataset.createOntClass(jenaModel); 

     OT.OTClass.DataEntry.createOntClass(jenaModel); 

     OT.OTClass.Feature.createOntClass(jenaModel); 

     OT.OTClass.FeatureValue.createOntClass(jenaModel); 

     OT.OTClass.Compound.createOntClass(jenaModel); 

     Individual dataEntry = jenaModel.createIndividual(OT.OTClass.DataEntry.getOntClass(jenaModel)); 
     dataset.addProperty(OT.dataEntry, dataEntry); 

     Individual compound = jenaModel.createIndividual("compoundURI", OT.OTClass.Compound.getOntClass(jenaModel)); 
     dataEntry.addProperty(OT.compound, compound); 

     // First 
     Individual feature1 = jenaModel.createIndividual("featureURI1", OT.OTClass.Feature.getOntClass(jenaModel)); 
     Individual featureValue1 = jenaModel.createIndividual(OT.OTClass.FeatureValue.getOntClass(jenaModel)); 
     featureValue1.addProperty(OT.feature, feature1); 
     featureValue1.addLiteral(OT.value, jenaModel.createTypedLiteral("formaldehyde", XSDDatatype.XSDstring)); 

//Second value 
     Individual feature2 = jenaModel.createIndividual("featureURI2", OT.OTClass.Feature.getOntClass(jenaModel)); 
     Individual featureValue2 = jenaModel.createIndividual(OT.OTClass.FeatureValue.getOntClass(jenaModel)); 
     featureValue2.addProperty(OT.feature, feature2); 
     featureValue2.addLiteral(OT.value, jenaModel.createTypedLiteral(3.14, XSDDatatype.XSDdouble)); 

//and finally add values to the data entry 
     dataEntry.addProperty(OT.values, featureValue1); 
     dataEntry.addProperty(OT.values, featureValue2); 



     return jenaModel; 
    } 

    public void write(OutputStream output) { 
     MediaType mediaType = new MediaType(null); 
     if (mediaType.equals(MediaType.APPLICATION_RDF_XML)) //jenaModel.write(output,"RDF/XML"); //this is faster 
     { 
      jenaModel.write(output, "RDF/XML-ABBREV"); //this is more readable 
     } else if (mediaType.equals(MediaType.APPLICATION_RDF_XML)) { 
      jenaModel.write(output, "TURTLE"); 
     } else if (mediaType.equals(MediaType.TEXT_RDF_N3)) { 
      jenaModel.write(output, "N3"); 
     } else if (mediaType.equals(MediaType.TEXT_RDF_N3)) { 
      jenaModel.write(output, "N-TRIPLE"); 
     } else { 
      jenaModel.write(output, "RDF/XML-ABBREV"); 
     } 
    } 

    ; 
} 

OT.java이 도움이 될 수

public class OT { 

    public enum OTClass { 

     Compound, 
     Conformer, 
     Dataset, 
     DataEntry, 
     Feature, 
     FeatureValue, 
     Algorithm, 
     Model, 
     Validation, 
     ValidationInfo; 

     public String getNS() { 
      System.out.println("String.format(_NS, toString()) : " + String.format(_NS, toString())); 
      return String.format(_NS, toString()); 
     } 

     public OntClass getOntClass(OntModel model) { 
      return model.getOntClass(getNS()); 
     } 

     public OntClass createOntClass(OntModel model) { 
      return model.createClass(getNS()); 
     } 

     public Property createProperty(OntModel model) { 
      return model.createProperty(getNS()); 
     } 
    }; 
    /** <p>The RDF model that holds the vocabulary terms</p> */ 
    private static Model m_model = ModelFactory.createDefaultModel(); 
    /** <p>The namespace of the vocabalary as a string ({@value})</p> */ 
    protected static final String _NS = "http://www.opentox.org/api/1.1#%s"; 
    public static final String NS = String.format(_NS, ""); 

    public static String getURI() { 
     return NS; 
    } 
    /** <p>The namespace of the vocabalary as a resource</p> */ 
    public static final Resource NAMESPACE = m_model.createResource(NS); 
    /** 
    * Object properties 
    */ 
    public static final Property dataEntry = m_model.createProperty(String.format(_NS, "dataEntry")); 
    public static final Property compound = m_model.createProperty(String.format(_NS, "compound")); 
    public static final Property feature = m_model.createProperty(String.format(_NS, "feature")); 
    public static final Property values = m_model.createProperty(String.format(_NS, "values")); 
    public static final Property hasSource = m_model.createProperty(String.format(_NS, "hasSource")); 
    public static final Property conformer = m_model.createProperty(String.format(_NS, "conformer")); 
    public static final Property isA = m_model.createProperty(String.format(_NS, "isA")); 
    public static final Property model = m_model.createProperty(String.format(_NS, "model")); 
    public static final Property report = m_model.createProperty(String.format(_NS, "report")); 
    public static final Property algorithm = m_model.createProperty(String.format(_NS, "algorithm")); 
    public static final Property dependentVariables = m_model.createProperty(String.format(_NS, "dependentVariables")); 
    public static final Property independentVariables = m_model.createProperty(String.format(_NS, "independentVariables")); 
    public static final Property predictedVariables = m_model.createProperty(String.format(_NS, "predictedVariables")); 
    public static final Property trainingDataset = m_model.createProperty(String.format(_NS, "trainingDataset")); 
    public static final Property validationReport = m_model.createProperty(String.format(_NS, "validationReport")); 
    public static final Property validation = m_model.createProperty(String.format(_NS, "validation")); 
    public static final Property hasValidationInfo = m_model.createProperty(String.format(_NS, "hasValidationInfo")); 
    public static final Property validationModel = m_model.createProperty(String.format(_NS, "validationModel")); 
    public static final Property validationPredictionDataset = m_model.createProperty(String.format(_NS, "validationPredictionDataset")); 
    public static final Property validationTestDataset = m_model.createProperty(String.format(_NS, "validationTestDataset")); 
    /** 
    * Data properties 
    */ 
    public static final Property value = m_model.createProperty(String.format(_NS, "value")); 
    public static final Property units = m_model.createProperty(String.format(_NS, "units")); 
    public static final Property has3Dstructure = m_model.createProperty(String.format(_NS, "has3Dstructure")); 
    public static final Property hasStatus = m_model.createProperty(String.format(_NS, "hasStatus")); 
    public static final Property paramScope = m_model.createProperty(String.format(_NS, "paramScope")); 
    public static final Property paramValue = m_model.createProperty(String.format(_NS, "paramValue")); 
    public static final Property statisticsSupported = m_model.createProperty(String.format(_NS, "statisticsSupported")); 
} 

?

답변

4

귀하의 질문이 명확하지 않습니다. 당신은 당신이 만든 모델을 저장하는 방법에 대한 요구가있는 경우, 당신은 파일에 쓰는해야합니다

OntModel m = .... your model .... ; 
FileWriter out = null; 
try { 
    // XML format - long and verbose 
    out = new FileWriter("mymodel.xml"); 
    m.write(out, "RDF/XML-ABBREV"); 

    // OR Turtle format - compact and more readable 
    // use this variant if you're not sure which to use! 
    out = new FileWriter("mymodel.ttl"); 
    m.write(out, "Turtle"); 
} 
finally { 
    if (out != null) { 
    try {out.close()} catch (IOException ignore) {} 
    } 
} 

가 작성 RDF에 대한 자세한 내용은 Jena documentation을 참조하십시오.

또는 온톨로지의 인스턴스를 추가하는 방법에 대한 질문은 ontology API documentation의 예를 참조하십시오. 자세한 내용과 질문을 업데이트하시기 바랍니다이 문제가 해결되지 않는 경우

OntModel m = ... your model ... ; 
String ns = "http://example.com/example#"; 
OntClass foo = m.getOntClass(ns + "Foo"); 
Individual fubar = foo.createInstance(ns + "fubar"); 

: 힌트로서, 대략 당신은 당신의 인스턴스를 생성 할 OWL 클래스에 해당하는 OntClass 객체를 얻고 싶은 말 , 그리고 이상적으로는 이미 시도한 코드 샘플입니다.

업데이트

OK, 나는 업데이트 된 코드를 본 적이있다. Protégé의 경우 파일을 XML로 작성하기 만하면되므로 터틀 형식을 작성하는 선을 제거 할 수 있습니다. - 오류 메시지가 당신을 말하고 무엇을하는

jenaModel.createIndividual("compoundURI") 

"compoundURI"가 아닌 유효한 URI :하지만 당신의 진짜 문제는이 같은 라인이다. HTTP와 같은 유효한 URI 스킴 중 하나와 일치하는 전체 URI가 필요합니다. 그래서, 다음과 같은 것 :

jenaModel.createIndividual(OT.NS + compoundURI); 
+0

JENA를 사용하여 .owl 파일을 만들 수 있습니까? 내 질문에 언급했듯이 나는 계층적인 클러스터 (즉, 사실상 트리 형태로 표현 될 수있는 벡터)를 가지고있다. 이제 해당 트리에 대한 OWL 파일을 만들고 싶습니다.이 ".owl"파일은 protege를 사용하여 열립니다. 질문이 아직 명확하지 않은 경우, 제가 정교하게 알려주십시오. –

+0

'.owl' 파일은 RDF/XML 형식으로 저장된 RDF 파일입니다. 그것은 다른 형식이 아닙니다. out = new FileWriter ("mymodel.xml");'대신'out = new FileWriter ("mymodel.owl");을 사용하십시오. –

+0

나는 당신의 요지를 얻었으나 나는 지금 몇 가지 문제에 직면하고있다. 질문을 업데이트했습니다. 한번보십시오. –

관련 문제