package behave.execution;

import behave.models.Node;
import behave.models.Types;
import behave.tools.Log;

import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;

public class Executor {
    private Node m_root;
    private ExecutionContext m_context;
    private Timer m_timer;

    public void initialize(Node root) {
        initialize(root, new ExecutionContext());
    }

    public void initialize(Node root, ExecutionContext context) {
        m_context = context;
        m_root = root;
    }

    public void start(long period, long delay) {
        //In this early phase we use a simple Timer to execute the traversal.
        //This tick could come from external source also, such as some event/game-loop
        //TODO create separate classes, TimerExecutor and ExternalExecutor
        m_timer = new Timer();
        m_root.initialize(m_context);
        m_timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                //let's assume timer is deterministic, but TODO measure the step using System.currentTimeInMillis
                Log.debug("Executor running one Tick");
                m_context.setLastTimeStep(period);
                Types.Status status = m_root.tick(m_context);
                if (status != Types.Status.Running) {
                    Log.info("Root node has finished with status " + status + ". Ending execution.");
                    stop();
                }
            }
        }, delay, period);
    }

    public void stop() {
        m_timer.cancel();
    }
}

@@ -293,16 +293,58 @@
 m_timer;
+%0A    private long m_lastTickTimestamp = 0;
 %0A%0A    pu
@@ -848,16 +848,75 @@
 xecutor%0A
+        m_lastTickTimestamp = System.currentTimeMillis();%0A%0A
         
@@ -1104,114 +1104,100 @@
     
-//let's assume timer is deterministic, but TODO measure the step using System.currentTimeInMillis
+tickNode();%0A            %7D%0A        %7D, delay, period);
 %0A    
+%7D%0A%0A
     
+protected void tickNode() %7B%0A
     
@@ -1240,32 +1240,97 @@
 Tick%22);%0A        
+long period = System.currentTimeMillis() - m_lastTickTimestamp;%0A%0A
         m_contex
@@ -1356,24 +1356,16 @@
 eriod);%0A
-        
         
@@ -1418,24 +1418,16 @@
         
-   
-     
 if (stat
@@ -1468,24 +1468,16 @@
         
-     
-   
 Log.info
@@ -1566,21 +1566,37 @@
     
-        stop(
+Log.info(m_context.toString()
 );%0A 
@@ -1602,34 +1602,32 @@
             
-    %7D%0A    
+stop();%0A
         %7D%0A  
@@ -1632,32 +1632,63 @@
         
-%7D, delay, period
+m_lastTickTimestamp = System.currentTimeMillis(
 );%0A    %7D

Replace hardcoded period with properly calculated one in Executor