Example usage for org.apache.commons.scxml2 SCXMLExecutor go

List of usage examples for org.apache.commons.scxml2 SCXMLExecutor go

Introduction

In this page you can find the example usage for org.apache.commons.scxml2 SCXMLExecutor go.

Prototype

public void go() throws ModelException 

Source Link

Document

Initiate state machine execution.

Usage

From source file:com.github.woonsan.commons.scxml.examples.helloworld.Main.java

public static void main(String[] args) throws Exception {
    // evaluator instance which is used by SCXML engine to evaluate expressions in SCXML
    Evaluator evaluator = new JexlEvaluator();
    // engine to execute the scxml instance
    SCXMLExecutor executor = new SCXMLExecutor(evaluator, null, new SimpleErrorReporter());

    // parse SCXML URL into SCXML model
    SCXML scxml = SCXMLReader.read(SCXML);
    // set state machine (scxml instance) to execute
    executor.setStateMachine(scxml);//from   w  w  w .ja  va 2 s.c o  m

    // create root context storing variables and being used by evaluator
    Context rootContext = evaluator.newContext(null);
    // set the root context for the engine
    executor.setRootContext(rootContext);

    // initiate the execution of the state machine
    executor.go();
}

From source file:com.github.woonsan.commons.scxml.examples.stopwatch.Main.java

public static void main(String[] args) throws Exception {
    // evaluator instance which is used by SCXML engine to evaluate expressions in SCXML
    Evaluator evaluator = new JexlEvaluator();
    // engine to execute the scxml instance
    SCXMLExecutor executor = new SCXMLExecutor(evaluator, null, new SimpleErrorReporter());

    // parse SCXML URL into SCXML model
    SCXML scxml = SCXMLReader.read(SCXML);
    // set state machine (scxml instance) to execute
    executor.setStateMachine(scxml);//from www. ja  v a2 s .c o m

    // create root context storing variables and being used by evaluator
    Context rootContext = evaluator.newContext(null);

    // create stopWatch object and add it to rootContext
    // to be able to script with that in SCXML.
    StopWatch stopWatch = new StopWatch();
    rootContext.set("stopWatch", stopWatch);

    // set the root context for the engine
    executor.setRootContext(rootContext);

    // initiate the execution of the state machine
    executor.go();

    new StopWatchFrame(executor);
}

From source file:org.onehippo.repository.scxml.ActionAndResultActionTest.java

@Test
public void testActionAndResultAction() throws Exception {
    MockRepositorySCXMLRegistry registry = new MockRepositorySCXMLRegistry();

    RepositorySCXMLExecutorFactory execFactory = new RepositorySCXMLExecutorFactory();
    execFactory.initialize();/*from w  ww  . j a v  a  2 s  .  c  o m*/

    MockNode scxmlConfigNode = registry.createConfigNode();
    MockNode scxmlDefNode = registry.addScxmlNode(scxmlConfigNode, "helloScxml", SCXML_HELLO_ACTION_AND_RESULT);
    registry.addCustomAction(scxmlDefNode, "http://www.onehippo.org/cms7/repository/scxml", "action",
            ActionAction.class.getName());
    registry.addCustomAction(scxmlDefNode, "http://www.onehippo.org/cms7/repository/scxml", "result",
            ResultAction.class.getName());
    registry.setUp(scxmlConfigNode);

    SCXMLDefinition helloScxml = registry.getSCXMLDefinition("helloScxml");
    assertNotNull(helloScxml);

    SCXMLExecutor helloExec = execFactory.createSCXMLExecutor(helloScxml);

    SCXMLWorkflowContext workflowContext = new SCXMLWorkflowContext(helloScxml.getId(),
            new MockWorkflowContext("testuser"));
    helloExec.getRootContext().set(SCXMLWorkflowContext.SCXML_CONTEXT_KEY, workflowContext);
    helloExec.getRootContext().set("world", Boolean.TRUE);
    helloExec.go();

    assertTrue(workflowContext.getActions().get("hello"));
    assertEquals(workflowContext, workflowContext.getResult());
}

From source file:org.onehippo.repository.scxml.SCXMLStrictErrorReporterTest.java

@Test
public void testLoadWithErrorJexlScripts() throws Exception {
    MockNode scxmlConfigNode = registry.createConfigNode();
    registry.addScxmlNode(scxmlConfigNode, "hello-with-error-jexl-scripts",
            SCXML_HELLO_WITH_ERROR_JEXL_SCRIPTS);
    registry.setUp(scxmlConfigNode);/*from w  ww .  j  a  v a 2 s . c o m*/

    SCXMLDefinition helloScxml = registry.getSCXMLDefinition("hello-with-error-jexl-scripts");
    SCXMLExecutor helloExec = execFactory.createSCXMLExecutor(helloScxml);

    try {
        helloExec.go();
        fail("SCML should have failed loading");
    } catch (SCXMLExecutionError e) {
        assertTrue(e.getErrorDetail()
                .contains("in /hippo:moduleconfig/hipposcxml:definitions/hello-with-error-jexl-scripts"));
    }
}