committing revision 8270. Merged from trunk.

This commit is contained in:
anupsawant 2011-06-21 20:27:19 +00:00
commit a78ae5acf7
25 changed files with 298 additions and 164 deletions

View file

@ -151,18 +151,13 @@ public class SparqlQueryServlet extends BaseEditController {
String queryParam = vreq.getParameter("query");
boolean graphPresent = false;
StringTokenizer tokenizer = new StringTokenizer(queryParam, " ");
while(tokenizer.hasMoreTokens()){
if("graph".equalsIgnoreCase(tokenizer.nextToken())){
graphPresent = true;
break;
String[] tokens = queryParam.split("\\s");
for(int i = 0; i < tokens.length; i++){
if("graph".equalsIgnoreCase(tokens[i])){
return vreq.getDataset();
}
}
Dataset dataset = vreq.getDataset();
if (dataset != null && graphPresent) {
return dataset;
}
DataSource dataSource = DatasetFactory.create();
dataSource.setDefaultModel(vreq.getJenaOntModel());
return dataSource;

View file

@ -70,6 +70,10 @@ public class VitroRequest extends HttpServletRequestWrapper {
setAttribute("dataset", dataset);
}
public void setJenaOntModel(OntModel ontModel) {
setAttribute("jenaOntModel", ontModel);
}
/** gets assertions + inferences WebappDaoFactory with no filtering **/
public WebappDaoFactory getFullWebappDaoFactory() {
Object webappDaoFactoryAttr = _req.getAttribute("fullWebappDaoFactory");
@ -106,6 +110,10 @@ public class VitroRequest extends HttpServletRequestWrapper {
}
public OntModel getJenaOntModel() {
Object ontModel = getAttribute("jenaOntModel");
if (ontModel instanceof OntModel) {
return (OntModel) ontModel;
}
OntModel jenaOntModel = (OntModel)_req.getSession().getAttribute( JenaBaseDao.JENA_ONT_MODEL_ATTRIBUTE_NAME );
if ( jenaOntModel == null ) {
jenaOntModel = (OntModel)_req.getSession().getServletContext().getAttribute( JenaBaseDao.JENA_ONT_MODEL_ATTRIBUTE_NAME );

View file

@ -78,6 +78,7 @@ public class IndividualController extends FreemarkerHttpServlet {
private static final String INCLUDE_ALL = "all";
private static final Map<String, String> namespaces = new HashMap<String, String>() {{
put("display", VitroVocabulary.DISPLAY);
put("vitro", VitroVocabulary.vitroURI);
put("vitroPublic", VitroVocabulary.VITRO_PUBLIC);
}};
@ -334,13 +335,7 @@ public class IndividualController extends FreemarkerHttpServlet {
private ResponseValues doRdf(VitroRequest vreq, Individual individual,
ContentType rdfFormat) throws IOException, ServletException {
OntModel ontModel = null;
HttpSession session = vreq.getSession(false);
if( session != null )
ontModel = (OntModel)session.getAttribute("jenaOntModel");
if( ontModel == null)
ontModel = (OntModel)getServletContext().getAttribute("jenaOntModel");
OntModel ontModel = vreq.getJenaOntModel();
String[] includes = vreq.getParameterValues("include");
Model newModel = getRDF(individual,ontModel,ModelFactory.createDefaultModel(),0,includes);

View file

@ -10,6 +10,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
@ -26,18 +27,14 @@ import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.AnonId;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.util.iterator.ClosableIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
@ -105,67 +102,42 @@ public class IndividualDaoSDB extends IndividualDaoJena {
ents.addAll(getIndividualsByVClass(vc));
}
} else {
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
String[] graphVars = {"?g", "?h", "?i"};
String query =
"SELECT DISTINCT ?ind ?label ?moniker " +
"WHERE " +
"{ \n" +
"{ ?ind a <" + theClass.getURI() + "> } \n" +
"UNION { \n" +
" ?ind a <" + theClass.getURI() + "> . \n" +
" ?ind <" + RDFS.label.getURI() + "> ?label \n" +
"} \n" +
"UNION { \n" +
" ?ind a <" + theClass.getURI() + "> . \n" +
" ?ind <" + VitroVocabulary.MONIKER + "> ?moniker \n" +
"} \n" +
"} ORDER BY ?ind ?label";
ResultSet rs =QueryExecutionFactory.create(
QueryFactory.create(query), dataset)
.execSelect();
String uri = null;
String label = null;
String moniker = null;
while (rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
Resource currRes = sol.getResource("ind");
if (currRes.isAnon()) {
continue;
}
if (uri != null && !uri.equals(currRes.getURI())) {
Individual ent = makeIndividual(uri, label, moniker);
if (ent != null) {
ents.add(ent);
}
uri = currRes.getURI();
label = null;
moniker = null;
} else if (uri == null) {
uri = currRes.getURI();
}
Literal labelLit = sol.getLiteral("label");
if (labelLit != null) {
label = labelLit.getLexicalForm();
}
Literal monikerLit = sol.getLiteral("moniker");
if (monikerLit != null) {
moniker = monikerLit.getLexicalForm();
}
if (!rs.hasNext()) {
Individual ent = makeIndividual(uri, label, moniker);
if (ent != null) {
ents.add(ent);
}
}
}
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
List<Individual> individualList;
// Check if there is a graph filter.
// If so, we will use it in a slightly strange way. Unfortunately,
// performance is quite bad if we add several graph variables in
// order to account for the fact that an individual's type
// declaration may be in a different graph from its label or
// moniker. Thus, we will run two queries: one with a single
// graph variable to get the list of URIs, and a second against
// the union graph to get individuals with their labels and
// monikers. We will then toss out any individual in the second
// list that is not also in the first list.
// Annoying, yes, but better than the alternative.
// Note that both queries need to sort identically or
// the results may be very strange.
String[] graphVars = {"?g"};
String filterStr = WebappDaoFactorySDB.getFilterBlock(
graphVars, datasetMode);
if (!StringUtils.isEmpty(filterStr)) {
List<Individual> graphFilteredIndividualList =
getGraphFilteredIndividualList(theClass, filterStr);
List<Individual> unfilteredIndividualList = getIndividualList(
theClass);
Iterator<Individual> unfilteredIt = unfilteredIndividualList
.iterator();
for (Individual filt : graphFilteredIndividualList) {
Individual unfilt = unfilteredIt.next();
while (!unfilt.getURI().equals(filt.getURI())) {
unfilt = unfilteredIt.next();
}
ents.add(unfilt);
}
} else {
ents = getIndividualList(theClass);
}
}
java.util.Collections.sort(ents);
@ -181,6 +153,105 @@ public class IndividualDaoSDB extends IndividualDaoJena {
return ents;
}
private List<Individual> getIndividualList(Resource theClass) {
List<Individual> ents = new ArrayList<Individual>();
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
String query =
"SELECT DISTINCT ?ind ?label ?moniker " +
"WHERE " +
"{ \n" +
"{ ?ind a <" + theClass.getURI() + "> } \n" +
"UNION { \n" +
" ?ind a <" + theClass.getURI() + "> . \n" +
" ?ind <" + RDFS.label.getURI() + "> ?label \n" +
"} \n" +
"UNION { \n" +
" ?ind a <" + theClass.getURI() + "> . \n" +
" ?ind <" + VitroVocabulary.MONIKER + "> ?moniker \n" +
"} \n" +
"} ORDER BY ?ind ?label";
ResultSet rs =QueryExecutionFactory.create(
QueryFactory.create(query), dataset)
.execSelect();
String uri = null;
String label = null;
String moniker = null;
while (rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
Resource currRes = sol.getResource("ind");
if (currRes.isAnon()) {
continue;
}
if (uri != null && !uri.equals(currRes.getURI())) {
Individual ent = makeIndividual(uri, label, moniker);
if (ent != null) {
ents.add(ent);
}
uri = currRes.getURI();
label = null;
moniker = null;
} else if (uri == null) {
uri = currRes.getURI();
}
Literal labelLit = sol.getLiteral("label");
if (labelLit != null) {
label = labelLit.getLexicalForm();
}
Literal monikerLit = sol.getLiteral("moniker");
if (monikerLit != null) {
moniker = monikerLit.getLexicalForm();
}
if (!rs.hasNext()) {
Individual ent = makeIndividual(uri, label, moniker);
if (ent != null) {
ents.add(ent);
}
}
}
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
return ents;
}
private List<Individual> getGraphFilteredIndividualList(Resource theClass,
String filterStr) {
List<Individual> filteredIndividualList = new ArrayList<Individual>();
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
String query =
"SELECT DISTINCT ?ind ?label ?moniker " +
"WHERE " +
"{ GRAPH ?g { \n" +
"{ ?ind a <" + theClass.getURI() + "> } \n" +
" } \n" + filterStr +
"} ORDER BY ?ind";
ResultSet rs =QueryExecutionFactory.create(
QueryFactory.create(query), dataset)
.execSelect();
while (rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
Resource currRes = sol.getResource("ind");
if (currRes.isAnon()) {
continue;
}
filteredIndividualList.add(
makeIndividual(currRes.getURI(), null, null));
}
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
return filteredIndividualList;
}
private Individual makeIndividual(String uri, String label, String moniker) {
Individual ent = new IndividualSDB(uri,

View file

@ -933,21 +933,18 @@ public class IndividualSDB extends IndividualImpl implements Individual {
if (ind.getModel().contains((Resource) null, RDF.type, (RDFNode) null)){
tempModel = ind.getModel();
} else {
String[] graphVars = { "?g" };
String getTypes =
"CONSTRUCT{ <" + this.individualURI + "> <" + RDF.type +
"> ?types }\n" +
"WHERE{ GRAPH " +
((direct)
? "<http://vitro.mannlib.cornell.edu/default/vitro-kb-2>"
: "?g")
+ " { <" + this.individualURI +"> <" +RDF.type+ "> ?types } \n" ;
if (!direct) {
String[] graphVars = { "?g" };
getTypes += WebappDaoFactorySDB.getFilterBlock(graphVars, datasetMode);
}
getTypes += "} \n";
"WHERE{ GRAPH ?g"
+ " { <" + this.individualURI +"> <" +RDF.type+ "> ?types } \n"
+ WebappDaoFactorySDB.getFilterBlock(
graphVars, (direct
? WebappDaoFactorySDB.SDBDatasetMode
.ASSERTIONS_ONLY
: datasetMode))
+ "} \n";
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);

View file

@ -23,6 +23,7 @@ import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
public class WebappDaoFactorySDB extends WebappDaoFactoryJena {
public static final String UNION_GRAPH = "urn:x-arq:UnionGraph";
private SDBDatasetMode datasetMode = SDBDatasetMode.ASSERTIONS_AND_INFERENCES;
/**

View file

@ -21,7 +21,9 @@ import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sdb.SDBFactory;
import com.hp.hpl.jena.sdb.Store;
import com.hp.hpl.jena.sdb.StoreDesc;
@ -106,6 +108,9 @@ public class WebappDaoFactorySDBPrep implements Filter {
vreq.setWebappDaoFactory(wadf);
vreq.setFullWebappDaoFactory(wadf);
vreq.setDataset(dataset);
vreq.setJenaOntModel(ModelFactory.createOntologyModel(
OntModelSpec.OWL_MEM, dataset.getNamedModel(
WebappDaoFactorySDB.UNION_GRAPH)));
}
} catch (Throwable t) {
log.error("Unable to filter request to set up SDB connection", t);