package org.graphwalker.core.machine;

/*
 * #%L
 * GraphWalker Core
 * %%
 * Copyright (C) 2011 - 2014 GraphWalker
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.script.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @author Nils Olsson
 */
public final class SimpleMachine implements Machine {

    private static final Logger logger = LoggerFactory.getLogger(SimpleMachine.class);

    private final static String DEFAULT_SCRIPT_LANGUAGE = "JavaScript";
    private final List<ExecutionContext> contexts = new ArrayList<>();
    private final ScriptEngine scriptEngine;

    private ExecutionContext currentContext;

    public SimpleMachine(ExecutionContext context) {
        this(Arrays.asList(context));
    }

    public SimpleMachine(List<ExecutionContext> contexts) {
        this.contexts.addAll(contexts);
        this.scriptEngine = new ScriptEngineManager().getEngineByName(DEFAULT_SCRIPT_LANGUAGE);
        this.currentContext = contexts.get(0);
    }

    @Override
    public Context getNextStep() {
        currentContext.getProfiler().stop();
        currentContext.getPathGenerator().getNextStep(currentContext);
MDC.put("trace", UUID.randomUUID().toString());
logger.info("Execute {}", currentContext.getCurrentElement().getName());
        currentContext.getProfiler().start();
        execute(currentContext.getCurrentElement().getName());
        return currentContext;
    }

    @Override
    public boolean hasNextStep() {
        if (!currentContext.getPathGenerator().hasNextStep(currentContext)) {
            return true;
        }
        // Find another context to execute!?
        // ...
        return false;
    }

    private void execute(String name) {
        if (null != name && !"".equals(name)) {
            try {
                scriptEngine.setContext(currentContext);
                Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
                bindings.put("impl", currentContext);
                scriptEngine.eval( "impl." + name + "()");
            } catch (ScriptException e) {
                throw new MachineException(e);
            }
        }
    }
}

@@ -2202,32 +2202,88 @@
 getNextStep() %7B%0A
+        MDC.put(%22trace%22, UUID.randomUUID().toString());%0A
         currentC
@@ -2386,129 +2386,8 @@
 t);%0A
-MDC.put(%22trace%22, UUID.randomUUID().toString());%0Alogger.info(%22Execute %7B%7D%22, currentContext.getCurrentElement().getName());%0A
     
@@ -2570,32 +2570,88 @@
 hasNextStep() %7B%0A
+        MDC.put(%22trace%22, UUID.randomUUID().toString());%0A
         if (!cur
@@ -2872,24 +2872,105 @@
 ing name) %7B%0A
+        logger.info(%22Execute %7B%7D%22, currentContext.getCurrentElement().getName());%0A
         if (

Add logging with a Mapped Diagnostic Context