java - 如何使用 JENA 创建 owl 文件?

标签 java jena ontology owl

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

提前致谢。

到目前为止我已经尝试过了。我明白了狄金森的观点。现在这是我的代码,我收到一个异常 线程“main”中的异常 com.hp.hpl.jena.shared.BadURIException:只有格式正确的绝对 URIref 才能包含在 RDF/XML 输出中:方案中的代码:57/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"));
}

你能帮忙吗?

最佳答案

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

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

请参阅Jena documentation有关编写 RDF 的更多详细信息。

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

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

如果这不能解决您的问题,请使用更多详细信息(最好是您已尝试过的代码示例)更新您的问题。

更新

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

jenaModel.createIndividual("compoundURI" )

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

jenaModel.createIndividual( OT.NS + compoundURI );

关于java - 如何使用 JENA 创建 owl 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17442969/

相关文章:

java - 生成具有一定数量顶点的 DAG (java)

java - WAS 7 REST 客户端中的 Jersey 2

java - Jena getNsPrefixUri 返回 null(如何为 OWL 本体定义基本 URI?)

java - 使用 Jena、Sparql 和初始 DBPedia URI 进行简单深度搜索

java - 如何在Java中动态改变对象的泛型类型?

java - 无法从JDK动态代理获取方法注释

java - 尝试删除个体会出现 NullPointerException

java - 保存本体后owl API错误

rdf - 有没有办法将Weka j48决策树输出映射为RDF格式?

java - 为本体 hibernate ?