Search in sources :

Example 6 with GroupElement

use of org.drools.core.rule.GroupElement in project drools by kiegroup.

the class KnowledgeBuilderTest method testExists.

@Test
public void testExists() throws Exception {
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    // Make sure we can't accessa  variable bound inside the not node
    RuleImpl rule = createRule(new ExistsDescr(), builder, "update(stilton);");
    assertTrue(builder.hasErrors());
    builder = new KnowledgeBuilderImpl();
    rule = createRule(new ExistsDescr(), builder, "");
    assertEquals(0, builder.getErrors().getErrors().length);
    final GroupElement lhs = rule.getLhs();
    assertLength(1, lhs.getChildren());
    final GroupElement exists = (GroupElement) lhs.getChildren().get(0);
    assertLength(1, exists.getChildren());
    final Pattern pattern = (Pattern) exists.getChildren().get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) ExistsDescr(org.drools.compiler.lang.descr.ExistsDescr) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 7 with GroupElement

use of org.drools.core.rule.GroupElement in project drools by kiegroup.

the class KnowledgeBuilderTest method testAnd.

@Test
public void testAnd() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final RuleImpl rule = createRule(new AndDescr(), builder, "update(stilton);");
    assertLength(0, builder.getErrors().getErrors());
    final GroupElement lhs = rule.getLhs();
    assertLength(1, lhs.getChildren());
    final GroupElement and = (GroupElement) lhs.getChildren().get(0);
    assertLength(1, and.getChildren());
    final Pattern pattern = (Pattern) and.getChildren().get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) AndDescr(org.drools.compiler.lang.descr.AndDescr) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 8 with GroupElement

use of org.drools.core.rule.GroupElement in project drools by kiegroup.

the class KnowledgeBuilderTest method testNot.

