public void CompilingAnInvalidRuleSetThrowsAnException()
 {
     var rs = new RuleSet();
     Assert.Throws(typeof(Exception), delegate
     {
         IRuleEngine engine = WFRuleEngine.Compile(rs);
     });
 }
 public void CompilingAValidRuleSetReturnsARuleEngine()
 {
     var rs = new RuleSet { Name = "Whatever" };
     Assert.DoesNotThrow(delegate
     {
         IRuleEngine engine = WFRuleEngine.Compile(rs);
         engine.Invoke(_workingMemory);
     });
 }
Example #3
0
        public void RuleSetIsValidIfRulesAreValid()
        {
            var rule = new Rule { Name = "First Rule", TopCondition = new LogicCondition { Operator = "all" } };
            var action1 = new AssignAction { LeftHandSide = "Person.Salutation", Value = "Mr." };
            var action2 = new AssignAction { LeftHandSide = "Person.Greeting", Value = "Hello" };
            rule.AddAction(action1);
            rule.AddAction(action2);

            var rs = new RuleSet { Name = "Whatever" };
            rs.AddInput("Person", typeof(TestPerson));
            rs.AddRule(rule);

            Assert.IsTrue(rs.IsValid());
        }
Example #4
0
 public static WFRuleEngine Compile(RuleSet ruleSet)
 {
     if (ruleSet.IsValid())
     {
         var activity = new DynamicActivity();
         CompileInputs(activity, ruleSet);
         var implementation = CompileRules(ruleSet);
         activity.Implementation = () => implementation;
         return new WFRuleEngine(activity);
     }
     else
     {
         throw new Exception("RuleSet cannot be compiled.");
     }
 }
        public void InvokingWithIncorrectWorkingMemoryThrowsAnException()
        {
            var rule = new Rule { Name = "First Rule", TopCondition = new LogicCondition { Operator = "all" } };
            var action1 = new AssignAction { LeftHandSide = "Person.Salutation", Value = "Mr." };
            rule.AddAction(action1);

            var rs = new RuleSet { Name = "Whatever" };
            rs.AddInput("Person", typeof(TestPerson));
            rs.AddRule(rule);

            IRuleEngine engine = WFRuleEngine.Compile(rs);

            Assert.Throws(typeof(InvalidWorkflowException), delegate
            {
                engine.Invoke(_workingMemory);
            });
        }
        public void SetUp()
        {
            _workingMemory = new Dictionary<string, object>();

            _ruleSet = new RuleSet { Name = "Test RuleSet" };
            _ruleSet.AddInput("Person", typeof(TestPerson));
        }
Example #7
0
 private static Sequence CompileRules(RuleSet ruleSet)
 {
     var sequence = new Sequence();
     foreach (var rule in ruleSet.Rules)
     {
         var condition = CompileConditions(rule.TopCondition, ruleSet.Inputs);
         condition.Then = CompileActions(rule, ruleSet.Inputs);
         sequence.Activities.Add(condition);
     }
     return sequence;
 }
Example #8
0
        private static void CompileInputs(DynamicActivity activity, RuleSet ruleSet)
        {
            var settings = new VisualBasicSettings();
            foreach (var input in ruleSet.Inputs)
            {
                var inProperty = new DynamicActivityProperty
                {
                    Name = input.Key,
                    Type = typeof(InArgument<>).MakeGenericType(input.Value)
                };
                activity.Properties.Add(inProperty);

                settings.ImportReferences.Add(new VisualBasicImportReference
                    {
                        Assembly = input.Value.Assembly.GetName().Name,
                        Import = input.Value.Namespace
                    });
            }
            VisualBasic.SetSettings(activity, settings);
        }
Example #9
0
 public void RuleSetWithoutNameIsInvalid()
 {
     var rs = new RuleSet();
     Assert.IsFalse(rs.IsValid());
 }
Example #10
0
 public void EmptyRuleSetIsValid()
 {
     var rs = new RuleSet { Name = "Whatever" };
     Assert.IsTrue(rs.IsValid());
 }
Example #11
0
 public void CanAddRulesToRuleSet()
 {
     var rs = new RuleSet { Name = "Whatever" };
     var rule = new Rule { TopCondition = new LogicCondition { Operator = "all" } };
     Assert.DoesNotThrow(delegate { rs.AddRule(rule); });
 }
Example #12
0
 public void CanAddInputsToRuleSet()
 {
     var rs = new RuleSet { Name = "Whatever" };
     Assert.DoesNotThrow(delegate { rs.AddInput("Person", typeof(TestPerson)); });
 }