Java Code Examples for org.eclipse.rdf4j.query.algebra.Var#setConstant()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Var#setConstant() . 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: HasSelfVisitorTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypePattern() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<IRI> narcissistProps = new HashSet<>();
    narcissistProps.add(love);
    when(inferenceEngine.getHasSelfImplyingType(narcissist)).thenReturn(narcissistProps);
    final Var subj = new Var("s");
    final Var obj = new Var("o", narcissist);
    obj.setConstant(true);
    final Var pred = new Var("p", RDF.TYPE);
    pred.setConstant(true);

    final Projection query = new Projection(new StatementPattern(subj, pred, obj),
            new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new HasSelfVisitor(conf, inferenceEngine));

    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union = (Union) query.getArg();
    Assert.assertTrue(union.getRightArg() instanceof StatementPattern);
    Assert.assertTrue(union.getLeftArg() instanceof StatementPattern);
    final StatementPattern expectedLeft = new StatementPattern(subj, pred, obj);
    final StatementPattern expectedRight = new StatementPattern(subj, new Var("urn:love", love), subj);
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
 
Example 2
Source File: FluoStringConverterTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void statementPatternToString() throws MalformedQueryException {
       // Setup a StatementPattern that represents "?x <http://worksAt> <http://Chipotle>."
       final Var subject = new Var("x");
       final Var predicate = new Var(prependConstant("http://worksAt"), VF.createIRI("http://worksAt"));
       predicate.setConstant(true);
       final Var object = new Var(prependConstant("http://Chipotle"), VF.createIRI("http://Chipotle"));
       object.setConstant(true);
       final StatementPattern pattern = new StatementPattern(subject, predicate, object);

       // Convert the pattern to a String.
       final String spString = FluoStringConverter.toStatementPatternString(pattern);

       // Ensure it converted to the expected result.
       final String expected = "x:::" +
               prependConstant("http://worksAt<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::") +
               prependConstant("http://Chipotle<<~>>http://www.w3.org/2001/XMLSchema#anyURI");

       assertEquals(spString, expected);
}
 
Example 3
Source File: FluoStringConverterTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void stringToStatementPattern() {
    // Setup the String representation of a statement pattern.
    final String patternString = "x:::" +
            prependConstant("http://worksAt<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::") +
            prependConstant("http://Chipotle<<~>>http://www.w3.org/2001/XMLSchema#anyURI");

    // Convert it to a StatementPattern.
    final StatementPattern statementPattern = FluoStringConverter.toStatementPattern(patternString);

    // Enusre it converted to the expected result.
    final Var subject = new Var("x");
    final Var predicate = new Var(prependConstant("http://worksAt"), VF.createIRI("http://worksAt"));
    predicate.setConstant(true);
    final Var object = new Var(prependConstant("http://Chipotle"), VF.createIRI("http://Chipotle"));
    object.setConstant(true);
    final StatementPattern expected = new StatementPattern(subject, predicate, object);

    assertEquals(expected, statementPattern);
}
 
Example 4
Source File: TupleExprs.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates an (anonymous) Var representing a constant value. The variable name will be derived from the actual value
 * to guarantee uniqueness.
 *
 * @param value
 * @return an (anonymous) Var representing a constant value.
 */
public static Var createConstVar(Value value) {
	String varName = getConstVarName(value);
	Var var = new Var(varName);
	var.setConstant(true);
	var.setAnonymous(true);
	var.setValue(value);
	return var;
}
 
Example 5
Source File: HasSelfVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyPattern_constantSubj() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<Resource> loveTypes = new HashSet<>();
    loveTypes.add(narcissist);
    when(inferenceEngine.getHasSelfImplyingProperty(love)).thenReturn(loveTypes);
    final Var subj = new Var("s", self);
    subj.setConstant(true);
    final Var obj = new Var("o");
    final Var pred = new Var("p", love);
    pred.setConstant(true);

    final Projection query = new Projection(new StatementPattern(subj, pred, obj),
            new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new HasSelfVisitor(conf, inferenceEngine));

    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union = (Union) query.getArg();
    Assert.assertTrue(union.getRightArg() instanceof StatementPattern);
    Assert.assertTrue(union.getLeftArg() instanceof Extension);
    final StatementPattern expectedRight = new StatementPattern(subj, pred, obj);
    final Extension expectedLeft = new Extension(
            new StatementPattern(subj, new Var(RDF.TYPE.stringValue(), RDF.TYPE), new Var("urn:Narcissist", narcissist)),
            new ExtensionElem(subj, "o"));
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
 
Example 6
Source File: HasSelfVisitorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyPattern_constantObj() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<Resource> loveTypes = new HashSet<>();
    loveTypes.add(narcissist);
    when(inferenceEngine.getHasSelfImplyingProperty(love)).thenReturn(loveTypes);
    final Var subj = new Var("s");
    final Var obj = new Var("o", self);
    obj.setConstant(true);
    final Var pred = new Var("p", love);
    pred.setConstant(true);

    final Projection query = new Projection(new StatementPattern(subj, pred, obj),
            new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new HasSelfVisitor(conf, inferenceEngine));

    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union = (Union) query.getArg();
    Assert.assertTrue(union.getRightArg() instanceof StatementPattern);
    Assert.assertTrue(union.getLeftArg() instanceof Extension);
    final StatementPattern expectedRight = new StatementPattern(subj, pred, obj);
    final Extension expectedLeft = new Extension(
            new StatementPattern(obj, new Var(RDF.TYPE.stringValue(), RDF.TYPE), new Var("urn:Narcissist", narcissist)),
            new ExtensionElem(obj, "s"));
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
 
Example 7
Source File: FluoStringConverterTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void toVar_uri() {
    // Setup the string representation of the variable.
    final String varString = String.format(prependConstant("http://Chipotle%s%s"),TYPE_DELIM,XMLSchema.ANYURI );

    // Convert it to a Var object.
    final Var var = FluoStringConverter.toVar(varString);

    // Ensure it converted to the expected result.
    final Var expected = new Var(prependConstant("http://Chipotle"), VF.createIRI("http://Chipotle"));
    expected.setConstant(true);

    assertEquals(expected, var);
}
 
Example 8
Source File: FluoStringConverterTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void toVar_int() throws MalformedQueryException {
    // Setup the string representation of the variable.
    final String varString = prependConstant("5<<~>>http://www.w3.org/2001/XMLSchema#integer");

    // Convert it to a Var object.
    final Var result = FluoStringConverter.toVar(varString);

    // Ensure it converted to the expected result.
    final Var expected = new Var(prependConstant("5"), VF.createLiteral("5", XMLSchema.INTEGER));
    expected.setConstant(true);

    assertEquals(expected, result);
}
 
Example 9
Source File: FluoStringConverterTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void toVar_string() {
    // Setup the string representation of the variable.
    final String varString = prependConstant("Chipotle<<~>>http://www.w3.org/2001/XMLSchema#string");

    // Convert it to a Var object.
    final Var result = FluoStringConverter.toVar(varString);

    // Ensure it converted to the expected result.
    final Var expected = new Var(prependConstant("Chipotle"), VF.createLiteral("Chipotle", XMLSchema.STRING));
    expected.setConstant(true);

    assertEquals(expected, result);
}
 
Example 10
Source File: SpinConstructRule.java    From rya with Apache License 2.0 5 votes vote down vote up
public TypeRequirementVisitor(String varName, Resource requiredType) {
    final Var typeVar = VarNameUtils.createUniqueConstVar(requiredType);
    typeVar.setConstant(true);
    this.varName = varName;
    if (BASE_TYPES.contains(requiredType)) {
        this.typeRequirement = null;
    }
    else {
        this.typeRequirement = new StatementPattern(new Var(varName), RDF_TYPE_VAR, typeVar);
    }
}
 
Example 11
Source File: SourceSelectorWithQueryTransform.java    From semagrow with Apache License 2.0 5 votes vote down vote up
private Var transformVar(Var v, EquivalentIRI uri) {
    if (!v.hasValue())
        return v;
    else {
        Var v1 = new Var("const-" + uri.getTargetURI().toString(), uri.getTargetURI());
        v1.setConstant(true);
        return v1;
    }
}