コード例 #1
0
ファイル: Program.cs プロジェクト: Matt-Mirage34d/NRules
        static void Main(string[] args)
        {
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(AssignFirstSeat).Assembly));

            var factory = repository.Compile();
            var session = factory.CreateSession();

            session.Events.FactInsertingEvent     += EventProviderOnFactInsertingEvent;
            session.Events.FactUpdatingEvent      += EventProviderOnFactUpdatingEvent;
            session.Events.FactRetractingEvent    += EventProviderOnFactRetractingEvent;
            session.Events.ActivationCreatedEvent += EventProviderOnActivationCreatedEvent;
            session.Events.ActivationDeletedEvent += EventProviderOnActivationDeletedEvent;
            session.Events.RuleFiringEvent        += EventProviderOnRuleFiringEvent;

            session.Insert(new Guest("N1", Gender.Male, new Hobby("H1")));
            session.Insert(new Guest("N2", Gender.Female, new Hobby("H1")));
            session.Insert(new Guest("N2", Gender.Female, new Hobby("H3")));
            session.Insert(new Guest("N3", Gender.Male, new Hobby("H3")));
            session.Insert(new Guest("N4", Gender.Male, new Hobby("H1")));
            session.Insert(new Guest("N4", Gender.Female, new Hobby("H2")));
            session.Insert(new Guest("N4", Gender.Female, new Hobby("H3")));
            session.Insert(new Guest("N5", Gender.Female, new Hobby("H2")));
            session.Insert(new Guest("N5", Gender.Female, new Hobby("H1")));
            session.Insert(new Count(1));
            session.Insert(new LastSeat(5));

            var context = new Context();

            session.Insert(context);

            session.Fire();
        }
コード例 #2
0
        private ISessionFactory CreateTarget()
        {
            var repository = new RuleRepository();

            repository.Load(x => x.NestedTypes().From(typeof(TestRule)));
            return(repository.Compile());
        }
コード例 #3
0
        public void Test1()
        {
            //Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(PreferredCustomerDiscountRule).Assembly));

            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();

            //Load domain model
            var customer = new Customer("John Doe")
            {
                IsPreferred = true
            };
            var order1 = new Order(123456, customer, 2, 25.0);
            var order2 = new Order(123457, customer, 1, 100.0);

            session.Insert(customer);
            session.Insert(order1);
            session.Insert(order2);

            session.Fire();
        }
