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.

Answers


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.


Need Your Help

How to list all embedded files from a microsoft office document, using Apache POI?

java apache-poi text-extraction embedded-object

is there any opportunity to list all embedded objects (doc, ..., txt) in a office file (doc, docx, xls, xlsx, ppt, pptx, ...)?

Fill dropdown menu according to onchange action PHP

php sql onchange

I need to fill a dropdown menu from a database table then according to selection in this dropdown menu, in the same page I have to fill another dropdown menu by using sql queries and variable comin...