Search in sources :

Example 1 with DataIntegrityException

use of won.protocol.exception.DataIntegrityException in project webofneeds by researchstudio-sat.

the class TensorEntrySparqlGenerator method generateTensorEntries.

public Collection<TensorEntry> generateTensorEntries() {
    Collection<TensorEntry> tensorEntries = new LinkedList<>();
    ParameterizedSparqlString pss = new ParameterizedSparqlString();
    pss.setCommandText(query);
    for (String key : parameterBindings.keySet()) {
        Object value = parameterBindings.get(key);
        if (value instanceof String) {
            pss.setLiteral(key, (String) value);
        } else if (value instanceof Long || value instanceof Integer) {
            pss.setLiteral(key, (Long) value);
        } else if (value instanceof Float || value instanceof Double) {
            pss.setLiteral(key, (Double) value);
        } else {
            throw new IllegalArgumentException("Variable must be of type String/Long/Integer/Float/Double");
        }
    }
    Query q = pss.asQuery();
    QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, q);
    ResultSet results = qexec.execSelect();
    // check that the query returns the right variables
    if (!results.getResultVars().containsAll(Arrays.asList(variableNames))) {
        throw new DataIntegrityException("sparql query is expected to return variables: " + variableNames);
    }
    while (results.hasNext()) {
        TensorEntry entry = new TensorEntry();
        QuerySolution qs = results.next();
        RDFNode node = qs.get("slice");
        entry.setSliceName(node.isResource() ? node.asResource().getURI() : node.asLiteral().getString());
        node = qs.get("need");
        entry.setNeedUri(node.isResource() ? node.asResource().getURI() : node.asLiteral().getString());
        node = qs.get("value");
        entry.setValue(node.isResource() ? node.asResource().getURI() : node.asLiteral().getString());
        tensorEntries.add(entry);
    }
    return tensorEntries;
}
Also used : DataIntegrityException(won.protocol.exception.DataIntegrityException) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 2 with DataIntegrityException

use of won.protocol.exception.DataIntegrityException in project webofneeds by researchstudio-sat.

the class WonNodeSparqlService method getWonNodeInfoFromDataset.

/**
 * Get the {@link won.protocol.service.WonNodeInfo} as an object from a {@link Dataset}
 *
 * @param ds Dataset which holds won node information
 * @return
 */
public WonNodeInfo getWonNodeInfoFromDataset(Dataset ds) {
    String dsWonNodeUri = getWonNodeUriFromDataset(ds);
    WonNodeInfo nodeInfo = WonRdfUtils.WonNodeUtils.getWonNodeInfo(URI.create(dsWonNodeUri), ds);
    if (nodeInfo == null) {
        throw new DataIntegrityException("Could not load won node info from dataset with URI: " + dsWonNodeUri);
    }
    return nodeInfo;
}
Also used : DataIntegrityException(won.protocol.exception.DataIntegrityException) WonNodeInfo(won.protocol.service.WonNodeInfo)

Example 3 with DataIntegrityException

use of won.protocol.exception.DataIntegrityException in project webofneeds by researchstudio-sat.

the class DatasetBackedOwnerCallbackAdapter method makeConnection.

@Override
protected Connection makeConnection(final WonMessage wonMessage) {
    URI connUri = wonMessage.getReceiverURI();
    ParameterizedSparqlString pss = new ParameterizedSparqlString();
    pss.setNsPrefix("won", WON.BASE_URI);
    pss.setCommandText(QUERY_CONNECTION);
    pss.setIri("con", connUri.toString());
    Query query = pss.asQuery();
    QueryExecution qExec = QueryExecutionFactory.create(query, dataset);
    qExec.getContext().set(TDB.symUnionDefaultGraph, true);
    try {
        Connection con = null;
        final ResultSet results = qExec.execSelect();
        if (results.hasNext()) {
            QuerySolution soln = results.next();
            if (results.hasNext()) {
                throw new DataIntegrityException("Query must not yield multiple solutions");
            }
            con = new Connection();
            con.setConnectionURI(getURIFromSolution(soln, "con"));
            con.setTypeURI(getURIFromSolution(soln, "type"));
            con.setNeedURI(getURIFromSolution(soln, "need"));
            con.setState(ConnectionState.fromURI(getURIFromSolution(soln, "state")));
            con.setRemoteNeedURI(getURIFromSolution(soln, "remoteNeed"));
            con.setRemoteConnectionURI(getURIFromSolution(soln, "remoteCon"));
        }
        return con;
    } finally {
        if (!qExec.isClosed()) {
            qExec.close();
        }
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) DataIntegrityException(won.protocol.exception.DataIntegrityException) Connection(won.protocol.model.Connection) ResultSet(org.apache.jena.query.ResultSet) ParameterizedSparqlString(org.apache.jena.query.ParameterizedSparqlString) URI(java.net.URI) QueryExecution(org.apache.jena.query.QueryExecution)

Aggregations

DataIntegrityException (won.protocol.exception.DataIntegrityException)3 URI (java.net.URI)1 ParameterizedSparqlString (org.apache.jena.query.ParameterizedSparqlString)1 Query (org.apache.jena.query.Query)1 QueryExecution (org.apache.jena.query.QueryExecution)1 QuerySolution (org.apache.jena.query.QuerySolution)1 ResultSet (org.apache.jena.query.ResultSet)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 Connection (won.protocol.model.Connection)1 WonNodeInfo (won.protocol.service.WonNodeInfo)1