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; DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try { try {
DatasetWrapper w = getDatasetWrapper(); String query =
Dataset dataset = w.getDataset(); "SELECT DISTINCT ?ind ?label ?moniker " +
dataset.getLock().enterCriticalSection(Lock.READ); "WHERE " +
try { "{ GRAPH ?g { \n" +
String query = " ?ind a <" + theClass.getURI() + "> \n" +
"CONSTRUCT " + "} \n" +
"{ ?ind <" + RDFS.label.getURI() + "> ?ooo. \n" + "OPTIONAL { GRAPH ?h { ?ind <" + RDFS.label.getURI() + "> ?label } }\n" +
"?ind a <" + theClass.getURI() + "> . \n" + "OPTIONAL { GRAPH ?i { ?ind <" + VitroVocabulary.MONIKER + "> ?moniker } } \n" +
"?ind <" + VitroVocabulary.MONIKER + "> ?moniker \n" + "} ORDER BY ?label";
"} WHERE " + ResultSet rs =QueryExecutionFactory.create(
"{ GRAPH ?g { \n" + QueryFactory.create(query), dataset)
" ?ind a <" + theClass.getURI() + "> \n" + .execSelect();
"} \n" + Resource res = null;
"OPTIONAL { GRAPH ?h { ?ind <" + RDFS.label.getURI() + "> ?ooo } }\n" + while (rs.hasNext()) {
"OPTIONAL { GRAPH ?i { ?ind <" + VitroVocabulary.MONIKER + "> ?moniker } } \n" + QuerySolution sol = rs.nextSolution();
"}"; Resource currRes = sol.getResource("ind");
model = QueryExecutionFactory.create(QueryFactory.create(query), dataset).execConstruct(); if ((res == null || !res.equals(currRes))
} finally { && !currRes.isAnon()) {
dataset.getLock().leaveCriticalSection(); res = currRes;
w.close(); Individual ent = new IndividualSDB(currRes.getURI(),
} this.dwf, getWebappDaoFactory(),
ResIterator resIt = model.listSubjects(); SKIP_INITIALIZATION);
try { Literal label = sol.getLiteral("label");
while (resIt.hasNext()) { if (label != null) {
Resource ind = resIt.nextResource(); ent.setName(label.getLexicalForm());
if (!ind.isAnon()) { }
//Individual indd = new IndividualImpl(ind.getURI()); Literal moniker = sol.getLiteral("moniker");
//indd.setLocalName(ind.getLocalName()); if (moniker != null) {
//indd.setName(ind.getLocalName()); ent.setMoniker(moniker.getLexicalForm());
//ents.add(indd); }
ents.add(new IndividualSDB(ind.getURI(), this.dwf, getWebappDaoFactory(), model)); ents.add(ent);
} }
} }
} finally {
resIt.close();
}
} finally { } finally {
if (model != null && !model.isClosed()) { dataset.getLock().leaveCriticalSection();
model.close(); w.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,67 +97,75 @@ 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;
DatasetWrapper w = getDatasetWrapper(); if (skipInitialization) {
Dataset dataset = w.getDataset(); OntModel ontModel = ModelFactory.createOntologyModel(
try { OntModelSpec.OWL_MEM);
dataset.getLock().enterCriticalSection(Lock.READ); this.ind = ontModel.createOntResource(individualURI);
String getStatements = } else {
"CONSTRUCT " + DatasetWrapper w = getDatasetWrapper();
"{ <"+individualURI+"> <" + RDFS.label.getURI() + "> ?ooo. \n" + Dataset dataset = w.getDataset();
"<"+individualURI+"> a ?type . \n" + try {
"<"+individualURI+"> <" + VitroVocabulary.MONIKER + "> ?moniker \n" + dataset.getLock().enterCriticalSection(Lock.READ);
"} WHERE {" + String getStatements =
"{ GRAPH ?g { \n" + "CONSTRUCT " +
"{ <"+individualURI+"> <" + RDFS.label.getURI() + "> ?ooo } \n" + "{ <"+individualURI+"> <" + RDFS.label.getURI() + "> ?ooo. \n" +
"UNION { <"+individualURI+"> <" + VitroVocabulary.MONIKER + "> ?moniker } \n" + "<"+individualURI+"> a ?type . \n" +
"} \n" + "<"+individualURI+"> <" + VitroVocabulary.MONIKER + "> ?moniker \n" +
"} UNION { GRAPH <http://vitro.mannlib.cornell.edu/default/vitro-kb-2> { <"+individualURI+"> a ?type } } \n" + "} WHERE {" +
"}"; "{ GRAPH ?g { \n" +
model = QueryExecutionFactory.create(QueryFactory.create(getStatements), dataset).execConstruct(); "{ <"+individualURI+"> <" + RDFS.label.getURI() + "> ?ooo } \n" +
} finally { "UNION { <"+individualURI+"> <" + VitroVocabulary.MONIKER + "> ?moniker } \n" +
dataset.getLock().leaveCriticalSection(); "} \n" +
w.close(); "} UNION { GRAPH <http://vitro.mannlib.cornell.edu/default/vitro-kb-2> { <"+individualURI+"> a ?type } } \n" +
} "}";
model = QueryExecutionFactory.create(QueryFactory.create(getStatements), dataset).execConstruct();
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model); } finally {
dataset.getLock().leaveCriticalSection();
this.ind = ontModel.createOntResource(individualURI); w.close();
}
if (ind != null) {
if (ind.isAnon()) { OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model);
this.setNamespace(VitroVocabulary.PSEUDO_BNODE_NS);
this.setLocalName(ind.getId().toString()); this.ind = ontModel.createOntResource(individualURI);
} 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);
} }
setUpURIParts(ind);
this.webappDaoFactory = wadf; 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.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);
}
}
private DatasetWrapper getDatasetWrapper() { private DatasetWrapper getDatasetWrapper() {
return this.dwf.getDatasetWrapper(); return this.dwf.getDatasetWrapper();
} }
@ -1016,36 +1024,45 @@ 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);
LinkedList<String> list = new LinkedList<String>(); try {
while(stmtItr.hasNext()){ tempModel = QueryExecutionFactory.create(
Statement stmt = stmtItr.nextStatement(); QueryFactory.create(getTypes), dataset).execConstruct();
if (stmt.getObject().isResource() && !stmt.getObject().isAnon()) { } finally {
list.add(((Resource) stmt.getObject()).getURI()); dataset.getLock().leaveCriticalSection();
} w.close();
} }
Iterator<String> itr = null; }
VClassDao checkSubClass = this.webappDaoFactory.getVClassDao(); StmtIterator stmtItr = tempModel.listStatements(
boolean directTypes = false; (Resource) null, RDF.type, (RDFNode) null);
String currentType = null; LinkedList<String> list = new LinkedList<String>();
ArrayList<String> done = new ArrayList<String>(); while(stmtItr.hasNext()){
Statement stmt = stmtItr.nextStatement();
if (stmt.getObject().isResource() && !stmt.getObject().isAnon()) {
list.add(((Resource) stmt.getObject()).getURI());
}
}
Iterator<String> itr = null;
VClassDao checkSubClass = this.webappDaoFactory.getVClassDao();
boolean directTypes = false;
String currentType = null;
ArrayList<String> done = new ArrayList<String>();
/* Loop for comparing starts here */ /* Loop for comparing starts here */
if(direct){ if(direct){
while(!directTypes){ while(!directTypes){
itr = list.listIterator(); itr = list.listIterator();
@ -1058,48 +1075,50 @@ 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();
} }
}
done.add(currentType); // add the uri to done list.
} }
}
done.add(currentType); // add the uri to done list. /* Loop for comparing ends here */
} Iterator<String> typeIt = list.iterator();
}
/* Loop for comparing ends here */
Iterator<String> typeIt = list.iterator();
try {
for (Iterator it = typeIt; it.hasNext();) {
Resource type = ResourceFactory.createResource(it.next().toString());
String typeURI = (!type.isAnon()) ? 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) {
vClassList.add(vc);
}
}
for (Iterator it = typeIt; it.hasNext();) {
Resource type = ResourceFactory
.createResource(it.next().toString());
String typeURI = (!type.isAnon())
? 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) {
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;
} }