Giter VIP home page Giter VIP logo

Comments (4)

ddossot avatar ddossot commented on August 15, 2024

Can you provide an example of a Price fact and a OrderQty fact for the same product?

PS: it's label not lable (in Implies/oid/Ind)

from nxbre.

alpesh-miles avatar alpesh-miles commented on August 15, 2024

Hi David,
Thanks for the quick revert.

Following custom data binder is used to assert Price & OrderQty fact in fact base

    public class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
        public string Category { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

    public class Order
    {
        public string CustomerName { get; set; }
        public string Product { get; set; }
        public double Quantity { get; set; }
        public double GrossAmount { get; set; }
        public double Discount { get; set; }
        public double TotalAmount { get; set; }

        public override string ToString()
        {
            return CustomerName;
        }
    }

    class ProductBinder : AbstractBinder
    {
        public ProductBinder() : base(BindingTypes.BeforeAfter) { }

        public override void BeforeProcess()
        {
            foreach (Product p in (ArrayList)BusinessObjects["PRODUCTS"])
            {
                IEF.AssertNewFactOrFail("Price", new object[] { p, p.Price });
            }

            foreach (Order p in (ArrayList)BusinessObjects["ORDERS"])
            {
                IEF.AssertNewFactOrFail("OrderQty", new object[] { p, p.Product, p.Quantity });
            }
        }

        public override void AfterProcess() { }

        public override NewFactEvent OnNewFact
        {
            get
            {
                return new NewFactEvent(NewFactHandler);
            }
        }

        public override object Compute(string operationName, IDictionary arguments)
        {
            switch (operationName)
            {
                case "CalulateTotal":
                    return Convert.ToDouble(arguments["Qty"]) * Convert.ToDouble(arguments["Value"]);
            }

            throw new Exception("Binder can not evaluate " + operationName);
        }

        private void NewFactHandler(NewFactEventArgs nfea)
        {
            if (nfea.Fact.Type == "Category")
                ((Product)nfea.Fact.GetPredicateValue(0)).Category = nfea.Fact.GetPredicateValue(1).ToString();
            if (nfea.Fact.Type == "GrossAmount")
                ((Order)nfea.Fact.GetPredicateValue(0)).GrossAmount = Convert.ToDouble(nfea.Fact.GetPredicateValue(1));
            if (nfea.Fact.Type == "OrderDiscount")
            { 
                ((Order)nfea.Fact.GetPredicateValue(0)).Discount = Convert.ToDouble(nfea.Fact.GetPredicateValue(1));
                Order obj = (Order)nfea.Fact.GetPredicateValue(0);
                obj.TotalAmount = obj.GrossAmount - obj.Discount;
            }
        }
    }

Invoker code looks like below

        private ArrayList GetProducts(int nCount)
        {
            ArrayList prods = new ArrayList();
            Random rnd = new Random();

            for (int i = 0; i < nCount; i++)
            {
                prods.Add(new Product() { Name = "PRODUCT-" + (i + 1), Price = rnd.Next(10000) });
            }
            return prods;
        }

        private ArrayList GetOrders(int nCount, ArrayList prods)
        {
            ArrayList orders = new ArrayList();
            Random rnd = new Random();

            for (int i = 0; i < nCount; i++)
            {
                orders.Add(new Order() { CustomerName = "Customer-" + (i + 1), Product = "PRODUCT-"+rnd.Next(1,5), Quantity = rnd.Next(1, 10) });
            }
            return orders;
        }

        public void PerformProductProcess(IBinder binder)
        {
            string ruleBaseFile = Application.StartupPath + "\\productrating2.ruleml";

            // generate dummy business objects
            Hashtable bo = new Hashtable();
            ArrayList prodList = GetProducts(5);
            bo.Add("PRODUCTS", prodList);
            bo.Add("ORDERS", GetOrders(10, prodList));
            IDictionary businessObjects = bo;

            try
            {

                // instantiate an inference engine, bind my data and process the rules
                IInferenceEngine ie = new IEImpl(binder);
                ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleBaseFile, System.IO.FileAccess.Read));

                ie.Process(businessObjects);

                RefreshStatus(ie);
                IList<IList<NxBRE.InferenceEngine.Rules.Fact>> op = ie.RunQuery(0);
                //double.Parse()
            }
            catch (Exception e)
            {

            }
        }

Do ask for any other detailed information.

Here when I am debugging, CalcuateTotal binder function is not getting called so it seems facts are not matching..

from nxbre.

ddossot avatar ddossot commented on August 15, 2024

I notice the Product class does not override GetHashCode, which means it's going to use the default hashcode (RuntimeHelpers.GetHashCode) ie. be based on the object identity.

This can be a problem in your case if Product and Order.Product are not the same object, which is totally possible if they're deserialized from different sources or hydrated from different DB selects.

It is strongly advised to override GetHashCode (and thus Equals) in all objects that NxBRE has to deal with. Please give it a try.

from nxbre.

ddossot avatar ddossot commented on August 15, 2024

Closing for the lack of OP's responsiveness.

from nxbre.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.