001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.reteoo.builder;
018:
019: import java.util.List;
020:
021: import junit.framework.Assert;
022: import junit.framework.TestCase;
023:
024: import org.drools.WorkingMemory;
025: import org.drools.base.ClassObjectType;
026: import org.drools.reteoo.ReteooBuilder;
027: import org.drools.reteoo.ReteooRuleBase;
028: import org.drools.reteoo.RuleTerminalNode;
029: import org.drools.reteoo.ReteooBuilder.IdGenerator;
030: import org.drools.rule.Package;
031: import org.drools.rule.Pattern;
032: import org.drools.rule.GroupElement;
033: import org.drools.rule.GroupElementFactory;
034: import org.drools.rule.Rule;
035: import org.drools.spi.Consequence;
036: import org.drools.spi.KnowledgeHelper;
037:
038: /**
039: * @author etirelli
040: *
041: */
042: public class ReteooRuleBuilderTest extends TestCase {
043: private ReteooRuleBuilder builder;
044: private ReteooRuleBase rulebase;
045:
046: /* (non-Javadoc)
047: * @see junit.framework.TestCase#setUp()
048: */
049: protected void setUp() throws Exception {
050: super .setUp();
051:
052: this .builder = new ReteooRuleBuilder();
053: this .rulebase = new ReteooRuleBase("default");
054: }
055:
056: /* (non-Javadoc)
057: * @see junit.framework.TestCase#tearDown()
058: */
059: protected void tearDown() throws Exception {
060: super .tearDown();
061: }
062:
063: /**
064: * Test method for {@link org.drools.reteoo.builder.ReteooRuleBuilder#addRule(org.drools.rule.Rule, org.drools.reteoo.ReteooRuleBase, java.util.Map, int)}.
065: */
066: public void testAddRuleWithPatterns() {
067: final Rule rule = new Rule("only patterns");
068: final Pattern c1 = new Pattern(0, new ClassObjectType(
069: String.class));
070: final Pattern c2 = new Pattern(1, new ClassObjectType(
071: String.class));
072: final Pattern c3 = new Pattern(2, new ClassObjectType(
073: String.class));
074:
075: final GroupElement lhsroot = GroupElementFactory
076: .newAndInstance();
077: lhsroot.addChild(c1);
078: lhsroot.addChild(c2);
079: lhsroot.addChild(c3);
080:
081: rule.setLhs(lhsroot);
082:
083: final Consequence consequence = new Consequence() {
084: public void evaluate(KnowledgeHelper knowledgeHelper,
085: WorkingMemory workingMemory) throws Exception {
086: System.out.println("Consequence!");
087: }
088:
089: };
090:
091: rule.setConsequence(consequence);
092:
093: final List terminals = this .builder.addRule(rule,
094: this .rulebase, new ReteooBuilder.IdGenerator(1));
095:
096: Assert.assertEquals("Rule must have a single terminal node", 1,
097: terminals.size());
098:
099: final RuleTerminalNode terminal = (RuleTerminalNode) terminals
100: .get(0);
101:
102: }
103:
104: }
|