1. Changed the way person to grants & person to publications models were created, now we create just one model for all the grants & other for all the publication relationships & query against it.

This commit is contained in:
cdtank 2011-03-21 20:11:28 +00:00
parent daa9d61d04
commit 77a7ce801a
7 changed files with 381 additions and 21 deletions

View file

@ -13,6 +13,8 @@ import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructo
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory.OrganizationModelWithTypesFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory.OrganizationToGrantsForSubOrganizationsFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory.OrganizationToPublicationsForSubOrganizationsFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory.PeopleToGrantsFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory.PeopleToPublicationsFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory.PersonToGrantsFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory.PersonToPublicationsFactory;
@ -21,7 +23,9 @@ public class ModelConstructorUtilities {
@SuppressWarnings("serial")
private static final Map<String, ModelFactoryInterface> modelTypeIdentifierToFactory = new HashMap<String, ModelFactoryInterface>() {{
put(PersonToPublicationsModelConstructor.MODEL_TYPE, new PersonToPublicationsFactory());
put(PeopleToPublicationsModelConstructor.MODEL_TYPE, new PeopleToPublicationsFactory());
put(PersonToGrantsModelConstructor.MODEL_TYPE, new PersonToGrantsFactory());
put(PeopleToGrantsModelConstructor.MODEL_TYPE, new PeopleToGrantsFactory());
put(OrganizationToPublicationsForSubOrganizationsModelConstructor.MODEL_TYPE, new OrganizationToPublicationsForSubOrganizationsFactory());
put(OrganizationToGrantsForSubOrganizationsModelConstructor.MODEL_TYPE, new OrganizationToGrantsForSubOrganizationsFactory());
put(OrganizationAssociatedPeopleModelWithTypesConstructor.MODEL_TYPE, new OrganizationAssociatedPeopleModelWithTypesFactory());

View file

@ -15,7 +15,6 @@ import com.hp.hpl.jena.rdf.model.ModelFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.CachedModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.ModelConstructor;
public class OrganizationModelWithTypesConstructor implements ModelConstructor {

View file

@ -0,0 +1,170 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.ModelConstructor;
public class PeopleToGrantsModelConstructor implements ModelConstructor {
protected static final Syntax SYNTAX = Syntax.syntaxARQ;
private Dataset dataset;
public static final String MODEL_TYPE = "PEOPLE_TO_GRANTS";
private Log log = LogFactory.getLog(PeopleToGrantsModelConstructor.class.getName());
private long before, after;
public PeopleToGrantsModelConstructor(Dataset dataset) {
this.dataset = dataset;
}
private Set<String> constructPersonGrantsQueryTemplate(String constructProperty, String roleTypeProperty) {
Set<String> differentPerspectiveQueries = new HashSet<String>();
String justGrantsQuery = ""
+ " CONSTRUCT { "
+ " ?person vivosocnet:lastCachedAt ?now . "
+ " ?person vivosocnet:" + constructProperty + " ?Grant . "
+ " "
+ " ?Grant rdf:type core:Grant . "
+ " ?Grant rdfs:label ?grantLabel . "
+ " "
+ " } "
+ " WHERE { "
+ " ?person core:" + roleTypeProperty + " ?Role . "
+ " ?Role core:roleIn ?Grant . "
+ " ?Grant rdfs:label ?grantLabel . "
+ " "
+ " LET(?now := afn:now()) "
+ " } ";
String justDateTimeOnGrantsQuery = ""
+ " CONSTRUCT { "
+ " ?person vivosocnet:lastCachedAt ?now . "
+ " ?Grant vivosocnet:startDateTimeOnGrant ?startDateTimeValueForGrant . "
// + " ?Grant vivosocnet:endDateTimeOnGrant ?endDateTimeValueForGrant . "
+ " "
+ " } "
+ " WHERE { "
+ " ?person core:" + roleTypeProperty + " ?Role . "
+ " ?Role core:roleIn ?Grant . "
+ " "
+ " ?Grant core:dateTimeInterval ?dateTimeIntervalValueForGrant . "
// + " OPTIONAL { "
+ " ?dateTimeIntervalValueForGrant core:start ?startDateForGrant . "
+ " ?startDateForGrant core:dateTime ?startDateTimeValueForGrant . "
// + " } "
// + " OPTIONAL { "
// + " ?dateTimeIntervalValueForGrant core:end ?endDateForGrant . "
// + " ?endDateForGrant core:dateTime ?endDateTimeValueForGrant "
// + " } "
+ " "
+ " LET(?now := afn:now()) "
+ " } ";
String justDateTimeOnRolesQuery = ""
+ " CONSTRUCT { "
+ " ?person vivosocnet:lastCachedAt ?now . "
+ " ?Grant vivosocnet:startDateTimeOnRole ?startDateTimeValue . "
// + " ?Grant vivosocnet:endDateTimeOnRole ?endDateTimeValue . "
+ " } "
+ " WHERE { "
+ " ?person core:" + roleTypeProperty + " ?Role . "
+ " ?Role core:roleIn ?Grant . "
+ " "
+ " ?Role core:dateTimeInterval ?dateTimeIntervalValue . "
// + " OPTIONAL { "
+ " ?dateTimeIntervalValue core:start ?startDate . "
+ " ?startDate core:dateTime ?startDateTimeValue . "
// + " } "
// + " "
// + " OPTIONAL { "
// + " ?dateTimeIntervalValue core:end ?endDate . "
// + " ?endDate core:dateTime ?endDateTimeValue . "
// + " } "
+ " "
+ " LET(?now := afn:now()) "
+ " } ";
differentPerspectiveQueries.add(justGrantsQuery);
differentPerspectiveQueries.add(justDateTimeOnGrantsQuery);
differentPerspectiveQueries.add(justDateTimeOnRolesQuery);
return differentPerspectiveQueries;
}
private Set<String> constructPeopleToGrantsQuery() {
Set<String> differentInvestigatorTypeQueries = new HashSet<String>();
Set<String> investigatorRoleQuery = constructPersonGrantsQueryTemplate("hasGrantAsAnInvestigator", "hasInvestigatorRole");
Set<String> piRoleQuery = constructPersonGrantsQueryTemplate("hasGrantAsPI", "hasPrincipalInvestigatorRole");
Set<String> coPIRoleQuery = constructPersonGrantsQueryTemplate("hasGrantAsCoPI", "hasCo-PrincipalInvestigatorRole");
differentInvestigatorTypeQueries.addAll(investigatorRoleQuery);
differentInvestigatorTypeQueries.addAll(piRoleQuery);
differentInvestigatorTypeQueries.addAll(coPIRoleQuery);
return differentInvestigatorTypeQueries;
}
private Model executeQuery(Set<String> constructQueries) {
Model constructedModel = ModelFactory.createDefaultModel();
before = System.currentTimeMillis();
log.debug("CONSTRUCT query string : " + constructQueries);
for (String currentQuery : constructQueries) {
Query query = null;
try {
query = QueryFactory.create(QueryConstants.getSparqlPrefixQuery() + currentQuery, SYNTAX);
} catch (Throwable th) {
log.error("Could not create CONSTRUCT SPARQL query for query "
+ "string. " + th.getMessage());
log.error(currentQuery);
}
QueryExecution qe = QueryExecutionFactory.create(query, dataset);
try {
qe.execConstruct(constructedModel);
} finally {
qe.close();
}
}
after = System.currentTimeMillis();
log.debug("Time taken to execute the CONSTRUCT queries is in milliseconds: "
+ (after - before));
return constructedModel;
}
public Model getConstructedModel() throws MalformedQueryParametersException {
return executeQuery(constructPeopleToGrantsQuery());
}
}

View file

@ -0,0 +1,99 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.ModelConstructor;
public class PeopleToPublicationsModelConstructor implements ModelConstructor {
protected static final Syntax SYNTAX = Syntax.syntaxARQ;
private Dataset dataset;
public static final String MODEL_TYPE = "PEOPLE_TO_PUBLICATIONS";
private Log log = LogFactory.getLog(PeopleToPublicationsModelConstructor.class.getName());
private long before, after;
public PeopleToPublicationsModelConstructor(Dataset dataset) {
this.dataset = dataset;
}
private String constructPeopleToPublicationsQuery() {
return ""
+ " CONSTRUCT { "
+ " ?person vivosocnet:lastCachedAt ?now . "
+ " ?person vivosocnet:hasPublication ?Document . "
+ " ?Document rdf:type bibo:Document . "
+ " ?Document rdfs:label ?DocumentLabel . "
+ " ?Document core:dateTimeValue ?dateTimeValue . "
+ " ?dateTimeValue core:dateTime ?publicationDate . "
+ " } "
+ " WHERE { "
+ " ?person core:authorInAuthorship ?Resource . "
+ " ?Resource core:linkedInformationResource ?Document . "
+ " ?Document rdfs:label ?DocumentLabel . "
+ " "
+ " OPTIONAL { "
+ " ?Document core:dateTimeValue ?dateTimeValue . "
+ " ?dateTimeValue core:dateTime ?publicationDate . "
+ " } "
+ " "
+ " LET(?now := afn:now()) "
+ " } ";
}
private Model executeQuery(String constructQuery) {
System.out.println("in constructed model for ALL people publications ");
Model constructedModel = ModelFactory.createDefaultModel();
before = System.currentTimeMillis();
log.debug("CONSTRUCT query string : " + constructQuery);
Query query = null;
try {
query = QueryFactory.create(QueryConstants.getSparqlPrefixQuery()
+ constructQuery, SYNTAX);
} catch (Throwable th) {
log.error("Could not create CONSTRUCT SPARQL query for query "
+ "string. " + th.getMessage());
log.error(constructQuery);
}
QueryExecution qe = QueryExecutionFactory.create(query, dataset);
try {
qe.execConstruct(constructedModel);
} finally {
qe.close();
}
after = System.currentTimeMillis();
log.debug("Try to see Time taken to execute the CONSTRUCT queries is in milliseconds: "
+ (after - before));
return constructedModel;
}
public Model getConstructedModel() throws MalformedQueryParametersException {
return executeQuery(constructPeopleToPublicationsQuery());
}
}

View file

@ -0,0 +1,43 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.PeopleToGrantsModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.ConstructedModelTracker;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.ModelConstructor;
public class PeopleToGrantsFactory implements ModelFactoryInterface {
@Override
public Model getOrCreateModel(String uri, Dataset dataset)
throws MalformedQueryParametersException {
Model candidateModel = ConstructedModelTracker.getModel(
ConstructedModelTracker
.generateModelIdentifier(
null,
PeopleToGrantsModelConstructor.MODEL_TYPE));
if (candidateModel != null) {
return candidateModel;
} else {
ModelConstructor model = new PeopleToGrantsModelConstructor(dataset);
Model constructedModel = model.getConstructedModel();
ConstructedModelTracker.trackModel(
ConstructedModelTracker
.generateModelIdentifier(
null,
PeopleToGrantsModelConstructor.MODEL_TYPE),
constructedModel);
return constructedModel;
}
}
}

View file

@ -0,0 +1,45 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.factory;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.PeopleToPublicationsModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.ConstructedModelTracker;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.ModelConstructor;
public class PeopleToPublicationsFactory implements ModelFactoryInterface {
@Override
public Model getOrCreateModel(String uri, Dataset dataset)
throws MalformedQueryParametersException {
Model candidateModel = ConstructedModelTracker.getModel(
ConstructedModelTracker
.generateModelIdentifier(
null,
PeopleToPublicationsModelConstructor.MODEL_TYPE));
if (candidateModel != null) {
return candidateModel;
} else {
ModelConstructor model = new PeopleToPublicationsModelConstructor(dataset);
Model constructedModel = model.getConstructedModel();
ConstructedModelTracker.trackModel(
ConstructedModelTracker
.generateModelIdentifier(
null,
PeopleToPublicationsModelConstructor.MODEL_TYPE),
constructedModel);
return constructedModel;
}
}
}

View file

@ -23,8 +23,8 @@ import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructo
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationModelWithTypesConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationToGrantsForSubOrganizationsModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationToPublicationsForSubOrganizationsModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.PersonToGrantsModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.PersonToPublicationsModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.PeopleToGrantsModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.PeopleToPublicationsModelConstructor;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Activity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Entity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity;
@ -383,21 +383,21 @@ public class SelectOnModelUtilities {
for (SubEntity person : people) {
System.out.println(person.getIndividualURI() + " -- " + person.getIndividualLabel());
}
long before = System.currentTimeMillis();
Model peopleGrantsModel = ModelConstructorUtilities
.getOrConstructModel(
null,
PeopleToGrantsModelConstructor.MODEL_TYPE,
dataset);
System.out.print("\t construct took " + (System.currentTimeMillis() - before));
for (SubEntity person : people) {
System.out.println("constructing grants for " + person.getIndividualLabel() + " :: " + person.getIndividualURI());
long before = System.currentTimeMillis();
Model personGrantsModel = ModelConstructorUtilities
.getOrConstructModel(
person.getIndividualURI(),
PersonToGrantsModelConstructor.MODEL_TYPE,
dataset);
System.out.print("\t construct took " + (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
Map<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
@ -439,7 +439,7 @@ public class SelectOnModelUtilities {
"",
whereClause,
"",
personGrantsModel);
peopleGrantsModel);
person.addActivities(getGrantForEntity(
personGrantsQuery.getQueryResult(),
@ -456,14 +456,14 @@ public class SelectOnModelUtilities {
throws MalformedQueryParametersException {
Map<String, Activity> allDocumentURIToVOs = new HashMap<String, Activity>();
Model peoplePublicationsModel = ModelConstructorUtilities
.getOrConstructModel(
null,
PeopleToPublicationsModelConstructor.MODEL_TYPE,
dataset);
for (SubEntity person : people) {
Model personPublicationsModel = ModelConstructorUtilities
.getOrConstructModel(
person.getIndividualURI(),
PersonToPublicationsModelConstructor.MODEL_TYPE,
dataset);
System.out.println("getting publications for " + person.getIndividualLabel());
Map<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
@ -483,7 +483,7 @@ public class SelectOnModelUtilities {
"",
whereClause,
"",
personPublicationsModel);
peoplePublicationsModel);
person.addActivities(getPublicationForEntity(
personPublicationsQuery.getQueryResult(),