-- 作者:anzhiruosu
-- 发布时间:5/19/2009 10:37:00 PM
-- 如何使Jena程序输出的三元组是缩写形式?
我在Jena平台上读入一个本体文件,然后通过listStatement()方法输出所有三元组,大概的代码如下: Model model = ModelFactory.createDefaultModel(); // read the RDF/XML file model.read("file:e:D2RROOT/examples/SmartRoom/output/output.owl"); StmtIterator iter=model.listStatements(null,null,(RDFNode)null); //打印本体模型中的三元组 while(iter.hasNext()){ System.out.println("***"+iter.nextStatement()); } 输出结果的三元组形式如下: ***[http://www.owl-ontologies.com/MyOntology.owl#Living_Room2, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.owl-ontologies.com/MyOntology/schema.owl#Room] 可以看出,输出的三元组各个资源是以URI全名的输出的,而我在本体文档中已经定义了名字空间: xmlns:users="http://www.owl-ontologies.com/MyOntology/schema.owl#" 我所希望输出的形式是: [uers:Living_Room2,rdfs:type,users:Room] 所以我想问一下,在Jena中应该怎么设置才能达到以上的效果呢? 另外: 在jena读取的规则文档中也可以使用prefix来定义前缀。因为功能需求,我为Jena添加了一个功能子(functor)ruleTrace,这个functor的基本功能是当某条规则触发时获取规则的前件和后件。功能子ruleTrace对应的内嵌原语RuleTrace的headAction方法的主要代码如下: public void headAction(Node[] args, int length, RuleContext context) { //通过循环把每个在ruleTrace中指定的前后件找出来 for (int i = 0; i < length; i++) { //获得在ruleTrace中指定的参数内容 Node clauseN = getArg(i, args, context); //判定参数是否为数字,因为参数指定的位置信息,所以只能是整数。 if (Util.isNumeric(clauseN)) { //转化为整型 int clauseIndex = Util.getIntValue(clauseN); //前件三元组 Object bodyclause ; //后件三元组 Object headClause; //如果是正整数,则获取前件信息 if(clauseIndex >=0){ //从规则中获取指定前件 bodyclause = context.getRule().getBodyElement(clauseIndex); if (bodyclause instanceof TriplePattern) { //这里打印前件 System.out.println(bodyclause); } else { throw new BuiltinException(this, context, "illegal triple to remove non-triple clause"); } } //否则获取后件信息 else{ headClause = context.getRule().getHeadElement(-1-clauseIndex); if (headClause instanceof TriplePattern) { //这里仅仅是打印后件, System.out.println(headClause); // context.remove(t); } else { throw new BuiltinException(this, context, "illegal triple to remove non-triple clause"); } } } else { throw new BuiltinException(this, context, "illegal arg to remove (" + clauseN + "), must be an integer"); } } } 例如我写得规则是: @prefix mys http://www.owl-ontologies.com/MyOntology/schema.owl# @prefix my http://www.owl-ontologies.com/MyOntology.owl# [ (?user rdf:type mys:User) (?room rdf:type mys:Room) (?room mys:has_light ?light)(?light mys:status my:closed) -> (?light mys:need my:open_light_server )( my:open_light_server rdf:type mys:Server )ruleTrace(0,-1)print("rule11") ] 当这条规则被触发,我指定获取第一个前件,理想输出为: (?user rdf:type mys:User) 但实际输出为: ?user @rdf:type User 少了自定义的前缀。请问这如何解决?
|