Mahoni
Mahoni

Reputation: 7466

Delete all resources with no properties in Apache Jena?

Is there a way in Apache Jena to remove all resources from the current model, which do not have any properties?

I only found how to delete certain triples, but not something like iterating the resources, checking how much properties they have, etc.

Upvotes: 2

Views: 676

Answers (1)

RobV
RobV

Reputation: 28655

It doesn't exist because there is no need for it to exist. The RDF data model describes a Graph based upon triples where the subject and objects map to Resources in the Jena parlance and the predicates map to properties.

It is not possible to have a resource without any properties thus there is no need for a method to remove such resources to exist.

For iterating the resources try the listSubjects() and listObjects() methods

To get the number of properties associated with a specific resource you can use the listStatements() method to get a StmtIterator and then count the statements returned by that e.g.

//Assuming you have a Model in variable model
//Assuming you already have some Resource in variable res
StmtIterator stmts = model.listStatements(res, null, null);
int count = 0;
while (stmts.hasNext())
{
  count++;
  stmts.next();
}

If you want to count a whole variety of things (e.g. number of properties for each resource) at once you may want to use a SPARQL query instead e.g.

SELECT ?s (COUNT(?p) AS ?NumProperties)
WHERE { ?s ?p ?o } GROUP BY ?s

See the documentation for how to run SPARQL queries.

Upvotes: 4

Related Questions