Search in sources :

Example 1 with RDFNode

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

the class SparqlTemplate method query.

public <T> T query(String sparql, ResultSetMapper<T> rsm, Object... args) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Map<String, Object> bindingMap = new HashMap<String, Object>();
    int i = 0;
    for (Object o : args) {
        String argName = "?_arg" + i++;
        sparql = sparql.replaceFirst("\\?\\?", argName);
        bindingMap.put(argName, o);
    }
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QuerySolutionMap initialBinding = new QuerySolutionMap();
    for (String argName : bindingMap.keySet()) {
        Object argValue = bindingMap.get(argName);
        RDFNode arg;
        if (argValue instanceof URI) {
            arg = new ResourceImpl(argValue.toString());
        } else {
            arg = getLiteralNode(argValue);
        }
        initialBinding.add(argName, arg);
    }
    ParameterizedSparqlString queryString = new ParameterizedSparqlString(q1.toString(), initialBinding);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, queryString.asQuery(), false);
        ResultSet results = execute.execSelect();
        return rsm.mapResultSet(results);
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute query '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Query(com.hp.hpl.jena.query.Query) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) URI(java.net.URI) QueryExecution(com.hp.hpl.jena.query.QueryExecution) QuerySolutionMap(com.hp.hpl.jena.query.QuerySolutionMap) Graph(com.hp.hpl.jena.graph.Graph) ResourceImpl(com.hp.hpl.jena.rdf.model.impl.ResourceImpl) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) ResultSet(com.hp.hpl.jena.query.ResultSet) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 2 with RDFNode

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

the class JenaToOwlConvert method ResourceJenaToOwlAxiom.

// //////////////////////////////////////////////////////////////////////////////
/**
 * This function converts every statments relative to a resource in an a set of OWLAxiom objects
 *
 * @param jenadata
 *            {A resource in the form (S,P,O), it could be any kind of resource (a class, a data property,
 *            an object property and an instance) except a litteral}
 * @param format
 *            {The format of the ontology, i.e. "RDF/XML"}
 * @return {A set of axiom in the form of Set<OWLAxiom>}
 */
public synchronized Set<OWLAxiom> ResourceJenaToOwlAxiom(Resource jenadata, String format) {
    while (available == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("ResourceJenaToOwlAxiom::: " + e);
        }
    }
    available = false;
    try {
        OntModel model = ModelFactory.createOntologyModel();
        StmtIterator prop = jenadata.listProperties();
        while (prop.hasNext()) {
            Statement stat = prop.nextStatement();
            model.add(stat);
            RDFNode obj = stat.getObject();
            if (obj.isResource()) {
                if (!obj.isURIResource()) {
                    StmtIterator aux = ((Resource) obj).listProperties();
                    while (aux.hasNext()) {
                        Statement stataux = aux.nextStatement();
                        model.add(stataux);
                    }
                }
            }
        }
        OWLOntology owlmodel = ModelJenaToOwlConvert(model, format);
        available = true;
        notifyAll();
        return owlmodel.getAxioms();
    } catch (Exception e) {
        System.err.print("ResourceJenaToOwlAxiom::: ");
        e.printStackTrace();
        return null;
    }
}
Also used : StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Resource(com.hp.hpl.jena.rdf.model.Resource) OntModel(com.hp.hpl.jena.ontology.OntModel) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 3 with RDFNode

use of com.hp.hpl.jena.rdf.model.RDFNode in project lucene-skos by behas.

the class SKOSEngineImpl method indexObject.

private void indexObject(Resource skos_concept, Document conceptDoc, ObjectProperty property, String field) {
    StmtIterator stmt_iter = skos_concept.listProperties(property);
    while (stmt_iter.hasNext()) {
        RDFNode concept = stmt_iter.nextStatement().getObject();
        if (!concept.canAs(Resource.class)) {
            logger.warn("Error when indexing relationship of concept " + skos_concept.getURI() + " .");
            continue;
        }
        Resource resource = concept.as(Resource.class);
        Field conceptField = new Field(field, resource.getURI(), TextField.TYPE_STORED);
        conceptDoc.add(conceptField);
    }
}
Also used : StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) Resource(com.hp.hpl.jena.rdf.model.Resource) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 4 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 5 with RDFNode

use of com.hp.hpl.jena.rdf.model.RDFNode in project nextprot-api by calipho-sib.

the class RdfHelpServiceImpl method getDataFromSolutionVar.

private Object getDataFromSolutionVar(QuerySolution sol, String var, boolean useQuotes) {
    RDFNode n = sol.get(var);
    if (n == null)
        return "";
    RDFBasicVisitor rdfVisitor = new RDFBasicVisitor(sparqlDictionary.getSparqlPrefixes());
    rdfVisitor.setSurroundLiteralStringWithQuotes(useQuotes);
    return n.visitWith(rdfVisitor);
}
Also used : RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Aggregations

RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)6 Resource (com.hp.hpl.jena.rdf.model.Resource)3 StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)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 Query (com.hp.hpl.jena.query.Query)1 QueryExecution (com.hp.hpl.jena.query.QueryExecution)1 QuerySolution (com.hp.hpl.jena.query.QuerySolution)1 QuerySolutionMap (com.hp.hpl.jena.query.QuerySolutionMap)1 ResultSet (com.hp.hpl.jena.query.ResultSet)1 Property (com.hp.hpl.jena.rdf.model.Property)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 TreeMap (java.util.TreeMap)1 Field (org.apache.lucene.document.Field)1 StringField (org.apache.lucene.document.StringField)1 TextField (org.apache.lucene.document.TextField)1