ActionExpression.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 » ActionExpression.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 ActionExpression
    {
        #region internal
        public ActionExpression()
        {
        }
        #endregion
        #region strings
        /// <summary>
        /// Confirm we can write a new value to a double
        /// </summary>
        [Test]
        public void double1()
        {
            //init variables
            string modelname = "1.xml";
            bool changed = false;
            XmlNode model1 = null;
            XmlNode model2 = null;
            RuleEngine.Evidence.Fact f1 = new RuleEngine.Evidence.Fact("f1", 1, 2d, typeof(double));
            RuleEngine.Evidence.Fact f2 = new RuleEngine.Evidence.Fact("f2", 1, 4d, typeof(double));
            #region delegates
            f1.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f1.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f1.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            #region delegates
            f2.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f2.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f2.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            f1.IsEvaluatable = true;
            f2.IsEvaluatable = true;
            f1.Evaluate();
            f2.Evaluate();

            RuleEngine.Evidence.Actions.ActionExpression f = new RuleEngine.Evidence.Actions.ActionExpression("a1", "f1", "3", 1);
            #region delegates
            f.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            f.IsEvaluatable = true;

            //init model
            changed = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            Assert.AreEqual(true, changed);
            Assert.IsTrue(f1.Value is double);
            Assert.AreEqual(3, (double)f1.Value);
        }
        /// <summary>
        /// Confirm we can read from one fact and write its value to another.
        /// </summary>
        [Test]
        public void double2()
        {
            //init variables
            string modelname = "1.xml";
            bool changed = false;
            XmlNode model1 = null;
            XmlNode model2 = null;
            RuleEngine.Evidence.Fact f1 = new RuleEngine.Evidence.Fact("f1", 1, 2d, typeof(double));
            RuleEngine.Evidence.Fact f2 = new RuleEngine.Evidence.Fact("f2", 1, 4d, typeof(double));
            #region delegates
            f1.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f1.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f1.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            #region delegates
            f2.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f2.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f2.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            f1.IsEvaluatable = true;
            f2.IsEvaluatable = true;
            f1.Evaluate();
            f2.Evaluate();

            RuleEngine.Evidence.Actions.ActionExpression f = new RuleEngine.Evidence.Actions.ActionExpression("a1", "f1", "f2", 1);
            #region delegates
            f.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            f.IsEvaluatable = true;

            //init model
            changed = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            Assert.AreEqual(true, changed);
            Assert.IsTrue(f1.Value is double);
            Assert.AreEqual(4, (double)f1.Value);
        }
        /// <summary>
        /// Confirm we can read from one fact, add one, and write its value to another.
        /// </summary>
        [Test]
        public void double3()
        {
            //init variables
            string modelname = "1.xml";
            bool changed = false;
            XmlNode model1 = null;
            XmlNode model2 = null;
            RuleEngine.Evidence.Fact f1 = new RuleEngine.Evidence.Fact("f1", 1, 2d, typeof(double));
            RuleEngine.Evidence.Fact f2 = new RuleEngine.Evidence.Fact("f2", 1, 4d, typeof(double));
            #region delegates
            f1.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f1.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f1.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            #region delegates
            f2.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f2.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f2.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            f1.IsEvaluatable = true;
            f2.IsEvaluatable = true;
            f1.Evaluate();
            f2.Evaluate();

            RuleEngine.Evidence.Actions.ActionExpression f = new RuleEngine.Evidence.Actions.ActionExpression("a1", "f1", "f2+1", 1);
            #region delegates
            f.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            f.IsEvaluatable = true;

            //init model
            changed = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            Assert.AreEqual(true, changed);
            Assert.IsTrue(f1.Value is double);
            Assert.AreEqual(5, (double)f1.Value);
        }
        #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.