Java Code Examples for com.hp.hpl.jena.sparql.core.Var#alloc()

The following examples show how to use com.hp.hpl.jena.sparql.core.Var#alloc() . 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: IsLiteralTestEvaluator.java    From anno4j with Apache License 2.0 6 votes vote down vote up
@Override
public Var evaluate(NodeSelector nodeSelector, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    TestingSelector testingSelector = (TestingSelector) nodeSelector;
    FunctionTest functionTest = (FunctionTest) testingSelector.getTest();

    if(functionTest.getArgSelectors().get(0) instanceof PropertySelector) {
        PropertySelector arg = (PropertySelector) functionTest.getArgSelectors().get(0);
        PropertySelector delegate = (PropertySelector) testingSelector.getDelegate();

        Var target = Var.alloc(VarIDGenerator.createID());
        elementGroup.addTriplePattern(new Triple(var.asNode(), NodeFactory.createURI(delegate.getProperty().toString()), target));

        Var selector = Var.alloc(VarIDGenerator.createID());
        elementGroup.addTriplePattern(new Triple(target.asNode(), NodeFactory.createURI(arg.getProperty().toString()), selector.asNode()));

        elementGroup.addElementFilter(new ElementFilter(new E_IsLiteral(new ExprVar(selector))));

        return selector;
    } else {
        throw new IllegalStateException("Argument of function isLiteral has to be a PropertySelector");
    }
}
 
Example 2
Source File: IsATestEvaluator.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public Expr evaluate(NodeTest nodeTest, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    IsATest isATest = (IsATest) nodeTest;
    Var tmpVar = Var.alloc(Var.alloc(VarIDGenerator.createID()));
    elementGroup.addTriplePattern(new Triple(var.asNode(), RDF.type.asNode(), tmpVar.asNode()));
    return new E_Equals(new ExprVar(tmpVar.asNode()), new NodeValueNode(NodeFactory.createURI(isATest.getPathExpression(new SesameValueBackend()).replace("<", "").replace(">", "").replaceFirst("is-a ", ""))));
}
 
Example 3
Source File: PropertySelectorEvaluator.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) {
    PropertySelector propertySelector = (PropertySelector) nodeSelector;
    if (propertySelector instanceof WildcardSelector) {
        throw new IllegalStateException(propertySelector.getClass() + " is not supported.");
    }

    Var id = Var.alloc(VarIDGenerator.createID());
    elementGroup.addTriplePattern(new Triple(var.asNode(), NodeFactory.createURI(propertySelector.getProperty().toString()), id.asNode()));

    return id;
}
 
Example 4
Source File: ReversePropertySelectorEvaluator.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) {
    ReversePropertySelector reversePropertySelector = (ReversePropertySelector) nodeSelector;
    Var id = Var.alloc(VarIDGenerator.createID());
    ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(new TriplePath(var.asNode(), new P_Inverse(new P_Link(NodeFactory.createURI(reversePropertySelector.getProperty().toString()))), id.asNode()));
    ElementGroup group = new ElementGroup();
    group.addElement(epb);
    elementGroup.addElement(group);
    return id;
}
 
Example 5
Source File: RecursivePathSelectorEvaluator.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) {
    RecursivePathSelector recursivePathSelector = (RecursivePathSelector) nodeSelector;

    Var id = Var.alloc(VarIDGenerator.createID());
    ElementPathBlock epb = new ElementPathBlock();
    String pathExpression = recursivePathSelector.getPathExpression(new SesameValueBackend());
    /**
     * initial pathExpression contains:
     *      (<http://www.w3.org/ns/oa#hasBody> / <http://www.example.com/schema#recursiveBodyValue>)*
     *
     * Because P_ZeroOrMore and so one, creates the same expression we have to strip the redundant chars.
     */
    String strippedPathExpression = pathExpression.substring(2, pathExpression.length() -3 );

    TriplePath triplePath = null;
    if (pathExpression.contains("*")) {
        triplePath = new TriplePath(var.asNode(), new P_ZeroOrMore1(new P_Link(NodeFactory.createURI(strippedPathExpression))), id.asNode());
    } else if (pathExpression.contains("+")) {
        triplePath = new TriplePath(var.asNode(), new P_OneOrMore1(new P_Link(NodeFactory.createURI(strippedPathExpression))), id.asNode());
    } else {
        throw new IllegalStateException("Only ZeroOrMorePath(*), OneOrMorePath(+) path selectors are currently supported.");
    }

    epb.addTriple(triplePath);
    ElementGroup group = new ElementGroup();
    group.addElement(epb);
    elementGroup.addElement(group);
    return id;
}
 
Example 6
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 7
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;
    }
 
Example 8
Source File: D2RQQueryHandler.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public Domain map1(Binding binding) {
	Domain d = new Domain(variables.length);
	for (int i = 0; i < variables.length; i++) {
		Var v = Var.alloc(variables[i]);
		Node value = binding.get(v);
		int index = ((Integer) indexes.get(v)).intValue();
		d.setElement(index, value);
	}
	return d;
}
 
Example 9
Source File: SelfSelectionEvaluator.java    From anno4j with Apache License 2.0 4 votes vote down vote up
@Override
public Var evaluate(NodeSelector nodeSelector, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    return Var.alloc("root");
}