jena如何将默认前缀名称更改为我的前缀名称

时间:2015-03-06 19:15:27

标签: java jena semantic-web owl apache-jena

我已经生成了这个RDF / XML数据

  <rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://marco_student/" > 
  <rdf:Description rdf:nodeID="A0">
    <j.0:description>Departamento de Engenharia Civil</j.0:description>
    <j.0:abbreviation>DEC</j.0:abbreviation>
    <rdf:type rdf:resource="http://marco_student/Department"/>
  </rdf:Description>
  <rdf:Description rdf:nodeID="A1">
    <j.0:description>Departamento de Engenharia Informática</j.0:description>
    <j.0:abbreviation>DEI</j.0:abbreviation>
    <rdf:type rdf:resource="http://marco_student/Department"/>
  </rdf:Description>
  <rdf:Description rdf:nodeID="A2">
    <j.0:description>Departamento de Engenharia Electrotécnica</j.0:description>
    <j.0:abbreviation>DEE</j.0:abbreviation>
    <rdf:type rdf:resource="http://marco_student/Department"/>
  </rdf:Description>
</rdf:RDF>

使用此代码:

String myNameSpace = "http://william_student/";
            Resource departmentClass = ResourceFactory.createResource(myNameSpace+"Department");
            Property abbreviationProperty = ResourceFactory.createProperty(myNameSpace, "abbreviation");
            Property descriptionProperty = ResourceFactory.createProperty(myNameSpace, "description");
            Model departmentModel = ModelFactory.createDefaultModel();
            Resource departmentInstance1 = departmentModel.createResource();
            departmentInstance1.addProperty(RDF.type, departmentClass);
            departmentInstance1.addProperty(abbreviationProperty, "DEI");

我使用这个简单的代码写入文件

File file = new File("D:/departments.rdf");
            fos = new FileOutputStream(file);
            departmentModel.write(fos);

如您所见,在RDF生成的数据中,有j.0前缀:

我的问题:

如何替换默认前缀j.0,但我的前缀如vocabularyMarco

2 个答案:

答案 0 :(得分:1)

要编写RDF / XML,所有属性都必须具有qname。当需要但不提供时,耶拿发明了“j.0”等。因此,在模型上设置您选择的前缀名称

model.setNsPrefix("vocabularyMarco", "http://marco_student/")

您的代码和数据不同意“http://william_student/”。

答案 1 :(得分:0)

// Namespace declarations

static final String companyUri = "citylsdi.org#";

Model model = ModelFactory.createDefaultModel();

model.setNsPrefix( "Indicator", "citylsdi.org#" );

它在上面的代码片段中创建了前缀“citylsdi.org#”。

为了获得“vocabularyMarco”作为前缀 只需使用

Model model = ModelFactory.createDefaultModel();
model.setNsPrefix( "Indicator", "vocabularyMarco" );

// Create the types of Property we need to describe relationships in the model

Property cu_role = model.createProperty(companyUri,domain);

// Create resources representing the people in our model

Resource rs2 = model.createResource(companyUri+name);

rs2.addProperty(cu_role,"'"+role+"'");