Search in sources :

Example 6 with RDFNode

use of com.hp.hpl.jena.rdf.model.RDFNode 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)

Example 7 with RDFNode

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

the class MetadataRDFMapping method compileResult.

protected void compileResult(Model m, Resource result, String dsoIRI, String name, String value, String lang) throws MetadataMappingException {
    // for better debug messages.
    String uri = "";
    if (result.isURIResource()) {
        uri = " (" + result.getURI() + ")";
    }
    // check the subject
    RDFNode subjectNode;
    try {
        subjectNode = getSingularProperty(result, DMRM.subject);
    } catch (IllegalArgumentException ex) {
        throw new MetadataMappingException("There are multiple 'subject' " + "properties in a mapping result" + uri + ".");
    }
    if (subjectNode == null) {
        throw new MetadataMappingException("Mapping result" + uri + " does not have a subject.");
    }
    if (!subjectNode.isResource()) {
        throw new MetadataMappingException("Subject of a result" + uri + " is a Literal not a URIResource.");
    }
    log.debug("Found subject: " + subjectNode.toString());
    // check the predicate
    RDFNode predicateNode;
    try {
        predicateNode = getSingularProperty(result, DMRM.predicate);
    } catch (IllegalArgumentException ex) {
        throw new MetadataMappingException("There are multiple 'predicate' " + "properties in a mapping result" + uri + ".");
    }
    if (predicateNode == null) {
        throw new MetadataMappingException("Mapping result" + uri + " does not have a predicate.");
    }
    if (!predicateNode.isResource()) {
        throw new MetadataMappingException("Predicate of a result" + uri + " is a Literal not a URIResource.");
    }
    log.debug("Found predicate: " + predicateNode.toString());
    RDFNode objectNode;
    try {
        objectNode = getSingularProperty(result, DMRM.object);
    } catch (IllegalArgumentException ex) {
        throw new MetadataMappingException("There are multiple 'object' " + "properties in a mapping result" + uri + ".");
    }
    if (objectNode == null) {
        throw new MetadataMappingException("Mapping result" + uri + " does not have a object.");
    }
    log.debug("Found object: " + objectNode.toString());
    Resource subject = parseSubject(m, subjectNode.asResource(), dsoIRI, name, value);
    if (subject == null) {
        throw new MetadataMappingException("Cannot parse subject of a " + "reified statement " + uri + ".");
    }
    Property predicate = parsePredicate(m, predicateNode.asResource(), dsoIRI, name, value);
    if (predicate == null) {
        throw new MetadataMappingException("Cannot parse predicate of a " + "reified statement " + uri + ".");
    }
    RDFNode object = parseObject(m, objectNode, dsoIRI, name, value, lang);
    if (object == null) {
        throw new MetadataMappingException("Cannot parse object of a " + "reified statement " + uri + ".");
    }
    m.add(subject, predicate, object);
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) Property(com.hp.hpl.jena.rdf.model.Property) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 8 with RDFNode

use of com.hp.hpl.jena.rdf.model.RDFNode in project Synapse-Repository-Services by Sage-Bionetworks.

the class ConceptJenaDAOImpl method getAllConcepts.

