如何使用JENA创建owl文件? [英] How to create owl file using JENA?

查看:224
本文介绍了如何使用JENA创建owl文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了本体创作的中间。我想用Java创建一个OWL文件,这是我的目标。我已经创建了层次结构集群,但现在我必须在本体创建中使用这些集群。



提前致谢。



<到目前为止我已经尝试过了。我得到了狄金森的观点。现在这是我的代码,我得到一个Exception为
线程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)抛出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格式 - 紧凑且更具可读性
//如果您不确定使用哪种变体,请使用此变体!
fw = new FileWriter(D:/mymodel.ttl);
jenaModel.write(fw,Turtle);
// output = new FileOutputStream(new File(D:/Sample.owl));
// jenaModel.write(输出);
//jo.write(output);
}最后{
if(fw!= null){
try {
fw.close();
}
catch(忽略IOException){
}
}


}
}
// 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()));
个人数据集= 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);

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

单个化合物= jenaModel.createIndividual(compoundURI,OT.OTClass.Compound.getOntClass(jenaModel));
dataEntry.addProperty(OT.compound,compound);

//第一个
个人feature1 = jenaModel.createIndividual(featureURI1,OT.OTClass.Feature.getOntClass(jenaModel));
个人featureValue1 = jenaModel.createIndividual(OT.OTClass.FeatureValue.getOntClass(jenaModel));
featureValue1.addProperty(OT.feature,feature1);
featureValue1.addLiteral(OT.value,jenaModel.createTypedLiteral(formaldehyde,XSDDatatype.XSDstring));

//第二个值
个人特征2 = 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));

//最后将值添加到数据条目
dataEntry.addProperty(OT.values,featureValue1);
dataEntry.addProperty(OT.values,featureValue2);



返回jenaModel;
}

