From ad0ae3c09a167fc4735a688cee1cadd41321873a Mon Sep 17 00:00:00 2001 From: hudajkhan Date: Mon, 12 Aug 2013 13:09:04 -0400 Subject: [PATCH] updates for semantic services, temporarily commenting out concept semantic type which is still in progress --- .../edit/forms/addAssociatedConcept.ftl | 6 +- .../mannlib/semservices/bo/Concept.java | 11 ++- .../service/impl/AgrovocService.java | 39 +++++++-- .../service/impl/GemetService.java | 35 +++++--- .../semservices/service/impl/UMLSService.java | 15 ++-- .../AddAssociatedConceptGenerator.java | 19 ++--- .../AddAssociatedConceptsPreprocessor.java | 80 ++++++++++++++++++- 7 files changed, 165 insertions(+), 40 deletions(-) diff --git a/productMods/templates/freemarker/edit/forms/addAssociatedConcept.ftl b/productMods/templates/freemarker/edit/forms/addAssociatedConcept.ftl index 5fca58d8..f08e0f35 100644 --- a/productMods/templates/freemarker/edit/forms/addAssociatedConcept.ftl +++ b/productMods/templates/freemarker/edit/forms/addAssociatedConcept.ftl @@ -95,9 +95,9 @@

${i18n().or}  ${i18n().create_own_concept}  - - - + + +

