use of org.drools.core.impl.RuleBase in project drools by kiegroup.
the class DefaultTemplateRuleBaseTest method testSimpleTemplate.
@SuppressWarnings("unchecked")
@Test
public void testSimpleTemplate() throws Exception {
TemplateContainer tc = new TemplateContainer() {
private Column[] columns = new Column[] { new LongColumn("column1"), new LongColumn("column2"), new StringColumn("column3") };
public Column[] getColumns() {
return columns;
}
public String getHeader() {
return null;
}
public Map<String, RuleTemplate> getTemplates() {
Map<String, RuleTemplate> templates = new HashMap<String, RuleTemplate>();
RuleTemplate ruleTemplate = new RuleTemplate("template1", this);
ruleTemplate.addColumn("column1 == 10");
ruleTemplate.addColumn("column2 < 5 || > 20");
ruleTemplate.addColumn("column3 == \"xyz\"");
templates.put("template1", ruleTemplate);
return templates;
}
public Column getColumn(String name) {
return columns[Integer.parseInt(name.substring(6)) - 1];
}
};
DefaultTemplateRuleBase ruleBase = new DefaultTemplateRuleBase(tc);
InternalKnowledgePackage[] packages = ((RuleBase) ruleBase.newStatefulSession().getKieBase()).getPackages();
assertEquals(1, packages.length);
Map<String, Class<?>> globals = packages[0].getGlobals();
assertEquals(DefaultGenerator.class, globals.get("generator"));
Collection<org.kie.api.definition.rule.Rule> rules = packages[0].getRules();
assertEquals(1, rules.size());
assertEquals("template1", rules.iterator().next().getName());
GroupElement lhs = ((RuleImpl) rules.iterator().next()).getLhs();
// when
// r : Row()
// column1 : Column(name == "column1")
// exists LongCell(row == r, column == column1, value == 10)
// column2 : Column(name == "column2")
// exists LongCell(row == r, column == column2, value < 5 | > 20)
// column3 : Column(name == "column3")
// exists StringCell(row == r, column == column3, value == "xyz")
assertEquals(7, lhs.getChildren().size());
org.drools.core.rule.Pattern pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(1);
assertEquals(1, pattern.getConstraints().size());
Constraint constraint = pattern.getConstraints().get(0);
GroupElement exists = (GroupElement) lhs.getChildren().get(2);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
IndexableConstraint vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getRightExtractor().getExtractToClass());
assertEquals("column1", vconstraint.getRequiredDeclarations()[0].getIdentifier());
pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(3);
assertEquals(1, pattern.getConstraints().size());
constraint = pattern.getConstraints().get(0);
exists = (GroupElement) lhs.getChildren().get(4);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getRightExtractor().getExtractToClass());
assertEquals("column2", vconstraint.getRequiredDeclarations()[0].getIdentifier());
pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(5);
assertEquals(1, pattern.getConstraints().size());
constraint = pattern.getConstraints().get(0);
exists = (GroupElement) lhs.getChildren().get(6);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getRightExtractor().getExtractToClass());
assertEquals("column3", vconstraint.getRequiredDeclarations()[0].getIdentifier());
}
use of org.drools.core.impl.RuleBase in project drools by kiegroup.
the class BetaNodeTest method testEqualsObject.
@Test
public void testEqualsObject() {
RuleBase kBase = RuleBaseFactory.newRuleBase();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
final LeftTupleSource ts = new MockTupleSource(1, buildContext);
final ObjectSource os = new MockObjectSource(2, buildContext);
final BetaNode j1 = new JoinNode(1, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
final BetaNode j2 = new JoinNode(2, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
final BetaNode n1 = new NotNode(3, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
final BetaNode n2 = new NotNode(4, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
assertEquals(j1, j1);
assertEquals(j2, j2);
assertEquals(j1, j2);
assertEquals(n1, n1);
assertEquals(n2, n2);
assertEquals(n1, n2);
assertFalse(j1.equals(n1));
assertFalse(j1.equals(n2));
assertFalse(n1.equals(j1));
assertFalse(n1.equals(j2));
}
use of org.drools.core.impl.RuleBase in project drools by kiegroup.
the class TupleSourceTest method testAddTupleSink.
@Test
public void testAddTupleSink() throws Exception {
KieBaseConfiguration kconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
RuleBase kBase = RuleBaseFactory.newRuleBase(kconf);
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
final MockTupleSource source = new MockTupleSource(15, buildContext);
// We need to re-assign this var each time the sink changes references
final Field field = LeftTupleSource.class.getDeclaredField("sink");
field.setAccessible(true);
LeftTupleSinkPropagator sink = (LeftTupleSinkPropagator) field.get(source);
assertSame(EmptyLeftTupleSinkAdapter.getInstance(), sink);
final MockLeftTupleSink sink1 = new MockLeftTupleSink(buildContext);
source.addTupleSink(sink1);
sink = (LeftTupleSinkPropagator) field.get(source);
assertSame(SingleLeftTupleSinkAdapter.class, sink.getClass());
assertEquals(1, sink.getSinks().length);
final MockLeftTupleSink sink2 = new MockLeftTupleSink(buildContext);
source.addTupleSink(sink2);
sink = (LeftTupleSinkPropagator) field.get(source);
assertSame(CompositeLeftTupleSinkAdapter.class, sink.getClass());
assertEquals(2, sink.getSinks().length);
final MockLeftTupleSink sink3 = new MockLeftTupleSink(buildContext);
source.addTupleSink(sink3);
assertSame(CompositeLeftTupleSinkAdapter.class, sink.getClass());
assertEquals(3, sink.getSinks().length);
source.removeTupleSink(sink2);
assertSame(CompositeLeftTupleSinkAdapter.class, sink.getClass());
assertEquals(2, sink.getSinks().length);
source.removeTupleSink(sink1);
sink = (LeftTupleSinkPropagator) field.get(source);
assertSame(SingleLeftTupleSinkAdapter.class, sink.getClass());
assertEquals(1, sink.getSinks().length);
source.removeTupleSink(sink3);
sink = (LeftTupleSinkPropagator) field.get(source);
assertSame(EmptyLeftTupleSinkAdapter.getInstance(), sink);
assertEquals(0, sink.getSinks().length);
}
use of org.drools.core.impl.RuleBase in project drools by kiegroup.
the class TupleSourceTest method testObjectTupleConstructor.
@Test
public void testObjectTupleConstructor() {
KieBaseConfiguration kconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
RuleBase kBase = RuleBaseFactory.newRuleBase(kconf);
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
final MockTupleSource source = new MockTupleSource(15, buildContext);
assertEquals(15, source.getId());
}
use of org.drools.core.impl.RuleBase in project drools by kiegroup.
the class PersisterHelper method writeToStreamWithHeader.
public static void writeToStreamWithHeader(MarshallerWriteContext context, Message payload) throws IOException {
ProtobufMessages.Header.Builder _header = ProtobufMessages.Header.newBuilder();
_header.setVersion(ProtobufMessages.Version.newBuilder().setVersionMajor(Drools.getMajorVersion()).setVersionMinor(Drools.getMinorVersion()).setVersionRevision(Drools.getRevisionVersion()).build());
writeStrategiesIndex(context, _header);
RuleBase kBase = context.getKnowledgeBase();
if (kBase != null) {
TraitFactory traitFactory = RuntimeComponentFactory.get().getTraitFactory(kBase);
if (traitFactory != null) {
writeRuntimeDefinedClasses(traitFactory, context, _header);
}
}
byte[] buff = payload.toByteArray();
sign(_header, buff);
_header.setPayload(ByteString.copyFrom(buff));
context.write(_header.build().toByteArray());
}
Aggregations