Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 JBoss Inc
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License. You may obtain a copy of
   * the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations under
  * the License.
  */
 package org.drools.guvnor.models.guided.dtable.backend;
 
 import org.junit.Test;
 
 import static org.junit.Assert.*;

Tests relating to the extended function of Fact\Field bindings
 
 public class BRLRuleModelTest {
 
     @Test
     public void testOnlyDecisionTableColumns() {
         GuidedDecisionTable52 dt = new GuidedDecisionTable52();
 
         Pattern52 p1 = new Pattern52();
         p1.setFactType"Driver" );
         p1.setBoundName"$p1" );
 
         ConditionCol52 c1 = new ConditionCol52();
         c1.setFactField"name" );
         c1.setBinding"$c1" );
 
         p1.getChildColumns().addc1 );
         dt.getConditions().addp1 );
 
         ActionInsertFactCol52 ins = new ActionInsertFactCol52();
         ins.setBoundName"$ins" );
         ins.setFactField"rating" );
         ins.setFactType"Person" );
         ins.setType. );
         dt.getActionCols().addins );
 
         BRLRuleModel model = new BRLRuleModeldt );
 
         assertNotNullmodel.getAllVariables() );
         assertEquals( 3,
                       model.getAllVariables().size() );
         assertTruemodel.getAllVariables().contains"$p1" ) );
         assertTruemodel.getAllVariables().contains"$c1" ) );
         assertTruemodel.getAllVariables().contains"$ins" ) );
     }
 
     @Test
     public void testDecisionTableColumnsWithLHS() {
         GuidedDecisionTable52 dt = new GuidedDecisionTable52();
 
         Pattern52 p1 = new Pattern52();
         p1.setFactType"Driver" );
         p1.setBoundName"$p1" );
 
         ConditionCol52 c1 = new ConditionCol52();
         c1.setFactField"name" );
         c1.setBinding"$c1" );
 
         p1.getChildColumns().addc1 );
         dt.getConditions().addp1 );
 
         BRLConditionColumn brlCondition = new BRLConditionColumn();
         FactPattern fp = new FactPattern"Driver" );
         fp.setBoundName"$brl1" );
 
         SingleFieldConstraint sfc1 = new SingleFieldConstraint();
         sfc1.setFieldBinding"$sfc1" );
         sfc1.setOperator"==" );
        sfc1.setFactType"Driver" );
        sfc1.setFieldName"name" );
        sfc1.setFieldType. );
        fp.addConstraintsfc1 );
        brlCondition.getDefinition().addfp );
        dt.getConditions().addbrlCondition );
        ActionInsertFactCol52 ins = new ActionInsertFactCol52();
        ins.setBoundName"$ins" );
        ins.setFactField"rating" );
        ins.setFactType"Person" );
        ins.setType. );
        dt.getActionCols().addins );
        BRLRuleModel model = new BRLRuleModeldt );
        assertNotNullmodel.getAllVariables() );
        assertEquals( 5,
                      model.getAllVariables().size() );
        assertTruemodel.getAllVariables().contains"$p1" ) );
        assertTruemodel.getAllVariables().contains"$c1" ) );
        assertTruemodel.getAllVariables().contains"$ins" ) );
        assertTruemodel.getAllVariables().contains"$brl1" ) );
        assertTruemodel.getAllVariables().contains"$sfc1" ) );
    }
    @Test
        GuidedDecisionTable52 dt = new GuidedDecisionTable52();
        Pattern52 p1 = new Pattern52();
        p1.setFactType"Driver" );
        p1.setBoundName"$p1" );
        ConditionCol52 c1 = new ConditionCol52();
        c1.setFactField"name" );
        c1.setBinding"$c1" );
        p1.getChildColumns().addc1 );
        dt.getConditions().addp1 );
        BRLConditionColumn brlCondition = new BRLConditionColumn();
        FactPattern fp = new FactPattern"Driver" );
        fp.setBoundName"$brl1" );
        SingleFieldConstraint sfc1 = new SingleFieldConstraint();
        sfc1.setFieldBinding"$sfc1" );
        sfc1.setOperator"==" );
        sfc1.setFactType"Driver" );
        sfc1.setFieldName"name" );
        sfc1.setFieldType. );
        fp.addConstraintsfc1 );
        brlCondition.getDefinition().addfp );
        dt.getConditions().addbrlCondition );
        ActionInsertFactCol52 ins = new ActionInsertFactCol52();
        ins.setBoundName"$ins" );
        ins.setFactField"rating" );
        ins.setFactType"Person" );
        ins.setType. );
        dt.getActionCols().addins );
        BRLRuleModel model = new BRLRuleModeldt );
        assertNotNullmodel.getLHSBoundFacts() );
        assertEquals( 2,
                      model.getLHSBoundFacts().size() );
        assertTruemodel.getLHSBoundFacts().contains"$p1" ) );
        assertTruemodel.getLHSBoundFacts().contains"$brl1" ) );
        assertNotNullmodel.getLHSBindingType"$p1" ) );
        assertEquals"Driver",
                      model.getLHSBindingType"$p1" ) );
        assertNotNullmodel.getLHSBindingType"$brl1" ) );
        assertEquals"Driver",
                      model.getLHSBindingType"$brl1" ) );
        FactPattern r1 = model.getLHSBoundFact"$p1" );
        assertNotNullr1 );
        assertTruer1 instanceof Pattern52FactPatternAdaptor );
        assertEquals"Driver",
                      raif1.getFactType() );
        FactPattern r2 = model.getLHSBoundFact"$brl1" );
        assertNotNullr2 );
        assertEquals"Driver",
                      r2.getFactType() );
    }
    @Test
        GuidedDecisionTable52 dt = new GuidedDecisionTable52();
        Pattern52 p1 = new Pattern52();
        p1.setFactType"Driver" );
        p1.setBoundName"$p1" );
        ConditionCol52 c1 = new ConditionCol52();
        c1.setFactField"name" );
        c1.setBinding"$c1" );
        p1.getChildColumns().addc1 );
        dt.getConditions().addp1 );
        BRLConditionColumn brlCondition = new BRLConditionColumn();
        FactPattern fp = new FactPattern"Driver" );
        fp.setBoundName"$brl1" );
        SingleFieldConstraint sfc1 = new SingleFieldConstraint();
        sfc1.setFieldBinding"$sfc1" );
        sfc1.setOperator"==" );
        sfc1.setFactType"Driver" );
        sfc1.setFieldName"name" );
        sfc1.setFieldType. );
        fp.addConstraintsfc1 );
        brlCondition.getDefinition().addfp );
        dt.getConditions().addbrlCondition );
        ActionInsertFactCol52 ins = new ActionInsertFactCol52();
        ins.setBoundName"$ins" );
        ins.setFactField"rating" );
        ins.setFactType"Person" );
        ins.setType. );
        dt.getActionCols().addins );
        BRLRuleModel model = new BRLRuleModeldt );
        FieldConstraint fcr1 = model.getLHSBoundField"$sfc1" );
        assertNotNullfcr1 );
        assertTruefcr1 instanceof SingleFieldConstraint );
        SingleFieldConstraint fcr1sfc = (SingleFieldConstraintfcr1;
        assertEquals"name",
                      fcr1sfc.getFieldName() );
                      fcr1sfc.getFieldType() );
    }
    @Test
    public void testDecisionTableColumnsWithRHS() {
        GuidedDecisionTable52 dt = new GuidedDecisionTable52();
        Pattern52 p1 = new Pattern52();
        p1.setFactType"Driver" );
        p1.setBoundName"$p1" );
        ConditionCol52 c1 = new ConditionCol52();
        c1.setFactField"name" );
        c1.setBinding"$c1" );
        p1.getChildColumns().addc1 );
        dt.getConditions().addp1 );
        ActionInsertFactCol52 ins = new ActionInsertFactCol52();
        ins.setBoundName"$ins" );
        ins.setFactField"rating" );
        ins.setFactType"Person" );
        ins.setType. );
        dt.getActionCols().addins );
        BRLActionColumn brlAction = new BRLActionColumn();
        ActionInsertFact aif = new ActionInsertFact"Person" );
        aif.setBoundName"$aif" );
        aif.addFieldValuenew ActionFieldValue"rating",
                                                 null,
                                                 . ) );
        brlAction.getDefinition().addaif );
        dt.getActionCols().addbrlAction );
        BRLRuleModel model = new BRLRuleModeldt );
        assertNotNullmodel.getAllVariables() );
        assertEquals( 4,
                      model.getAllVariables().size() );
        assertTruemodel.getAllVariables().contains"$p1" ) );
        assertTruemodel.getAllVariables().contains"$c1" ) );
        assertTruemodel.getAllVariables().contains"$ins" ) );
        assertTruemodel.getAllVariables().contains"$aif" ) );
        assertNotNullmodel.getRHSBoundFacts() );
        assertEquals( 2,
                      model.getRHSBoundFacts().size() );
        assertTruemodel.getRHSBoundFacts().contains"$ins" ) );
        assertTruemodel.getRHSBoundFacts().contains"$aif" ) );
        ActionInsertFact r1 = model.getRHSBoundFact"$ins" );
        assertNotNullr1 );
        assertEquals"Person",
                      raif1.getFactType() );
        assertEquals"rating",
                      raif1.getFieldValues()[ 0 ].getField() );
                      raif1.getFieldValues()[ 0 ].getType() );
        assertNullraif1.getFieldValues()[ 0 ].getValue() );
                      raif1.getFieldValues()[ 0 ].getNature() );
        ActionInsertFact r2 = model.getRHSBoundFact"$aif" );
        assertNotNullr2 );
        assertTruer2 instanceof ActionInsertFact );
        ActionInsertFact raif2 = (ActionInsertFactr2;
        assertEquals"Person",
                      raif2.getFactType() );
        assertEquals"rating",
                      raif2.getFieldValues()[ 0 ].getField() );
                      raif2.getFieldValues()[ 0 ].getType() );
        assertNullraif2.getFieldValues()[ 0 ].getValue() );
                      raif2.getFieldValues()[ 0 ].getNature() );
    }
    @Test
        GuidedDecisionTable52 dt = new GuidedDecisionTable52();
        Pattern52 p1 = new Pattern52();
        p1.setFactType"Driver" );
        p1.setBoundName"$p1" );
        ConditionCol52 c1 = new ConditionCol52();
        c1.setFactField"name" );
        c1.setBinding"$c1" );
        p1.getChildColumns().addc1 );
        dt.getConditions().addp1 );
        ActionInsertFactCol52 ins = new ActionInsertFactCol52();
        ins.setBoundName"$ins" );
        ins.setFactField"rating" );
        ins.setFactType"Person" );
        ins.setType. );
        dt.getActionCols().addins );
        ActionInsertFactCol52 ins2 = new ActionInsertFactCol52();
        ins2.setInsertLogicaltrue );
        ins2.setBoundName"$ins2" );
        ins2.setFactField"rating2" );
        ins2.setFactType"Person2" );
        ins2.setType. );
        dt.getActionCols().addins2 );
        BRLActionColumn brlAction = new BRLActionColumn();
        ActionInsertFact aif = new ActionInsertFact"Person" );
        aif.setBoundName"$aif" );
        aif.addFieldValuenew ActionFieldValue"rating",
                                                 null,
                                                 . ) );
        brlAction.getDefinition().addaif );
        dt.getActionCols().addbrlAction );
        BRLRuleModel model = new BRLRuleModeldt );
        assertNotNullmodel.getRHSBoundFacts() );
        assertEquals( 3,
                      model.getRHSBoundFacts().size() );
        assertTruemodel.getRHSBoundFacts().contains"$ins" ) );
        assertTruemodel.getRHSBoundFacts().contains"$ins2" ) );
        assertTruemodel.getRHSBoundFacts().contains"$aif" ) );
        ActionInsertFact r1 = model.getRHSBoundFact"$ins" );
        assertNotNullr1 );
        assertTruer1 instanceof ActionInsertFactCol52ActionInsertFactAdaptor );
        assertEquals"Person",
                      raif1.getFactType() );
        assertEquals"rating",
                      raif1.getFieldValues()[ 0 ].getField() );
                      raif1.getFieldValues()[ 0 ].getType() );
        assertNullraif1.getFieldValues()[ 0 ].getValue() );
                      raif1.getFieldValues()[ 0 ].getNature() );
        ActionInsertFact r2 = model.getRHSBoundFact"$ins2" );
        assertNotNullr2 );
        assertEquals"Person2",
                      raif2.getFactType() );
        assertEquals"rating2",
                      raif2.getFieldValues()[ 0 ].getField() );
                      raif2.getFieldValues()[ 0 ].getType() );
        assertNullraif2.getFieldValues()[ 0 ].getValue() );
                      raif2.getFieldValues()[ 0 ].getNature() );
        ActionInsertFact r3 = model.getRHSBoundFact"$aif" );
        assertNotNullr3 );
        assertTruer3 instanceof ActionInsertFact );
        ActionInsertFact raif3 = (ActionInsertFactr3;
        assertEquals"Person",
                      raif3.getFactType() );
        assertEquals"rating",
                      raif3.getFieldValues()[ 0 ].getField() );
                      raif3.getFieldValues()[ 0 ].getType() );
        assertNullraif3.getFieldValues()[ 0 ].getValue() );
                      raif3.getFieldValues()[ 0 ].getNature() );
    }
    @Test
    public void testRuleModelLHSBoundFacts() {
        GuidedDecisionTable52 dt = new GuidedDecisionTable52();
        //Setup Decision Table columns
        Pattern52 p1 = new Pattern52();
        p1.setFactType"Driver" );
        p1.setBoundName"$p1" );
        ConditionCol52 c1 = new ConditionCol52();
        c1.setFactField"name" );
        c1.setBinding"$c1" );
        p1.getChildColumns().addc1 );
        dt.getConditions().addp1 );
        //Setup RuleModel columns (new BRLConditionColumn being added)
        BRLRuleModel model = new BRLRuleModeldt );
        FactPattern fp = new FactPattern"Driver" );
        fp.setBoundName"$brl1" );
        SingleFieldConstraint sfc1 = new SingleFieldConstraint();
        sfc1.setFieldBinding"$sfc1" );
        sfc1.setOperator"==" );
        sfc1.setFactType"Driver" );
        sfc1.setFieldName"name" );
        sfc1.setFieldType. );
        fp.addConstraintsfc1 );
        model.addLhsItemfp );
        //Checks
        assertNotNullmodel.getLHSBoundFacts() );
        assertEquals( 2,
                      model.getLHSBoundFacts().size() );
        assertTruemodel.getLHSBoundFacts().contains"$p1" ) );
        assertTruemodel.getLHSBoundFacts().contains"$brl1" ) );
        assertNotNullmodel.getLHSBindingType"$p1" ) );
        assertEquals"Driver",
                      model.getLHSBindingType"$p1" ) );
        assertNotNullmodel.getLHSBindingType"$brl1" ) );
        assertEquals"Driver",
                      model.getLHSBindingType"$brl1" ) );
        FactPattern r1 = model.getLHSBoundFact"$p1" );
        assertNotNullr1 );
        assertTruer1 instanceof Pattern52FactPatternAdaptor );
        assertEquals"Driver",
                      raif1.getFactType() );
        FactPattern r2 = model.getLHSBoundFact"$brl1" );
        assertNotNullr2 );
        assertEquals"Driver",
                      r2.getFactType() );
    }
    @Test
        GuidedDecisionTable52 dt = new GuidedDecisionTable52();
        //Setup Decision Table columns (with existing BRLConditionColumn)
        Pattern52 p1 = new Pattern52();
        p1.setFactType"Driver" );
        p1.setBoundName"$p1" );
        ConditionCol52 c1 = new ConditionCol52();
        c1.setFactField"name" );
        c1.setBinding"$c1" );
        p1.getChildColumns().addc1 );
        dt.getConditions().addp1 );
        BRLConditionColumn brlCondition = new BRLConditionColumn();
        FactPattern fp1 = new FactPattern"Driver" );
        fp1.setBoundName"$brl1" );
        SingleFieldConstraint sfc1 = new SingleFieldConstraint();
        sfc1.setFieldBinding"$sfc1" );
        sfc1.setOperator"==" );
        sfc1.setFactType"Driver" );
        sfc1.setFieldName"name" );
        sfc1.setFieldType. );
        fp1.addConstraintsfc1 );
        brlCondition.getDefinition().addfp1 );
        dt.getConditions().addbrlCondition );
        //Setup RuleModel columns (existing BRLConditionColumn being edited)
        BRLRuleModel model = new BRLRuleModeldt );
        FactPattern fp2 = new FactPattern"Driver" );
        fp2.setBoundName"$brl1" );
        SingleFieldConstraint sfc2 = new SingleFieldConstraint();
        sfc2.setFieldBinding"$sfc1" );
        sfc2.setOperator"==" );
        sfc2.setFactType"Driver" );
        sfc2.setFieldName"name" );
        sfc2.setFieldType. );
        fp2.addConstraintsfc2 );
        model.addLhsItemfp2 );
        //Checks
        assertNotNullmodel.getLHSBoundFacts() );
        assertEquals( 2,
                      model.getLHSBoundFacts().size() );
        assertTruemodel.getLHSBoundFacts().contains"$p1" ) );
        assertTruemodel.getLHSBoundFacts().contains"$brl1" ) );
        assertNotNullmodel.getLHSBindingType"$p1" ) );
        assertEquals"Driver",
                      model.getLHSBindingType"$p1" ) );
        assertNotNullmodel.getLHSBindingType"$brl1" ) );
        assertEquals"Driver",
                      model.getLHSBindingType"$brl1" ) );
        FactPattern r1 = model.getLHSBoundFact"$p1" );
        assertNotNullr1 );
        assertTruer1 instanceof Pattern52FactPatternAdaptor );
        assertEquals"Driver",
                      raif1.getFactType() );
        FactPattern r2 = model.getLHSBoundFact"$brl1" );
        assertNotNullr2 );
        assertEquals"Driver",
                      r2.getFactType() );
    }
    @Test
    public void testRuleModelRHSBoundFacts() {
        GuidedDecisionTable52 dt = new GuidedDecisionTable52();
        //Setup Decision Table columns
        ActionInsertFactCol52 ins = new ActionInsertFactCol52();
        ins.setBoundName"$ins" );
        ins.setFactField"rating" );
        ins.setFactType"Person" );
        ins.setType. );
        dt.getActionCols().addins );
        //Setup RuleModel columns (new BRLActionColumn being added)
        BRLRuleModel model = new BRLRuleModeldt );
        ActionInsertFact aif = new ActionInsertFact"Person" );
        aif.setBoundName"$aif" );
        aif.addFieldValuenew ActionFieldValue"rating",
                                                 null,
                                                 . ) );
        model.addRhsItemaif );
        //Checks
        assertNotNullmodel.getRHSBoundFacts() );
        assertEquals( 2,
                      model.getRHSBoundFacts().size() );
        assertTruemodel.getRHSBoundFacts().contains"$ins" ) );
        assertTruemodel.getRHSBoundFacts().contains"$aif" ) );
        ActionInsertFact r1 = model.getRHSBoundFact"$ins" );
        assertNotNullr1 );
        assertTruer1 instanceof ActionInsertFactCol52ActionInsertFactAdaptor );
        assertEquals"Person",
                      raif1.getFactType() );
        assertEquals"rating",
                      raif1.getFieldValues()[ 0 ].getField() );
                      raif1.getFieldValues()[ 0 ].getType() );
        assertNullraif1.getFieldValues()[ 0 ].getValue() );
                      raif1.getFieldValues()[ 0 ].getNature() );
        ActionInsertFact r2 = model.getRHSBoundFact"$aif" );
        assertNotNullr2 );
        assertTruer2 instanceof ActionInsertFact );
        ActionInsertFact raif2 = (ActionInsertFactr2;
        assertEquals"Person",
                      raif2.getFactType() );
        assertEquals"rating",
                      raif2.getFieldValues()[ 0 ].getField() );
                      raif2.getFieldValues()[ 0 ].getType() );
        assertNullraif2.getFieldValues()[ 0 ].getValue() );
                      raif2.getFieldValues()[ 0 ].getNature() );
    }
    @Test
        GuidedDecisionTable52 dt = new GuidedDecisionTable52();
        //Setup Decision Table columns (with existing BRLActionColumn)
        ActionInsertFactCol52 ins = new ActionInsertFactCol52();
        ins.setBoundName"$ins" );
        ins.setFactField"rating" );
        ins.setFactType"Person" );
        ins.setType. );
        dt.getActionCols().addins );
        BRLActionColumn brlAction = new BRLActionColumn();
        ActionInsertFact aif1 = new ActionInsertFact"Person" );
        aif1.setBoundName"$aif" );
        aif1.addFieldValuenew ActionFieldValue"rating",
                                                  null,
                                                  . ) );
        brlAction.getDefinition().addaif1 );
        dt.getActionCols().addbrlAction );
        //Setup RuleModel columns (existing BRLActionColumn being edited)
        BRLRuleModel model = new BRLRuleModeldt );
        ActionInsertFact aif2 = new ActionInsertFact"Person" );
        aif2.setBoundName"$aif" );
        aif2.addFieldValuenew ActionFieldValue"rating",
                                                  null,
                                                  . ) );
        model.addRhsItemaif2 );
        //Checks
        assertNotNullmodel.getRHSBoundFacts() );
        assertEquals( 2,
                      model.getRHSBoundFacts().size() );
        assertTruemodel.getRHSBoundFacts().contains"$ins" ) );
        assertTruemodel.getRHSBoundFacts().contains"$aif" ) );
        ActionInsertFact r1 = model.getRHSBoundFact"$ins" );
        assertNotNullr1 );
        assertTruer1 instanceof ActionInsertFactCol52ActionInsertFactAdaptor );
        assertEquals"Person",
                      raif1.getFactType() );
        assertEquals"rating",
                      raif1.getFieldValues()[ 0 ].getField() );
                      raif1.getFieldValues()[ 0 ].getType() );
        assertNullraif1.getFieldValues()[ 0 ].getValue() );
                      raif1.getFieldValues()[ 0 ].getNature() );
        ActionInsertFact r2 = model.getRHSBoundFact"$aif" );
        assertNotNullr2 );
        assertTruer2 instanceof ActionInsertFact );
        ActionInsertFact raif2 = (ActionInsertFactr2;
        assertEquals"Person",
                      raif2.getFactType() );
        assertEquals"rating",
                      raif2.getFieldValues()[ 0 ].getField() );
                      raif2.getFieldValues()[ 0 ].getType() );
        assertNullraif2.getFieldValues()[ 0 ].getValue() );
                      raif2.getFieldValues()[ 0 ].getNature() );
    }
New to GrepCode? Check out our FAQ X