/// <summary> /// convienance method for creating a Non-Shadow fact. /// </summary> /// <param name="data">The data.</param> /// <param name="dclass">The dclass.</param> /// <param name="id">The id.</param> /// <returns></returns> protected internal virtual IFact createNSFact(Object data, Defclass dclass, long id) { Deftemplate dft = (Deftemplate)CurrentFocus.getTemplate(dclass); NSFact fact = new NSFact(dft, dclass, data, dft.AllSlots, id); return(fact); }
public void testThreeBinding() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); Defclass dc2 = new Defclass(typeof (TestBean3)); Deftemplate dtemp2 = dc.createDeftemplate("testBean3"); Slot[] slts = dtemp.AllSlots; Slot[] slts2 = dtemp2.AllSlots; Binding bn = new Binding(); bn.LeftRow = (0); bn.LeftIndex = (0); bn.RightIndex = (0); Binding bn2 = new Binding(); bn2.LeftRow = (0); bn2.LeftIndex = (2); bn2.RightIndex = (2); Binding bn3 = new Binding(); bn3.LeftRow = (1); bn3.LeftIndex = (0); bn3.RightIndex = (0); Binding[] binds = {bn, bn2, bn3}; HashedEqBNode btnode = new HashedEqBNode(1); btnode.Bindings = (binds); Console.WriteLine("betaNode::" + btnode.toPPString()); Assert.IsNotNull(btnode.toPPString()); }
/// <summary> /// The current implementation of assertObject is simple, but flexible. This /// version is not multi-threaded and doesn't use an event queue. Later on a /// multi-threaded version will be written which overrides the base /// implementation. If the user passes a specific template name, the engine /// will attempt to only propogate the fact down that template. if no /// template name is given, the engine will propogate the fact down all input /// nodes, including parent templates. /// </summary> /// <param name="data">The data.</param> /// <param name="template">The template.</param> /// <param name="statc">if set to <c>true</c> [statc].</param> /// <param name="shadow">if set to <c>true</c> [shadow].</param> public virtual void assertObject(Object data, String template, bool statc, bool shadow) { Defclass dc = null; if (template == null) { dc = engine.findDefclass(data); } else { dc = engine.findDefclassByTemplate(template); } if (dc != null) { if (statc && !StaticFacts.ContainsKey(data)) { IFact shadowfact = createFact(data, dc, template, engine.nextFactId()); // Add it to the static fact map StaticFacts.Put(data, shadowfact); assertFact(shadowfact); } else if (!DynamicFacts.ContainsKey(data)) { if (shadow) { // first Add the rule engine as a listener if (dc.JavaBean) { try { MethodInfo miHandler = typeof(Rete).GetMethod("PropertyHasChanged", BindingFlags.NonPublic | BindingFlags.Instance); Delegate d = Delegate.CreateDelegate(dc.DelegateType, this.engine, miHandler); dc.AddListenerMethod.Invoke(data, (Object[])new Object[] { d }); } catch (TargetInvocationException e) { System.Diagnostics.Trace.WriteLine(e.Message); } catch (UnauthorizedAccessException e) { System.Diagnostics.Trace.WriteLine(e.Message); } } // second, lookup the deftemplate and create the // shadow fact IFact shadowfact = createFact(data, dc, template, engine.nextFactId()); // Add it to the dynamic fact map DynamicFacts.Put(data, shadowfact); assertFact(shadowfact); } else { IFact nsfact = createNSFact(data, dc, engine.nextFactId()); DynamicFacts.Put(data, nsfact); assertFact(nsfact); } } } }
/// <summary> /// </summary> public NSFact(Deftemplate template, Defclass clazz, Object instance, Slot[] values, long id) : base(template, instance, values, id) { deftemplate = template; dclazz = clazz; objInstance = instance; slots = values; this.id = id; timeStamp_Renamed_Field = (DateTime.Now.Ticks - 621355968000000000) / 10000; }
public void testGetDeftemplate() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Assert.AreEqual("testBean2", dtemp.Name); Console.WriteLine("deftemplate name: " + dtemp.Name); Assert.AreEqual(6, dtemp.NumberOfSlots); Console.WriteLine("slot count: " + dtemp.NumberOfSlots); }
public void testCreateDeffactWithPrimitive() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); bean.Attr1 = ("testString"); IFact fact = dtemp.createFact(bean, dc, 1); Assert.IsNotNull(fact); Console.WriteLine(fact.toFactString()); }
/// <summary> The key is either the Defclass or a string name /// </summary> public virtual bool containsTemplate(Object key) { if (key is Defclass) { Defclass dc = (Defclass)key; return(deftemplates.ContainsKey(dc.ClassObject.FullName)); } else { return(deftemplates.ContainsKey(key)); } }
/// <summary> Method will make a copy and return it. When a copy is made, the /// Method classes are not cloned. Instead, just the HashMap is cloned. /// </summary> /// <returns> /// /// </returns> public virtual Defclass cloneDefclass() { Defclass dcl = new Defclass(OBJECT_CLASS); dcl.addListener = addListener; dcl.INFO = INFO; dcl.ISBEAN = ISBEAN; dcl.PROPS = PROPS; dcl.removeListener = removeListener; dcl.methods = CollectionFactory.localMap(); dcl.methods.putAll(methods); return(dcl); }
public void testOneSlot() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); Slot[] slts = dtemp.AllSlots; ObjectTypeNode otn = new ObjectTypeNode(1, dtemp); AlphaNode an = new AlphaNode(1); slts[0].Value = ConversionUtils.convert(110); an.Operator = Constants.EQUAL; an.Slot = (slts[0]); Console.WriteLine("node::" + an.ToString()); Assert.IsNotNull(an.ToString(), "Should have a value."); }
/// <summary> /// Modify will call retract with the old fact, followed by updating the fact /// instance and asserting the fact. /// </summary> /// <param name="data">The data.</param> public virtual void modifyObject(Object data) { if (DynamicFacts.ContainsKey(data)) { Defclass dc = (Defclass)engine.findDefclass(data); // first we retract the fact IFact ft = (IFact)DynamicFacts.RemoveWithReturn(data); String tname = ft.Deftemplate.Name; long fid = ft.FactId; retractFact(ft); // create a new fact with the same ID ft = createFact(data, dc, tname, fid); DynamicFacts.Put(data, ft); assertFact(ft); } }
public void testIndex() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); bean.Attr1 = ("testString"); bean.Attr2 = (1); short a3 = 3; bean.Attr3 = (a3); long a4 = 101; bean.Attr4 = (a4); float a5 = 10101; bean.Attr5 = (a5); double a6 = 101.101; bean.Attr6 = (a6); IFact fact = dtemp.createFact(bean, dc, 1); Assert.IsNotNull(fact); Console.WriteLine(fact.toFactString()); CompositeIndex ci = new CompositeIndex("attr1", Constants.EQUAL, fact.getSlotValue(0)); Assert.IsNotNull(ci); Console.WriteLine(ci.toPPString()); GenericHashMap<object, object> map = new GenericHashMap<object, object>(); map.Put(ci, bean); CompositeIndex ci2 = new CompositeIndex("attr1", Constants.EQUAL, fact.getSlotValue(0)); Assert.IsTrue(map.ContainsKey(ci2)); CompositeIndex ci3 = new CompositeIndex("attr1", Constants.NOTEQUAL, fact.getSlotValue(0)); Assert.IsFalse(map.ContainsKey(ci3)); CompositeIndex ci4 = new CompositeIndex("attr1", Constants.NILL, fact.getSlotValue(0)); Assert.IsFalse(map.ContainsKey(ci4)); CompositeIndex ci5 = new CompositeIndex("attr1", Constants.NOTNILL, fact.getSlotValue(0)); Assert.IsFalse(map.ContainsKey(ci5)); }
public void testCreateDeffactWithNull() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); bean.Attr1 = (null); bean.Attr2 = (1); short a3 = 3; bean.Attr3 = (a3); long a4 = 101; bean.Attr4 = (a4); float a5 = 10101; bean.Attr5 = (a5); double a6 = 101.101; bean.Attr6 = (a6); IFact fact = dtemp.createFact(bean, dc, 1); Assert.IsNotNull(fact); Console.WriteLine(fact.toFactString()); }
/// <summary> Method will create a Fact from the given object instance /// /// </summary> /// <param name="">data /// </param> /// <returns> /// /// </returns> public virtual IFact createFact(Object data, Defclass clazz, long id) { // first we clone the slots Slot[] values = cloneAllSlots(); // now we set the values for (int idx = 0; idx < values.Length; idx++) { Object val = clazz.getSlotValue(idx, data); if (val == null) { values[idx].Value = Constants.NIL_SYMBOL; } else { values[idx].Value = val; } } Deffact newfact = new Deffact(this, data, values, id); return(newfact); }
/// <summary> /// The implementation will look in the current module in focus. If it isn't /// found, it will search the other modules. The last module it checks should /// be the main module. /// </summary> /// <param name="data">The data.</param> /// <param name="dclass">The dclass.</param> /// <param name="template">The template.</param> /// <param name="id">The id.</param> /// <returns></returns> protected internal virtual IFact createFact(Object data, Defclass dclass, String template, long id) { IFact ft = null; ITemplate dft = null; if (template == null) { dft = CurrentFocus.getTemplate(dclass.ClassObject.FullName); } else { dft = CurrentFocus.getTemplate(template); } // if the deftemplate is null, check the other modules if (dft == null) { // Get the entry set from the agenda and iterate IEnumerator itr = modules.Values.GetEnumerator(); while (itr.MoveNext()) { IModule mod = (IModule)itr.Current; if (mod.containsTemplate(dclass)) { dft = mod.getTemplate(dclass); } } // we've searched every module, so now check main if (dft == null && main.containsTemplate(dclass)) { dft = main.getTemplate(dclass); } else { // throw an exception throw new AssertException("Could not find the template"); } } ft = dft.createFact(data, dclass, id); return(ft); }
public void testTwoSlots() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); Slot[] slts = dtemp.AllSlots; ObjectTypeNode otn = new ObjectTypeNode(1, dtemp); AlphaNode an1 = new AlphaNode(1); AlphaNode an2 = new AlphaNode(1); slts[0].Value = ("testString"); slts[1].Value = (ConversionUtils.convert(999)); an1.Slot = (slts[0]); an1.Operator = (Constants.EQUAL); Console.WriteLine("node::" + an1.toPPString()); Assert.IsNotNull(an1.toPPString()); an2.Slot = (slts[1]); an2.Operator = (Constants.GREATER); Console.WriteLine("node::" + an2.toPPString()); Assert.IsNotNull(an2.toPPString()); }
public void testEqual() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); bean.Attr1 = ("testString"); bean.Attr2 = (1); short a3 = 3; bean.Attr3 = (a3); long a4 = 101; bean.Attr4 = (a4); float a5 = 10101; bean.Attr5 = (a5); double a6 = 101.101; bean.Attr6 = (a6); IFact fact = dtemp.createFact(bean, dc, 1); Assert.IsNotNull(fact); Console.WriteLine(fact.toFactString()); CompositeIndex ci = new CompositeIndex("attr1", Constants.EQUAL, fact.getSlotValue(0)); Assert.IsNotNull(ci); Console.WriteLine(ci.toPPString()); }
public void testCreateNode2() { // first create a rule engine instance Rete engine = new Rete(); NotJoin bn = new NotJoin(engine.nextNodeId()); Assert.IsNotNull(bn); // create a defclass Defclass dc = new Defclass(typeof (TestBean2)); // create deftemplate Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Binding[] binds = new Binding[1]; Binding b1 = new Binding(); b1.LeftIndex = (0); b1.IsObjectVar = (false); b1.LeftRow = (0); b1.RightIndex = (0); b1.VarName = ("var1"); binds[0] = b1; // set the binding bn.Bindings = (binds); engine.close(); }
public void testAssertRightMultiple() { // first create a rule engine instance Rete engine = new Rete(); NotJoin bn = new NotJoin(engine.nextNodeId()); Assert.IsNotNull(bn); // create a defclass Defclass dc = new Defclass(typeof (TestBean2)); // create deftemplate Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Binding[] binds = new Binding[1]; Binding b1 = new Binding(); b1.LeftIndex = (0); b1.IsObjectVar = (false); b1.LeftRow = (0); b1.RightIndex = (0); b1.VarName = ("var1"); binds[0] = b1; // set the binding bn.Bindings = (binds); int count = 10; ArrayList data = new ArrayList(); for (int idx = 0; idx < count; idx++) { TestBean2 bean = new TestBean2(); bean.Attr1 = ("random" + (idx + 1)); bean.Attr2 = (101); short s = 10001; bean.Attr3 = (s); long l = 10101018; bean.Attr4 = (l); bean.Attr5 = (1010101); bean.Attr6 = (1001.1001); IFact fact = dtemp.createFact(bean, dc, engine.nextFactId()); data.Add(fact); } IEnumerator itr = data.GetEnumerator(); while (itr.MoveNext()) { try { IFact f1 = (IFact) itr.Current; bn.assertRight(f1, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } } IGenericMap<IFact, IFact> bmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn); Assert.AreEqual(count, bmem.Count); engine.close(); }
public void testAssertLeftOne() { // first create a rule engine instance Rete engine = new Rete(); NotJoin bn = new NotJoin(engine.nextNodeId()); Assert.IsNotNull(bn); // create a defclass Defclass dc = new Defclass(typeof (TestBean2)); // create deftemplate Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Binding[] binds = new Binding[1]; Binding b1 = new Binding(); b1.LeftIndex = (0); b1.IsObjectVar = (false); b1.LeftRow = (0); b1.RightIndex = (0); b1.VarName = ("var1"); binds[0] = b1; // set the binding bn.Bindings = (binds); TestBean2 bean = new TestBean2(); bean.Attr1 = ("random1"); bean.Attr2 = (101); short s = 10001; bean.Attr3 = (s); long l = 10101018; bean.Attr4 = (l); bean.Attr5 = (1010101); bean.Attr6 = (1001.1001); IFact f1 = dtemp.createFact(bean, dc, engine.nextFactId()); try { bn.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory); IGenericMap<Object, Object> bmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn); Assert.AreEqual(1, bmem.Count); } catch (AssertException e) { Console.WriteLine(e.Message); } engine.close(); }
/// <summary> /// Declares the object. /// </summary> /// <param name="obj">The obj.</param> /// <param name="templateName">Name of the template.</param> /// <param name="parent">- /// the parent template</param> public virtual void declareObject(Type obj, String templateName, String parent) { // if the class hasn't already been declared, we create a defclass // and deftemplate for the class. if (!defclass.ContainsKey(obj)) { Defclass dclass = new Defclass(obj); defclass.Put(obj, dclass); if (templateName == null) { templateName = obj.FullName; } templateToDefclass.Put(templateName, dclass); if (!CurrentFocus.containsTemplate(dclass)) { Deftemplate dtemp = null; // if the parent is found, we set it if (String.IsNullOrEmpty(parent) == false) { ITemplate ptemp = workingMem.CurrentFocus.findParentTemplate(parent); if (ptemp != null) { dtemp = dclass.createDeftemplate(templateName, ptemp); dtemp.Parent = ptemp; } else { // we need to throw an exception to let users know the // parent template wasn't found } } else { dtemp = dclass.createDeftemplate(templateName); } // the key for the deftemplate is the declass, this means // that when we assert an object instance to the engine, // we need to use the Class to lookup defclass and then // use the defclass to lookup the deftemplate. Once we // have the deftemplate, we can use it to create the shadow // fact for the object instance. if (dtemp != null) { CurrentFocus.addTemplate(dtemp, this, workingMem); writeMessage(dtemp.Name, "t"); } } } }
/// <summary> Method will make a copy and return it. When a copy is made, the /// Method classes are not cloned. Instead, just the HashMap is cloned. /// </summary> /// <returns> /// /// </returns> public virtual Defclass cloneDefclass() { Defclass dcl = new Defclass(OBJECT_CLASS); dcl.addListener = addListener; dcl.INFO = INFO; dcl.ISBEAN = ISBEAN; dcl.PROPS = PROPS; dcl.removeListener = removeListener; dcl.methods = CollectionFactory.localMap(); dcl.methods.putAll(methods); return dcl; }
public void testObject() { Defclass dc = new Defclass(typeof (Account)); Assert.IsNotNull(dc); Deftemplate dtemp = dc.createDeftemplate("account"); Assert.IsNotNull(dtemp); }
public void testPropertyDescriptor() { Defclass dc = new Defclass(typeof (TestBean2)); Assert.IsNotNull(dc.PropertyDescriptors); }
public void testNonJavaBeans() { Defclass dc = new Defclass(typeof (TestBean)); Assert.AreEqual(false, dc.JavaBean); }
public void testOneFact() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); bean.Attr1 = "testString"; bean.Attr2 = 1; short a3 = 3; bean.Attr3 = a3; long a4 = 101; bean.Attr4 = a4; float a5 = 10101; bean.Attr5 = a5; double a6 = 101.101; bean.Attr6 = a6; IFact fact = dtemp.createFact(bean, dc, 1); IFact[] list1 = new IFact[] {fact}; IFact[] list2 = new IFact[] {fact}; Index in1 = new Index(list1); Index in2 = new Index(list2); Assert.AreEqual(true, in1.Equals(in2)); }
public void testPropogateNoMatch() { Console.WriteLine("testPropogateNoMatch"); // first create a rule engine instance Rete engine = new Rete(); NotJoin nj = new NotJoin(engine.nextNodeId()); HashedEqBNode bn2 = new HashedEqBNode(engine.nextNodeId()); Assert.IsNotNull(nj); // create a defclass Defclass dc = new Defclass(typeof (TestBean2)); // create deftemplate Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Binding[] binds = new Binding[1]; Binding b1 = new Binding(); b1.LeftIndex = (0); b1.IsObjectVar = (false); b1.LeftRow = (0); b1.RightIndex = (0); b1.VarName = ("var1"); binds[0] = b1; Binding[] binds2 = new Binding[1]; Binding b2 = new Binding(); b2.LeftIndex = (1); b2.IsObjectVar = (false); b2.LeftRow = (0); b2.RightIndex = (1); b2.VarName = ("var2"); binds2[0] = b2; // set the binding nj.Bindings = (binds); bn2.Bindings = (binds2); // now add the second Not to the first try { nj.addSuccessorNode(bn2, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } int count = 10; ArrayList data = new ArrayList(); for (int idx = 0; idx < count; idx++) { TestBean2 bean = new TestBean2(); bean.Attr1 = ("random" + idx); bean.Attr2 = (101 + idx); short s = 10001; bean.Attr3 = (s); long l = 10101018 + idx; bean.Attr4 = (l); bean.Attr5 = (1010101); bean.Attr6 = (1001.1001); IFact fact = dtemp.createFact(bean, dc, engine.nextFactId()); data.Add(fact); } IEnumerator itr = data.GetEnumerator(); while (itr.MoveNext()) { try { IFact f1 = (IFact) itr.Current; nj.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory); nj.assertRight(f1, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } } IGenericMap<IFact, IFact> rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(nj); Assert.AreEqual(count, rbmem.Count); IGenericMap<Object, Object> lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(nj); Assert.AreEqual(count, lbmem.Count); // now check the BetaMemory has matches Console.WriteLine(nj.toPPString()); IEnumerator mitr = lbmem.Values.GetEnumerator(); while (mitr.MoveNext()) { IBetaMemory btm = (IBetaMemory) mitr.Current; Assert.AreEqual(0, btm.matchCount()); Console.WriteLine("match count=" + btm.matchCount() + " - " + btm.toPPString()); } IGenericMap<Object, Object> lbmem2 = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn2); Assert.AreEqual(count, lbmem2.Count); Console.WriteLine(bn2.toPPString()); IEnumerator mitr2 = lbmem2.Values.GetEnumerator(); engine.close(); // TODO need to update the test to check the match count // by getting the right memory }
/// <summary> Method will create a Fact from the given object instance /// /// </summary> /// <param name="">data /// </param> /// <returns> /// /// </returns> public virtual IFact createFact(Object data, Defclass clazz, long id) { // first we clone the slots Slot[] values = cloneAllSlots(); // now we set the values for (int idx = 0; idx < values.Length; idx++) { Object val = clazz.getSlotValue(idx, data); if (val == null) { values[idx].Value = Constants.NIL_SYMBOL; } else { values[idx].Value = val; } } Deffact newfact = new Deffact(this, data, values, id); return newfact; }
public void testFiveFacts() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); bean.Attr1 = "testString"; bean.Attr2 = 1; short a3 = 3; bean.Attr3 = a3; long a4 = 101; bean.Attr4 = a4; float a5 = 10101; bean.Attr5 = a5; double a6 = 101.101; bean.Attr6 = a6; TestBean2 bean2 = new TestBean2(); bean2.Attr1 = "testString2"; bean2.Attr2 = 12; short a32 = 32; bean2.Attr3 = a32; long a42 = 1012; bean2.Attr4 = a42; float a52 = 101012; bean2.Attr5 = a52; double a62 = 101.1012; bean2.Attr6 = a62; TestBean2 bean3 = new TestBean2(); bean3.Attr1 = "testString3"; bean3.Attr2 = 13; short a33 = 33; bean3.Attr3 = a33; long a43 = 1013; bean3.Attr4 = a43; float a53 = 101013; bean3.Attr5 = a53; double a63 = 101.1013; bean3.Attr6 = a63; TestBean2 bean4 = new TestBean2(); bean4.Attr1 = "testString4"; bean4.Attr2 = 14; short a34 = 34; bean4.Attr3 = a34; long a44 = 1014; bean4.Attr4 = a44; float a54 = 101014; bean4.Attr5 = a54; double a64 = 101.1014; bean4.Attr6 = a64; TestBean2 bean5 = new TestBean2(); bean5.Attr1 = "testString5"; bean5.Attr2 = 15; short a35 = 35; bean5.Attr3 = a35; long a45 = 1015; bean5.Attr4 = a45; float a55 = 101015; bean5.Attr5 = a55; double a65 = 101.1015; bean5.Attr6 = a65; IFact fact = dtemp.createFact(bean, dc, 1); IFact fact2 = dtemp.createFact(bean2, dc, 1); IFact fact3 = dtemp.createFact(bean3, dc, 1); IFact fact4 = dtemp.createFact(bean4, dc, 1); IFact fact5 = dtemp.createFact(bean5, dc, 1); IFact[] list1 = new IFact[] {fact, fact2, fact3, fact4, fact5}; IFact[] list2 = new IFact[] {fact, fact2, fact3, fact4, fact5}; Index in1 = new Index(list1); Index in2 = new Index(list2); Assert.AreEqual(true, in1.Equals(in2)); }
public void testPropertyCount() { Defclass dc = new Defclass(typeof (TestBean2)); if (dc.PropertyDescriptors != null) { PropertyInfo[] pds = dc.PropertyDescriptors; Console.WriteLine(pds.Length); for (int idx = 0; idx < pds.Length; idx++) { PropertyInfo pd = pds[idx]; Console.WriteLine(idx + " name=" + pd.Name); } Assert.AreEqual(6, pds.Length); } else { // this will report the test failed Assert.IsNotNull(dc.PropertyDescriptors); } }
public void testInterface() { Defclass dc = new Defclass(typeof (IAccount)); Assert.IsNotNull(dc); Deftemplate dtemp = dc.createDeftemplate("account"); Assert.IsNotNull(dtemp); Assert.AreEqual(14, dtemp.AllSlots.Length); }
public void testAssertAndRetract() { // first create a rule engine instance Rete engine = new Rete(); NotJoin bn = new NotJoin(engine.nextNodeId()); Assert.IsNotNull(bn); // create a defclass Defclass dc = new Defclass(typeof (TestBean2)); // create deftemplate Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Binding[] binds = new Binding[1]; Binding b1 = new Binding(); b1.LeftIndex = (0); b1.IsObjectVar = (false); b1.LeftRow = (0); b1.RightIndex = (0); b1.VarName = ("var1"); binds[0] = b1; // set the binding bn.Bindings = (binds); int count = 10; ArrayList data = new ArrayList(); for (int idx = 0; idx < count; idx++) { TestBean2 bean = new TestBean2(); bean.Attr1 = ("random"); bean.Attr2 = (101); short s = 10001; bean.Attr3 = (s); long l = 10101018; bean.Attr4 = (l); bean.Attr5 = (1010101); bean.Attr6 = (1001.1001); IFact fact = dtemp.createFact(bean, dc, engine.nextFactId()); data.Add(fact); } IEnumerator itr = data.GetEnumerator(); while (itr.MoveNext()) { try { IFact f1 = (IFact) itr.Current; bn.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory); bn.assertRight(f1, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } } IGenericMap<IFact, IFact> rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn); Assert.AreEqual(count, rbmem.Count); IGenericMap<Object, Object> lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn); Assert.AreEqual(count, lbmem.Count); int retract = 5; try { for (int idx = 0; idx < retract; idx++) { IFact f2 = (IFact) data[idx]; bn.retractRight(f2, engine, engine.WorkingMemory); } } catch (RetractException e) { Console.WriteLine(e.Message); } rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn); Assert.AreEqual(retract, rbmem.Count); lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn); Assert.AreEqual(count, lbmem.Count); // now check the BetaMemory has matches Console.WriteLine(bn.toPPString()); IEnumerator mitr = lbmem.Values.GetEnumerator(); while (mitr.MoveNext()) { IBetaMemory btm = (IBetaMemory) mitr.Current; Console.WriteLine("match count=" + btm.matchCount() + " - " + btm.toPPString()); } engine.close(); }
public void testPropogateChange() { Console.WriteLine("testPropogateChange"); // first create a rule engine instance Rete engine = new Rete(); NotJoin nj = new NotJoin(engine.nextNodeId()); HashedEqBNode bn2 = new HashedEqBNode(engine.nextNodeId()); Assert.IsNotNull(nj); // create a defclass Defclass dc = new Defclass(typeof (TestBean2)); // create deftemplate Deftemplate dtemp = dc.createDeftemplate("testBean2"); Assert.IsNotNull(dtemp); Binding[] binds = new Binding[1]; Binding b1 = new Binding(); b1.LeftIndex = (0); b1.IsObjectVar = (false); b1.LeftRow = (0); b1.RightIndex = (0); b1.VarName = ("var1"); binds[0] = b1; Binding[] binds2 = new Binding[1]; Binding b2 = new Binding(); b2.LeftIndex = (1); b2.IsObjectVar = (false); b2.LeftRow = (0); b2.RightIndex = (1); b2.VarName = ("var2"); binds2[0] = b2; // set the binding nj.Bindings = (binds); bn2.Bindings = (binds2); // now add the second Not to the first try { nj.addSuccessorNode(bn2, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } int count = 2; ArrayList data = new ArrayList(); for (int idx = 0; idx < count; idx++) { TestBean2 bean = new TestBean2(); bean.Attr1 = ("random"); bean.Attr2 = (101 + idx); short s = 10001; bean.Attr3 = (s); long l = 10101018 + idx; bean.Attr4 = (l); bean.Attr5 = (1010101); bean.Attr6 = (1001.1001); IFact fact = dtemp.createFact(bean, dc, engine.nextFactId()); data.Add(fact); } IEnumerator itr = data.GetEnumerator(); while (itr.MoveNext()) { try { IFact f1 = (IFact) itr.Current; nj.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } } IGenericMap<Object, Object> lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(nj); Assert.AreEqual(count, lbmem.Count); IGenericMap<Object, Object> lbmem2 = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn2); Assert.AreEqual(2, lbmem2.Count); itr = data.GetEnumerator(); while (itr.MoveNext()) { try { IFact f1 = (IFact) itr.Current; nj.assertRight(f1, engine, engine.WorkingMemory); } catch (AssertException e) { Console.WriteLine(e.Message); } } IGenericMap<IFact, IFact> rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(nj); Assert.AreEqual(count, rbmem.Count); // once the facts are asserted to the right, there should be no // facts in successor. this makes sure that assertRight correctly // results in a retract. lbmem2 = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn2); Assert.AreEqual(0, lbmem2.Count); engine.close(); }
public void testBeanInfo() { Defclass dc = new Defclass(typeof (TestBean2)); Assert.IsNotNull(dc.BeanInfo); }
public void testHashMapIndex() { Defclass dc = new Defclass(typeof (TestBean2)); Deftemplate dtemp = dc.createDeftemplate("testBean2"); TestBean2 bean = new TestBean2(); bean.Attr1 = "testString"; bean.Attr2 = 1; short a3 = 3; bean.Attr3 = a3; long a4 = 101; bean.Attr4 = a4; float a5 = 10101; bean.Attr5 = a5; double a6 = 101.101; bean.Attr6 = a6; TestBean2 bean2 = new TestBean2(); bean2.Attr1 = "testString2"; bean2.Attr2 = 12; short a32 = 32; bean2.Attr3 = a32; long a42 = 1012; bean2.Attr4 = a42; float a52 = 101012; bean2.Attr5 = a52; double a62 = 101.1012; bean2.Attr6 = a62; TestBean2 bean3 = new TestBean2(); bean3.Attr1 = "testString3"; bean3.Attr2 = 13; short a33 = 33; bean3.Attr3 = a33; long a43 = 1013; bean3.Attr4 = a43; float a53 = 101013; bean3.Attr5 = a53; double a63 = 101.1013; bean3.Attr6 = a63; TestBean2 bean4 = new TestBean2(); bean4.Attr1 = "testString4"; bean4.Attr2 = 14; short a34 = 34; bean4.Attr3 = a34; long a44 = 1014; bean4.Attr4 = a44; float a54 = 101014; bean4.Attr5 = a54; double a64 = 101.1014; bean4.Attr6 = a64; TestBean2 bean5 = new TestBean2(); bean5.Attr1 = "testString5"; bean5.Attr2 = 15; short a35 = 35; bean5.Attr3 = a35; long a45 = 1015; bean5.Attr4 = a45; float a55 = 101015; bean5.Attr5 = a55; double a65 = 101.1015; bean5.Attr6 = a65; IFact fact = dtemp.createFact(bean, dc, 1); IFact fact2 = dtemp.createFact(bean2, dc, 1); IFact fact3 = dtemp.createFact(bean3, dc, 1); IFact fact4 = dtemp.createFact(bean4, dc, 1); IFact fact5 = dtemp.createFact(bean5, dc, 1); IFact[] list1 = new IFact[] {fact, fact2, fact3, fact4, fact5}; IFact[] list2 = new IFact[] {fact, fact2, fact3, fact4, fact5}; Index in1 = new Index(list1); Index in2 = new Index(list2); Assert.AreEqual(true, in1.Equals(in2)); GenericHashMap<object, object> map = new GenericHashMap<object, object>(); map.Put(in1, list1); // simple test to see if HashMap.ContainsKey(in1) works Assert.AreEqual(true, map.ContainsKey(in1)); // now test with the second instance of index, this should return // true, since Index class overrides Equals() and GetHashCode(). Assert.AreEqual(true, map.ContainsKey(in2)); }
/// <summary> implementation looks up the template and assumes the key /// is the classname or the user define name. /// </summary> public virtual ITemplate getTemplate(Defclass key) { return((Deftemplate)deftemplates.Get(key.ClassObject.FullName)); }
/// <summary> /// convienance method for creating a Non-Shadow fact. /// </summary> /// <param name="data">The data.</param> /// <param name="dclass">The dclass.</param> /// <param name="id">The id.</param> /// <returns></returns> protected internal virtual IFact createNSFact(Object data, Defclass dclass, long id) { Deftemplate dft = (Deftemplate) CurrentFocus.getTemplate(dclass); NSFact fact = new NSFact(dft, dclass, data, dft.AllSlots, id); return fact; }
public void testJavaBean() { Defclass dc = new Defclass(typeof (TestBean2)); Assert.AreEqual(true, dc.JavaBean); }
/// <summary> implementation looks up the template and assumes the key /// is the classname or the user define name. /// </summary> public virtual ITemplate getTemplate(Defclass key) { return (Deftemplate) deftemplates.Get(key.ClassObject.FullName); }
/// <summary> /// The implementation will look in the current module in focus. If it isn't /// found, it will search the other modules. The last module it checks should /// be the main module. /// </summary> /// <param name="data">The data.</param> /// <param name="dclass">The dclass.</param> /// <param name="template">The template.</param> /// <param name="id">The id.</param> /// <returns></returns> protected internal virtual IFact createFact(Object data, Defclass dclass, String template, long id) { IFact ft = null; ITemplate dft = null; if (template == null) { dft = CurrentFocus.getTemplate(dclass.ClassObject.FullName); } else { dft = CurrentFocus.getTemplate(template); } // if the deftemplate is null, check the other modules if (dft == null) { // Get the entry set from the agenda and iterate IEnumerator itr = modules.Values.GetEnumerator(); while (itr.MoveNext()) { IModule mod = (IModule) itr.Current; if (mod.containsTemplate(dclass)) { dft = mod.getTemplate(dclass); } } // we've searched every module, so now check main if (dft == null && main.containsTemplate(dclass)) { dft = main.getTemplate(dclass); } else { // throw an exception throw new AssertException("Could not find the template"); } } ft = dft.createFact(data, dclass, id); return ft; }