/// <summary> /// /// </summary> /// <param name="current_node"></param> /// <param name="condition">The NCC condition.</param> /// <param name="conds_higher_up"></param> /// <returns>Returns the NCC node</returns> private ReteNode BuildOrShareNccNodes(ReteNode parent, NCCCondition condition, List <Condition> earlier_conds) { ReteNode bottom_of_subnetwork = BuildOrShareNetworkForConditions(parent, condition.SubConditions, earlier_conds); // look for an existing node to share foreach (ReteNode child in parent.Children) { NCC_Node ncc_child = child as NCC_Node; if (null != ncc_child) { if (ncc_child.Partner.Parent == bottom_of_subnetwork) { return(child); } } } NCC_Node new_ncc_partner = new NCC_Node(); NCC_Partner_Node new_partner = new NCC_Partner_Node(); new_ncc_partner.Parent = parent; // foamliu, 2008/11/21, more comments // insert new at the tail of the list bottom_of_subnetwork.children, so the subnetwork comes first. // parent.Children.Add(new_ncc_partner); new_partner.Parent = bottom_of_subnetwork; bottom_of_subnetwork.Children.Insert(0, new_partner); //new_ncc_partner.Children.Clear(); //new_partner.Children.Clear(); new_ncc_partner.Partner = new_partner; new_partner.NCC_Node = new_ncc_partner; //new_ncc_partner.Items.Clear(); //new_partner.New_Result_Buffer.Clear(); new_partner.Number_Of_Conjuncts = condition.SubConditions.Count; // foamliu, 2008/11/21, more comments // Note: we have to inform NCC node of existing matches before informing the partner, otherwise // lots of matches would all get mixed together in the new-result-buffer. // UpdateNewNodeWithMatchesFromAbove(new_ncc_partner); UpdateNewNodeWithMatchesFromAbove(new_partner); return(new_ncc_partner); }
public static void Main(string[] args) { WME[] wmes = new WME[] { new WME("B1", "^on", "B2"), // w1 new WME("B1", "^on", "B3"), // w2 new WME("B1", "^color", "red"), // w3 new WME("B2", "^on", "table"), // w4 new WME("B2", "^left-of", "B3"), // w5 new WME("B2", "^color", "blue"), // w6 new WME("B3", "^left-of", "B4"), // w7 new WME("B3", "^on", "table"), // w8 new WME("B3", "^color", "red"), // w9 }; Production prod = new Production(); prod.Name = "find-stack-of-two-blocks-to-the-left-of-a-red-block"; Variable x = new Variable("x"); Variable y = new Variable("y"); Variable z = new Variable("z"); Variable w = new Variable("w"); prod.LHS.Add(new PositiveCondition("C1", x, "^on", y)); prod.LHS.Add(new PositiveCondition("C2", y, "^left-of", z)); NCCCondition neg = new NCCCondition(); neg.SubConditions.Add(new PositiveCondition("C3", z, "^color", "red")); neg.SubConditions.Add(new PositiveCondition("C4", z, "^on", w)); prod.LHS.Add(neg); ReteInferenceEngine rete = new ReteInferenceEngine(); rete.AddProduction(prod); foreach (WME wme in wmes) { rete.AddWme(wme); } System.Console.WriteLine("Completed."); }
public void NCCConditionTest() { Production prod = new Production(); prod.Label = "find-stack-of-two-blocks-to-the-left-of-a-red-block"; Variable x = new Variable("x"); Variable y = new Variable("y"); Variable z = new Variable("z"); Variable w = new Variable("w"); prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y)); prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z)); NCCCondition neg = new NCCCondition("C3", z, "color", "red"); neg.ConditionType = ConditionType.NCC; //neg.IsPositive = false; //neg.IsNegative = false; neg.SubConditions.Add(new PositiveCondition("C4", z, "on", w)); prod.AddConditionToLHS(neg); AssertCondition rhs = new AssertCondition("C4", x, "is", "on top"); rhs.ConditionType = ConditionType.Assert; prod.AddConditionToRHS(rhs); Rete rete = new Rete(); rete.AddProduction(prod); WME wme1 = new WME("W1"); wme1.Fields[0] = "B1"; wme1.Fields[1] = "on"; wme1.Fields[2] = "B2"; rete.AddWME(wme1); WME wme2 = new WME("W2"); wme2.Fields[0] = "B1"; wme2.Fields[1] = "on"; wme2.Fields[2] = "B3"; rete.AddWME(wme2); WME wme3 = new WME("W3"); wme3.Fields[0] = "B1"; wme3.Fields[1] = "color"; wme3.Fields[2] = "red"; rete.AddWME(wme3); WME wme4 = new WME("W4"); wme4.Fields[0] = "B2"; wme4.Fields[1] = "on"; wme4.Fields[2] = "table"; rete.AddWME(wme4); WME wme5 = new WME("W5"); wme5.Fields[0] = "B2"; wme5.Fields[1] = "left of"; wme5.Fields[2] = "B3"; rete.AddWME(wme5); WME wme6 = new WME("W6"); wme6.Fields[0] = "B2"; wme6.Fields[1] = "color"; wme6.Fields[2] = "blue"; rete.AddWME(wme6); WME wme7 = new WME("W7"); wme7.Fields[0] = "B3"; wme7.Fields[1] = "left of"; wme7.Fields[2] = "B4"; rete.AddWME(wme7); WME wme8 = new WME("W8"); wme8.Fields[0] = "B3"; wme8.Fields[1] = "on"; wme8.Fields[2] = "table"; rete.AddWME(wme8); WME wme9 = new WME("W9"); wme9.Fields[0] = "B3"; wme9.Fields[1] = "color"; wme9.Fields[2] = "red"; rete.AddWME(wme9); Assert.IsTrue(prod.InferredFacts.Count == 1, "Wrong number of conclusions"); NetworkPrinter printer = new NetworkPrinter(); rete.DummyTopNode.Accept(printer); using (StreamWriter writer = new StreamWriter(@"C:\Temp\NCCTest.log", false)) { writer.Write(printer.Output); writer.Flush(); } Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad"); }