diff --git a/src/edu/cornell/mannlib/semservices/bo/Concept.java b/src/edu/cornell/mannlib/semservices/bo/Concept.java index b2739a27..35cc5712 100644 --- a/src/edu/cornell/mannlib/semservices/bo/Concept.java +++ b/src/edu/cornell/mannlib/semservices/bo/Concept.java @@ -17,7 +17,8 @@ public class Concept { private List narrowerURIList; private List exactMatchURIList; private List closeMatchURIList; - + private List altLabelList; + /** * default constructor */ @@ -149,4 +150,12 @@ public void setCloseMatchURIList(List closeMatchURIList) { this.closeMatchURIList = closeMatchURIList; } +public List getAltLabelList() { + return altLabelList; +} + +public void setAltLabelList(List altLabelList) { + this.altLabelList = altLabelList; +} + } diff --git a/src/edu/cornell/mannlib/semservices/service/impl/AgrovocService.java b/src/edu/cornell/mannlib/semservices/service/impl/AgrovocService.java index 93de713e..37452846 100644 --- a/src/edu/cornell/mannlib/semservices/service/impl/AgrovocService.java +++ b/src/edu/cornell/mannlib/semservices/service/impl/AgrovocService.java @@ -65,7 +65,6 @@ public class AgrovocService implements ExternalConceptService { private final String codeName = "hasCodeAgrovoc"; private final String searchMode = "Exact Match"; protected final String dbpedia_endpoint = " http://dbpedia.org/sparql"; - @Override @@ -94,16 +93,16 @@ public class AgrovocService implements ExternalConceptService { return conceptList; } - System.out.println("uri: "+uri); + //System.out.println("uri: "+uri); SKOSDataset dataset = manager.loadDataset(uri); for (SKOSConcept skosConcept : dataset.getSKOSConcepts()) { Concept concept = new Concept(); - System.out.println("Concept: " + skosConcept.getURI()); + //System.out.println("Concept: " + skosConcept.getURI()); concept.setUri(skosConcept.getURI().toString()); concept.setConceptId(stripConceptId(skosConcept.getURI().toString())); concept.setBestMatch("true"); - concept.setDefinedBy(this.schemeUri); + concept.setDefinedBy(schemeUri); concept.setSchemeURI(this.schemeUri); concept.setType(""); String lang = ""; @@ -126,6 +125,32 @@ public class AgrovocService implements ExternalConceptService { } } + // get altLabels + List altLabelList = new ArrayList(); + for (SKOSLiteral literal : skosConcept + .getSKOSRelatedConstantByProperty(dataset, manager + .getSKOSDataFactory().getSKOSAltLabelProperty())) { + + if (!literal.isTyped()) { + // if it has language + SKOSUntypedLiteral untypedLiteral = literal + .getAsSKOSUntypedLiteral(); + if (untypedLiteral.hasLang()) { + lang = untypedLiteral.getLang(); + } else { + lang = ""; + } + } + //System.out.println("literal: "+ literal.getLiteral()); + if (lang.equals("en")) { + //System.out.println("altLabel: " + literal.getLiteral()); + altLabelList.add(literal.getLiteral()); + } + } + concept.setAltLabelList(altLabelList); + + + // get the broader property URI List broaderURIList = new ArrayList(); for (SKOSAnnotation annotation: skosConcept.getSKOSAnnotationsByURI(dataset, manager.getSKOSDataFactory().getSKOSBroaderProperty().getURI())) { @@ -347,9 +372,9 @@ public class AgrovocService implements ExternalConceptService { public List getConceptsByURIWithSparql(String uri) throws Exception { - //John Ferreira's original code has implementation - List newConceptList = new ArrayList(); - return newConceptList; + // deprecating this method...just return an empty list + List conceptList = new ArrayList(); + return conceptList; } protected String getAgrovocTermCode(String rdf) throws Exception { diff --git a/src/edu/cornell/mannlib/semservices/service/impl/GemetService.java b/src/edu/cornell/mannlib/semservices/service/impl/GemetService.java index 495aee66..7ee4c52e 100644 --- a/src/edu/cornell/mannlib/semservices/service/impl/GemetService.java +++ b/src/edu/cornell/mannlib/semservices/service/impl/GemetService.java @@ -34,8 +34,7 @@ public class GemetService implements ExternalConceptService { private final String endpoint = "http://cr.eionet.europa.eu/sparql"; private final String schemeURI = "http://www.eionet.europa.eu/gemet/gemetThesaurus"; - - + @Override public List getConcepts(String term) throws Exception { List conceptList = new ArrayList(); @@ -65,11 +64,12 @@ public class GemetService implements ExternalConceptService { } - public List getConceptsByURIWithSparql(String uri) throws Exception { - //John Ferreira's original code has implementation - List newConceptList = new ArrayList(); - return newConceptList; - } + public List getConceptsByURIWithSparql(String uri) + throws Exception { + // deprecating this method...just return an empty list + List conceptList = new ArrayList(); + return conceptList; + } /** * @param results @@ -87,7 +87,7 @@ public class GemetService implements ExternalConceptService { for (int i = 0; i < jsonArray.size(); i++) { Concept concept = new Concept(); - concept.setDefinedBy(this.schemeURI); + concept.setDefinedBy(schemeURI); concept.setBestMatch("true"); JSONObject json = jsonArray.getJSONObject(i); String uri = getJsonValue(json, "uri"); @@ -124,7 +124,7 @@ public class GemetService implements ExternalConceptService { for (String s: relatedURIList) { System.out.println("related uri: "+s); }*/ - + //String altLabels = getAllTranslationsForConcept(uri, "nonPreferredLabels"); conceptList.add(concept); @@ -218,9 +218,10 @@ public class GemetService implements ExternalConceptService { "?concept_uri=" + concept_uri + "&property_uri=" + property_uri + "&language=en"; - try { result = getGemetResults(serviceUrl); + List props = getPropertyFromJson(result); + } catch (Exception e) { e.printStackTrace(); throw e; @@ -310,6 +311,7 @@ public class GemetService implements ExternalConceptService { return results; } + protected List getRelatedUris(String json) { List uriList = new ArrayList(); String uri = new String(); @@ -327,6 +329,19 @@ public class GemetService implements ExternalConceptService { } + protected List getPropertyFromJson(String json) { + List props = new ArrayList(); + JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); + if (jsonArray.size() == 0) { + return new ArrayList(); + } + for (int i = 0; i < jsonArray.size(); i++) { + JSONObject jsonObj = jsonArray.getJSONObject(i); + System.out.println(jsonObj.toString()); + } + return props; + } + protected String stripConceptId(String uri) { String conceptId = new String(); int lastslash = uri.lastIndexOf('/'); diff --git a/src/edu/cornell/mannlib/semservices/service/impl/UMLSService.java b/src/edu/cornell/mannlib/semservices/service/impl/UMLSService.java index 21c83edd..c95c0e8e 100644 --- a/src/edu/cornell/mannlib/semservices/service/impl/UMLSService.java +++ b/src/edu/cornell/mannlib/semservices/service/impl/UMLSService.java @@ -29,14 +29,14 @@ public class UMLSService implements ExternalConceptService { private static final String baseUri = "http://link.informatics.stonybrook.edu/umls/CUI/"; private static final String endpoint = "http://link.informatics.stonybrook.edu/sparql/"; private static final String schemeURI = "http://link.informatics.stonybrook.edu/umls"; - + + @Override public List getConcepts(String term) throws Exception { List conceptList = new ArrayList(); String results = null; - //Format in rdf/xml is possible but would require separate processing method String dataUrl = submissionUrl + "textToProcess=" + URLEncoder.encode(term, "UTF-8") + "&format=json"; @@ -98,11 +98,12 @@ public class UMLSService implements ExternalConceptService { * @param uri * @return */ - public List getConceptsByURIWithSparql(String uri) throws Exception { - //John Ferreira's original code has implementation - List newConceptList = new ArrayList(); - return newConceptList; - } + public List getConceptsByURIWithSparql(String uri) + throws Exception { + // deprecating this method...just return an empty list + List conceptList = new ArrayList(); + return conceptList; + } /** * @param results diff --git a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddAssociatedConceptGenerator.java b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddAssociatedConceptGenerator.java index 02f7efc0..84b29774 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddAssociatedConceptGenerator.java +++ b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddAssociatedConceptGenerator.java @@ -190,7 +190,7 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements //There are no new resources here, the concept node uri doesn't //get created but already exists, and vocab uri should already exist as well //Adding concept semantic type uri just to test - note this isn't really on the form at all - newResources.put("conceptSemanticTypeURI", null); + //newResources.put("conceptSemanticTypeURI", null); return newResources; } @@ -239,11 +239,11 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements //The URI of the node that defines the concept urisOnForm.add("conceptNode"); urisOnForm.add("conceptSource"); - urisOnForm.add("conceptSemanticTypeURI"); + // urisOnForm.add("conceptSemanticTypeURI"); editConfiguration.setUrisOnform(urisOnForm); //Also need to add the label of the concept literalsOnForm.add("conceptLabel"); - literalsOnForm.add("conceptSemanticTypeLabel"); + // literalsOnForm.add("conceptSemanticTypeLabel"); editConfiguration.setLiteralsOnForm(literalsOnForm); } @@ -272,8 +272,8 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements setConceptNodeField(editConfiguration, vreq); setConceptLabelField(editConfiguration, vreq); setVocabURIField(editConfiguration, vreq); - setConceptSemanticTypeURIField(editConfiguration,vreq); - setConceptSemanticTypeLabelField(editConfiguration,vreq); + //setConceptSemanticTypeURIField(editConfiguration,vreq); + //setConceptSemanticTypeLabelField(editConfiguration,vreq); } //this field will be hidden and include the concept node URI @@ -325,7 +325,7 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements //An Edit submission preprocessor for enabling addition of multiple terms for a single search editConfiguration.addEditSubmissionPreprocessor( - new AddAssociatedConceptsPreprocessor(editConfiguration)); + new AddAssociatedConceptsPreprocessor(editConfiguration, wadf)); } @@ -411,12 +411,13 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements String conceptSemanticTypeURI = null; String conceptSemanticTypeLabel = null; //Can a concept have multiple semantic types? Currently we are only returning the first one + /* List semanticTypeStatements = conceptIndividual.getObjectPropertyStatements(VIVOCore + "conceptSemanticType"); if(semanticTypeStatements.size() > 0) { conceptSemanticTypeURI = semanticTypeStatements.get(0).getObjectURI(); Individual conceptSemanticTypeIndividual = EditConfigurationUtils.getIndividual(vreq, conceptSemanticTypeURI); conceptSemanticTypeLabel = conceptSemanticTypeIndividual.getName(); - } + }*/ //get label //Assuming this is from an external vocabulary source @@ -441,8 +442,8 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements this.vocabURI = inputVocabURI; this.vocabLabel = inputVocabLabel; this.type = inputType; - this.conceptSemanticTypeURI = inputConceptSemanticTypeURI; - this.conceptSemanticTypeLabel = inputConceptSemanticTypeLabel; + //this.conceptSemanticTypeURI = inputConceptSemanticTypeURI; + //this.conceptSemanticTypeLabel = inputConceptSemanticTypeLabel; } //Getters diff --git a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/preprocessors/AddAssociatedConceptsPreprocessor.java b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/preprocessors/AddAssociatedConceptsPreprocessor.java index ec24711a..b49b9b45 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/preprocessors/AddAssociatedConceptsPreprocessor.java +++ b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/preprocessors/AddAssociatedConceptsPreprocessor.java @@ -13,6 +13,9 @@ import org.apache.commons.logging.LogFactory; import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.vocabulary.RDFS; import com.hp.hpl.jena.vocabulary.XSD; + +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.BaseEditSubmissionPreprocessorVTwo; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationUtils; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo; @@ -24,6 +27,7 @@ public class AddAssociatedConceptsPreprocessor extends protected static final Log log = LogFactory .getLog(AddAssociatedConceptsPreprocessor.class.getName()); + protected WebappDaoFactory wadf = null; // Field names/variables names for n3 - these will have numbers added as // suffix if more than one term @@ -34,14 +38,17 @@ public class AddAssociatedConceptsPreprocessor extends private static String conceptNodeValues = null; private static String conceptLabelValues = null; private static String conceptSourceValues = null; + private static String conceptSemanticTypeLabelValues = null; + private static String conceptSemanticTypeURIValues = null; private static MultiValueEditSubmission submission = null; // String datatype // Will be editing the edit configuration as well as edit submission here - public AddAssociatedConceptsPreprocessor(EditConfigurationVTwo editConfig) { + public AddAssociatedConceptsPreprocessor(EditConfigurationVTwo editConfig, WebappDaoFactory wadf) { super(editConfig); + this.wadf = wadf; } public void preprocess(MultiValueEditSubmission inputSubmission) { @@ -50,7 +57,9 @@ public class AddAssociatedConceptsPreprocessor extends // as vocab uri (which is based on thge // For query parameters, check whether CUI copySubmissionValues(); - + //This will put the URI value in scope for the first semantic type label + //and generate the rest if need be + //processConceptSemanticValues(); if (conceptNodeValues != null) { String[] conceptNodes = convertDelimitedStringToArray(conceptNodeValues); int numberConcepts = conceptNodes.length; @@ -71,8 +80,26 @@ public class AddAssociatedConceptsPreprocessor extends conceptLabelValues = getConceptLabelValues(); conceptNodeValues = getConceptNodeValues(); conceptSourceValues = getConceptSourceValues(); + + } + + // + private void processConceptSemanticValues() { + conceptSemanticTypeLabelValues = getConceptSemanticTypeLabelValues(); + conceptSemanticTypeURIValues = getConceptSemanticTypeURIValues(); + + //We are first going to handle the single value case and then handle additional values + //where the rest of the additional values are handled + } + + + + + + + //This is for additional concept nodes (i.e. if user selects more than one concept) private void processConceptNodes(int numberConcepts) { //There are no "new" resources b/c the concept nodes are URIs from external vocabularies // Add N3Required @@ -98,6 +125,9 @@ public class AddAssociatedConceptsPreprocessor extends addConceptNodeInputs(numberConcepts); addConceptSourceInputs(numberConcepts); addConceptLabelInputs(numberConcepts); + //for concept semantic type labels and uris where they exist + //addConceptSemanticTypeLabelInputs(numberConcepts); + //addConceptSemanticTypeURIInputs(numberConcepts); } @@ -167,8 +197,17 @@ public class AddAssociatedConceptsPreprocessor extends log.error("Concept labels returned were null"); } } + + private void addConceptSemanticTypeLabelInputs(int numberConcepts) { + + } + + private void addConceptSemanticTypeURIInputs(int numberConcepts) { + + } - + //Fields + private void addFields(int numberConcepts) { //Clear out all fields in edit configuration first editConfiguration.setFields(new HashMap()); @@ -290,6 +329,7 @@ public class AddAssociatedConceptsPreprocessor extends } + //Get values from submission private String getConceptNodeValues() { Map> urisFromForm = submission.getUrisFromForm(); List conceptNodes = urisFromForm.get("conceptNode"); @@ -308,6 +348,40 @@ public class AddAssociatedConceptsPreprocessor extends } + private String getConceptSemanticTypeLabelValues() { + Map> literalsFromForm = submission.getLiteralsFromForm(); + Map> transformed = EditConfigurationUtils.transformLiteralMap(literalsFromForm); + return (String) getFirstElement(transformed.get("conceptSemanticTypeLabel")); + } + + //This will either generate or retrieve URIs for the concept semantic type labels if they exist + //We will then update the submission to include this + private String getConceptSemanticTypeURIValues() { + List conceptSemanticTypeURIs = new ArrayList(); + String[] conceptSemanticTypeLabels = convertDelimitedStringToArray(conceptSemanticTypeLabelValues); + int numberLabels = conceptSemanticTypeLabels.length; + + for(int i = 0; i < numberLabels; i++) { + String label = conceptSemanticTypeLabels[i]; + //Make or retrieve URI for this label + String uri = getURIForSemanticTypeLabel(label); + conceptSemanticTypeURIs.add(uri); + } + + //Set uris as string + //Essentially this will allow the URI to be put in scope + return conceptSemanticTypeURIs.toString(); + } + + private String getURIForSemanticTypeLabel(String label) { + //Check if system has an individual with this URI + // + List individualsWithLabel = this.wadf.getIndividualDao().getIndividualsByDataProperty(RDFS.label.getURI(), label); + //We are only interested in concepts + return null; + + } + private Object getFirstElement(List inputList) { if(inputList == null || inputList.size() == 0) return null;