SELECT-based implementation of getIndividualsByVClassURI for SDB

This commit is contained in:
bjl23 2010-12-10 21:53:12 +00:00
parent e9ca1f9986
commit 30bd6c7d88
3 changed files with 186 additions and 159 deletions

View file

@ -25,7 +25,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
public class IndividualsListingController extends BaseEditController { public class IndividualsListingController extends BaseEditController {
private static final int MAX_INDIVIDUALS = 50; //private static final int MAX_INDIVIDUALS = 50;
public void doGet(HttpServletRequest request, HttpServletResponse response) { public void doGet(HttpServletRequest request, HttpServletResponse response) {
VitroRequest vrequest = new VitroRequest(request); VitroRequest vrequest = new VitroRequest(request);
@ -59,7 +59,8 @@ public class IndividualsListingController extends BaseEditController {
String vclassURI = request.getParameter("VClassURI"); String vclassURI = request.getParameter("VClassURI");
VClass vc = vcDao.getVClassByURI(vclassURI); VClass vc = vcDao.getVClassByURI(vclassURI);
List inds = dao.getIndividualsByVClassURI(vclassURI,1,MAX_INDIVIDUALS); List inds = dao.getIndividualsByVClassURI(vclassURI);
//List inds = dao.getIndividualsByVClassURI(vclassURI,1,MAX_INDIVIDUALS);
ArrayList results = new ArrayList(); ArrayList results = new ArrayList();
results.add("XX"); results.add("XX");

View file

@ -71,6 +71,8 @@ public class IndividualDaoSDB extends IndividualDaoJena {
return getOntModelSelector().getABoxModel(); return getOntModelSelector().getABoxModel();
} }
private static final boolean SKIP_INITIALIZATION = true;
@Override @Override
public List getIndividualsByVClassURI(String vclassURI, int offset, int quantity ) { public List getIndividualsByVClassURI(String vclassURI, int offset, int quantity ) {
@ -78,7 +80,7 @@ public class IndividualDaoSDB extends IndividualDaoJena {
return null; return null;
} }
List ents = new ArrayList(); List<Individual> ents = new ArrayList<Individual>();
Resource theClass = (vclassURI.indexOf(PSEUDO_BNODE_NS) == 0) Resource theClass = (vclassURI.indexOf(PSEUDO_BNODE_NS) == 0)
? getOntModel().createResource(new AnonId(vclassURI.split("#")[1])) ? getOntModel().createResource(new AnonId(vclassURI.split("#")[1]))
@ -91,54 +93,59 @@ public class IndividualDaoSDB extends IndividualDaoJena {
ents.addAll(getIndividualsByVClass(vc)); ents.addAll(getIndividualsByVClass(vc));
} }
} else { } else {
Model model = null;
try {
DatasetWrapper w = getDatasetWrapper(); DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset(); Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ); dataset.getLock().enterCriticalSection(Lock.READ);
try { try {
String query = String query =
"CONSTRUCT " + "SELECT DISTINCT ?ind ?label ?moniker " +
"{ ?ind <" + RDFS.label.getURI() + "> ?ooo. \n" + "WHERE " +
"?ind a <" + theClass.getURI() + "> . \n" +
"?ind <" + VitroVocabulary.MONIKER + "> ?moniker \n" +
"} WHERE " +
"{ GRAPH ?g { \n" + "{ GRAPH ?g { \n" +
" ?ind a <" + theClass.getURI() + "> \n" + " ?ind a <" + theClass.getURI() + "> \n" +
"} \n" + "} \n" +
"OPTIONAL { GRAPH ?h { ?ind <" + RDFS.label.getURI() + "> ?ooo } }\n" + "OPTIONAL { GRAPH ?h { ?ind <" + RDFS.label.getURI() + "> ?label } }\n" +
"OPTIONAL { GRAPH ?i { ?ind <" + VitroVocabulary.MONIKER + "> ?moniker } } \n" + "OPTIONAL { GRAPH ?i { ?ind <" + VitroVocabulary.MONIKER + "> ?moniker } } \n" +
"}"; "} ORDER BY ?label";
model = QueryExecutionFactory.create(QueryFactory.create(query), dataset).execConstruct(); ResultSet rs =QueryExecutionFactory.create(
QueryFactory.create(query), dataset)
.execSelect();
Resource res = null;
while (rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
Resource currRes = sol.getResource("ind");
if ((res == null || !res.equals(currRes))
&& !currRes.isAnon()) {
res = currRes;
Individual ent = new IndividualSDB(currRes.getURI(),
this.dwf, getWebappDaoFactory(),
SKIP_INITIALIZATION);
Literal label = sol.getLiteral("label");
if (label != null) {
ent.setName(label.getLexicalForm());
}
Literal moniker = sol.getLiteral("moniker");
if (moniker != null) {
ent.setMoniker(moniker.getLexicalForm());
}
ents.add(ent);
}
}
} finally { } finally {
dataset.getLock().leaveCriticalSection(); dataset.getLock().leaveCriticalSection();
w.close(); w.close();
} }
ResIterator resIt = model.listSubjects();
try {
while (resIt.hasNext()) {
Resource ind = resIt.nextResource();
if (!ind.isAnon()) {
//Individual indd = new IndividualImpl(ind.getURI());
//indd.setLocalName(ind.getLocalName());
//indd.setName(ind.getLocalName());
//ents.add(indd);
ents.add(new IndividualSDB(ind.getURI(), this.dwf, getWebappDaoFactory(), model));
} }
}
} finally {
resIt.close();
}
} finally {
if (model != null && !model.isClosed()) {
model.close();
}
}
}
java.util.Collections.sort(ents); java.util.Collections.sort(ents);
if (quantity > 0 && offset > 0) {
List<Individual> sublist = new ArrayList<Individual>();
for (int i = offset - 1; i < ((offset - 1) + quantity); i++) {
sublist.add(ents.get(i));
}
return sublist;
}
return ents; return ents;
} }