コード例 #4
0
        private static void RestReaminingSample()
        {
            //Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(NoticeNewRequestRule).Assembly));

            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();
            //Fake Web Action
            var fakeAction = new FakeAction();

            //設定補修只有2天
            var person = fakeAction.GetFakePerson("皓之", 2);

            session.Insert(person);
            session.Fire();
            //請假
            var request = fakeAction.SendAskForRestRequest();

            session.Insert(request);
            session.Fire();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: MarianBusila/poc-csharp
        static void Main(string[] args)
        {
            //Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(PreferredCustomerDiscountRule).Assembly));

            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();

            //Load domain model
            var customer = new Customer("John Doe")
            {
                IsPreferred = true
            };
            var order1 = new Order(123456, customer, 2, 25.0);
            var order2 = new Order(123457, customer, 1, 100.0);

            Console.WriteLine($"Order1 price: {order1.Price}");
            Console.WriteLine($"Order2 price: {order2.Price}");

            //Insert facts into rules engine's memory
            session.Insert(customer);
            session.Insert(order1);
            session.Insert(order2);

            //Start match/resolve/act cycle
            session.Fire();

            Console.WriteLine($"Order1 price: {order1.Price}");
            Console.WriteLine($"Order2 price: {order2.Price}");
        }
コード例 #6
0
        public async Task <string> TipMessageAsync()
        {
            TipList = new List <Tip>();
            var tipList = await MainDialog.QueryTipsAsync();

            // Rules engine here
            // Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(TipChooseRule).Assembly));

            // Compile rules
            var factory = repository.Compile();

            // Create a working session
            var session = factory.CreateSession();

            // Insert facts into rules engine's memory
            session.Insert(PersonalDetailsDialog.PersonalDetails);
            foreach (Tip obj in tipList)
            {
                session.Insert(obj);
            }

            // Start match/resolve/act cycle
            session.Fire();


            return(RandomiseList(TipList.Select(l => l.TipMessage).ToList()));
        }
コード例 #7
0
        static void Main(string[] args)
        {
            //Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(PreferredCustomerDiscountRule).Assembly));

            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();

            //Load domain model
            var customer = new Customer("John Doe")
            {
                IsPreferred = false
            };
            var order1 = new Order(123456, customer, 2, 25.0);
            var order2 = new Order(123457, customer, 1, 100.0);

            session.Insert(customer);
            session.Insert(order1);
            session.Insert(order2);

            var fire = session.Fire();

            Console.WriteLine(fire);
            Console.Read();
        }
コード例 #8
0
        public void Compile()
        {
            //Load rules
            var repository = new RuleRepository();

            // Here is for the scriptedAssembly ...
            // repository.Load(x => x.From(typeof(PreferredCustomerDiscountRule).Assembly));

            // Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();

            //Load domain model
            // var customer = new Customer("John Doe") { IsPreferred = true };
            // var order1 = new Order(123456, customer, 2, 25.0);
            // var order2 = new Order(123457, customer, 1, 100.0);

            //Insert facts into rules engine's memory
            // session.Insert(customer);
            // session.Insert(order1);
            // session.Insert(order2);

            //Start match/resolve/act cycle
            session.Fire();
        }
        public bool Shipped([FromBody] Shipment s)
        {
            Shipment shipment = _context.Shipment
                                .Where(x => x.TrackingNumber == s.TrackingNumber)
                                .FirstOrDefault();

            shipment.IsFullShipment = true;
            //When the shipment is done,
            //then update the shipment
            _context.Shipment.Update(shipment);

            SalesOrder salesOrder = _context.SalesOrder.Where(s1 => s1.SalesOrderId == shipment.SalesOrderId).Single();
            Customer   customer   = _context.Customer.Where(cust => cust.CustomerId == salesOrder.CustomerId).Single();

            salesOrder.OrderProgressTypeId = OrderProgressStatus.CLOSED;

            //rule start
            var repository = new RuleRepository();

            repository.Load(x => x.From(Assembly.GetExecutingAssembly()));
            var factory = repository.Compile();
            var session = factory.CreateSession();

            session.Insert(salesOrder);
            session.Fire();
            //rule end

            _context.SaveChanges();
            return(true);
        }
コード例 #10
0
        public void RuleRepositoryExtension_Compile_CancellationRequested()
        {
            using (var cancellationSource = new CancellationTokenSource())
            {
                // Arrange
                var ruleCompiler = new RuleCompiler();

                var rule1 = CreateRuleDefinitionMock();
                var rule2 = CreateRuleDefinitionMock();
                rule2.Setup(r => r.LeftHandSide).Returns(() =>
                {
                    cancellationSource.Cancel();
                    return(new AndElement(Enumerable.Empty <RuleElement>()));
                });

                var rule3 = CreateRuleDefinitionMock();
                var rules = new RuleSet("R1");
                rules.Add(new[] { rule1.Object, rule2.Object, rule3.Object });

                var ruleRepository = new RuleRepository();
                ruleRepository.Add(rules);

                // Act
                ruleRepository.Compile(cancellationSource.Token);

                // Assert
                rule1.Verify(r => r.LeftHandSide, Times.AtLeastOnce);
                rule2.Verify(r => r.LeftHandSide, Times.AtLeastOnce);
                rule3.Verify(r => r.LeftHandSide, Times.Never);
            }
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: simmyspike/nrule
        private static void Main(string[] args)
        {
            //Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(Assembly.GetExecutingAssembly()));

            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();

            //Load domain model
            var ToWinMarket = new Market("ToWin");
            var Scoreboard  = new Scoreboard();

            //Insert facts into rules engine's memory
            session.Insert(ToWinMarket);
            session.Insert(Scoreboard);

            //Start match/resolve/act cycle
            //session.Update(ToWinMarket);
            session.Fire();

            foreach (Participant part in ToWinMarket.Participants)
            {
                Console.WriteLine(part.Description + " - " + part.isWinner);
            }

            Console.ReadLine();
        }
コード例 #12
0
ファイル: BenchmarkBase.cs プロジェクト: zhrjin/NRules
        protected void SetupRule <T>() where T : Rule
        {
            var repository = new RuleRepository();

            repository.Load(x => x.NestedTypes().PrivateTypes().From(xx => xx.Type(typeof(T))));

            Factory = repository.Compile();
        }
        public void Execute(AddressValueObject address)
        {
            RuleRepository repository = new RuleRepository();

            repository.Load(x => x.From(typeof(AddressRule).Assembly));
            ISessionFactory factory = repository.Compile();
            ISession        session = factory.CreateSession();

            session.Insert(address);
            session.Fire();
        }
コード例 #14
0
        public void Execute(CustomerEntity customer)
        {
            RuleRepository repository = new RuleRepository();

            repository.Load(x => x.From(typeof(CustomerRule).Assembly));
            ISessionFactory factory = repository.Compile();
            ISession        session = factory.CreateSession();

            session.Insert(customer);
            session.Fire();
        }
コード例 #15
0
        private void RunRule(SalesOrder salesOrder)
        {
            //rule start
            var repository = new RuleRepository();

            repository.Load(x => x.From(Assembly.GetExecutingAssembly()));
            var factory = repository.Compile();
            var session = factory.CreateSession();

            session.Insert(salesOrder);
            session.Fire();
            //rule end
        }
コード例 #16
0
ファイル: RuleService.cs プロジェクト: ylerjen/ExampleApi
        public RuleService()
        {
            //Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(UserShouldBeBornIn1984Rule).Assembly));

            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            this.RulesSession = factory.CreateSession();
        }
コード例 #17
0
        public ISessionFactory InitializeEngine()
        {
            //Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(TestRule).Assembly));

            //Compile rules
            return(repository.Compile());

            //Create a working session
            //return factory.CreateSession();
        }
コード例 #18
0
        private void setupRuleEngine()
        {
            var ruleRepo = new RuleRepository();

            ruleRepo.Load(x => x.From(typeof(PeriodicRuleEngine).Assembly));
            // rules are (should be ?) in the same assembly as PeriodicRuleEngine

            ISessionFactory factory = ruleRepo.Compile();

            engineSession = factory.CreateSession();
            engineSession.DependencyResolver =
                new AspNetCoreDepResolver(serviceProvider);
        }
コード例 #19
0
        public static void BeforeTest()
        {
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(NotifyDevelopersAboutMissingEstimateRule).Assembly));
            factory = repository.Compile();

            container = new Container();
            container.Register(typeof(IHandle <>), typeof(CommandHandler));
            container.Register(() => factory);
            container.Verify();

            factory.DependencyResolver = new SimpleInjectorDependencyResolver(container);
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: marksundt/SimpleNRules
        static void Main(string[] args)
        {
            //Load rules
            var repository = new RuleRepository();

            //repository.Load(x => x.From(Assembly.GetExecutingAssembly()));
            repository.Load(x => x.From(typeof(ActiveCustomerAccountRule).Assembly));
            repository.Load(x => x.From(typeof(PerfCustDiscountRule).Assembly));
            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();

            //Load domain model
            var customer1 = new Customer("John Doe")
            {
                IsPreferred = true
            };
            var account11 = new Account("12456789", customer1);
            var account12 = new Account("12456790", customer1);
            var account13 = new Account("12456791", customer1)
            {
                IsActive = false, IsDelinquent = true
            };
            var order11 = new Order(123456, customer1, 2, 25.0);
            var order12 = new Order(123457, customer1, 1, 100.0);
            var order13 = new Order(123458, customer1, 1, 5.0);

            var customer2 = new Customer("Sarah Jones");
            var account21 = new Account("22456789", customer2);
            var order21   = new Order(223456, customer2, 2, 2225.0);

            //Insert facts into rules engine's memory
            session.Insert(customer1);
            session.Insert(account11);
            session.Insert(account12);
            session.Insert(account13);
            session.Insert(order11);
            session.Insert(order12);
            session.Insert(order13);

            session.Insert(customer2);
            session.Insert(account21);
            session.Insert(order21);

            //Start match/resolve/act cycle
            session.Update(customer1);
            session.Fire();
        }
コード例 #21
0
ファイル: Program.cs プロジェクト: arastoul/NRules
        private static void Main(string[] args)
        {
            var repository = new RuleRepository();

            repository.Load(x => x.From(Assembly.GetExecutingAssembly()));

            ISessionFactory factory = repository.Compile();
            ISession        session = factory.CreateSession();

            session.Insert(new Fact1 {
                TestProperty = "Valid Value"
            });

            VisualizerHost.Visualize(session);
        }
コード例 #22
0
        public void SetUp()
        {
            _firedRulesMap = new Dictionary <string, List <AgendaEventArgs> >();
            _ruleMap       = new Dictionary <Type, IRuleMetadata>();

            Repository = new RuleRepository {
                Activator = new InstanceActivator()
            };

            SetUpRules();

            ISessionFactory factory = Repository.Compile();

            Session = factory.CreateSession();
            Session.Events.RuleFiredEvent += (sender, args) => _firedRulesMap[args.Rule.Name].Add(args);
        }
コード例 #23
0
        public void SetUp()
        {
            _notifiers = new Dictionary <Type, Mock <INotifier> >();
            _rules     = new List <BaseRule>();
            SetUpRules();

            var repository = new RuleRepository {
                Activator = new InstanceActivator(_rules)
            };

            repository.Load("Test", x => x.From(_rules.Select(r => r.GetType()).ToArray()));

            ISessionFactory factory = repository.Compile();

            Session = factory.CreateSession();
        }
コード例 #24
0
ファイル: Executor.cs プロジェクト: gbrooksman/XRule-OLD
        public void  RunProject()
        {
            //Load rules
            var repository = new RuleRepository();
            //repository.Load(x => x.From(typeof(PreferredCustomerDiscountRule).Assembly));

            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();


            //Start match/resolve/act cycle
            session.Fire();
        }
コード例 #25
0
        public SchemaValidator()
        {
            Validation = new List <ValidationContext>();
            _fields    = new FieldList();

            var repository = new RuleRepository();

            repository.Load(x => x.Where(r => r.IsTagged("FieldValidation")));
            repository.Load(x => x.From(typeof(NullFieldRule).Assembly));

            var factory = repository.Compile();

            _session = factory.CreateSession();

            _session.Events.RuleFiredEvent += OnRuleFiredEventHandler;
        }
コード例 #26
0
        public void Execute(Patient patient)
        {
            var repository = new RuleRepository();

            repository.Load(x => x.From(Assembly.GetExecutingAssembly()));

            var factory = repository.Compile();

            var session = factory.CreateSession();

            //Load Domain

            session.Insert(patient);

            session.Fire();
        }
コード例 #27
0
        public void OneTimeSetup()
        {
            mail = Substitute.For <IMailService>();

            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(NotifyDevelopersAboutMissingEstimateRule).Assembly));
            factory = repository.Compile();

            container = new Container();
            container.Register(typeof(IHandle <>), typeof(CommandHandler));
            container.Register(() => factory);
            container.Register(() => mail);
            container.Verify();

            factory.DependencyResolver = new SimpleInjectorDependencyResolver(container);
        }