public void write(OutputStream输出){
MediaType mediaType = new MediaType(null);
if(mediaType.equals(MediaType.APPLICATION_RDF_XML))// jenaModel.write(output,\"RDF/XML); //这是更快的
{
jenaModel.write(输出,RDF / XML-ABBREV); //这是更可读的
} 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

 公共等级OT {

公共枚举OTClass {

化合物,
Conformer,
数据集,
DataEntry,
功能,
FeatureValue,
算法,
模型,
验证,
ValidationInfo;

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

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

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

public属性createProperty(OntModel model){
return model.createProperty(getNS());
}
};
/ **< p>保存词汇术语< / p>的RDF模型* /
private static Model m_model = ModelFactory.createDefaultModel();
/ **< p> vocabalary的名称空间为字符串({@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>作为资源的vocabalary的命名空间< / p> * /
public static final资源NAMESPACE = m_model.createResource(NS);
/ **
*对象属性
* /
public static final属性dataEntry = m_model.createProperty(String.format(_NS,dataEntry));
public static final属性compound = m_model.createProperty(String.format(_NS,compound));
public static final属性功能= m_model.createProperty(String.format(_NS,feature));
public static final属性值= m_model.createProperty(String.format(_NS,values));
public static final属性hasSource = m_model.createProperty(String.format(_NS,hasSource));
public static final属性conformer = m_model.createProperty(String.format(_NS,conformer));
public static final属性isA = m_model.createProperty(String.format(_NS,isA));
public static final属性模型= m_model.createProperty(String.format(_NS,model));
public static final属性report = m_model.createProperty(String.format(_NS,report));
public static final属性算法= m_model.createProperty(String.format(_NS,algorithm));
public static final属性dependentVariables = m_model.createProperty(String.format(_NS,dependentVariables));
public static final属性independentVariables = m_model.createProperty(String.format(_NS,independentVariables));
public static final属性predictVariables = m_model.createProperty(String.format(_NS,predictVariables));
public static final属性trainingDataset = m_model.createProperty(String.format(_NS,trainingDataset));
public static final属性validationReport = m_model.createProperty(String.format(_NS,validationReport));
public static final属性验证= m_model.createProperty(String.format(_NS,validation));
public static final属性hasValidationInfo = m_model.createProperty(String.format(_NS,hasValidationInfo));
public static final属性validationModel = m_model.createProperty(String.format(_NS,validationModel));
public static final属性validationPredictionDataset = m_model.createProperty(String.format(_NS,validationPredictionDataset));
public static final属性validationTestDataset = m_model.createProperty(String.format(_NS,validationTestDataset));
/ **
*数据属性
* /
public static final属性值= m_model.createProperty(String.format(_NS,value));
public static final属性单位= m_model.createProperty(String.format(_NS,units));
public static final属性has3Dstructure = m_model.createProperty(String.format(_NS,has3Dstructure));
public static final属性hasStatus = m_model.createProperty(String.format(_NS,hasStatus));
public static final属性paramScope = m_model.createProperty(String.format(_NS,paramScope));
public static final属性paramValue = m_model.createProperty(String.format(_NS,paramValue));
public static final属性statisticsSupported = m_model.createProperty(String.format(_NS,statisticsSupported));
}

你可以帮忙吗?

解决方案

你的问题不是很清楚。如果您询问如何保存您创建的模型,则需要将其写入文件:

  OntModel m = ....你的模型......; 
FileWriter out = null;
try {
// XML格式 - 冗长且详细
out = new FileWriter(mymodel.xml);
m.write(out,RDF / XML-ABBREV);

//或者Turtle格式 - 紧凑且更具可读性
//如果您不确定使用哪种变体,请使用此变体!
out = new FileWriter(mymodel.ttl);
m.write(out,Turtle);
}
finally {
if(out!= null){
try {out.close()} catch(IOException ignore){}
}
}

参见 Jena文档



或者,如果您的问题是关于如何要添加本体的实例,请参阅本体API文档中的示例。作为提示,粗略地说,您希望获得一个 OntClass 对象,该对象对应于您要创建实例的OWL类:

  OntModel m = ...你的模特......; 
String ns =http://example.com/example#;
OntClass foo = m.getOntClass(ns +Foo);
个人fubar = foo.createInstance(ns +fubar);

如果这不能解决您的问题,请更新您的问题并提供更多详细信息,理想情况下,您已经尝试过的代码示例。



更新



确定,我已经看到了你的更新代码。对于Protégé,您只需要用XML编写文件,这样就可以删除行来编写Turtle格式。但你真正的问题是这样的行:

  jenaModel.createIndividual(compoundURI)

compoundURI不是有效的URI - 这是错误消息告诉的内容您。您需要一个符合HTTP之类的有效URI方案的完整URI。所以,比如:

  jenaModel.createIndividual(OT.NS + compoundURI); 


I am stuck in the middle of ontology creation. I want to create an OWL file using Java which is my objective. I have created the hierarchical clusters but now I have to use those clusters in ontology creation.

Thanks in advance.

I have tried this so far. I got Dickinson's point. Now this is my code and I am getting an Exception as Exception in thread "main" com.hp.hpl.jena.shared.BadURIException: Only well-formed absolute URIrefs can be included in RDF/XML output: Code: 57/REQUIRED_COMPONENT_MISSING in SCHEME: A component that is required by the scheme is 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"));
}

can you help in this?

解决方案

Your question isn't very clear. If you're asking about how to save the model that you have created, you need to write it out to a file:

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) {}
  }
}

See the Jena documentation for more details on writing RDF.

Alternatively, if your question is about how to add instances of your ontology, see the examples in the ontology API documentation. As a hint, roughly speaking you want to get an OntClass object that corresponds to the OWL class you want to create an instance of:

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

If that doesn't address your issue, please update your question with more details, and, ideally, a sample of the code that you have already tried.

Update

OK, I've seen your updated code. For Protégé, you only need to write the file in XML, so you can remove the lines to write the Turtle format. But your real problem is lines like this:

jenaModel.createIndividual("compoundURI" )

"compoundURI" is not a valid URI - which is what the error message is telling you. You need a full URI that conforms to one of the valid URI schemes like HTTP. So, something like:

jenaModel.createIndividual( OT.NS + compoundURI );

这篇关于如何使用JENA创建owl文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