View file

@ -97,26 +97,22 @@ public class IndividualSDB extends IndividualImpl implements Individual {
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model); OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model);
this.ind = ontModel.createOntResource(individualURI); this.ind = ontModel.createOntResource(individualURI);
setUpURIParts(ind);
if (ind != null) {
if (ind.isAnon()) {
this.setNamespace(VitroVocabulary.PSEUDO_BNODE_NS);
this.setLocalName(ind.getId().toString());
} else {
this.URI = ind.getURI();
this.namespace = ind.getNameSpace();
this.localName = ind.getLocalName();
}
} else if (individualURI != null) {
log.warn("Null individual returned for URI " + individualURI);
}
this.webappDaoFactory = wadf; this.webappDaoFactory = wadf;
} }
public IndividualSDB(String individualURI, DatasetWrapperFactory datasetWrapperFactory, WebappDaoFactoryJena wadf) { public IndividualSDB(String individualURI,
DatasetWrapperFactory datasetWrapperFactory,
WebappDaoFactoryJena wadf,
boolean skipInitialization) {
this.individualURI = individualURI; this.individualURI = individualURI;
this.dwf = datasetWrapperFactory; this.dwf = datasetWrapperFactory;
if (skipInitialization) {
OntModel ontModel = ModelFactory.createOntologyModel(
OntModelSpec.OWL_MEM);
this.ind = ontModel.createOntResource(individualURI);
} else {
DatasetWrapper w = getDatasetWrapper(); DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset(); Dataset dataset = w.getDataset();
try { try {
@ -142,7 +138,20 @@ public class IndividualSDB extends IndividualImpl implements Individual {
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model); OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model);
this.ind = ontModel.createOntResource(individualURI); this.ind = ontModel.createOntResource(individualURI);
}
setUpURIParts(ind);
this.webappDaoFactory = wadf;
}
private static final boolean SKIP_INITIALIZATION = true;
public IndividualSDB(String individualURI,
DatasetWrapperFactory datasetWrapperFactory,
WebappDaoFactoryJena wadf) {
this(individualURI, datasetWrapperFactory, wadf, !SKIP_INITIALIZATION);
}
private void setUpURIParts(OntResource ind) {
if (ind != null) { if (ind != null) {
if (ind.isAnon()) { if (ind.isAnon()) {
this.setNamespace(VitroVocabulary.PSEUDO_BNODE_NS); this.setNamespace(VitroVocabulary.PSEUDO_BNODE_NS);
@ -155,7 +164,6 @@ public class IndividualSDB extends IndividualImpl implements Individual {
} else if (individualURI != null) { } else if (individualURI != null) {
log.warn("Null individual returned for URI " + individualURI); log.warn("Null individual returned for URI " + individualURI);
} }
this.webappDaoFactory = wadf;
} }
private DatasetWrapper getDatasetWrapper() { private DatasetWrapper getDatasetWrapper() {
@ -1016,21 +1024,30 @@ public class IndividualSDB extends IndividualImpl implements Individual {
} }
private List<VClass> getMyVClasses(boolean direct) { private List<VClass> getMyVClasses(boolean direct) {
long mstart = System.currentTimeMillis();
List<VClass> vClassList = new ArrayList<VClass>(); List<VClass> vClassList = new ArrayList<VClass>();
//this.dataset.getLock().enterCriticalSection(Lock.READ); Model tempModel = null;
//Model tempModel = ModelFactory.createDefaultModel(); if (ind.getModel().size() > 0) {
Model tempModel = ind.getModel(); tempModel = ind.getModel();
//try { }
//String getTypes = else {
// "CONSTRUCT{ <" + this.individualURI + "> <" + RDF.type + "> ?types }\n" + String getTypes =
// "WHERE{ GRAPH " + ((true) ? "<http://vitro.mannlib.cornell.edu/default/vitro-kb-2>" : "?g") "CONSTRUCT{ <" + this.individualURI + "> <" + RDF.type + "> ?types }\n" +
// + " { <" + this.individualURI +"> <" +RDF.type+ "> ?types \n" + "WHERE{ GRAPH " + ((true) ? "<http://vitro.mannlib.cornell.edu/default/vitro-kb-2>" : "?g")
// "} } \n"; + " { <" + this.individualURI +"> <" +RDF.type+ "> ?types \n" +
//long startTime = System.currentTimeMillis(); "} } \n";
//tempModel = QueryExecutionFactory.create(QueryFactory.create(getTypes), dataset).execConstruct(); DatasetWrapper w = getDatasetWrapper();
//System.out.println((System.currentTimeMillis() - startTime)); Dataset dataset = w.getDataset();
StmtIterator stmtItr = tempModel.listStatements((Resource) null, RDF.type, (RDFNode) null); dataset.getLock().enterCriticalSection(Lock.READ);
try {
tempModel = QueryExecutionFactory.create(
QueryFactory.create(getTypes), dataset).execConstruct();
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
}
StmtIterator stmtItr = tempModel.listStatements(
(Resource) null, RDF.type, (RDFNode) null);
LinkedList<String> list = new LinkedList<String>(); LinkedList<String> list = new LinkedList<String>();
while(stmtItr.hasNext()){ while(stmtItr.hasNext()){
Statement stmt = stmtItr.nextStatement(); Statement stmt = stmtItr.nextStatement();
@ -1058,14 +1075,15 @@ public class IndividualSDB extends IndividualImpl implements Individual {
}while(done.contains(currentType)); }while(done.contains(currentType));
if(directTypes) if(directTypes)
break; // check to see if its all over otherwise start comparing break;
// check to see if it's all over otherwise start comparing
else else
itr = list.listIterator(); itr = list.listIterator();
while(itr.hasNext()){ while(itr.hasNext()){
String nextType = itr.next(); String nextType = itr.next();
if(checkSubClass.isSubClassOf(currentType, nextType) && !currentType.equalsIgnoreCase(nextType)){ if(checkSubClass.isSubClassOf(currentType, nextType)
//System.out.println(currentType + " is subClassOf " + nextType); && !currentType.equalsIgnoreCase(nextType)){
itr.remove(); itr.remove();
} }
} }
@ -1076,30 +1094,31 @@ public class IndividualSDB extends IndividualImpl implements Individual {
/* Loop for comparing ends here */ /* Loop for comparing ends here */
Iterator<String> typeIt = list.iterator(); Iterator<String> typeIt = list.iterator();
try {
for (Iterator it = typeIt; it.hasNext();) { for (Iterator it = typeIt; it.hasNext();) {
Resource type = ResourceFactory.createResource(it.next().toString()); Resource type = ResourceFactory
String typeURI = (!type.isAnon()) ? type.getURI() : VitroVocabulary.PSEUDO_BNODE_NS + type.getId().toString(); .createResource(it.next().toString());
if (type.getNameSpace() == null || (!webappDaoFactory.getNonuserNamespaces().contains(type.getNameSpace())) ) { String typeURI = (!type.isAnon())
VClass vc = webappDaoFactory.getVClassDao().getVClassByURI(type.getURI()); ? type.getURI()
: VitroVocabulary.PSEUDO_BNODE_NS
+ type.getId().toString();
if (type.getNameSpace() == null ||
(!webappDaoFactory.getNonuserNamespaces()
.contains(type.getNameSpace())) ) {
VClass vc = webappDaoFactory.getVClassDao()
.getVClassByURI(type.getURI());
if (vc != null) { if (vc != null) {
vClassList.add(vc); vClassList.add(vc);
} }
} }
}
}
} finally {
//typeIt.close();
}
//} finally {
// tempModel.close();
// this.dataset.getLock().leaveCriticalSection();
//}
try { try {
Collections.sort(vClassList); Collections.sort(vClassList);
} catch (Exception e) {} } catch (Exception e) {
log.error("Unable to sort VClass list", e);
}
//System.out.println("Overall: " + (System.currentTimeMillis() - mstart));
return vClassList; return vClassList;
} }