org.eclipse.rdf4j.query.algebra.BNodeGenerator Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.BNodeGenerator. 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: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(BNodeGenerator node, BindingSet bindings)
		throws QueryEvaluationException {
	ValueExpr nodeIdExpr = node.getNodeIdExpr();

	if (nodeIdExpr != null) {
		Value nodeId = evaluate(nodeIdExpr, bindings);

		if (nodeId instanceof Literal) {
			String nodeLabel = ((Literal) nodeId).getLabel() + (bindings.toString().hashCode());
			return tripleSource.getValueFactory().createBNode(nodeLabel);
		} else {
			throw new ValueExprEvaluationException("BNODE function argument must be a literal");
		}
	}
	return tripleSource.getValueFactory().createBNode();
}
 
Example #2
Source File: MultiProjectionEvaluator.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Make a {@link MultiProjectionEvaluator} that processes the logic of a {@link MultiProjection}.
 *
 * @param multiProjection - Defines the projections that will be processed. (not null)
 * @param bNodeIdFactory - Creates the IDs for Blank Nodes. (not null)
 * @return A {@link MultiProjectionEvaluator} for the provided {@link MultiProjection}.
 */
public static MultiProjectionEvaluator make(final MultiProjection multiProjection, final BNodeIdFactory bNodeIdFactory) {
    requireNonNull(multiProjection);

    // Figure out if there are extensions.
    final TupleExpr arg = multiProjection.getArg();
    final Optional<Extension> extension = (arg instanceof Extension) ? Optional.of((Extension)arg): Optional.empty();

    // If there are, iterate through them and find any blank node source names.
    final Set<String> blankNodeSourceNames = new HashSet<>();
    if(extension.isPresent()) {
        for(final ExtensionElem elem : extension.get().getElements()) {
            if(elem.getExpr() instanceof BNodeGenerator) {
                blankNodeSourceNames.add( elem.getName() );
            }
        }
    }

    // Create a ProjectionEvaluator for each projection that is part of the multi.
    final Set<ProjectionEvaluator> projections = new HashSet<>();
    for(final ProjectionElemList projectionElemList : multiProjection.getProjections()) {
        projections.add( new ProjectionEvaluator(projectionElemList, extension) );
    }

    return new MultiProjectionEvaluator(projections, blankNodeSourceNames, bNodeIdFactory);
}
 
Example #3
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(BNodeGenerator node) throws RDFHandlerException {
	Resource currentSubj = subject;
	flushPendingStatement();
	handler.handleStatement(valueFactory.createStatement(subject, RDF.TYPE, SP.BNODE));
	if (node.getNodeIdExpr() != null) {
		predicate = SP.ARG1_PROPERTY;
		node.getNodeIdExpr().visit(this);
	}
	subject = currentSubj;
	predicate = null;
}
 
Example #4
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Object visit(ASTBNodeFunc node, Object data) throws VisitorException {

	BNodeGenerator generator = new BNodeGenerator();

	if (node.jjtGetNumChildren() > 0) {
		ValueExpr nodeIdExpr = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
		generator.setNodeIdExpr(nodeIdExpr);
	}

	return generator;
}
 
Example #5
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate a {@link BNodeGenerator} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return the value of the evaluation
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(BNodeGenerator node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    ValueExpr nodeIdExpr = node.getNodeIdExpr();
    if (nodeIdExpr != null) {
        Value nodeId = evaluate(nodeIdExpr, bindings);
        if (nodeId instanceof Literal) {
            String nodeLabel = ((Literal) nodeId).getLabel() + (bindings.toString().hashCode());
            return valueFactory.createBNode(nodeLabel);
        } else {
            throw new ValueExprEvaluationException("BNODE function argument must be a literal");
        }
    }
    return valueFactory.createBNode();
}
 