@Override
public List<ConceptSummary> getAllConcepts(String parentConceptURI) throws DatastoreException {
    if (parentConceptURI == null)
        throw new IllegalArgumentException("parentConceptURI cannot be null");
    // Create a query
    String queryString = String.format(SPARQL_GET_CHILDREN_CONCEPTS, parentConceptURI);
    // System.out.println(queryString);
    Query query = QueryFactory.create(queryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, this.rdfModel);
    try {
        ResultSet resultSet = qexec.execSelect();
        List<ConceptSummary> results = new LinkedList<ConceptSummary>();
        while (resultSet.hasNext()) {
            QuerySolution soln = resultSet.nextSolution();
            if (soln == null)
                throw new DatastoreException("Null QuerySolution");
            RDFNode node = soln.get(URI);
            if (node == null)
                throw new DatastoreException("Failed to get URI from QuerySolution :" + soln.toString());
            // System.out.println(node);
            ConceptSummary summary = new ConceptSummary();
            summary.setUri(node.toString());
            Literal prefLit = soln.getLiteral(PREFERED_LABEL);
            if (prefLit == null)
                throw new DatastoreException("No preferred label for " + node.toString());
            summary.setPreferredLabel(prefLit.toString());
            results.add(summary);
        }
        return results;
    } finally {
        qexec.close();
    }
}
Also used : Query(com.hp.hpl.jena.query.Query) QuerySolution(com.hp.hpl.jena.query.QuerySolution) Literal(com.hp.hpl.jena.rdf.model.Literal) ResultSet(com.hp.hpl.jena.query.ResultSet) DatastoreException(org.sagebionetworks.repo.model.DatastoreException) QueryExecution(com.hp.hpl.jena.query.QueryExecution) LinkedList(java.util.LinkedList) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 9 with RDFNode

use of com.hp.hpl.jena.rdf.model.RDFNode in project wikidata-query-rdf by wikimedia.

the class BigdataStatementToJenaStatementMapper method map1.

@Override
public Statement map1(final BigdataStatement blzgStmt) {
    final Resource s = convertToJenaResource(blzgStmt.getSubject());
    final Property p = convertToJenaProperty(blzgStmt.getPredicate());
    final RDFNode o = convertToJenaRDFNode(blzgStmt.getObject());
    return ResourceFactory.createStatement(s, p, o);
}
Also used : BigdataResource(com.bigdata.rdf.model.BigdataResource) Resource(com.hp.hpl.jena.rdf.model.Resource) Property(com.hp.hpl.jena.rdf.model.Property) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 10 with RDFNode

use of com.hp.hpl.jena.rdf.model.RDFNode in project eol-globi-data by jhpoelen.

the class StudyImporterForSaproxylic method toInteractions.

public void toInteractions(ResultSet results) throws StudyImporterException {
    final InteractionListener listener = new InteractionListenerImpl(nodeFactory, getGeoNamesService(), getLogger());
    while (results.hasNext()) {
        QuerySolution next = results.next();
        Iterator<String> nameIter = next.varNames();
        Map<String, String> props = new TreeMap<>();
        while (nameIter.hasNext()) {
            String key = nameIter.next();
            RDFNode rdfNode = next.get(key);
            if (rdfNode.isURIResource()) {
                props.put(key, next.getResource(key).getURI());
            } else {
                props.put(key, next.getLiteral(key).getString());
            }
        }
        props.put(StudyImporterForTSV.STUDY_SOURCE_CITATION, getDataset().getCitation());
        listener.newLink(props);
    }
}
Also used : QuerySolution(com.hp.hpl.jena.query.QuerySolution) TreeMap(java.util.TreeMap) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Aggregations

RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)15 Resource (com.hp.hpl.jena.rdf.model.Resource)7 StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 Query (com.hp.hpl.jena.query.Query)2 QueryExecution (com.hp.hpl.jena.query.QueryExecution)2 QuerySolution (com.hp.hpl.jena.query.QuerySolution)2 ResultSet (com.hp.hpl.jena.query.ResultSet)2 Property (com.hp.hpl.jena.rdf.model.Property)2 RDFList (com.hp.hpl.jena.rdf.model.RDFList)2 Pattern (java.util.regex.Pattern)2 BigdataResource (com.bigdata.rdf.model.BigdataResource)1 Graph (com.hp.hpl.jena.graph.Graph)1 OntModel (com.hp.hpl.jena.ontology.OntModel)1 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)1 QuerySolutionMap (com.hp.hpl.jena.query.QuerySolutionMap)1 Literal (com.hp.hpl.jena.rdf.model.Literal)1 Statement (com.hp.hpl.jena.rdf.model.Statement)1 ResourceImpl (com.hp.hpl.jena.rdf.model.impl.ResourceImpl)1 URI (java.net.URI)1