Search in sources :

Example 16 with RuleBase

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());
}
Also used : HashMap(java.util.HashMap) Constraint(org.drools.core.spi.Constraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IndexableConstraint(org.drools.core.rule.IndexableConstraint) RuleBase(org.drools.core.impl.RuleBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 17 with RuleBase

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));
}
Also used : BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBase(org.drools.core.impl.RuleBase) Test(org.junit.Test)

Example 18 with RuleBase

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);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) Field(java.lang.reflect.Field) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBase(org.drools.core.impl.RuleBase) Test(org.junit.Test)

Example 19 with RuleBase

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());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBase(org.drools.core.impl.RuleBase) Test(org.junit.Test)

Example 20 with RuleBase

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());
}
Also used : Header(org.drools.serialization.protobuf.ProtobufMessages.Header) RuleBase(org.drools.core.impl.RuleBase) TraitFactory(org.drools.core.factmodel.traits.TraitFactory)

Aggregations

RuleBase (org.drools.core.impl.RuleBase)21 Test (org.junit.Test)16 KieBase (org.kie.api.KieBase)9 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 KieSession (org.kie.api.runtime.KieSession)6 Rete (org.drools.core.reteoo.Rete)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)4 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 BuildContext (org.drools.core.reteoo.builder.BuildContext)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClassObjectType (org.drools.core.base.ClassObjectType)2 AlphaNode (org.drools.core.reteoo.AlphaNode)2 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)2 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)2 IndexableConstraint (org.drools.core.rule.IndexableConstraint)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 ReleaseId (org.kie.api.builder.ReleaseId)2 KiePackage (org.kie.api.definition.KiePackage)2