Example #6
Source File: ConstructConsequentVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
private void recordConsequent(ProjectionElemList variables, List<ExtensionElem> extensionElements) {
    Map<String, Value> bindings = new ConcurrentHashMap<>();
    Map<String, Value> values = new ConcurrentHashMap<>();
    Set<String> queryBnodes = new HashSet<>();
    Set<String> projectedBnodes = new HashSet<>();
    for (ExtensionElem ee : extensionElements) {
        if (ee.getExpr() instanceof ValueConstant) {
            bindings.put(ee.getName(), ((ValueConstant) ee.getExpr()).getValue());
        }
        else if (ee.getExpr() instanceof BNodeGenerator) {
            queryBnodes.add(ee.getName());
        }
    }
    for (ProjectionElem var : variables.getElements()) {
        String sourceName = var.getSourceName();
        String targetName = var.getTargetName();
        Value constValue = bindings.get(sourceName);
        if (constValue != null) {
            values.put(targetName, constValue);
        }
        else if (queryBnodes.contains(sourceName)) {
            projectedBnodes.add(targetName);
        }
    }
    Var subjVar = new Var(SUBJECT_VAR_NAME, values.get(SUBJECT_VAR_NAME));
    Var predVar = new Var(PREDICATE_VAR_NAME, values.get(PREDICATE_VAR_NAME));
    Var objVar = new Var(OBJECT_VAR_NAME, values.get(OBJECT_VAR_NAME));
    subjVar.setAnonymous(projectedBnodes.contains(SUBJECT_VAR_NAME));
    predVar.setAnonymous(projectedBnodes.contains(PREDICATE_VAR_NAME));
    objVar.setAnonymous(projectedBnodes.contains(OBJECT_VAR_NAME));
    StatementPattern sp = new StatementPattern(subjVar, predVar, objVar);
    consequentStatementPatterns.add(sp);
}
 
