Java Code Examples for com.hp.hpl.jena.query.Query#addResultVar()

The following examples show how to use com.hp.hpl.jena.query.Query#addResultVar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnionSelectorEvaluator.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public Var evaluate(NodeSelector nodeSelector, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    UnionSelector unionSelector = (UnionSelector) nodeSelector;

    NodeSelector nodeSelectorLeft = unionSelector.getLeft();
    NodeSelector nodeSelectorRight = unionSelector.getRight();

    ElementGroup leftGroup = new ElementGroup();
    ElementGroup rightGroup = new ElementGroup();

    Var leftVar = LDPathEvaluator.evaluate(nodeSelectorLeft, leftGroup, var, evaluatorConfiguration);
    Var rightVar = LDPathEvaluator.evaluate(nodeSelectorRight, rightGroup, var, evaluatorConfiguration);

    Var subVar = Var.alloc(VarIDGenerator.createID());

    Query leftSubQuery = new Query();
    leftGroup.addElement(new ElementBind(subVar, new NodeValueNode(leftVar.asNode())));
    leftSubQuery.setQueryPattern(leftGroup);
    leftSubQuery.addResultVar(var);
    leftSubQuery.addResultVar(subVar);
    leftSubQuery.setQuerySelectType();
    ElementSubQuery leftESubQuery = new ElementSubQuery(leftSubQuery);

    Query rightSubQuery = new Query();
    rightGroup.addElement(new ElementBind(subVar, new NodeValueNode(rightVar.asNode())));
    rightSubQuery.setQueryPattern(rightGroup);
    rightSubQuery.addResultVar(var);
    rightSubQuery.addResultVar(subVar);
    rightSubQuery.setQuerySelectType();
    ElementSubQuery rightESubQuery = new ElementSubQuery(rightSubQuery);


    ElementUnion elementUnion = new ElementUnion();

    elementUnion.addElement(leftESubQuery);
    elementUnion.addElement(rightESubQuery);
    elementGroup.addElement(elementUnion);

    return subVar;
}
 
Example 2
Source File: EvalQuery.java    From anno4j with Apache License 2.0 5 votes vote down vote up
public static <T extends ResourceObject> Query evaluate(QueryServiceConfiguration queryServiceDTO, URI rootType) throws ParseException {

        Query query = QueryFactory.make();
        query.setQuerySelectType();

        ElementGroup elementGroup = new ElementGroup();

        Var objectVar = Var.alloc("root");

        // Creating and adding the first triple - could be something like: "?objectVar rdf:type oa:Annotation
        Triple t1 = new Triple(objectVar, RDF.type.asNode(), NodeFactory.createURI(rootType.toString()));
        elementGroup.addTriplePattern(t1);

        // Evaluating the criteria
        for (Criteria c : queryServiceDTO.getCriteria()) {
            SesameValueBackend backend = new SesameValueBackend();

            LdPathParser parser = new LdPathParser(backend, queryServiceDTO.getConfiguration(), new StringReader(c.getLdpath()));
            Var var = LDPathEvaluator.evaluate(parser.parseSelector(queryServiceDTO.getPrefixes()), elementGroup, objectVar, queryServiceDTO.getEvaluatorConfiguration());

            if (c.getConstraint() != null) {
                String resolvedConstraint = resolveConstraintPrefix(c.getConstraint(), queryServiceDTO, parser);
                EvalComparison.evaluate(elementGroup, c, var, resolvedConstraint);
            }
        }

        // Adding all generated patterns to the query object
        query.setQueryPattern(elementGroup);

        // Choose what we want so select - SELECT ?annotation in this case
        query.addResultVar(objectVar);

        // Setting the default prefixes, like rdf: or dc:
        query.getPrefixMapping().setNsPrefixes(queryServiceDTO.getPrefixes());

        return query;
    }