コード例 #28
0
        static void Main(string[] args)
        {
            //Load the rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(typeof(Program).Assembly));

            //Compile the rule
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();

            //Load model
            var passenger1 = new Passenger("Rajkumar", Gender.Male, 39)
            {
                IsSeniorCitizen = true
            };
            var passenger2 = new Passenger("Selvakumar", Gender.Male, 61);
            var passenger3 = new Passenger("Nancy", Gender.Female, 62);
            var passenger4 = new Passenger("Jenifer", Gender.Female, 4);

            // To insert the fact into rule engine memory
            session.Insert(passenger1);
            session.Insert(passenger2);
            session.Insert(passenger3);
            session.Insert(passenger4);

            //Start rule validation
            Console.WriteLine("Running rules:");
            session.Fire();
            var errorList = session.Query <string>().ToList();

            if (errorList.Count > 0)
            {
                Console.WriteLine("==============");
            }

            foreach (var item in errorList)
            {
                Console.WriteLine(item);
            }

            Console.Read();//To show the validation message in console
        }
コード例 #29
0
        public void BootStrapEngine()
        {
            var rulesRepo = new RuleRepository();

            try
            {
                Assembly targetassembly = RuleEngineHelper.GetRuleAssembly(typeof(Rule));

                rulesRepo.Load(x => x.From(targetassembly));

                var factory = rulesRepo.Compile();
                _session = factory.CreateSession();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #30
0
        public void Run(List <Node> nodes)
        {
            //Load rules
            var repository = new RuleRepository();

            repository.Load(x => x.From(Assembly.GetExecutingAssembly()));

            //Compile rules
            var factory = repository.Compile();

            //Create a working session
            var session = factory.CreateSession();

            session.Events.RuleFiringEvent += OnRuleFiringEvent;

            foreach (var node in nodes)
            {
                session.Insert(node);
            }

            session.Fire();
        }