SELECT-based implementation of getIndividualsByVClassURI for SDB
This commit is contained in:
parent
e9ca1f9986
commit
30bd6c7d88
3 changed files with 186 additions and 159 deletions
|
@ -25,7 +25,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
|||
|
||||
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) {
|
||||
VitroRequest vrequest = new VitroRequest(request);
|
||||
|
@ -59,7 +59,8 @@ public class IndividualsListingController extends BaseEditController {
|
|||
String vclassURI = request.getParameter("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();
|
||||
results.add("XX");
|
||||
|
|
|
@ -71,6 +71,8 @@ public class IndividualDaoSDB extends IndividualDaoJena {
|
|||
return getOntModelSelector().getABoxModel();
|
||||
}
|
||||
|
||||
private static final boolean SKIP_INITIALIZATION = true;
|
||||
|
||||
@Override
|
||||
public List getIndividualsByVClassURI(String vclassURI, int offset, int quantity ) {
|
||||
|
||||
|
@ -78,7 +80,7 @@ public class IndividualDaoSDB extends IndividualDaoJena {
|
|||
return null;
|
||||
}
|
||||
|
||||
List ents = new ArrayList();
|
||||
List<Individual> ents = new ArrayList<Individual>();
|
||||
|
||||
Resource theClass = (vclassURI.indexOf(PSEUDO_BNODE_NS) == 0)
|
||||
? getOntModel().createResource(new AnonId(vclassURI.split("#")[1]))
|
||||
|
@ -91,54 +93,59 @@ public class IndividualDaoSDB extends IndividualDaoJena {
|
|||
ents.addAll(getIndividualsByVClass(vc));
|
||||
}
|
||||
} else {
|
||||
Model model = null;
|
||||
try {
|
||||
DatasetWrapper w = getDatasetWrapper();
|
||||
Dataset dataset = w.getDataset();
|
||||
dataset.getLock().enterCriticalSection(Lock.READ);
|
||||
try {
|
||||
String query =
|
||||
"CONSTRUCT " +
|
||||
"{ ?ind <" + RDFS.label.getURI() + "> ?ooo. \n" +
|
||||
"?ind a <" + theClass.getURI() + "> . \n" +
|
||||
"?ind <" + VitroVocabulary.MONIKER + "> ?moniker \n" +
|
||||
"} WHERE " +
|
||||
"SELECT DISTINCT ?ind ?label ?moniker " +
|
||||
"WHERE " +
|
||||
"{ GRAPH ?g { \n" +
|
||||
" ?ind a <" + theClass.getURI() + "> \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" +
|
||||
"}";
|
||||
model = QueryExecutionFactory.create(QueryFactory.create(query), dataset).execConstruct();
|
||||
"} ORDER BY ?label";
|
||||
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 {
|
||||
dataset.getLock().leaveCriticalSection();
|
||||
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);
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
|
|
@ -97,26 +97,22 @@ public class IndividualSDB extends IndividualImpl implements Individual {
|
|||
|
||||
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model);
|
||||
this.ind = ontModel.createOntResource(individualURI);
|
||||
|
||||
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);
|
||||
}
|
||||
setUpURIParts(ind);
|
||||
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.dwf = datasetWrapperFactory;
|
||||
|
||||
if (skipInitialization) {
|
||||
OntModel ontModel = ModelFactory.createOntologyModel(
|
||||
OntModelSpec.OWL_MEM);
|
||||
this.ind = ontModel.createOntResource(individualURI);
|
||||
} else {
|
||||
DatasetWrapper w = getDatasetWrapper();
|
||||
Dataset dataset = w.getDataset();
|
||||
try {
|
||||
|
@ -142,7 +138,20 @@ public class IndividualSDB extends IndividualImpl implements Individual {
|
|||
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model);
|
||||
|
||||
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.isAnon()) {
|
||||
this.setNamespace(VitroVocabulary.PSEUDO_BNODE_NS);
|
||||
|
@ -155,7 +164,6 @@ public class IndividualSDB extends IndividualImpl implements Individual {
|
|||
} else if (individualURI != null) {
|
||||
log.warn("Null individual returned for URI " + individualURI);
|
||||
}
|
||||
this.webappDaoFactory = wadf;
|
||||
}
|
||||
|
||||
private DatasetWrapper getDatasetWrapper() {
|
||||
|
@ -1016,21 +1024,30 @@ public class IndividualSDB extends IndividualImpl implements Individual {
|
|||
}
|
||||
|
||||
private List<VClass> getMyVClasses(boolean direct) {
|
||||
long mstart = System.currentTimeMillis();
|
||||
List<VClass> vClassList = new ArrayList<VClass>();
|
||||
//this.dataset.getLock().enterCriticalSection(Lock.READ);
|
||||
//Model tempModel = ModelFactory.createDefaultModel();
|
||||
Model tempModel = ind.getModel();
|
||||
//try {
|
||||
//String getTypes =
|
||||
// "CONSTRUCT{ <" + this.individualURI + "> <" + RDF.type + "> ?types }\n" +
|
||||
// "WHERE{ GRAPH " + ((true) ? "<http://vitro.mannlib.cornell.edu/default/vitro-kb-2>" : "?g")
|
||||
// + " { <" + this.individualURI +"> <" +RDF.type+ "> ?types \n" +
|
||||
// "} } \n";
|
||||
//long startTime = System.currentTimeMillis();
|
||||
//tempModel = QueryExecutionFactory.create(QueryFactory.create(getTypes), dataset).execConstruct();
|
||||
//System.out.println((System.currentTimeMillis() - startTime));
|
||||
StmtIterator stmtItr = tempModel.listStatements((Resource) null, RDF.type, (RDFNode) null);
|
||||
Model tempModel = null;
|
||||
if (ind.getModel().size() > 0) {
|
||||
tempModel = ind.getModel();
|
||||
}
|
||||
else {
|
||||
String getTypes =
|
||||
"CONSTRUCT{ <" + this.individualURI + "> <" + RDF.type + "> ?types }\n" +
|
||||
"WHERE{ GRAPH " + ((true) ? "<http://vitro.mannlib.cornell.edu/default/vitro-kb-2>" : "?g")
|
||||
+ " { <" + this.individualURI +"> <" +RDF.type+ "> ?types \n" +
|
||||
"} } \n";
|
||||
DatasetWrapper w = getDatasetWrapper();
|
||||
Dataset dataset = w.getDataset();
|
||||
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>();
|
||||
while(stmtItr.hasNext()){
|
||||
Statement stmt = stmtItr.nextStatement();
|
||||
|
@ -1058,14 +1075,15 @@ public class IndividualSDB extends IndividualImpl implements Individual {
|
|||
}while(done.contains(currentType));
|
||||
|
||||
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
|
||||
itr = list.listIterator();
|
||||
|
||||
while(itr.hasNext()){
|
||||
String nextType = itr.next();
|
||||
if(checkSubClass.isSubClassOf(currentType, nextType) && !currentType.equalsIgnoreCase(nextType)){
|
||||
//System.out.println(currentType + " is subClassOf " + nextType);
|
||||
if(checkSubClass.isSubClassOf(currentType, nextType)
|
||||
&& !currentType.equalsIgnoreCase(nextType)){
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
|
@ -1076,30 +1094,31 @@ public class IndividualSDB extends IndividualImpl implements Individual {
|
|||
|
||||
/* 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());
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue