summaryrefslogtreecommitdiffstats
path: root/test/src/test-extensions.cpp
blob: 626f8e55bb5a9a0e593f798a648babca938643ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "uscxml/Interpreter.h"
#include "uscxml/interpreter/InterpreterMonitor.h"
#include "uscxml/interpreter/InterpreterImpl.h"
#include "uscxml/interpreter/BasicEventQueue.h"
#include "uscxml/interpreter/BasicDelayedEventQueue.h"
#include "uscxml/PausableDelayedEventQueue.h"
#include "uscxml/ExtendedLuaDataModel.h"

#include <event2/util.h>                // for evutil_socket_t

#include <chrono>
#include <mutex>
#include <iostream>

using namespace uscxml;
class MyPausableDelayedEventQueue;

std::shared_ptr<MyPausableDelayedEventQueue> nestedDelayQueue;

class MyPausableDelayedEventQueue : public PausableDelayedEventQueue {
	MyPausableDelayedEventQueue(DelayedEventQueueCallbacks* callbacks) : PausableDelayedEventQueue(callbacks) {
	}

	std::shared_ptr<DelayedEventQueueImpl> create(DelayedEventQueueCallbacks* callbacks) {
		// remember as nestedDelayQueue in global scope
		nestedDelayQueue = std::shared_ptr<MyPausableDelayedEventQueue>(new MyPausableDelayedEventQueue(callbacks));
		return nestedDelayQueue;
	}
};


// from issue 96:
// https://github.com/tklab-tud/uscxml/issues/96

static const char *customDelayedEQ =
    "<scxml initial=\"StateShape1\" name=\"ScxmlShape1\" version=\"1.0\" xmlns=\"http://www.w3.org/2005/07/scxml\">"
    "   <state id=\"StateShape1\">"
    "       <invoke autoforward=\"true\" type=\"scxml\">"
    "			<content>"
    "				<scxml initial=\"StateShape1\" name=\"Include\" version=\"1.0\" xmlns=\"http://www.w3.org/2005/07/scxml\">"
    "					<state id=\"StateShape1\">"
    "						<transition event=\"error.*\" target=\"FinalShape1\"/>"
    "						<state id=\"Step1\">"
    "							<onentry>"
    "								<send delay=\"1s\" event=\"Timer2\"/>"
    "								<log expr=\"'Hello from step1'\"/>"
    "							</onentry>"
    "							<transition event=\"Timer2\" target=\"Step2\"/>"
    "						</state>"
    "						<state id=\"Step2\">"
    "							<onentry>"
    "								<send delay=\"1s\" event=\"Timer2\"/>"
    "								<log expr=\"'Hello from step2'\"/>"
    "							</onentry>"
    "							<transition event=\"Timer2\" target=\"Step1\"/>"
    "						</state>"
    "					</state>"
    "					<final id=\"FinalShape1\"/>"
    "				</scxml>"
    "			</content>"
    "		</invoke>"
    "	</state>"
    "	<final id=\"FinalShape1\"/>"
    "</scxml>";

bool testPausableEventQueue() {
	Interpreter interpreter = Interpreter::fromXML(customDelayedEQ, "");

	PausableDelayedEventQueue* queue = new PausableDelayedEventQueue(interpreter.getImpl().get());
	ActionLanguage lang;
	lang.delayQueue = DelayedEventQueue(std::shared_ptr<DelayedEventQueueImpl>(queue));

	interpreter.setActionLanguage(lang);

	StateTransitionMonitor mon;
	mon.copyToInvokers(true);
	interpreter.addMonitor(&mon);

	size_t iterations = 10;
	int now = time(NULL);
	int startedAt = time(NULL);

	InterpreterState state = InterpreterState::USCXML_UNDEF;
	while (state != USCXML_FINISHED && now - startedAt < 5) {
		state = interpreter.step(200);
		now = time(NULL);

		if (nestedDelayQueue) {
			/*
			 * As soon as we have the nested event queue instantiated, we pause and resume it
			 * We pause for 500ms, and run for 500ms. This will effectively double the time required
			 * for delayed events.
			 * This would usually done in another thread ..
			 */
			std::cout << "<- pausing" << std::endl;
			nestedDelayQueue->pause();
			std::this_thread::sleep_for(std::chrono::milliseconds(500));
			std::cout << "-> continuing" << std::endl;
			nestedDelayQueue->resume();
			std::this_thread::sleep_for(std::chrono::milliseconds(500));

			if (iterations-- == 0)
				return true;
		}
	}

	return true;

}


static const char *customLuaExtension =
    "<scxml datamodel=\"lua\">"
    "  <script>"
    "    GetSomeResult();"
    "  </script>"
    "  <state/>"
    "</scxml>"
    ;

bool testLuaExtension() {
	Factory::getInstance()->registerDataModel(new ExtendedLuaDataModel());
	Interpreter exLua = Interpreter::fromXML(customLuaExtension, "");
	InterpreterState state;

	while ((state = exLua.step(0)) != USCXML_IDLE) {}

	return true;
}

int main(int argc, char** argv) {
	testLuaExtension();
//	testPausableEventQueue();
}