简体   繁体   中英

In Jena API, How to count how many individuals of a class using Jena RULE?

I want to get how many individuals of a class. For example, for the Person class, there is A, B, C. I tried to count it using the following rule:

[r3: (?p rdf:type hv:Person),(?classx1 hv:hasvalue ?n),addOne(?n,?new)
-> drop(1),(?class1 hv:hasvalue ?new)
]

However, it was fired in a closed loop since the (?class1 hv:hasvalue ?new) will change each time. I dont like to introduce SPARQL.

What I do to count individuals is :

OntModel model;

Resource individual = model.getResource("http://www.w3.org/2002/07/owl#NamedIndividual"));
Property property = model.getProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");

ResIterator iterator = infModel.listResourcesWithProperty(property, individual) ;

int count = Iterators.size(iterator);

Last line is possible because of Guava Library .

It can be improved but still a solution.

More information and corrections in the comments. Thanks to @ssz.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM