Search in sources :

Example 1 with StellarAssignment

use of org.apache.metron.stellar.common.StellarAssignment in project metron by apache.

the class StellarAssignmentTest method testNonAssignment.

@Test
public void testNonAssignment() {
    for (String statement : ImmutableList.of("bar + grok", "  bar + grok", "bar + grok   ")) {
        StellarAssignment assignment = StellarAssignment.from(statement);
        Assert.assertNull(assignment.getKey());
        Assert.assertNull(assignment.getVariable());
        Assert.assertEquals("bar + grok", assignment.getStatement());
        Assert.assertEquals("bar + grok", assignment.getValue());
    }
}
Also used : StellarAssignment(org.apache.metron.stellar.common.StellarAssignment) Test(org.junit.Test)

Example 2 with StellarAssignment

use of org.apache.metron.stellar.common.StellarAssignment in project metron by apache.

the class StellarAssignmentTest method testAssignment.

@Test
public void testAssignment() {
    for (String statement : ImmutableList.of("foo := bar + grok", "foo   := bar + grok", "foo := bar + grok   ")) {
        StellarAssignment assignment = StellarAssignment.from(statement);
        Assert.assertEquals("foo", assignment.getKey());
        Assert.assertEquals("foo", assignment.getVariable());
        Assert.assertEquals("bar + grok", assignment.getStatement());
        Assert.assertEquals("bar + grok", assignment.getValue());
    }
}
Also used : StellarAssignment(org.apache.metron.stellar.common.StellarAssignment) Test(org.junit.Test)

Example 3 with StellarAssignment

use of org.apache.metron.stellar.common.StellarAssignment in project metron by apache.

the class AssignmentCommand method execute.

/**
 * Handles variable assignment.
 * @param input The assignment expression to execute.
 * @param executor A stellar execution environment.
 * @return
 */
@Override
public StellarResult execute(String input, StellarShellExecutor executor) {
    assert StellarAssignment.isAssignment(input);
    // extract the variable and assignment expression
    StellarAssignment assignment = StellarAssignment.from(input);
    String varName = assignment.getVariable();
    String varExpr = assignment.getStatement();
    // execute the stellar expression
    StellarResult result = executor.execute(varExpr);
    if (result.isSuccess()) {
        Object value = null;
        if (result.getValue().isPresent()) {
            value = result.getValue().get();
        } else if (result.isValueNull()) {
            value = null;
        }
        // variable assignment
        executor.assign(varName, value, Optional.of(varExpr));
        return result;
    } else {
        return result;
    }
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) StellarAssignment(org.apache.metron.stellar.common.StellarAssignment)

Example 4 with StellarAssignment

use of org.apache.metron.stellar.common.StellarAssignment in project metron by apache.

the class MagicDefineGlobal method execute.

@Override
public StellarResult execute(String command, StellarShellExecutor executor) {
    // grab the expression in '%define <assign-expression>'
    String assignExpr = StringUtils.trimToEmpty(command.substring(MAGIC_DEFINE.length()));
    if (StringUtils.length(assignExpr) < 1) {
        return error(MAGIC_DEFINE + " missing assignment expression");
    }
    // the expression must be an assignment
    if (!StellarAssignment.isAssignment(assignExpr)) {
        return error(MAGIC_DEFINE + " expected assignment expression");
    }
    // execute the expression
    StellarAssignment expr = StellarAssignment.from(assignExpr);
    StellarResult result = executor.execute(expr.getStatement());
    // execution must be successful
    if (!result.isSuccess()) {
        return error(MAGIC_DEFINE + " expression execution failed");
    }
    // expression should have a result
    if (!result.getValue().isPresent()) {
        return error(MAGIC_DEFINE + " expression produced no result");
    }
    // alter the global configuration
    Object value = result.getValue().get();
    executor.getGlobalConfig().put(expr.getVariable(), value);
    return success(value);
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) StellarAssignment(org.apache.metron.stellar.common.StellarAssignment)

Example 5 with StellarAssignment

use of org.apache.metron.stellar.common.StellarAssignment in project metron by apache.

the class StellarAssignmentTest method testImmutability.

@Test(expected = UnsupportedOperationException.class)
public void testImmutability() {
    StellarAssignment assignment = StellarAssignment.from("foo := bar");
    assignment.setValue("myval");
}
Also used : StellarAssignment(org.apache.metron.stellar.common.StellarAssignment) Test(org.junit.Test)

Aggregations

StellarAssignment (org.apache.metron.stellar.common.StellarAssignment)5 Test (org.junit.Test)3 StellarResult (org.apache.metron.stellar.common.shell.StellarResult)2