Rule.cs :  » Rule-Engines » SRE » UnitTests » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Rule Engines » SRE 
SRE » UnitTests » Rule.cs
using System;
using NUnit.Framework;
using System.Xml;
using System.Collections;
using System.Diagnostics;
using System.Resources;
using System.Reflection;
using System.Threading;

using RuleEngine.Evidence;
using RuleEngine;

namespace UnitTests{
    [TestFixture]
    public class Rule
    {
        #region instance variables
        private RuleEngine.Evidence.Actions.ActionExpression a1;
        private RuleEngine.Evidence.Actions.ActionExpression a2;
        private RuleEngine.Evidence.Fact f1;
        private RuleEngine.Evidence.Fact f2;
        private RuleEngine.Evidence.Rule r1;
        private RuleEngine.Evidence.Rule r2;

        private IEvidence EvidenceLookup(object sender, EvidenceLookupArgs args)
        {
            if (args.Key == "f1")
            {
                return f1;
            }
            else if (args.Key == "f2")
            {
                return f2;
            }
            else if (args.Key == "a1")
            {
                return a1;
            }
            else if (args.Key == "a2")
            {
                return a2;
            }
            else if (args.Key == "r1")
            {
                return r1;
            }
            else if (args.Key == "r2")
            {
                return r2;
            }
            else
                throw new Exception("Unknown evidence");
        }
        private XmlNode ModelLookup(object sender, ModelLookupArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        private void CreateFact1()
        {
            f1 = new RuleEngine.Evidence.Fact("f1", 1,2d, typeof(double));
            f1.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
            f1.IsEvaluatable = true;
            f1.Evaluate();
        }
        private void CreateFact2()
        {
            f2 = new RuleEngine.Evidence.Fact("f2", 1, 3d, typeof(double));
            f2.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
            f2.IsEvaluatable = true;
            f2.Evaluate();
        }
        private void CreateActionA1()
        {
            a1 = new RuleEngine.Evidence.Actions.ActionExpression("a1", "f2", "5", 1);
            a1.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
            a1.IsEvaluatable = true;
        }
        private void CreateActionA2()
        {
            a2 = new RuleEngine.Evidence.Actions.ActionExpression("a1", "f2", "6", 1);
            a2.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
            a2.IsEvaluatable = true;
        }
        private System.Collections.Generic.List<RuleEngine.Evidence.EvidenceSpecifier> CreateActionList()
        {
            System.Collections.Generic.List<RuleEngine.Evidence.EvidenceSpecifier> actionList = new System.Collections.Generic.List<RuleEngine.Evidence.EvidenceSpecifier>();
            actionList.Add(new RuleEngine.Evidence.EvidenceSpecifier(true, "a1"));
            actionList.Add(new RuleEngine.Evidence.EvidenceSpecifier(true, "a2"));
            return actionList;
        }
        private void CreateFact1(System.Collections.Generic.List<RuleEngine.Evidence.EvidenceSpecifier> actionList)
        {
            r1 = new RuleEngine.Evidence.Rule("r1", "f1==f1", actionList, 1, true);
            r1.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
            r1.IsEvaluatable = true;
            r1.Evaluate();
        }
        #endregion
        #region constructor
        public Rule()
        {
        }
        #endregion
        #region strings
        /// <summary>
        /// Confirm we can read the first name as text element
        /// </summary>
        [Test]
        public void string1()
        {
            CreateFact1();
            CreateFact2();
            CreateActionA1();
            CreateActionA2();
            CreateFact1(CreateActionList());
            Assert.AreEqual("true", (string)r1.Value.ToString().ToLower());
        }
        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.