如何在jena rdf中创建方法public static boolean setType(Resource resource,String typeName)

时间:2013-02-19 03:25:08

标签: rdf jena ontology

Can you please help me in building the code in setting a Resource to a Class

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.ReadWrite;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.tdb.TDBFactory;

public class ModelMain {

    String dbName = "DataBase";
    String ns1 =  "http://text.book/Someone#";
    String ns;
    Dataset ds;
    OntModel m;

//创建了一个模型并存储在数据库中         public StoryModel(String Name){

        ns = ns1 + Name;
        ds = TDBFactory.createDataset(dbName);
        m = ModelFactory.createOntologyModel();

    }

//将资源分配给班级         public void initModel(){

        m.createClass(ns + "Thing");
        m.createClass(ns + "Object");
        saveModel();

    }

//读写模型         public void saveModel(){

        ds.begin(ReadWrite.WRITE);
        m.write(System.out, "RDF/XML-ABBREV");

    }

//创建资源
        public Resource createResource(String resourceName,String clsName){

        String resourceuri = ns + resourceName;
        String classuri = ns + className;

        Resource classr = m.getResource(classuri);
        Individual i = m.createIndividual(resourceuri, classr);

        return i;

    }

//为资源分配类型
        public static boolean setType(资源资源,字符串typeName)         {             //如何为资源分配类型         }     }

2 个答案:

答案 0 :(得分:1)

您需要在数据中添加一个语句:

model.add(resource, RDF.type, TheTypeAsAResource) ;

答案 1 :(得分:1)

如果您有OntResourceIndividual是),那么您可能会考虑使用setPropertyValue(Property, RDFNode)方法来设置类型。请注意,这仅与@AndyS's answer不同,因为它会在添加新三元组之前删除图表中已存在的任何其他RDF.type属性。

从模型开始:

:a rdf:type :Cat .
:a rdf:type :DomesticAnimal .

以下代码(假设aIndividual):

a.setPropertyValue(RDF.type, TheTypeAsResource);

将导致模型:

a: rdf:type :theType .

当然,如果您不打算向资源添加其他类型,而是打算为该资源设置特定类型,那么这只会用于您的目的。