Search in sources :

Example 11 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult 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 12 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class DocCommand method execute.

@Override
public StellarResult execute(String command, StellarShellExecutor executor) {
    StellarResult result;
    // expect ?functionName
    String functionName = StringUtils.substring(command, 1);
    // grab any docs for the given function
    Spliterator<StellarFunctionInfo> fnIterator = executor.getFunctionResolver().getFunctionInfo().spliterator();
    Optional<StellarFunctionInfo> functionInfo = StreamSupport.stream(fnIterator, false).filter(info -> StringUtils.equals(functionName, info.getName())).findFirst();
    if (functionInfo.isPresent()) {
        result = success(docFormat(functionInfo.get()));
    } else {
        result = error(String.format("No docs available for function '%s'", functionName));
    }
    return result;
}
Also used : StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo) StellarResult.error(org.apache.metron.stellar.common.shell.StellarResult.error) StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) StringUtils(org.apache.commons.lang3.StringUtils) Spliterator(java.util.Spliterator) Function(java.util.function.Function) StellarResult.success(org.apache.metron.stellar.common.shell.StellarResult.success) StellarShellExecutor(org.apache.metron.stellar.common.shell.StellarShellExecutor) StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo) StellarResult(org.apache.metron.stellar.common.shell.StellarResult)

Example 13 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class MagicUndefineGlobal method execute.

@Override
public StellarResult execute(String command, StellarShellExecutor executor) {
    StellarResult result;
    String variable = StringUtils.trimToEmpty(command.substring(MAGIC_UNDEFINE.length()));
    if (StringUtils.isNotBlank(variable)) {
        // remove the variable from the globals
        Map<String, Object> globals = executor.getGlobalConfig();
        globals.remove(variable);
        result = noop();
    } else {
        result = error(String.format("%s expected name of global, got '%s'", MAGIC_UNDEFINE, variable));
    }
    return result;
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult)

Example 14 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class AssignmentCommandTest method testAssignNull.

@Test
public void testAssignNull() {
    StellarResult result = command.execute("x := NULL", executor);
    // validate the result
    assertTrue(result.isSuccess());
    assertTrue(result.isValueNull());
    // validate assignment
    assertNull(executor.getState().get("x").getResult());
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Test(org.junit.Test)

Example 15 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class AssignmentCommandTest method testAssignmentWithVar.

@Test
public void testAssignmentWithVar() {
    // define a variable
    executor.assign("x", 10, Optional.empty());
    // execute the assignment expression
    StellarResult result = command.execute("y := x + 2", executor);
    // validate the result
    assertTrue(result.isSuccess());
    assertTrue(result.getValue().isPresent());
    assertEquals(12, result.getValue().get());
    // validate assignment
    assertEquals(10, executor.getState().get("x").getResult());
    assertEquals(12, executor.getState().get("y").getResult());
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Test(org.junit.Test)

Aggregations

StellarResult (org.apache.metron.stellar.common.shell.StellarResult)28 Test (org.junit.Test)23 Optional (java.util.Optional)2 StellarAssignment (org.apache.metron.stellar.common.StellarAssignment)2 Spliterator (java.util.Spliterator)1 Function (java.util.function.Function)1 StreamSupport (java.util.stream.StreamSupport)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StellarResult.error (org.apache.metron.stellar.common.shell.StellarResult.error)1 StellarResult.success (org.apache.metron.stellar.common.shell.StellarResult.success)1 StellarShellExecutor (org.apache.metron.stellar.common.shell.StellarShellExecutor)1 StellarFunctionInfo (org.apache.metron.stellar.dsl.StellarFunctionInfo)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1