Example #7
Source File: ConstructConsequentVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testBNode() {
    Extension extension = new Extension(new SingletonSet(),
            new ExtensionElem(new Var("x"), "x"),
            new ExtensionElem(new BNodeGenerator(), "z"));
    Projection projection = new Projection(extension, new ProjectionElemList(
            new ProjectionElem("x", "subject"),
            new ProjectionElem("y", "predicate"),
            new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(
            new StatementPattern(s(null), p(null), anon(o(null))));
    Assert.assertEquals(expected, visitor.getConsequents());
}
 
Example #8
Source File: SPARQLValueExprRenderer.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(BNodeGenerator theGen)
        throws Exception
{
    mBuffer.append(theGen.getSignature());
}
 
Example #9
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Value evaluate(ValueExpr expr, BindingSet bindings)
		throws QueryEvaluationException {
	if (expr instanceof Var) {
		return evaluate((Var) expr, bindings);
	} else if (expr instanceof ValueConstant) {
		return evaluate((ValueConstant) expr, bindings);
	} else if (expr instanceof BNodeGenerator) {
		return evaluate((BNodeGenerator) expr, bindings);
	} else if (expr instanceof Bound) {
		return evaluate((Bound) expr, bindings);
	} else if (expr instanceof Str) {
		return evaluate((Str) expr, bindings);
	} else if (expr instanceof Label) {
		return evaluate((Label) expr, bindings);
	} else if (expr instanceof Lang) {
		return evaluate((Lang) expr, bindings);
	} else if (expr instanceof LangMatches) {
		return evaluate((LangMatches) expr, bindings);
	} else if (expr instanceof Datatype) {
		return evaluate((Datatype) expr, bindings);
	} else if (expr instanceof Namespace) {
		return evaluate((Namespace) expr, bindings);
	} else if (expr instanceof LocalName) {
		return evaluate((LocalName) expr, bindings);
	} else if (expr instanceof IsResource) {
		return evaluate((IsResource) expr, bindings);
	} else if (expr instanceof IsURI) {
		return evaluate((IsURI) expr, bindings);
	} else if (expr instanceof IsBNode) {
		return evaluate((IsBNode) expr, bindings);
	} else if (expr instanceof IsLiteral) {
		return evaluate((IsLiteral) expr, bindings);
	} else if (expr instanceof IsNumeric) {
		return evaluate((IsNumeric) expr, bindings);
	} else if (expr instanceof IRIFunction) {
		return evaluate((IRIFunction) expr, bindings);
	} else if (expr instanceof Regex) {
		return evaluate((Regex) expr, bindings);
	} else if (expr instanceof Coalesce) {
		return evaluate((Coalesce) expr, bindings);
	} else if (expr instanceof Like) {
		return evaluate((Like) expr, bindings);
	} else if (expr instanceof FunctionCall) {
		return evaluate((FunctionCall) expr, bindings);
	} else if (expr instanceof And) {
		return evaluate((And) expr, bindings);
	} else if (expr instanceof Or) {
		return evaluate((Or) expr, bindings);
	} else if (expr instanceof Not) {
		return evaluate((Not) expr, bindings);
	} else if (expr instanceof SameTerm) {
		return evaluate((SameTerm) expr, bindings);
	} else if (expr instanceof Compare) {
		return evaluate((Compare) expr, bindings);
	} else if (expr instanceof MathExpr) {
		return evaluate((MathExpr) expr, bindings);
	} else if (expr instanceof In) {
		return evaluate((In) expr, bindings);
	} else if (expr instanceof CompareAny) {
		return evaluate((CompareAny) expr, bindings);
	} else if (expr instanceof CompareAll) {
		return evaluate((CompareAll) expr, bindings);
	} else if (expr instanceof Exists) {
		return evaluate((Exists) expr, bindings);
	} else if (expr instanceof If) {
		return evaluate((If) expr, bindings);
	} else if (expr instanceof ListMemberOperator) {
		return evaluate((ListMemberOperator) expr, bindings);
	} else if (expr instanceof ValueExprTripleRef) {
		return evaluate((ValueExprTripleRef) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unsupported value expr type: " + expr.getClass());
	}
}
 
Example #10
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(BNodeGenerator node) throws X {
	meetNode(node);
}
 
Example #11
Source File: SparqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(BNodeGenerator theGen) throws Exception {
	mBuffer.append(theGen.getSignature());
}
 
Example #12
Source File: SerqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(BNodeGenerator theGen) throws Exception {
	mBuffer.append(theGen.getSignature());
}
 
Example #13
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 4 votes vote down vote up
/**
 * Determines which evaluate method to call based on the type of {@link ValueExpr}
 * @param expr the expression to evaluate
 * @param bindings the set of named value bindings the set of named value bindings
 * @return the {@link Value} resulting from the evaluation
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
Value evaluate(ValueExpr expr, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    if (expr instanceof Var) {
        return evaluate((Var) expr, bindings);
    } else if (expr instanceof ValueConstant) {
        return evaluate((ValueConstant) expr, bindings);
    } else if (expr instanceof BNodeGenerator) {
        return evaluate((BNodeGenerator) expr, bindings);
    } else if (expr instanceof Bound) {
        return evaluate((Bound) expr, bindings);
    } else if (expr instanceof Str) {
        return evaluate((Str) expr, bindings);
    } else if (expr instanceof Label) {
        return evaluate((Label) expr, bindings);
    } else if (expr instanceof Lang) {
        return evaluate((Lang) expr, bindings);
    } else if (expr instanceof LangMatches) {
        return evaluate((LangMatches) expr, bindings);
    } else if (expr instanceof Datatype) {
        return evaluate((Datatype) expr, bindings);
    } else if (expr instanceof Namespace) {
        return evaluate((Namespace) expr, bindings);
    } else if (expr instanceof LocalName) {
        return evaluate((LocalName) expr, bindings);
    } else if (expr instanceof IsResource) {
        return evaluate((IsResource) expr, bindings);
    } else if (expr instanceof IsURI) {
        return evaluate((IsURI) expr, bindings);
    } else if (expr instanceof IsBNode) {
        return evaluate((IsBNode) expr, bindings);
    } else if (expr instanceof IsLiteral) {
        return evaluate((IsLiteral) expr, bindings);
    } else if (expr instanceof IsNumeric) {
        return evaluate((IsNumeric) expr, bindings);
    } else if (expr instanceof IRIFunction) {
        return evaluate((IRIFunction) expr, bindings);
    } else if (expr instanceof Regex) {
        return evaluate((Regex) expr, bindings);
    } else if (expr instanceof Coalesce) {
        return evaluate((Coalesce) expr, bindings);
    } else if (expr instanceof Like) {
        return evaluate((Like) expr, bindings);
    } else if (expr instanceof FunctionCall) {
        return evaluate((FunctionCall) expr, bindings);
    } else if (expr instanceof And) {
        return evaluate((And) expr, bindings);
    } else if (expr instanceof Or) {
        return evaluate((Or) expr, bindings);
    } else if (expr instanceof Not) {
        return evaluate((Not) expr, bindings);
    } else if (expr instanceof SameTerm) {
        return evaluate((SameTerm) expr, bindings);
    } else if (expr instanceof Compare) {
        return evaluate((Compare) expr, bindings);
    } else if (expr instanceof MathExpr) {
        return evaluate((MathExpr) expr, bindings);
    } else if (expr instanceof In) {
        return evaluate((In) expr, bindings);
    } else if (expr instanceof CompareAny) {
        return evaluate((CompareAny) expr, bindings);
    } else if (expr instanceof CompareAll) {
        return evaluate((CompareAll) expr, bindings);
    } else if (expr instanceof Exists) {
        return evaluate((Exists) expr, bindings);
    } else if (expr instanceof If) {
        return evaluate((If) expr, bindings);
    } else if (expr instanceof ListMemberOperator) {
        return evaluate((ListMemberOperator) expr, bindings);
    } else if (expr == null) {
        throw new IllegalArgumentException("expr must not be null");
    } else {
        throw new QueryEvaluationException("Unsupported value expr type: " + expr.getClass());
    }
}