在耶拿,有没有办法通过对象查询主题?

时间:2015-06-26 14:56:29

标签: java jena

我有一个像这样的海龟文件:

<http://rdf.freebase.com/ns/m.05h40wd>  <http://rdf.freebase.com/key/wikipedia.en_id>   "21537998"  .
<http://rdf.freebase.com/ns/m.05h40xf>  <http://rdf.freebase.com/key/wikipedia.en_id>   "21557390"  .
<http://rdf.freebase.com/ns/m.05h4251>  <http://rdf.freebase.com/key/wikipedia.en_id>   "21550418"  .
<http://rdf.freebase.com/ns/m.05h4__1>  <http://rdf.freebase.com/key/wikipedia.en_id>   "21495513"  .

我现在只有对象(id),但我想得到主题。有办法吗?这是我的代码。

  public static void main(String[] args) throws FileNotFoundException {
      Model model=ModelFactory.createDefaultModel();
        model.read(new FileInputStream("RDFTest.ttl"),null,"TTL");
        Resource topic = model.getResource("http://rdf.freebase.com/ns/" + "m.05h4251");
          Property labelProperty = model.getProperty("http://rdf.freebase.com/key/wikipedia.en_id");
          System.out.println(topic.getProperty(labelProperty));
  }

结果是:

[http://rdf.freebase.com/ns/m.05h4251, http://rdf.freebase.com/key/wikipedia.en_id, "21550418"]

如果我反过来,我会得到null。

1 个答案:

答案 0 :(得分:1)

您可以使用Model#listResourcesWithProperty(Property p, RDFNode o)。如果您不关心属性是什么,只需将 null 作为属性来充当通配符。这会给你一个ResIterator而不是主题,因为可能有多个(就像给定主题的属性可能有多个对象一样;你使用getProperty的方法,只返回任意一个)