Search in sources :

Example 21 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project cdmlib by cybertaxonomy.

the class OwlExportUtil method createSourceResources.

static List<Resource> createSourceResources(TermBase termBase, StructureTreeOwlExportState state) {
    List<Resource> sources = new ArrayList<>();
    for (IdentifiableSource source : termBase.getSources()) {
        Resource sourceResource = getSourceResource(source, state).addProperty(OwlUtil.propSourceType, source.getType().getKey());
        if (source.getIdInSource() != null) {
            sourceResource.addProperty(OwlUtil.propSourceIdInSource, source.getIdInSource());
        }
        if (source.getCitation() != null) {
            sourceResource.addProperty(OwlUtil.propSourceHasCitation, createReferenceResource(source.getCitation(), state));
        }
        sources.add(sourceResource);
    }
    return sources;
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) IdentifiableSource(eu.etaxonomy.cdm.model.common.IdentifiableSource)

Example 22 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project cdmlib by cybertaxonomy.

the class TcsRdfTaxonNameImport method handleNameModel.

protected TaxonName handleNameModel(Model model, TcsRdfImportConfigurator config, MapWrapper<TaxonName> taxonNameMap, String uri) {
    Resource nameAbout = model.getResource(uri);
    TaxonName result = handleNameResource(nameAbout, config);
    taxonNameMap.put(uri, result);
    return result;
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName)

Example 23 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project cdmlib by cybertaxonomy.

the class TcsRdfTaxonNameImport method doInvoke.

@Override
protected void doInvoke(TcsRdfImportState state) {
    MapWrapper<TaxonName> taxonNameMap = (MapWrapper<TaxonName>) state.getStore(ICdmIO.TAXONNAME_STORE);
    MapWrapper<Reference> referenceMap = (MapWrapper<Reference>) state.getStore(ICdmIO.REFERENCE_STORE);
    MapWrapper<TeamOrPersonBase> authorMap = (MapWrapper<TeamOrPersonBase>) state.getStore(ICdmIO.TEAM_STORE);
    String tcsElementName;
    Namespace tcsNamespace;
    String value;
    logger.info("start makeTaxonNames ...");
    TcsRdfImportConfigurator config = state.getConfig();
    Model root = config.getSourceRoot();
    String rdfNamespace = config.getRdfNamespaceURIString();
    String taxonNameNamespace = config.getTnNamespaceURIString();
    String idNamespace = "TaxonName";
    Resource elTaxonName = root.getResource(taxonNameNamespace);
    int i = 0;
    TaxonName name;
    Property property = root.getProperty(taxonNameNamespace + "authorship");
    ResIterator iterator = root.listSubjectsWithProperty(property, (RDFNode) null);
    String id;
    while (iterator.hasNext()) {
        Resource resource = iterator.next();
        name = handleNameResource(resource, config);
        id = resource.getNameSpace();
        taxonNameMap.put(id, name);
    }
    logger.info(i + " names handled");
    getNameService().save(taxonNameMap.objects());
    // makeNameSpecificData(nameMap);
    logger.info("end makeTaxonNames ...");
    return;
}
Also used : TeamOrPersonBase(eu.etaxonomy.cdm.model.agent.TeamOrPersonBase) ResIterator(com.hp.hpl.jena.rdf.model.ResIterator) Reference(eu.etaxonomy.cdm.model.reference.Reference) Resource(com.hp.hpl.jena.rdf.model.Resource) MapWrapper(eu.etaxonomy.cdm.io.common.MapWrapper) Namespace(org.jdom.Namespace) Model(com.hp.hpl.jena.rdf.model.Model) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) Property(com.hp.hpl.jena.rdf.model.Property)

Example 24 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project DSpace by DSpace.

the class MetadataRDFMapping method parseValueProcessor.

