NIHVIVO-2416 Handle properties declared as both object and data properties in individual property display without throwing an error.

This commit is contained in:
ryounes 2011-04-04 20:02:35 +00:00
parent b8d07dc9a8
commit a5ef0e4f0b
8 changed files with 88 additions and 72 deletions

View file

@ -31,7 +31,7 @@ public interface ObjectPropertyStatementDao {
int insertNewObjectPropertyStatement(ObjectPropertyStatement objPropertyStmt );
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(String subjectUri, String propertyUri, String query);
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(String subjectUri, String propertyUri, String objectKey, String query);
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(String subjectUri, String propertyUri, String query, Set<String> constructQueries);
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(String subjectUri, String propertyUri, String objectKey, String query, Set<String> constructQueries);
}

View file

@ -86,15 +86,15 @@ class ObjectPropertyStatementDaoFiltering extends BaseFiltering implements Objec
@Override
// RY What about filtering?
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri, String propertyUri, String query) {
return innerObjectPropertyStatementDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, query);
String subjectUri, String propertyUri, String objectKey, String query) {
return innerObjectPropertyStatementDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, objectKey, query);
}
@Override
// RY What about filtering?
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri, String propertyUri, String query, Set<String> queryStrings) {
return innerObjectPropertyStatementDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, query, queryStrings);
String subjectUri, String propertyUri, String objectKey, String query, Set<String> queryStrings) {
return innerObjectPropertyStatementDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, objectKey, query, queryStrings);
}

View file

