Changing getAllPossibleDataPropsForIndividual to only call getVClasses once. NIHVIVO-2002

This commit is contained in:
bdc34 2011-02-04 18:05:31 +00:00
parent dad29a5516
commit 0c6aa67e06

View file

@ -418,9 +418,10 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
public Collection<DataProperty> getAllPossibleDatapropsForIndividual(String individualURI) { public Collection<DataProperty> getAllPossibleDatapropsForIndividual(String individualURI) {
Individual ind = getWebappDaoFactory().getIndividualDao().getIndividualByURI(individualURI); Individual ind = getWebappDaoFactory().getIndividualDao().getIndividualByURI(individualURI);
Collection<DataProperty> dpColl = new ArrayList<DataProperty>(); Collection<DataProperty> dpColl = new ArrayList<DataProperty>();
List<String> vclassURIs = getVClassURIs(ind);
try { try {
for (VClass currClass : ind.getVClasses(true)) { for (VClass currClass : ind.getVClasses( DIRECT )) {
List<DataProperty> currList = getDatapropsForClass(currClass.getURI()); List<DataProperty> currList = getDatapropsForClass(currClass.getURI());
for (Iterator<DataProperty> dpIter = currList.iterator(); dpIter.hasNext();) { for (Iterator<DataProperty> dpIter = currList.iterator(); dpIter.hasNext();) {
DataProperty dp = (DataProperty) dpIter.next(); DataProperty dp = (DataProperty) dpIter.next();
@ -444,7 +445,7 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
// now change range datatype based on individual // now change range datatype based on individual
// TODO: rethink all these methods to reduce inefficiency // TODO: rethink all these methods to reduce inefficiency
for (DataProperty dp : dpColl) { for (DataProperty dp : dpColl) {
dp.setRangeDatatypeURI(getRequiredDatatypeURI(ind, dp)); dp.setRangeDatatypeURI(getRequiredDatatypeURI(ind, dp, vclassURIs));
} }
} }
} catch (ProfileException pe) { } catch (ProfileException pe) {
@ -466,59 +467,68 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
); );
} }
public String getRequiredDatatypeURI(Individual individual, DataProperty dataprop) { private String getRequiredDatatypeURI(Individual individual, DataProperty dataprop, List<String> vclassURIs) {
OntModel ontModel = getOntModelSelector().getFullModel(); OntModel ontModel = getOntModelSelector().getFullModel();
String datatypeURI = dataprop.getRangeDatatypeURI(); String datatypeURI = dataprop.getRangeDatatypeURI();
List<String> vclassURIs = null;
if (reasoningAvailable()) { ontModel.enterCriticalSection(Lock.READ);
vclassURIs = new ArrayList<String>(); try {
for (VClass vc : individual.getVClasses(INDIRECT)) { // get universal restrictions applicable to data property
if (vc.getURI() != null) { Iterator<Resource> restIt = ontModel.listSubjectsWithProperty(OWL.onProperty, ontModel.getResource(dataprop.getURI()));
vclassURIs.add(vc.getURI()); while (restIt.hasNext()) {
} Resource restRes = restIt.next();
} if (restRes.canAs(Restriction.class)) {
} else { Restriction rest = (Restriction) restRes.as(Restriction.class);
vclassURIs = getSupertypeURIs(individual); if (rest.isAllValuesFromRestriction()) {
} AllValuesFromRestriction avfrest = rest.asAllValuesFromRestriction();
ontModel.enterCriticalSection(Lock.READ); if (avfrest.getAllValuesFrom() != null) {
try { // check if the individual has the restriction as one of its types
// get universal restrictions applicable to data property if (!individual.isAnonymous() &&
Iterator<Resource> restIt = ontModel.listSubjectsWithProperty(OWL.onProperty, ontModel.getResource(dataprop.getURI())); ontModel.contains(ontModel.getResource(individual.getURI()),
while (restIt.hasNext()) { RDF.type,
Resource restRes = restIt.next(); rest)
if (restRes.canAs(Restriction.class)) { ) {
Restriction rest = (Restriction) restRes.as(Restriction.class); datatypeURI = avfrest.getAllValuesFrom().getURI();
if (rest.isAllValuesFromRestriction()) { break;
AllValuesFromRestriction avfrest = rest.asAllValuesFromRestriction(); } else {
if (avfrest.getAllValuesFrom() != null) { // check if the restriction applies to one of the individual's types
// check if the individual has the restriction as one of its types List<Resource> equivOrSubResources = new ArrayList<Resource>();
if (!individual.isAnonymous() && equivOrSubResources.addAll(ontModel.listSubjectsWithProperty(RDFS.subClassOf, rest).toList());
ontModel.contains(ontModel.getResource(individual.getURI()), equivOrSubResources.addAll(ontModel.listSubjectsWithProperty(OWL.equivalentClass, rest).toList());
RDF.type, for(Resource equivOrSubRes : equivOrSubResources) {
rest) if (!equivOrSubRes.isAnon() && vclassURIs.contains(equivOrSubRes.getURI())) {
) { datatypeURI = avfrest.getAllValuesFrom().getURI();
datatypeURI = avfrest.getAllValuesFrom().getURI(); break;
break; }
} else { }
// check if the restriction applies to one of the individual's types }
List<Resource> equivOrSubResources = new ArrayList<Resource>(); }
equivOrSubResources.addAll(ontModel.listSubjectsWithProperty(RDFS.subClassOf, rest).toList()); }
equivOrSubResources.addAll(ontModel.listSubjectsWithProperty(OWL.equivalentClass, rest).toList()); }
for(Resource equivOrSubRes : equivOrSubResources) { }
if (!equivOrSubRes.isAnon() && vclassURIs.contains(equivOrSubRes.getURI())) { } finally {
datatypeURI = avfrest.getAllValuesFrom().getURI(); ontModel.leaveCriticalSection();
break; }
} return datatypeURI;
} }
}
} public String getRequiredDatatypeURI(Individual individual, DataProperty dataprop) {
} return getRequiredDatatypeURI(individual,dataprop,getVClassURIs(individual));
} }
}
} finally { private List<String> getVClassURIs(Individual individual){
ontModel.leaveCriticalSection(); List<String> vclassURIs = null;
} if (reasoningAvailable()) {
return datatypeURI; vclassURIs = new ArrayList<String>();
for (VClass vc : individual.getVClasses(INDIRECT)) {
if (vc.getURI() != null) {
vclassURIs.add(vc.getURI());
}
}
} else {
vclassURIs = getSupertypeURIs(individual);
}
return vclassURIs;
} }
private boolean DIRECT = true; private boolean DIRECT = true;