-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
PseudoClockEventsTest.java
161 lines (135 loc) · 5.27 KB
/
PseudoClockEventsTest.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2007 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on Dec 14, 2007
*/
package org.drools.integrationtests;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.drools.CommonTestMethodBase;
import org.drools.StockTick;
import org.drools.StockTickInterface;
import org.drools.compiler.DroolsParserException;
import org.drools.time.impl.PseudoClockScheduler;
import org.junit.Test;
import org.kie.KnowledgeBase;
import org.kie.KnowledgeBaseConfiguration;
import org.kie.KnowledgeBaseFactory;
import org.kie.conf.EventProcessingOption;
import org.kie.event.rule.AfterMatchFiredEvent;
import org.kie.event.rule.AgendaEventListener;
import org.kie.runtime.KnowledgeSessionConfiguration;
import org.kie.runtime.StatefulKnowledgeSession;
import org.kie.runtime.conf.ClockTypeOption;
import org.kie.time.SessionClock;
/**
* Tests related to the pseudo session clock
*/
public class PseudoClockEventsTest extends CommonTestMethodBase {
int evalFirePseudoClockStockCount = 15;
private static final String evalFirePseudoClockDeclaration = "package org.drools\n" +
"\n" +
"declare StockTick\n" +
" @role( event )\n" +
"end\n\n";
private static final String evalFirePseudoClockRuleA = "rule A\n"
+
"when\n"
+
" $a: StockTick( $priceA: price )\n"
+
" $b: StockTick( $priceA < price )\n"
+
"then \n"
+
" System.out.println(\"Rule A fired by thread \" + Thread.currentThread().getName() + \": \" + $a + \", \" + $b);\n"
+
"end\n" +
"";
private static final String evalFirePseudoClockRuleB = "rule B\n" +
"when\n" +
" $a: StockTick()\n" +
" not( StockTick( this after[1,10s] $a ) )\n" +
"then \n" +
" System.out.println(\"Rule B fired by thread \" + Thread.currentThread().getName());\n" +
"end\n" +
"";
@Test(timeout = 6000)
public void testEvenFirePseudoClockRuleA() throws Exception {
AgendaEventListener ael = mock(AgendaEventListener.class);
processStocks(evalFirePseudoClockStockCount, ael,
evalFirePseudoClockDeclaration + evalFirePseudoClockRuleA);
verify(ael,
times(evalFirePseudoClockStockCount * (evalFirePseudoClockStockCount - 1) / 2)).afterMatchFired(
any(AfterMatchFiredEvent.class));
}
@Test(timeout = 6000)
public void testEvenFirePseudoClockRuleB() throws Exception {
AgendaEventListener ael = mock(AgendaEventListener.class);
processStocks(evalFirePseudoClockStockCount, ael,
evalFirePseudoClockDeclaration + evalFirePseudoClockRuleB);
verify(ael,
times(evalFirePseudoClockStockCount - 1)).afterMatchFired(
any(AfterMatchFiredEvent.class));
}
@Test//(timeout = 60000)
public void testEvenFirePseudoClockRulesAB() throws Exception {
AgendaEventListener ael = mock(AgendaEventListener.class);
processStocks(evalFirePseudoClockStockCount, ael,
evalFirePseudoClockDeclaration + evalFirePseudoClockRuleA + evalFirePseudoClockRuleB);
final int expectedActivationCount = evalFirePseudoClockStockCount * (evalFirePseudoClockStockCount - 1) / 2
+ evalFirePseudoClockStockCount - 1;
verify(ael,
times(expectedActivationCount)).afterMatchFired(
any(AfterMatchFiredEvent.class));
}
private int processStocks(int stockCount, AgendaEventListener agendaEventListener, String drlContentString)
throws DroolsParserException, IOException, Exception {
KnowledgeBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KnowledgeBase kbase = loadKnowledgeBaseFromString(kconf, drlContentString);
KnowledgeSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
ksessionConfig.setProperty("keep.reference", "true");
final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(ksessionConfig,
null);
ksession.addEventListener(agendaEventListener);
PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock();
Runnable fireUntilHaltRunnable = new Runnable() {
public void run() {
ksession.fireUntilHalt();
}
};
Thread fireUntilHaltThread = new Thread(fireUntilHaltRunnable, "Engine's thread");
fireUntilHaltThread.start();
Thread.currentThread().setName( "Feeding thread" );
for (int stIndex = 1; stIndex <= stockCount; stIndex++) {
clock.advanceTime(20, TimeUnit.SECONDS);
final StockTickInterface st = new StockTick(stIndex,
"RHT",
100 * stIndex,
1000);
ksession.insert(st);
Thread.sleep(1);
}
Thread.sleep(2000);
ksession.halt();
return stockCount;
}
}