@ -27,6 +27,7 @@ import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
@ -75,31 +76,14 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
* This is a hack to throw out properties in the vitro, rdf, rdfs, and owl namespaces.
* It will be implemented in a better way in v1.3 (Editing and Display Configuration).
*/
protected static String propertyFilters = null;
protected static final String PROPERTY_FILTERS;
static {
List<String> namespaceFilters = new ArrayList<String>();
for (String s : EXCLUDED_NAMESPACES) {
namespaceFilters.add("(afn:namespace(?property) != \"" + s + "\")");
for (String namespace : EXCLUDED_NAMESPACES) {
namespaceFilters.add("( afn:namespace(?property) != \"" + namespace + "\" )");
}
propertyFilters = "FILTER (" + StringUtils.join(namespaceFilters, " && ") + ")\n";
PROPERTY_FILTERS = StringUtils.join(namespaceFilters, " && ");
}
protected static final String DATA_PROPERTY_QUERY_STRING =
prefixes + "\n" +
"SELECT DISTINCT ?property WHERE { \n" +
" ?subject ?property ?object . \n" +
" ?property rdf:type owl:DatatypeProperty . \n" +
propertyFilters +
"}";
protected static Query dataPropertyQuery;
static {
try {
dataPropertyQuery = QueryFactory.create(DATA_PROPERTY_QUERY_STRING);
} catch(Throwable th){
log.error("could not create SPARQL query for DATA_PROPERTY_QUERY_STRING " + th.getMessage());
log.error(DATA_PROPERTY_QUERY_STRING);
}
}
private class DataPropertyRanker implements Comparator {
public int compare (Object o1, Object o2) {
@ -767,9 +751,32 @@ public class DataPropertyDaoJena extends PropertyDaoJena implements
* into the new one in a future release.
*/
public List<DataProperty> getDataPropertyList(String subjectUri) {
log.debug("Data property query string:\n" + DATA_PROPERTY_QUERY_STRING);
log.debug("Data property query:\n" + dataPropertyQuery);
Iterator<QuerySolution> results = getPropertyQueryResults(subjectUri, dataPropertyQuery);
// Due to a Jena bug, prebinding on ?subject combined with the isLiteral()
// filter causes the query to fail. Using string concatenation to insert the
// subject uri instead.
String queryString =
prefixes + "\n" +
"SELECT DISTINCT ?property WHERE { \n" +
" <" + subjectUri + "> ?property ?object . \n" +
" ?property a owl:DatatypeProperty . \n" +
" FILTER ( \n" +
" isLiteral(?object) && \n" +
PROPERTY_FILTERS + "\n" +
" ) \n" +
"}";
Query query = null;
try {
query = QueryFactory.create(queryString);
} catch(Throwable th){
log.error("could not create SPARQL query for query string " + th.getMessage());
log.error(queryString);
return null;
}
log.debug("Data property query string:\n" + query);
Iterator<QuerySolution> results = getPropertyQueryResults(subjectUri, query);
List<DataProperty> properties = new ArrayList<DataProperty>();
while (results.hasNext()) {
QuerySolution sol = results.next();

View file

@ -26,6 +26,7 @@ import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
@ -55,44 +56,23 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
private static final Log log = LogFactory.getLog(ObjectPropertyDaoJena.class.getName());
protected static final List<String> EXCLUDED_NAMESPACES = Arrays.asList(
// Don't need to exclude these, because they are not owl:ObjectProperty
//"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
//"http://www.w3.org/2000/01/rdf-schema#",
"http://www.w3.org/2002/07/owl#"
);
/*
* This is a hack to throw out properties in the vitro, rdf, rdfs, and owl namespaces.
* It will be implemented in a better way in v1.3 (Editing and Display Configuration).
*/
protected static String propertyFilters = "";
protected static final String PROPERTY_FILTERS;
static {
List<String> namespaceFilters = new ArrayList<String>();
for (String s : EXCLUDED_NAMESPACES) {
namespaceFilters.add("(afn:namespace(?property) != \"" + s + "\")");
for (String namespace : EXCLUDED_NAMESPACES) {
namespaceFilters.add("( afn:namespace(?property) != \"" + namespace + "\" )");
}
// A hack to include the vitro:primaryLink and vitro:additionalLink properties in the list
namespaceFilters.add("( ?property = vitro:primaryLink ||" +
"?property = vitro:additionalLink ||" +
"afn:namespace(?property) != \"http://vitro.mannlib.cornell.edu/ns/vitro/0.7#\" )");
propertyFilters = "FILTER (" + StringUtils.join(namespaceFilters, " && ") + ")\n";
}
protected static final String OBJECT_PROPERTY_QUERY_STRING =
prefixes + "\n" +
"SELECT DISTINCT ?property WHERE { \n" +
" ?subject ?property ?object . \n" +
" ?property rdf:type owl:ObjectProperty . \n" +
propertyFilters +
"}";
protected static Query objectPropertyQuery;
static {
try {
objectPropertyQuery = QueryFactory.create(OBJECT_PROPERTY_QUERY_STRING);
} catch(Throwable th){
log.error("could not create SPARQL query for OBJECT_PROPERTY_QUERY_STRING " + th.getMessage());
log.error(OBJECT_PROPERTY_QUERY_STRING);
}
PROPERTY_FILTERS = StringUtils.join(namespaceFilters, " && ");
}
protected static final String LIST_VIEW_CONFIG_FILE_QUERY_STRING =
@ -101,7 +81,7 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
" ?property display:listViewConfigFile ?filename . \n" +
"}";
protected static Query listViewConfigFileQuery;
protected static Query listViewConfigFileQuery = null;
static {
try {
listViewConfigFileQuery = QueryFactory.create(LIST_VIEW_CONFIG_FILE_QUERY_STRING);
@ -884,9 +864,32 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
* into the new one in a future release.
*/
public List<ObjectProperty> getObjectPropertyList(String subjectUri) {
log.debug("Object property query string:\n" + OBJECT_PROPERTY_QUERY_STRING);
log.debug("Object property query:\n" + objectPropertyQuery);
Iterator<QuerySolution> results = getPropertyQueryResults(subjectUri, objectPropertyQuery);
// Due to a Jena bug, prebinding on ?subject combined with the isURI()
// filter causes the query to fail. Using string concatenation to insert the
// subject uri instead.
String queryString =
prefixes + "\n" +
"SELECT DISTINCT ?property WHERE { \n" +
" <" + subjectUri + "> ?property ?object . \n" +
" ?property a owl:ObjectProperty . \n" +
" FILTER ( \n" +
" isURI(?object) && \n" +
PROPERTY_FILTERS + "\n" +
" ) \n" +
"}";
Query query = null;
try {
query = QueryFactory.create(queryString);
} catch(Throwable th){
log.error("could not create SPARQL query for query string " + th.getMessage());
log.error(queryString);
return null;
}
log.debug("Object property query:\n" + query);
Iterator<QuerySolution> results = getPropertyQueryResults(subjectUri, query);
List<ObjectProperty> properties = new ArrayList<ObjectProperty>();
while (results.hasNext()) {
QuerySolution soln = results.next();

View file

@ -25,6 +25,7 @@ import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
@ -254,10 +255,10 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri,
String propertyUri,
String queryString) {
String objectKey, String queryString) {
return getObjectPropertyStatementsForIndividualByProperty(
subjectUri, propertyUri, null);
subjectUri, propertyUri, objectKey, objectKey, null);
}
@ -272,8 +273,8 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
public List<Map<String, String>> getObjectPropertyStatementsForIndividualByProperty(
String subjectUri,
String propertyUri,
String queryString,
Set<String> constructQueryStrings ) {
String objectKey,
String queryString, Set<String> constructQueryStrings ) {
Model constructedModel = constructModelForSelectQueries(
subjectUri, propertyUri, constructQueryStrings);
@ -313,7 +314,10 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec
while (results.hasNext()) {
QuerySolution soln = results.nextSolution();
list.add(QueryUtils.querySolutionToStringValueMap(soln));
RDFNode node = soln.get(objectKey);
if (node.isResource()) {
list.add(QueryUtils.querySolutionToStringValueMap(soln));
}
}
} finally {

View file

@ -404,18 +404,20 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao {
protected Iterator<QuerySolution> getPropertyQueryResults(String subjectUri, Query query) {
log.debug("SPARQL query:\n" + query.toString());
// Bind the subject's uri to the ?subject query term
QuerySolutionMap subjectBinding = new QuerySolutionMap();
subjectBinding.add("subject",
ResourceFactory.createResource(subjectUri));
// RY Removing prebinding due to Jena bug: when isLiteral(?object) or
// isURI(?object) is added to the query as a filter, the query fails with prebinding
// but succeeds when the subject uri is concatenated into the query string.
//QuerySolutionMap subjectBinding = new QuerySolutionMap();
//subjectBinding.add("subject", ResourceFactory.createResource(subjectUri));
// Run the SPARQL query to get the properties
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
QueryExecution qexec = QueryExecutionFactory.create(
query, dataset, subjectBinding);
query, dataset); //, subjectBinding);
try {
ResultSet rs = qexec.execSelect();
// consume iterator before wrapper w is closed in finally block

View file

@ -61,7 +61,7 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
String subjectUri = subject.getURI();
String propertyUri = op.getURI();
List<Map<String, String>> statementData =
opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getSelectQuery(), getConstructQueries());
opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getObjectKey(), getSelectQuery(), getConstructQueries());
/* Apply post-processing */
postprocess(statementData, wdf);

View file

@ -37,7 +37,7 @@ public class UncollatedObjectPropertyTemplateModel extends ObjectPropertyTemplat
String subjectUri = subject.getURI();
String propertyUri = op.getURI();
List<Map<String, String>> statementData =
opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getSelectQuery(), getConstructQueries());
opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getObjectKey(), getSelectQuery(), getConstructQueries());
/* Apply postprocessing */
postprocess(statementData, wdf);