我已经使用Jena API创建了一个本体,它包含以下类和DataType属性:
public class Onto {
OntClass USER,...;
OntModel model;
String uriBase;
DatatypeProperty Name,Surname,..;
ObjectProperty has_EDUCATION_LEVEL;
public Onto (){
model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF );
uriBase = "http://www.something.com/";
model.createOntology(uriBase);
//Classes
USER=model.createClass(uriBase+"USER");
...
//DatatTypesProperties
Name= model.createDatatypeProperty(uriBase+"Name");
Name.setDomain(USER_AUTNENTIFICATION);
Name.setRange(XSD.xstring);
Surname= model.createDatatypeProperty(uriBase+"Surname");
Surname.setDomain(USER_AUTNENTIFICATION);
Surname.setRange(XSD.xstring);
...
//ObjectProperties
has_EDUCATION_LEVEL= model.createObjectProperty(uriBase+"has_EDUCATION_LEVEL");
has_EDUCATION_LEVEL.setDomain(USER);
has_EDUCATION_LEVEL.setRange(EDUCATION_LEVEL);}然后,我创建了一个"USER“类的实例,在其中我通过网络为DataType属性"Name”和"Surname“插入了一些值。我的代码输出是一个.owl file.But,当我用Protege读取它时,我发现我所有的数据属性,我的对象属性,甚至我的类都包含两个前缀j.1和j.0。下面是OWL文件中我的实例的代码:
<j.1:USER rdf:about="http://www.something.com/#Jhon">
<j.0:has_EDUCATION_LEVEL rdf:resource="http://www.something.com/HIGH_EDUCATION_LEVEL"/>
<j.0:Surname>Smith</j.0:Surname>
<j.0:Name>Jhon</j.0:Name>
</j.1:USER>如有任何帮助或建议,我将不胜感激
发布于 2013-05-17 10:45:42
在RDF/XML文档的开头处,您将看到前缀j.1和j.0被适当地定义,因此http://www.something.com/has_EDUCATION_LEVEL与j.0:has_EDUCATION_LEVEL相同。Jena不会做任何更改属性URI的操作,所以如果您重新读入模型,或者将其发送给其他人,他们仍然会看到完全相同的数据。所使用的前缀只与阅读XML文本的人有关。
也就是说,Jena模型是一个PrefixMapping,因此您可以使用setNsPrefix来定义在编写模型时将使用的名称和前缀。下面是一个示例,大致基于您提供的数据:
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
public class PrefixDemo {
public static void main(String[] args) {
Model model = ModelFactory.createDefaultModel();
String base = "http://www.something.com/";
Resource john = model.createResource( base + "John" );
Property hasSurname = model.createProperty( base + "hasSurname" );
model.add( john, hasSurname, "Smith" );
// Before defining a prefix
model.write( System.out, "RDF/XML" );
// After defining a prefix
model.setNsPrefix( "something", base );
model.write( System.out, "RDF/XML" );
}
}输出(在模型之间加上一个额外的换行符)是:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://www.something.com/" >
<rdf:Description rdf:about="http://www.something.com/John">
<j.0:hasSurname>Smith</j.0:hasSurname>
</rdf:Description>
</rdf:RDF>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:something="http://www.something.com/" >
<rdf:Description rdf:about="http://www.something.com/John">
<something:hasSurname>Smith</something:hasSurname>
</rdf:Description>
</rdf:RDF>第一次打印模型时,您将看到自动生成的名称空间前缀j.0。但是,当我们显式定义一个前缀时,就会使用它,如第二个示例所示。
https://stackoverflow.com/questions/16600134
复制相似问题