How to generate rdf collection by Jena API?

717 views Asked by At

I have RDF/XML data which looks like:

<rdf:RDF> 
    <rdf:Description rdf:about="https://localhost/process/members/test">
        <process:user rdf:resource="https://localhost/users/test"/>
        <process:roleAssignments rdf:parseType="Collection">
            <rdf:Description rdf:about="https://localhost/process/role/ProductOwner">
                <process:roleUrl rdf:resource="https://localhost/role/ProductOwner"/>
            </rdf:Description>
            <rdf:Description rdf:about="https://localhost/process/role/TeamMember">
                <process:roleUrl rdf:resource="https://localhost/role/TeamMember"/>
            </rdf:Description>
        </process:roleAssignments>
    </rdf:Description>
</rdf:RDF>

It is not very strict, but I just want to know how to use Jena API to output the rdf:Collection like the above example.

1

There are 1 answers

2
user205512 On

Confusingly what parsetype="collection" creates is an rdf list, not a collection. This might have thwarted your searches.

What you want is an RDFList which you can create using a number of methods in Model. Let's assume you have most of what you need already in terms of resources and properties. The list can be created as follows:

// ... create model, resources, and properties as usual ...

// create the role assignments as you normally would
model.add(productOwner, roleUrl, productOwner);
model.add(teamMember, roleUrl, teamMember);

// Create a list containing the subjects of the role assignments in one go
RDFList list = model.createList(new RDFNode[] {productOwner, teamMember}));

// Add the list to the model
model.add(test, roleAssignments, list);

// ... now write the model out as rdf/xml or whatever you like ...

You could also create an empty list and add each item to it.