Search in sources :

Example 1 with PropertyImpl

use of org.apache.jena.rdf.model.impl.PropertyImpl in project webofneeds by researchstudio-sat.

the class DynamicDatasetToDatasetBySparqlGSPOSelectFunction method apply.

@Override
public Dataset apply(Dataset dataset) {
    dataset.begin(ReadWrite.READ);
    Dataset result = DatasetFactory.createGeneral();
    result.begin(ReadWrite.WRITE);
    Query query = QueryFactory.create(sparql);
    try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset, initialBinding)) {
        ResultSet resultSet = queryExecution.execSelect();
        RDFNode currentProposal = null;
        Model currentProposalContent = ModelFactory.createDefaultModel();
        while (resultSet.hasNext()) {
            QuerySolution solution = resultSet.next();
            RDFNode proposalNode = solution.get("g");
            if (currentProposal == null) {
                // first solution: remember uri of first proposal
                currentProposal = proposalNode;
            } else {
                // the currentProposal URI and prepare a new empty model for the next proposal
                if (!currentProposal.equals(proposalNode)) {
                    // we have seen all triples of currentProposal
                    result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
                    currentProposalContent = ModelFactory.createDefaultModel();
                }
                currentProposal = proposalNode;
            }
            // add current triple into currentAgreementModel
            RDFNode s = solution.get("s");
            RDFNode p = solution.get("p");
            RDFNode o = solution.get("o");
            Statement newStatement = new StatementImpl(s.asResource(), new PropertyImpl(p.asResource().getURI()), o);
            currentProposalContent.add(newStatement);
        }
        // add the last model
        if (currentProposal != null) {
            result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
        }
        return result;
    } finally {
        dataset.commit();
        result.commit();
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) StatementImpl(org.apache.jena.rdf.model.impl.StatementImpl) ResultSet(org.apache.jena.query.ResultSet) Model(org.apache.jena.rdf.model.Model) PropertyImpl(org.apache.jena.rdf.model.impl.PropertyImpl) QueryExecution(org.apache.jena.query.QueryExecution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 2 with PropertyImpl

use of org.apache.jena.rdf.model.impl.PropertyImpl in project webofneeds by researchstudio-sat.

the class DatasetToDatasetBySparqlGSPOSelectFunction method apply.

@Override
public Dataset apply(Dataset dataset) {
    dataset.begin(ReadWrite.READ);
    Dataset result = DatasetFactory.createGeneral();
    result.begin(ReadWrite.WRITE);
    Query query = QueryFactory.create(sparql);
    try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset)) {
        ResultSet resultSet = queryExecution.execSelect();
        RDFNode currentProposal = null;
        Model currentProposalContent = ModelFactory.createDefaultModel();
        while (resultSet.hasNext()) {
            QuerySolution solution = resultSet.next();
            RDFNode proposalNode = solution.get("g");
            if (currentProposal == null) {
                // first solution: remember uri of first proposal
                currentProposal = proposalNode;
            } else {
                // the currentProposal URI and prepare a new empty model for the next proposal
                if (!currentProposal.equals(proposalNode)) {
                    // we have seen all triples of currentProposal
                    result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
                    currentProposalContent = ModelFactory.createDefaultModel();
                }
                currentProposal = proposalNode;
            }
            // add current triple into currentAgreementModel
            RDFNode s = solution.get("s");
            RDFNode p = solution.get("p");
            RDFNode o = solution.get("o");
            Statement newStatement = new StatementImpl(s.asResource(), new PropertyImpl(p.asResource().getURI()), o);
            currentProposalContent.add(newStatement);
        }
        // add the last model
        if (currentProposal != null) {
            result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
        }
        return result;
    } finally {
        dataset.commit();
        result.commit();
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) StatementImpl(org.apache.jena.rdf.model.impl.StatementImpl) ResultSet(org.apache.jena.query.ResultSet) Model(org.apache.jena.rdf.model.Model) PropertyImpl(org.apache.jena.rdf.model.impl.PropertyImpl) QueryExecution(org.apache.jena.query.QueryExecution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

Dataset (org.apache.jena.query.Dataset)2 Query (org.apache.jena.query.Query)2 QueryExecution (org.apache.jena.query.QueryExecution)2 QuerySolution (org.apache.jena.query.QuerySolution)2 ResultSet (org.apache.jena.query.ResultSet)2 Model (org.apache.jena.rdf.model.Model)2 RDFNode (org.apache.jena.rdf.model.RDFNode)2 Statement (org.apache.jena.rdf.model.Statement)2 PropertyImpl (org.apache.jena.rdf.model.impl.PropertyImpl)2 StatementImpl (org.apache.jena.rdf.model.impl.StatementImpl)2