have created a Console App based on the Getting Started page at https://github.com/NRules/NRules/wiki/Getting-Started
I am not seeing the Customer object's NotifyAboutDiscount string output "Customer {0} was notified about a discount." What am I doing wrong?
Here are my models:
using System;
namespace NRulesTest
{
public class Customer
{
public string Name { get; private set; }
public bool IsPreferred { get; set; }
public Customer(string name)
{
Name = name;
}
public void NotifyAboutDiscount()
{
Console.WriteLine("Customer {0} was notified about a discount", Name);
}
}
public class Order
{
public int Id { get; private set; }
public Customer Customer { get; private set; }
public int Quantity { get; private set; }
public double UnitPrice { get; private set; }
public double PercentDiscount { get; internal set; }
public bool IsDiscounted { get { return PercentDiscount > 0; } }
public double Price
{
get { return UnitPrice * Quantity * (1.0 - PercentDiscount / 100.0); }
}
public bool IsOpen { get; internal set; }
public Order(int id, Customer customer, int quantity, double unitPrice, bool isOpen = false, double percentDiscount = 0.0)
{
Id = id;
Customer = customer;
Quantity = quantity;
UnitPrice = unitPrice;
IsOpen = isOpen;
PercentDiscount = percentDiscount;
}
public void ApplyDiscount(double percentDiscount)
{
PercentDiscount = percentDiscount;
}
}
}
DiscountNotificationRule class:
using NRules.Fluent.Dsl;
namespace NRulesTest
{
class DiscountNotificationRule : Rule
{
public override void Define()
{
Customer customer = null;
When()
.Match<Customer>(() => customer)
.Exists<Order>(o => o.Customer == customer, o => o.PercentDiscount > 0.0);
Then()
.Do(_ => customer.NotifyAboutDiscount());
}
}
}
using NRules.Fluent.Dsl;
using System.Collections.Generic;
using System.Linq;
namespace NRulesTest
{
class PreferredCustomerDiscountRule : Rule
{
public override void Define()
{
Customer customer = null;
IEnumerable<Order> orders = null;
When()
.Match<Customer>(() => customer, c => c.IsPreferred)
.Query(() => orders, x => x
.Match<Order>(
o => o.Customer == customer,
o => o.IsOpen,
o => !o.IsDiscounted)
.Collect()
.Where(c => c.Any()));
Then()
.Do(ctx => ApplyDiscount(orders, 10.0))
.Do(ctx => ctx.UpdateAll(orders));
}
private static void ApplyDiscount(IEnumerable<Order> orders, double discount)
{
foreach (var order in orders)
{
order.ApplyDiscount(discount);
}
}
}
}
using NRules;
using NRules.Fluent;
using System;
namespace NRulesTest
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Hello World!");
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, true);
var order2 = new Order(123457, customer, 1, 100.0, true);
//Insert facts into rules engine's memory
session.Insert(customer);
session.Insert(order1);
session.Insert(order2);
//Start match/resolve/act cycle
session.Fire();
Console.ReadKey();
}
}
}