use of org.drools.model.impl.Exchange in project drools by kiegroup.
the class SendReceiveTest method testAsync.
@Test
public void testAsync() {
Global<List> messages = globalOf(List.class, "defaultpkg", "messages");
Variable<Integer> length = declarationOf(Integer.class);
Exchange<String> exchange = exchangeOf(String.class);
Rule send = rule("send").build(send(exchange).message(() -> {
try {
Thread.sleep(1_000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "Hello World!";
}));
Rule receive = rule("receive").build(pattern(length), receive(exchange).expr(length, (s, l) -> s.length() > l), on(exchange, length, messages).execute((s, l, m) -> m.add("received message '" + s + "' longer than " + l)));
Model model = new ModelImpl().addRule(send).addRule(receive).addGlobal(messages);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
List<String> list = new ArrayList<>();
ksession.setGlobal("messages", list);
assertEquals(1, AsyncMessagesCoordinator.get().getListeners().size());
ksession.insert(10);
new Thread(() -> ksession.fireUntilHalt()).start();
try {
Thread.sleep(2_000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ksession.halt();
ksession.dispose();
assertEquals(1, list.size());
assertEquals("received message 'Hello World!' longer than 10", list.get(0));
assertEquals(0, AsyncMessagesCoordinator.get().getListeners().size());
}
use of org.drools.model.impl.Exchange in project drools by kiegroup.
the class SendReceiveTest method testAsyncWith2KBase.
@Test
public void testAsyncWith2KBase() {
Exchange<String> exchange = exchangeOf(String.class);
Rule send = rule("send").build(send(exchange).message(() -> {
try {
Thread.sleep(1_000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "Hello World!";
}));
Variable<Integer> length = declarationOf(Integer.class);
Global<List> messages = globalOf(List.class, "defaultpkg", "messages");
Rule receive = rule("receive").build(pattern(length), receive(exchange).expr(length, (s, l) -> s.length() > l), on(exchange, length, messages).execute((s, l, m) -> m.add("received message '" + s + "' longer than " + l)));
Model model1 = new ModelImpl().addRule(send);
KieBase kieBase1 = KieBaseBuilder.createKieBaseFromModel(model1);
KieSession ksession1 = kieBase1.newKieSession();
Model model2 = new ModelImpl().addRule(receive).addGlobal(messages);
KieBase kieBase2 = KieBaseBuilder.createKieBaseFromModel(model2);
KieSession ksession2 = kieBase2.newKieSession();
List<String> list = new ArrayList<>();
ksession2.setGlobal("messages", list);
ksession2.insert(10);
new Thread(() -> ksession2.fireUntilHalt()).start();
new Thread(() -> ksession1.fireUntilHalt()).start();
try {
Thread.sleep(2_000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ksession1.halt();
ksession1.dispose();
ksession2.halt();
ksession2.dispose();
assertEquals(1, list.size());
assertEquals("received message 'Hello World!' longer than 10", list.get(0));
}
use of org.drools.model.impl.Exchange in project drools by kiegroup.
the class KiePackagesBuilder method addPatternForVariable.
private Pattern addPatternForVariable(RuleContext ctx, GroupElement group, Variable patternVariable, Condition.Type type) {
Pattern pattern = null;
// If the variable is already bound to the result of previous accumulate result pattern, then find it.
if (patternVariable instanceof org.drools.model.Declaration) {
org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
if (decl.getSource() == null) {
Accumulate accSource = ctx.getAccumulateSource(patternVariable);
if (accSource != null) {
for (RuleConditionElement element : group.getChildren()) {
if (element instanceof Pattern && ((Pattern) element).getSource() == accSource) {
pattern = (Pattern) element;
break;
}
}
}
}
}
PatternSource priorSource = null;
if (pattern != null && type == Condition.Type.ACCUMULATE) {
// so it would be potentially tricky if this was a multi var, with multiple bindings.
if (pattern.getSource() instanceof SingleAccumulate) {
group.getChildren().remove(pattern);
priorSource = pattern.getSource();
pattern = null;
}
}
if (pattern == null) {
pattern = new Pattern(ctx.getNextPatternIndex(), // tupleIndex will be set by ReteooBuilder
0, // tupleIndex will be set by ReteooBuilder
0, getObjectType(patternVariable), patternVariable.getName(), true);
pattern.setSource(priorSource);
}
if (patternVariable instanceof org.drools.model.Declaration) {
org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
if (decl.getSource() != null) {
if (decl.getSource() instanceof EntryPoint) {
pattern.setSource(new EntryPointId(((EntryPoint) decl.getSource()).getName()));
} else if (decl.getSource() instanceof WindowReference) {
WindowReference<?> window = (WindowReference) decl.getSource();
if (!ctx.getPkg().getWindowDeclarations().containsKey(window.getName())) {
createWindowReference(ctx, window);
}
pattern.setSource(new org.drools.core.rule.WindowReference(window.getName()));
} else if (decl.getSource() instanceof From) {
pattern.setSource(buildFrom(ctx, pattern, (From) decl.getSource()));
} else if (decl.getSource() instanceof UnitData) {
UnitData unitData = (UnitData) decl.getSource();
pattern.setSource(new EntryPointId(ctx.getRule().getRuleUnitClassName() + "." + unitData.getName()));
} else {
throw new UnsupportedOperationException("Unknown source: " + decl.getSource());
}
}
if (decl.getWindow() != null) {
pattern.addBehavior(createWindow(decl.getWindow()));
ctx.setNeedStreamMode();
}
} else if (patternVariable instanceof Exchange) {
if (type == Condition.Type.SENDER) {
Function0 supplier = ((Exchange) patternVariable).getMessageSupplier();
DataProvider provider = new LambdaDataProvider(x -> supplier.apply(), false);
pattern.setSource(new AsyncSend(pattern, patternVariable.getName(), provider));
} else if (type == Condition.Type.RECEIVER) {
pattern.setSource(new AsyncReceive(pattern, patternVariable.getName()));
} else {
throw new UnsupportedOperationException();
}
}
ctx.registerPattern(patternVariable, pattern);
return pattern;
}
Aggregations