From 77a7ce801af27d1750694a8670e7c950cd106659 Mon Sep 17 00:00:00 2001 From: cdtank Date: Mon, 21 Mar 2011 20:11:28 +0000 Subject: [PATCH] 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. --- .../ModelConstructorUtilities.java | 4 + ...OrganizationModelWithTypesConstructor.java | 1 - .../PeopleToGrantsModelConstructor.java | 170 ++++++++++++++++++ .../PeopleToPublicationsModelConstructor.java | 99 ++++++++++ .../factory/PeopleToGrantsFactory.java | 43 +++++ .../factory/PeopleToPublicationsFactory.java | 45 +++++ .../visutils/SelectOnModelUtilities.java | 40 ++--- 7 files changed, 381 insertions(+), 21 deletions(-) create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/PeopleToGrantsModelConstructor.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/PeopleToPublicationsModelConstructor.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/factory/PeopleToGrantsFactory.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/factory/PeopleToPublicationsFactory.java diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/ModelConstructorUtilities.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/ModelConstructorUtilities.java index ec6466f3..c169ad3a 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/ModelConstructorUtilities.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/ModelConstructorUtilities.java @@ -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 modelTypeIdentifierToFactory = new HashMap() {{ 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()); diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/OrganizationModelWithTypesConstructor.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/OrganizationModelWithTypesConstructor.java index 9ec19e54..d310bb7e 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/OrganizationModelWithTypesConstructor.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/OrganizationModelWithTypesConstructor.java @@ -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 { diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/PeopleToGrantsModelConstructor.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/PeopleToGrantsModelConstructor.java new file mode 100644 index 00000000..83c6d2be --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/PeopleToGrantsModelConstructor.java @@ -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 constructPersonGrantsQueryTemplate(String constructProperty, String roleTypeProperty) { + + Set differentPerspectiveQueries = new HashSet(); + + 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 constructPeopleToGrantsQuery() { + + Set differentInvestigatorTypeQueries = new HashSet(); + + Set investigatorRoleQuery = constructPersonGrantsQueryTemplate("hasGrantAsAnInvestigator", "hasInvestigatorRole"); + Set piRoleQuery = constructPersonGrantsQueryTemplate("hasGrantAsPI", "hasPrincipalInvestigatorRole"); + Set coPIRoleQuery = constructPersonGrantsQueryTemplate("hasGrantAsCoPI", "hasCo-PrincipalInvestigatorRole"); + + differentInvestigatorTypeQueries.addAll(investigatorRoleQuery); + differentInvestigatorTypeQueries.addAll(piRoleQuery); + differentInvestigatorTypeQueries.addAll(coPIRoleQuery); + + return differentInvestigatorTypeQueries; + } + + private Model executeQuery(Set 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()); + } +} diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/PeopleToPublicationsModelConstructor.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/PeopleToPublicationsModelConstructor.java new file mode 100644 index 00000000..b24beed6 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/PeopleToPublicationsModelConstructor.java @@ -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()); + } +} diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/factory/PeopleToGrantsFactory.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/factory/PeopleToGrantsFactory.java new file mode 100644 index 00000000..cb4e5e62 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/factory/PeopleToGrantsFactory.java @@ -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; + } + } +} \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/factory/PeopleToPublicationsFactory.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/factory/PeopleToPublicationsFactory.java new file mode 100644 index 00000000..5a63cecb --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/modelconstructor/factory/PeopleToPublicationsFactory.java @@ -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; + } + + } + +} diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/SelectOnModelUtilities.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/SelectOnModelUtilities.java index 2053a271..4d2e74f4 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/SelectOnModelUtilities.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/SelectOnModelUtilities.java @@ -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 fieldLabelToOutputFieldLabel = new HashMap(); @@ -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 allDocumentURIToVOs = new HashMap(); + 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 fieldLabelToOutputFieldLabel = new HashMap(); @@ -483,7 +483,7 @@ public class SelectOnModelUtilities { "", whereClause, "", - personPublicationsModel); + peoplePublicationsModel); person.addActivities(getPublicationForEntity( personPublicationsQuery.getQueryResult(),