@Test
public void testNot() throws Exception {
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    // Make sure we can't accessa  variable bound inside the not node
    RuleImpl rule = createRule(new NotDescr(), builder, "update(stilton);");
    assertTrue(builder.hasErrors());
    builder = new KnowledgeBuilderImpl();
    rule = createRule(new NotDescr(), builder, "");
    assertEquals(0, builder.getErrors().getErrors().length);
    final GroupElement lhs = rule.getLhs();
    assertLength(1, lhs.getChildren());
    final GroupElement not = (GroupElement) lhs.getChildren().get(0);
    assertLength(1, not.getChildren());
    final Pattern pattern = (Pattern) not.getChildren().get(0);
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 9 with GroupElement

use of org.drools.core.rule.GroupElement in project drools by kiegroup.

the class DroolsObjectIOTest method testFileIO.

@Test
public void testFileIO() throws Exception {
    FooBar fooBar1 = new FooBar();
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    new ObjectOutputStream(byteArrayOut).writeObject(fooBar1);
    ByteArrayInputStream byteArrayIn = new ByteArrayInputStream(byteArrayOut.toByteArray());
    FooBar fooBar2 = (FooBar) new ObjectInputStream(byteArrayIn).readObject();
    final File testFile = new File("target/test/DroolsObjectIOTest_testFileIO.dat");
    testFile.getParentFile().mkdirs();
    GroupElement testGroupElement = new GroupElement();
    DroolsStreamUtils.streamOut(new FileOutputStream(testFile), testGroupElement);
    InputStream fis = new FileInputStream(testFile);
    GroupElement streamedGroupElement = (GroupElement) DroolsStreamUtils.streamIn(new FileInputStream(testFile));
    assertEquals(streamedGroupElement, testGroupElement);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GroupElement(org.drools.core.rule.GroupElement) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 10 with GroupElement

use of org.drools.core.rule.GroupElement in project drools by kiegroup.

the class BaseMannersTest method getMakePath.

/**
 * <pre>
 *    rule makePath() {
 *        Context context;
 *        int seatingId, seatingPid, pathSeat;
 *        String pathGuestName;
 *
 *        when {
 *            Context( state == Context.MAKE_PATH )
 *            Seating( seatingId:id, seatingPid:pid, pathDone == false )
 *            Path( id == seatingPid, pathGuestName:guest, pathSeat:seat )
 *            (not Path( id == seatingId, guestName == pathGuestName )
 *        } else {
 *            drools.assert( new Path( seatingId, pathSeat, pathGuestName ) );
 *
 *        }
 *    }
 * </pre>
 *
 * @return
 * @throws InvalidRuleException
 */
private RuleImpl getMakePath() throws InvalidRuleException {
    final RuleImpl rule = new RuleImpl("makePath");
    // -----------
    // context : Context( state == Context.MAKE_PATH )
    // -----------
    final Pattern contextPattern = new Pattern(0, this.contextType);
    contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.MAKE_PATH));
    rule.addPattern(contextPattern);
    // ---------------
    // Seating( seatingId:id, seatingPid:pid, pathDone == false )
    // ---------------
    final Pattern seatingPattern = new Pattern(1, this.seatingType);
    setFieldDeclaration(seatingPattern, "id", "seatingId");
    setFieldDeclaration(seatingPattern, "pid", "seatingPid");
    seatingPattern.addConstraint(getLiteralConstraint(seatingPattern, "pathDone", false));
    rule.addPattern(seatingPattern);
    final Declaration seatingIdDeclaration = rule.getDeclaration("seatingId");
    final Declaration seatingPidDeclaration = rule.getDeclaration("seatingPid");
    // -----------
    // Path( id == seatingPid, pathGuestName:guestName, pathSeat:seat )
    // -----------
    final Pattern pathPattern = new Pattern(2, this.pathType);
    pathPattern.addConstraint(getBoundVariableConstraint(pathPattern, "id", seatingPidDeclaration, "=="));
    setFieldDeclaration(pathPattern, "guestName", "pathGuestName");
    setFieldDeclaration(pathPattern, "seat", "pathSeat");
    rule.addPattern(pathPattern);
    final Declaration pathGuestNameDeclaration = rule.getDeclaration("pathGuestName");
    final Declaration pathSeatDeclaration = rule.getDeclaration("pathSeat");
    // -------------
    // (not Path( id == seatingId, guestName == pathGuestName )
    // -------------
    final Pattern notPathPattern = new Pattern(3, this.pathType);
    notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "id", seatingIdDeclaration, "=="));
    notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "guestName", pathGuestNameDeclaration, "=="));
    final GroupElement not = GroupElementFactory.newNotInstance();
    not.addChild(notPathPattern);
    rule.addPattern(not);
    // ------------
    // drools.assert( new Path( id, pathName, pathSeat ) );
    // ------------
    final Consequence consequence = new Consequence() {

        public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
            try {
                RuleImpl rule = drools.getRule();
                LeftTuple tuple = (LeftTuple) drools.getTuple();
                int id = seatingIdDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(seatingIdDeclaration).getObject());
                int seat = pathSeatDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(pathSeatDeclaration).getObject());
                String guestName = (String) drools.get(pathGuestNameDeclaration);
                Path path = new Path(id, seat, guestName);
                drools.insert(path);
            // System.err.println( "make path : " + path );
            } catch (Exception e) {
                e.printStackTrace();
                throw new ConsequenceException(e);
            }
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    };
    rule.setConsequence(consequence);
    return rule;
}
Also used : Pattern(org.drools.core.rule.Pattern) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) GroupElement(org.drools.core.rule.GroupElement) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) LeftTuple(org.drools.core.reteoo.LeftTuple) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) InvalidRuleException(org.drools.core.rule.InvalidRuleException) IOException(java.io.IOException) ConsequenceException(org.drools.core.spi.ConsequenceException) IntrospectionException(java.beans.IntrospectionException) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) ConsequenceException(org.drools.core.spi.ConsequenceException)

Aggregations

GroupElement (org.drools.core.rule.GroupElement)29 Pattern (org.drools.core.rule.Pattern)16 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)14 Test (org.junit.Test)13 Declaration (org.drools.core.rule.Declaration)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)5 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)5 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)5 RuleConditionElement (org.drools.core.rule.RuleConditionElement)4 ObjectInput (java.io.ObjectInput)3 ObjectOutput (java.io.ObjectOutput)3 HashMap (java.util.HashMap)3 AndDescr (org.drools.compiler.lang.descr.AndDescr)3 WorkingMemory (org.drools.core.WorkingMemory)3 ClassObjectType (org.drools.core.base.ClassObjectType)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2