protected String parseValueProcessor(Resource valueProcessor, String value) {
    // look if there's a modifier.
    RDFNode modifierNode;
    try {
        modifierNode = getSingularProperty(valueProcessor, DMRM.modifier);
    } catch (IllegalArgumentException ex) {
        log.error("The ResourceGenerator of a mapping result has " + "multiple 'modifier' properties, skipping this result.");
        return null;
    }
    if (modifierNode != null) {
        if (!modifierNode.isResource()) {
            log.error("The modifier of a result is a Literal not an Resource! " + "Ingoring this result.");
            return null;
        }
        Resource modifier = modifierNode.asResource();
        RDFNode matcherNode;
        try {
            matcherNode = getSingularProperty(modifier, DMRM.matcher);
        } catch (IllegalArgumentException ex) {
            log.error("The modifier of a mapping result has multiple " + "'matcher' properties. Ignoring this result.");
            return null;
        }
        if (matcherNode == null) {
            log.error("Found a modifier property to a result, but no " + "matcher property! Ignoring this result!");
            return null;
        }
        if (!matcherNode.isLiteral()) {
            log.error("A matcher of a result modifier is not a Literal! " + "Ignoring this result.");
            return null;
        }
        // get the replacement string
        RDFNode replacementNode;
        try {
            replacementNode = getSingularProperty(modifier, DMRM.replacement);
        } catch (IllegalArgumentException ex) {
            log.error("The modifier of a mapping result has multiple " + "'replacement' properties. Ignoring this result.");
            return null;
        }
        if (replacementNode == null) {
            log.error("Found a modifier property to a result, but no " + "replacement property! Ignoring this result!");
            return null;
        }
        if (!replacementNode.isLiteral()) {
            log.error("A replacement of a result modifier is not a Literal! " + "Ignoring this result.");
            return null;
        }
        String matcher = matcherNode.asLiteral().getLexicalForm();
        String replacement = replacementNode.asLiteral().getLexicalForm();
        try {
            Pattern pattern = Pattern.compile(matcher);
            String modifiedValue = pattern.matcher(value).replaceAll(replacement);
            log.debug("Found matcher '" + matcher + "'.\n" + "Found replacement '" + replacement + "'.\n" + "modified '" + value + "' => '" + modifiedValue + "'.");
            value = modifiedValue;
        } catch (PatternSyntaxException ex) {
            log.error("Property 'matcher' of a ValueModifider didn't specify a " + "valid java regex pattern. Will ignore this result.", ex);
            return null;
        }
    }
    // in case there is a modifier, we modified the value. Insert the
    // (possibly modified) value in the pattern
    RDFNode patternNode;
    try {
        patternNode = getSingularProperty(valueProcessor, DMRM.pattern);
    } catch (IllegalArgumentException ex) {
        log.error("The ValueProcessor of a mapping result has " + "multiple 'pattern' properties, skipping this result.");
        return null;
    }
    if (patternNode == null) {
        log.debug("Cannot find the property 'pattern' of a " + "ValueProcessor, will use \"$DSpaceValue\".");
        patternNode = valueProcessor.getModel().createLiteral("$DSpaceValue");
    }
    if (!patternNode.isLiteral()) {
        log.error("A 'pattern' property of a ValueProcessor is not a " + "Literal! Skipping this result.");
        return null;
    }
    String pattern = patternNode.asLiteral().getLexicalForm();
    String result = pattern.replace("$DSpaceValue", value);
    log.debug("Found pattern " + pattern + ".\n" + "Created result: " + result);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Resource(com.hp.hpl.jena.rdf.model.Resource) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 25 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project DSpace by DSpace.

the class MetadataRDFMapping method getMetadataRDFMapping.

public static MetadataRDFMapping getMetadataRDFMapping(Resource mappingResource, String dsoIdentifier) {
    // For better log message: try to get the uri of this mapping.
    String uri = null;
    if (mappingResource.getURI() != null) {
        uri = " (" + mappingResource.getURI() + ")";
    }
    if (log.isDebugEnabled()) {
        if (uri.equals("")) {
            log.debug("Processing blank node MetadataRDFMapping.");
        } else {
            log.debug("Processing MetadataRDFMapping" + uri + ".");
        }
    }
    // Parse the property DMRM.metadataName
    RDFNode nameNode;
    try {
        nameNode = getSingularProperty(mappingResource, DMRM.metadataName);
    } catch (IllegalArgumentException ex) {
        log.error("The Property 'metadataName' exists multiple times in one " + "DSpaceMetadataRDFMapping, ignoring it" + uri + ".");
        return null;
    }
    if (nameNode == null) {
        log.error("Cannot find property 'metadataName', ignoring mapping" + uri + ".");
        return null;
    }
    if (!nameNode.isLiteral()) {
        log.error("Property 'metadataName' is not a literal, ignoring mapping" + uri + ".");
        return null;
    }
    String name = nameNode.asLiteral().getLexicalForm();
    log.debug("Found mapping name '" + name + "'.");
    // Parse the property condition, if it exists.
    RDFNode conditionNode;
    try {
        conditionNode = getSingularProperty(mappingResource, DMRM.condition);
    } catch (IllegalArgumentException ex) {
        log.error("There are multiple properties 'condition' in one " + "DSpaceMetadataRDFMapping, ignoring it" + uri + ".");
        return null;
    }
    String regex = null;
    Pattern condition = null;
    if (conditionNode != null) {
        if (conditionNode.isLiteral()) {
            regex = conditionNode.asLiteral().getLexicalForm();
            log.debug("Found property condition '" + regex + "'.");
        } else {
            log.error("Property 'condition' is not a literal, ignoring " + "mapping" + uri + ".");
            return null;
        }
    } else {
        // there is no property "condition". As this property is optional
        // there is nothing to be done here.
        log.debug("Didn't find a property \"condition\".");
    }
    if (regex != null) {
        try {
            condition = Pattern.compile(regex);
        } catch (PatternSyntaxException ex) {
            log.error("Property 'condition' does not specify a valid java " + "regex pattern. Will ignore mapping" + uri + ".", ex);
            return null;
        }
    }
    // parse all properties DMRM.creates.
    List<Resource> results = new ArrayList<>();
    StmtIterator mappingIter = mappingResource.listProperties(DMRM.creates);
    if (!mappingIter.hasNext()) {
        log.warn("No 'creates' property in a DSpaceMetadataRDFMapping, " + "ignonring it" + uri + ".");
        return null;
    }
    while (mappingIter.hasNext()) {
        RDFNode result = mappingIter.nextStatement().getObject();
        if (!result.isResource()) {
            log.error("Mapping result" + uri + " is a Literal not a resource. " + "Ignoring mapping.");
            return null;
        }
        results.add(result.asResource());
    }
    // create mapping
    return new MetadataRDFMapping(name, condition, results);
}
Also used : Pattern(java.util.regex.Pattern) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Resource(com.hp.hpl.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

Resource (com.hp.hpl.jena.rdf.model.Resource)62 Statement (com.hp.hpl.jena.rdf.model.Statement)15 StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)11 Model (com.hp.hpl.jena.rdf.model.Model)10 Property (com.hp.hpl.jena.rdf.model.Property)10 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)8 ResIterator (com.hp.hpl.jena.rdf.model.ResIterator)8 UUID (java.util.UUID)6 ArrayList (java.util.ArrayList)5 Feature (eu.etaxonomy.cdm.model.description.Feature)4 FeatureState (eu.etaxonomy.cdm.model.description.FeatureState)4 TermVocabulary (eu.etaxonomy.cdm.model.term.TermVocabulary)4 URI (eu.etaxonomy.cdm.common.URI)3 DefinedTermBase (eu.etaxonomy.cdm.model.term.DefinedTermBase)3 TermNode (eu.etaxonomy.cdm.model.term.TermNode)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 OntModel (com.hp.hpl.jena.ontology.OntModel)2 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)2 Query (com.hp.hpl.jena.query.Query)2