How do I get the RDF resource that represents the objects which have a certain [property,object] pair?

StackOverflow https://stackoverflow.com/questions/11196754

  •  17-06-2021
  •  | 
  •  

Question

Okay, to clarify, I have an XML/RDF file that describes data with a natural categorical tree structure (like folders and files). The data is not structured in a tree, rather, there is information that explains how to rebuild the tree (namely the nested set values of each node). I am starting with no knowledge other than the assumption that some statement in the file has a RootTree property who's object is the URI of the statement describing the root node of the tree.

Obtaining that object is easy, I simply use:

// Obtain the node describing the root of the Pearltree.
mRootProp = mModel.createProperty(Pearltree.RDF.PearlTreeNS, "rootTree");

NodeIterator roots = mModel.listObjectsOfProperty(mRootProp);

Now, I am further able to list all statements which have the property pt:parentTree and the object roots.nextNode():

StmtIterator sit = mModel.listStatements(null, RDF.ParentTree, rootNode);

This gives me a list of all such statements. These statements are part of elements that look like such in the RDF/XML file (note these have a different parentTree value but appear in the same context):

<pt:RootPearl rdf:about="http://www.pearltrees.com/dcow/pearltrees-videos/id5296268#rootPearl">
   <dcterms:title><![CDATA[Pearltrees videos]]></dcterms:title>
   <pt:parentTree rdf:resource="http://www.pearltrees.com/dcow/pearltrees-videos/id5296268" />
   <pt:inTreeSinceDate>2012-06-11T20:25:55</pt:inTreeSinceDate>
   <pt:leftPos>1</pt:leftPos>
   <pt:rightPos>8</pt:rightPos>
</pt:RootPearl>

<pt:PagePearl rdf:about="http://www.pearltrees.com/dcow/pearltrees-videos/id5296268#pearl46838293">
   <dcterms:title><![CDATA[why Pearltrees?]]></dcterms:title>
   <dcterms:identifier>http://www.youtube.com/watch?v%3di4rDqMMFx8g</dcterms:identifier>
   <pt:parentTree rdf:resource="http://www.pearltrees.com/dcow/pearltrees-videos/id5296268" />
   <pt:inTreeSinceDate>2012-06-11T20:25:55</pt:inTreeSinceDate>
   <pt:leftPos>2</pt:leftPos>
   <pt:rightPos>3</pt:rightPos>
</pt:PagePearl>

...

Now, what I would like to do is obtain a reference to all statements with subject sit.nextStatement()'s subject. In this example:

"http://www.pearltrees.com/dcow/pearltrees-videos/id5296268#rootPearl"

and

"http://www.pearltrees.com/dcow/pearltrees-videos/id5296268#pearl46838293"

My goal is to obtain the content of each element including its rightPos and leftPos so I can reconstruct the tree.

Was it helpful?

Solution 2

The method:

getSubject()

of a Statement will return the Subject as a Resource. You can then use the

getProperty(Property p) 

method of the returned Resource to obtain the Statements that include the property in question.

So, in my case, I use:

Resource r;
Statement title, id, lpos, rpos;
while(sit.hasNext()) {
    r = sit.nextStatement().getSubject();

    title = r.getProperty(DCTerms.title);
    id    = r.getProperty(DCTerms.identifier);
    lpos  = r.getProperty(PearlTree.RDF.leftPos);
    rpos  = r.getProperty(PearlTree.RDF.rightPos);
    ...
}

OTHER TIPS

You can simplify your code somewhat as follows:

mRootProp = mModel.createProperty(Pearltree.RDF.PearlTreeNS, "rootTree");
Resource root = mModel.listResourcesWithProperty( mRootProp ).next();

This assumes you know you have exactly one root per model. If that might not be true, modify the code accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top