forked from hflicka/arden2bytecode
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTestCompilerImpl.java
More file actions
134 lines (115 loc) · 4.31 KB
/
TestCompilerImpl.java
File metadata and controls
134 lines (115 loc) · 4.31 KB
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
package arden.tests.specification.testcompiler.impl;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import arden.compiler.CompilerException;
import arden.runtime.ArdenDuration;
import arden.runtime.ArdenEvent;
import arden.runtime.ArdenTime;
import arden.runtime.ArdenValue;
import arden.runtime.MedicalLogicModule;
import arden.runtime.evoke.CallTrigger;
import arden.tests.specification.testcompiler.ArdenVersion;
import arden.tests.specification.testcompiler.TestCompiler;
import arden.tests.specification.testcompiler.TestCompilerCompiletimeException;
import arden.tests.specification.testcompiler.TestCompilerDelayedMessage;
import arden.tests.specification.testcompiler.TestCompilerException;
import arden.tests.specification.testcompiler.TestCompilerMappings;
import arden.tests.specification.testcompiler.TestCompilerResult;
import arden.tests.specification.testcompiler.TestCompilerRuntimeException;
import arden.tests.specification.testcompiler.TestCompilerSettings;
public class TestCompilerImpl implements TestCompiler {
private arden.compiler.Compiler compiler = new arden.compiler.Compiler();
private TestCompilerMappings mappings = new TestCompilerMappings(
TestContext.INTERFACE_MAPPING,
TestContext.MESSAGE_MAPPING,
TestContext.DESTINATION_MAPPING,
TestContext.READ_MAPPING,
TestContext.READ_MULTIPLE_MAPPING);
private TestCompilerSettings settings = new TestCompilerSettings(
ArdenVersion.V2_5, ArdenVersion.V1,
true, true, false);
@Override
public TestCompilerSettings getSettings() {
return settings;
}
@Override
public TestCompilerMappings getMappings() {
return mappings;
}
@Override
public void compile(String code) throws TestCompilerCompiletimeException {
try {
compiler.compile(new StringReader(code));
} catch (CompilerException e) {
throw new TestCompilerCompiletimeException(e);
} catch (IOException e) {
throw new TestCompilerCompiletimeException(e);
}
}
@Override
public TestCompilerResult compileAndRun(String code) throws TestCompilerException {
// compile
List<MedicalLogicModule> compiledMlms = new ArrayList<>();
try {
compiledMlms.addAll(compiler.compile(new StringReader(code)));
} catch (CompilerException e) {
throw new TestCompilerCompiletimeException(e);
} catch (IOException e) {
throw new TestCompilerCompiletimeException(e);
}
// run and save return values
MedicalLogicModule firstMlm = compiledMlms.get(0);
TestContext context = new TestContext(compiledMlms);
TestCompilerResult result = new TestCompilerResult();
ArdenValue[] returnValues;
try {
returnValues = firstMlm.run(context, null, new CallTrigger());
} catch (Exception e) {
throw new TestCompilerRuntimeException(e);
} catch (Error e) {
throw new TestCompilerRuntimeException(e);
}
if (returnValues != null) {
for (ArdenValue returnValue : returnValues) {
String stringValue = new NormalizedArdenValue(returnValue).toString();
result.returnValues.add(stringValue);
}
}
result.messages.addAll(context.getMessages());
return result;
}
@Override
public TestCompilerDelayedMessage[] compileAndRunForEvent(String code, String eventMapping, int messagesToCollect)
throws TestCompilerException {
// compile
List<MedicalLogicModule> compiledMlms = new ArrayList<>();
try {
compiledMlms.addAll(compiler.compile(new StringReader(code)));
} catch (CompilerException e) {
throw new TestCompilerCompiletimeException(e);
} catch (IOException e) {
throw new TestCompilerCompiletimeException(e);
}
// create constant time for deterministic tests
Calendar calendar = new GregorianCalendar();
calendar.clear();
ArdenTime startTime = new ArdenTime(calendar.getTimeInMillis());
ArdenEvent event = new ArdenEvent(eventMapping, calendar.getTimeInMillis());
TestEngine engine = new TestEngine(compiledMlms, startTime);
engine.call(event, ArdenDuration.ZERO, 50);
// collect messages
List<TestCompilerDelayedMessage> messages = new ArrayList<>();
try {
while (messages.size() < messagesToCollect) {
messages.add(engine.getNextDelayedMessage());
}
} catch (Exception e) {
throw new TestCompilerRuntimeException(e);
}
return messages.toArray(new TestCompilerDelayedMessage[messages.size()]);
}
}