forked from hflicka/arden2bytecode
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTestContext.java
More file actions
142 lines (124 loc) · 5.05 KB
/
TestContext.java
File metadata and controls
142 lines (124 loc) · 5.05 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
135
136
137
138
139
140
141
142
package arden.tests.specification.testcompiler.impl;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import arden.runtime.ArdenEvent;
import arden.runtime.ArdenList;
import arden.runtime.ArdenNumber;
import arden.runtime.ArdenRunnable;
import arden.runtime.ArdenString;
import arden.runtime.ArdenValue;
import arden.runtime.DatabaseQuery;
import arden.runtime.ExecutionContext;
import arden.runtime.ExecutionContextHelpers;
import arden.runtime.MedicalLogicModule;
import arden.runtime.evoke.Trigger;
import arden.tests.specification.testcompiler.TestCompiler;
/**
* See the requirements at the {@link TestCompiler} interface.
*/
public class TestContext extends ExecutionContext {
static final String INTERFACE_MAPPING = "test interface";
static final String MESSAGE_MAPPING = "test message";
static final String READ_MAPPING = "select id from database";
static final String READ_MULTIPLE_MAPPING = "select id,value from database";
static final String DESTINATION_MAPPING = "dest";
// TODO DatabaseQuery should automatically sort by time
private final ArdenValue[] values1 = new ArdenValue[] {
ArdenNumber.create(5, new GregorianCalendar(1970, Calendar.JANUARY, 1).getTimeInMillis()),
ArdenNumber.create(3, new GregorianCalendar(1990, Calendar.JANUARY, 1).getTimeInMillis()),
ArdenNumber.create(2, new GregorianCalendar(1990, Calendar.JANUARY, 2).getTimeInMillis()),
ArdenNumber.create(4, new GregorianCalendar(1990, Calendar.JANUARY, 3).getTimeInMillis()),
ArdenNumber.create(1, new GregorianCalendar(2000, Calendar.JANUARY, 1).getTimeInMillis()) };
private final ArdenValue[] values2 = new ArdenValue[] {
new ArdenString("e", new GregorianCalendar(1970, Calendar.JANUARY, 1).getTimeInMillis()),
new ArdenString("c", new GregorianCalendar(1990, Calendar.JANUARY, 1).getTimeInMillis()),
new ArdenString("b", new GregorianCalendar(1990, Calendar.JANUARY, 2).getTimeInMillis()),
new ArdenString("d", new GregorianCalendar(1990, Calendar.JANUARY, 3).getTimeInMillis()),
new ArdenString("a", new GregorianCalendar(2000, Calendar.JANUARY, 1).getTimeInMillis()) };
private List<String> messages = new LinkedList<String>();
private List<MedicalLogicModule> mlms;
public TestContext(List<MedicalLogicModule> mlms) {
this.mlms = mlms;
}
@Override
public MedicalLogicModule findModule(String name, String institution) {
MedicalLogicModule[] mlmArray = mlms.toArray(new MedicalLogicModule[mlms.size()]);
MedicalLogicModule foundModule = ExecutionContextHelpers.findModule(name, institution, mlmArray, null);
if (foundModule == null) {
throw new RuntimeException("MLM <" + name + "> from institution <" + institution + "> not found");
}
return foundModule;
}
@Override
public MedicalLogicModule[] findModules(ArdenEvent event) {
List<MedicalLogicModule> foundModules = new ArrayList<>();
for (MedicalLogicModule mlm : mlms) {
try {
for (Trigger trigger : mlm.getTriggers(this)) {
if (trigger.runOnEvent(event)) {
foundModules.add(mlm);
}
}
} catch (InvocationTargetException e) {
throw new RuntimeException("Could not load MLM " + mlm.getName() + " for event " + event.name);
}
}
return foundModules.toArray(new MedicalLogicModule[foundModules.size()]);
}
@Override
public ArdenRunnable findInterface(MedicalLogicModule mlm, String mapping) {
if (INTERFACE_MAPPING.equals(mapping)) {
return new ArdenRunnable() {
@Override
public ArdenValue[] run(ExecutionContext context, ArdenValue[] arguments, Trigger trigger)
throws InvocationTargetException {
// RETURN (args[0] + args[1], args[0] * args[1]);
ArdenNumber firstArg = (ArdenNumber) arguments[0];
ArdenNumber secondArg = (ArdenNumber) arguments[1];
ArdenNumber firstReturn = new ArdenNumber(firstArg.value + secondArg.value);
ArdenNumber secondReturn = new ArdenNumber(firstArg.value * secondArg.value);
return new ArdenValue[] { firstReturn, secondReturn };
}
};
}
return super.findInterface(mlm, mapping);
}
@Override
public ArdenEvent getEvent(MedicalLogicModule mlm, String mapping) {
return new ArdenEvent(mapping, getCurrentTime().value);
}
@Override
public DatabaseQuery createQuery(MedicalLogicModule mlm, String mapping) {
if (READ_MAPPING.equals(mapping)) {
return new DatabaseQuery() {
@Override
public ArdenValue[] execute() {
// single column
return new ArdenValue[] { new ArdenList(values1) };
}
};
} else if (READ_MULTIPLE_MAPPING.equals(mapping)) {
return new DatabaseQuery() {
@Override
public ArdenValue[] execute() {
// two columns
return new ArdenValue[] { new ArdenList(values1), new ArdenList(values2) };
}
};
}
return DatabaseQuery.NULL;
}
@Override
public void write(ArdenValue message, ArdenValue destination, double urgency) {
// save messages
String stringMessage = ((ArdenString) message).value;
messages.add(stringMessage);
}
public List<String> getMessages() {
return messages;
}
}