Beispiel #1
0
        /* (non-Javadoc)
         * @see woolfel.engine.rete.Activation#executeActivation(woolfel.engine.Creshendo.Util.Rete.Rete)
         */

        /// <summary>
        /// Execute the right-hand side (aka actions) of the rule.
        /// </summary>
        /// <param name="engine">The engine.</param>
        public void executeActivation(Rete engine)
        {
            // if previous and Current are not null, set the previous/Current
            // of each and then set the reference to null
            remove(engine);
            try
            {
                theRule.TriggerFacts = index.Facts;
                IAction[] actions = theRule.Actions;
                for (int idx = 0; idx < actions.Length; idx++)
                {
                    if (actions[idx] != null)
                    {
                        actions[idx].executeAction(engine, index.Facts);
                    }
                    else
                    {
                        throw new ExecuteException(ExecuteException.NULL_ACTION);
                    }
                }
            }
            catch (ExecuteException e)
            {
                throw e;
            }
        }
Beispiel #2
0
 /// <summary>
 /// method is used to make sure the activation is removed from
 /// TerminalNode2.
 /// </summary>
 /// <param name="engine">The engine.</param>
 protected internal void remove(Rete engine)
 {
     if (tnode != null)
     {
         tnode.removeActivation(engine.WorkingMemory, this);
     }
 }
Beispiel #3
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            DefaultReturnVector ret = new DefaultReturnVector();
            bool add = false;

            if (engine.findFunction(name) == null)
            {
                // first we Get the actual function from the shell function
                IFunction[]    functions  = new IFunction[this.functions.Count];
                IParameter[][] parameters = new IParameter[this.functions.Count][];
                for (int i = 0; i < functions.Length; ++i)
                {
                    ShellFunction sf = (ShellFunction)this.functions[i];
                    functions[i]  = engine.findFunction(sf.Name);
                    parameters[i] = sf.Parameters;
                }
                InterpretedFunction intrfunc = new InterpretedFunction(name, this.parameters, functions, parameters);
                intrfunc.configureFunction(engine);
                engine.declareFunction(intrfunc);
                add = true;
            }

            DefaultReturnValue rv = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, add);

            ret.addReturnValue(rv);
            return(ret);
        }
Beispiel #4
0
 /// <summary> Remove a successor node
 /// </summary>
 /// <param name="">node
 /// </param>
 /// <param name="">engine
 /// </param>
 /// <param name="">mem
 /// @throws AssertException
 ///
 /// </param>
 public virtual void removeSuccessorNode(BaseNode node, Rete engine, IWorkingMemory mem)
 {
     if (removeNode(node))
     {
         // we retract the memories first, before removing the node
         IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
         if (alpha.size() > 0)
         {
             IEnumerator itr = alpha.GetEnumerator();
             while (itr.MoveNext())
             {
                 if (node is BaseAlpha)
                 {
                     BaseAlpha next = (BaseAlpha)node;
                     next.retractFact((IFact)itr.Current, engine, mem);
                 }
                 else if (node is BaseJoin)
                 {
                     BaseJoin next = (BaseJoin)node;
                     next.retractRight((IFact)itr.Current, engine, mem);
                 }
             }
         }
     }
 }
Beispiel #5
0
 /// <summary>
 /// it's unlikely 2 rules are identical, except for the name. The implementation
 /// gets the current memory and propogates, but I wonder how much sense this
 /// makes in a real production environment. An user really shouldn't be deploying
 /// identical rules with different rule name.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="engine">The engine.</param>
 /// <param name="mem">The mem.</param>
 public virtual void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // first, we Get the memory for this node
         IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
         // now we iterate over the entry set
         IEnumerator itr = leftmem.Values.GetEnumerator();
         while (itr.MoveNext())
         {
             Object omem = itr.Current;
             if (omem is IBetaMemory)
             {
                 IBetaMemory bmem = (IBetaMemory)omem;
                 Index       left = bmem.Index;
                 // iterate over the matches
                 IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this);
                 IEnumerator ritr = rightmem.Keys.GetEnumerator();
                 while (ritr.MoveNext())
                 {
                     IFact rfcts = (IFact)ritr.Current;
                     // merge the left and right fact into a new Array
                     node.assertFacts(left.add(rfcts), engine, mem);
                 }
             }
         }
     }
 }
Beispiel #6
0
        public void testAssertWithInterface()
        {
            Console.WriteLine("-----------------------------");
            Console.WriteLine("start testAssertWithInterface");
            Rete engine = new Rete();

            engine.declareObject(typeof(IAccount), "account");
            Assert.IsNotNull(engine);
            Account acc1 = new Account();

            acc1.AccountId   = "1234";
            acc1.AccountType = "new";
            acc1.First       = "fName";
            acc1.Last        = "lName";
            acc1.Middle      = "m";
            acc1.OfficeCode  = "MA";
            acc1.RegionCode  = "NE";
            acc1.Status      = "active";
            acc1.Title       = "MR";
            acc1.Username    = "******";
            try
            {
                engine.assertObject(acc1, "account", false, true);
                Assert.IsTrue(true);
                Assert.AreEqual(1, engine.ObjectCount);
                Console.WriteLine("Number of facts: " + engine.ObjectCount);
                engine.printWorkingMemory(true, true);
            }
            catch (AssertException e)
            {
                Console.WriteLine(e.Message);
            }
            engine.close();
        }
Beispiel #7
0
        /// <summary>
        /// Method is used to pass a fact to the successor nodes
        /// </summary>
        /// <param name="inx">The inx.</param>
        /// <param name="engine">The engine.</param>
        /// <param name="mem">The mem.</param>
        protected internal virtual void propogateAssert(Index inx, Rete engine, IWorkingMemory mem)
        {
            for (int idx = 0; idx < successorNodes.Length; idx++)
            {
                BaseJoin baseJoin = successorNodes[idx] as BaseJoin;
                if (baseJoin != null)
                {
                    baseJoin.assertLeft(inx, engine, mem);
                    return;
                }

                TerminalNode terminalNode = successorNodes[idx] as TerminalNode;
                if (terminalNode != null)
                {
                    terminalNode.assertFacts(inx, engine, mem);
                    return;
                }

                //BaseNode node = successorNodes[idx];
                //if (node is BaseJoin)
                //{
                //    ((BaseJoin) node).assertLeft(inx, engine, mem);
                //}
                //else if (node is TerminalNode)
                //{
                //    ((TerminalNode) node).assertFacts(inx, engine, mem);
                //}
            }
        }
Beispiel #8
0
        /// <summary>
        /// method for propogating the retract
        /// </summary>
        /// <param name="inx">The inx.</param>
        /// <param name="engine">The engine.</param>
        /// <param name="mem">The mem.</param>
        protected internal virtual void propogateRetract(Index inx, Rete engine, IWorkingMemory mem)
        {
            for (int idx = 0; idx < successorNodes.Length; idx++)
            {
                BaseJoin node = successorNodes[idx] as BaseJoin;
                if (node != null)
                {
                    node.retractLeft(inx, engine, mem);
                }
                else
                {
                    TerminalNode tnode = successorNodes[idx] as TerminalNode;
                    if (tnode != null)
                    {
                        tnode.retractFacts(inx, engine, mem);
                    }
                }


                //BaseNode node = successorNodes[idx];
                //if (node is BaseJoin)
                //{
                //    ((BaseJoin) node).retractLeft(inx, engine, mem);
                //}
                //else if (node is TerminalNode)
                //{
                //    ((TerminalNode) node).retractFacts(inx, engine, mem);
                //}
            }
        }
Beispiel #9
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            bool   exec            = false;
            String mode            = "normal";
            DefaultReturnVector rv = new DefaultReturnVector();

            if (params_Renamed != null && params_Renamed.Length == 1)
            {
                exec = true;
                ValueParam vp = (ValueParam)params_Renamed[0];
                if (vp.StringValue.Equals("true", StringComparison.InvariantCultureIgnoreCase))
                {
                    engine.CurrentFocus.Lazy = true;
                    mode = "lazy";
                }
                else if (vp.StringValue.Equals("false", StringComparison.InvariantCultureIgnoreCase))
                {
                    engine.CurrentFocus.Lazy = false;
                }
            }
            DefaultReturnValue drv = new DefaultReturnValue(Constants.STRING_TYPE, mode);

            rv.addReturnValue(drv);
            return(rv);
        }
Beispiel #10
0
 /// <summary> Set the Current node in the sequence of 1-input nodes.
 /// The Current node can be an AlphaNode or a LIANode.
 /// </summary>
 /// <param name="">node
 ///
 /// </param>
 public override void addSuccessorNode(BaseNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // if there are matches, we propogate the facts to
         // the new successor only
         IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
         if (alpha.size() > 0)
         {
             IEnumerator itr = alpha.GetEnumerator();
             while (itr.MoveNext())
             {
                 if (node is BaseAlpha)
                 {
                     BaseAlpha next = (BaseAlpha)node;
                     next.assertFact((IFact)itr.Current, engine, mem);
                 }
                 else if (node is BaseJoin)
                 {
                     BaseJoin next = (BaseJoin)node;
                     next.assertRight((IFact)itr.Current, engine, mem);
                 }
                 else if (node is TerminalNode)
                 {
                     TerminalNode next = (TerminalNode)node;
                     Index        inx  = new Index(new IFact[] { (IFact)itr.Current });
                     next.assertFacts(inx, engine, mem);
                 }
             }
         }
     }
 }
Beispiel #11
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            Decimal bdval = new Decimal(0);

            if (params_Renamed.Length == 1)
            {
                if (params_Renamed[0] is ValueParam)
                {
                    ValueParam n = (ValueParam)params_Renamed[0];
                    bdval = n.BigDecimalValue;
                }
                else if (params_Renamed[0] is BoundParam)
                {
                    BoundParam bp = (BoundParam)params_Renamed[0];
                    bdval = (Decimal)engine.getBinding(bp.VariableName);
                }
                else if (params_Renamed[0] is FunctionParam2)
                {
                    FunctionParam2 n = (FunctionParam2)params_Renamed[0];
                    n.Engine = engine;
                    n.lookUpFunction();
                    IReturnVector rval = (IReturnVector)n.Value;
                    bdval = rval.firstReturnValue().BigDecimalValue;
                }
                double bdh = System.Math.Log(Decimal.ToDouble(bdval)); //.pow(Math.E, bdval.doubleValue());
                bdval = new Decimal(bdh);
            }
            DefaultReturnVector ret = new DefaultReturnVector();
            DefaultReturnValue  rv  = new DefaultReturnValue(Constants.BIG_DECIMAL, bdval);

            ret.addReturnValue(rv);
            return(ret);
        }
Beispiel #12
0
        /// <summary> the implementation just propogates the assert down the network
        /// </summary>
        public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
        {
            IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);

            alpha.addPartialMatch(fact);
            propogateAssert(fact, engine, mem);
        }
Beispiel #13
0
 public virtual void resolveValues(Rete engine, IFact[] triggerFacts)
 {
     for (int idx = 0; idx < boundSlots.Length; idx++)
     {
         if (boundSlots[idx] is MultiSlot)
         {
             // for multislot we have to resolve each slot
             Object[] mvals = (Object[])((MultiSlot)boundSlots[idx]).Value;
             for (int mdx = 0; mdx < mvals.Length; mdx++)
             {
                 if (mvals[mdx] is BoundParam)
                 {
                     BoundParam bp = (BoundParam)mvals[mdx];
                     bp.ResolvedValue = engine.getBinding(bp.VariableName);
                 }
             }
         }
         else if (boundSlots[idx].Value is BoundParam)
         {
             BoundParam bp = (BoundParam)boundSlots[idx].Value;
             if (bp.column > -1)
             {
                 bp.Facts = triggerFacts;
             }
             else
             {
                 bp.ResolvedValue = engine.getBinding(bp.VariableName);
             }
         }
     }
 }
Beispiel #14
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            bool removed = true;

            if (params_Renamed.Length == 1)
            {
                String    template = params_Renamed[0].StringValue;
                ITemplate t        = engine.CurrentFocus.getTemplate(template);
                if (!t.inUse())
                {
                    engine.CurrentFocus.removeTemplate(t, engine, engine.WorkingMemory);
                }
                else
                {
                    removed = false;
                }
            }
            else
            {
                removed = false;
            }
            DefaultReturnVector ret = new DefaultReturnVector();
            DefaultReturnValue  rv  = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, removed);

            ret.addReturnValue(rv);
            return(ret);
        }
Beispiel #15
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            IList       fgroups = engine.FunctionGroups;
            IEnumerator itr     = fgroups.GetEnumerator();
            int         counter = 0;

            while (itr.MoveNext())
            {
                // we iterate over the function groups and print out the
                // functions in each group
                IFunctionGroup fg = (IFunctionGroup)itr.Current;
                engine.writeMessage("++++ " + fg.Name + " ++++" + Constants.LINEBREAK, "t");
                IEnumerator listitr = fg.listFunctions().GetEnumerator();
                while (listitr.MoveNext())
                {
                    IFunction f = (IFunction)listitr.Current;
                    engine.writeMessage("  " + f.Name + Constants.LINEBREAK, "t");
                    counter++;
                }
            }
            engine.writeMessage(counter + " functions" + Constants.LINEBREAK, "t");
            DefaultReturnVector ret = new DefaultReturnVector();

            return(ret);
        }
Beispiel #16
0
        /// <summary> assertLeft takes an array of facts. Since the Current join may be
        /// joining against one or more objects, we need to pass all
        /// previously matched facts.
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            IBetaMemory bmem = new BetaMemoryImpl(linx);

            leftmem.Put(linx, bmem);
            IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this);
            IEnumerator itr = rightmem.Keys.GetEnumerator();

            if (itr != null)
            {
                while (itr.MoveNext())
                {
                    IFact vl = (IFact)itr.Current;
                    // we have to evaluate the function
                    if (vl != null && evaluate(linx.Facts, vl, engine))
                    {
                        bmem.addMatch(vl);
                    }
                }
            }
            if (bmem.matchCount() > 0)
            {
                propogateAssert(linx, engine, mem);
            }
        }
Beispiel #17
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            // the first thing we do is set the values
            DefaultReturnVector ret = new DefaultReturnVector();

            if (params_Renamed.Length == inputParams.Length)
            {
                for (int idx = 0; idx < inputParams.Length; idx++)
                {
                    BoundParam bp = (BoundParam)inputParams[idx];
                    bindings.Put(bp.VariableName, params_Renamed[idx].Value);
                }
                engine.pushScope(this);
                for (int idx = 0; idx < functionParams.Length; idx++)
                {
                    ret = (DefaultReturnVector)internalFunction[idx].executeFunction(engine, functionParams[idx]);
                }
                engine.popScope();
                return(ret);
            }
            else
            {
                DefaultReturnValue rv = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, false);
                ret.addReturnValue(rv);
                DefaultReturnValue rv2 = new DefaultReturnValue(Constants.STRING_TYPE, "incorrect number of parameters");
                ret.addReturnValue(rv2);
                return(ret);
            }
        }
Beispiel #18
0
        public void testFiveRules()
        {
            int       objCount = 25000;
            Random    ran      = new Random();
            ArrayList facts    = new ArrayList();

            // loop and create account and transaction objects
            for (int idx = 0; idx < objCount; idx++)
            {
                Account4 acc = new Account4();
                acc.AccountId   = "acc" + idx;
                acc.AccountType = Convert.ToString(ran.Next(100000));
                acc.First       = Convert.ToString(ran.Next(100000));
                acc.Last        = Convert.ToString(ran.Next(100000));
                acc.Middle      = Convert.ToString(ran.Next(100000));
                acc.Status      = Convert.ToString(ran.Next(100000));
                acc.Title       = Convert.ToString(ran.Next(100000));
                acc.Username    = Convert.ToString(ran.Next(100000));
                acc.CountryCode = "US";
                acc.Cash        = 1298.00;
                facts.Add(acc);
                Transaction tx = new Transaction();
                tx.AccountId = "acc" + idx;
                tx.Total     = 1200000;
                facts.Add(tx);
            }
            Console.WriteLine("created " + objCount + " Accounts and Transactions");
            Rete engine = new Rete();

            engine.declareObject(typeof(Account4));
            engine.declareObject(typeof(Transaction));
            Console.WriteLine("delcare the objects");
            engine.close();
        }
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            bool load = false;

            if (params_Renamed != null && params_Renamed.Length > 0)
            {
                for (int idx = 0; idx < params_Renamed.Length; idx++)
                {
                    String func = params_Renamed[idx].StringValue;
                    try
                    {
                        engine.declareFunction(func);
                        load = true;
                    }
                    catch (Exception e)
                    {
                        load = false;
                    }
                }
            }
            DefaultReturnVector ret = new DefaultReturnVector();
            DefaultReturnValue  rv  = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, load);

            ret.addReturnValue(rv);
            return(ret);
        }
Beispiel #20
0
 /// <summary> Retract a fact from the node
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 ///
 /// </param>
 public override void retractFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     if (evaluate(fact))
     {
         propogateRetract(fact, engine, mem);
     }
 }
Beispiel #21
0
        public void ObjectBindingTest1()
        {
            long ts          = DateTime.Now.Ticks;
            int  fired       = 0;
            int  activations = 0;

            using (TextWriter writer = Console.Out)
            {
                Rete engine = new Rete();
                engine.Watch = WatchType.WATCH_ALL;
                engine.addPrintWriter("Console", writer);
                engine.declareObject(typeof(Account), "Account");
                engine.loadRuleset(getRule1());
                engine.assertObject(GetAcct0(), "Account", false, false);
                activations = engine.CurrentFocus.ActivationCount;
                fired       = engine.fire();
                engine.printWorkingMemory(true, false);

                double endTime = DateTime.Now.Ticks - ts;
                Console.WriteLine(String.Format("ObjectBindingTest1 completed in {0} seconds.", (endTime / 10000000).ToString("0.000000")));
                writer.Flush();
                writer.Close();
                engine.close();
            }

            Assert.IsTrue(fired == 1);
            Assert.IsTrue(activations == 1);
            //AppDomain.Unload(AppDomain.CurrentDomain);
        }
Beispiel #22
0
        public void BasicObjectBindingTest1()
        {
            long  ts          = DateTime.Now.Ticks;
            int   fired       = 0;
            int   activations = 0;
            Basic basic       = new Basic("one", 1);

            using (TextWriter writer = Console.Out)
            {
                Rete engine = new Rete();
                engine.Watch = WatchType.WATCH_ALL;
                engine.addPrintWriter("Console", writer);
                engine.declareObject(typeof(Basic), "Basic");
                engine.loadRuleset(getRule3());

                foreach (Defrule rule in  engine.CurrentFocus.AllRules)
                {
                    Console.WriteLine(rule.toPPString());
                }

                engine.assertObject(basic, "Basic", false, false);
                activations = engine.CurrentFocus.ActivationCount;
                fired       = engine.fire();
                engine.printWorkingMemory(true, false);
                double endTime = DateTime.Now.Ticks - ts;
                Console.WriteLine(String.Format("BasicObjectBindingTest1 completed in {0} seconds.", (endTime / 10000000).ToString("0.000000")));
                writer.Flush();
                writer.Close();
                engine.close();
            }

            Assert.IsTrue(fired == 1);
            Assert.IsTrue(activations == 1);
            //AppDomain.Unload(AppDomain.CurrentDomain);
        }
        public virtual void loadFunctions(Rete engine)
        {
            StringCompareFunction compare = new StringCompareFunction();

            engine.declareFunction(compare);
            funcs.Add(compare);
            StringIndexFunction indx = new StringIndexFunction();

            engine.declareFunction(indx);
            funcs.Add(indx);
            StringLengthFunction strlen = new StringLengthFunction();

            engine.declareFunction(strlen);
            funcs.Add(strlen);
            StringLowerFunction lower = new StringLowerFunction();

            engine.declareFunction(lower);
            funcs.Add(lower);
            StringReplaceFunction strrepl = new StringReplaceFunction();

            engine.declareFunction(strrepl);
            funcs.Add(strrepl);
            StringUpperFunction upper = new StringUpperFunction();

            engine.declareFunction(upper);
            funcs.Add(upper);
            SubStringFunction sub = new SubStringFunction();

            engine.declareFunction(sub);
            funcs.Add(sub);
            StringTrimFunction trim = new StringTrimFunction();

            engine.declareFunction(trim);
            funcs.Add(trim);
        }
Beispiel #24
0
        /// <summary> Retracting from the left is different than retractRight for couple
        /// of reasons.
        /// <ul>
        /// <li> NotJoin will only propogate the facts from the left</li>
        /// <li> NotJoin never needs to merge the left and right</li>
        /// </ul>
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void retractLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);

            leftmem.Remove(linx);
            propogateRetract(linx, engine, mem);
        }
Beispiel #25
0
        /// <summary> if all the facts have not expired, the method returns true. If a fact has
        /// expired, the method will retract the fact.
        /// </summary>
        /// <param name="">inx
        /// </param>
        /// <param name="">engine
        /// </param>
        /// <param name="">mem
        /// </param>
        /// <returns>
        ///
        /// </returns>
        protected internal virtual bool checkFacts(Index inx, Rete engine, IWorkingMemory mem)
        {
            IFact[] facts   = inx.Facts;
            bool    fresh   = true;
            long    current = (DateTime.Now.Ticks - 621355968000000000) / 10000;

            for (int idx = 0; idx < facts.Length; idx++)
            {
                if (facts[idx] is ITemporalFact)
                {
                    TemporalDeffact tf = (TemporalDeffact)facts[idx];
                    if (tf.ExpirationTime < current)
                    {
                        // the fact has expired
                        fresh = false;
                        try
                        {
                            engine.retractFact(tf);
                        }
                        catch (RetractException e)
                        {
                            // we do nothing
                        }
                    }
                }
            }
            return(fresh);
        }
Beispiel #26
0
        /// <summary>
        /// assertLeft takes an array of facts. Since the Current join may be
        /// joining against one or more objects, we need to pass all
        /// previously matched facts.
        /// </summary>
        /// <param name="linx">The linx.</param>
        /// <param name="engine">The engine.</param>
        /// <param name="mem">The mem.</param>
        public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            // we create a new list for storing the matches.
            // any fact that isn't in the list will be evaluated.
            IBetaMemory bmem = new BetaMemoryImpl(linx);

            leftmem.Put(bmem.Index, bmem);
            IGenericMap <IFact, IFact> rightmem = (IGenericMap <IFact, IFact>)mem.getBetaRightMemory(this);
            int         prevCount = bmem.matchCount();
            IEnumerator itr       = rightmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                IFact rfcts = (IFact)itr.Current;
                if (evaluate(linx.Facts, rfcts, engine))
                {
                    // it matched, so we Add it to the beta memory
                    bmem.addMatch(rfcts);
                }
            }
            // since the Fact[] is entering the left for the first time,
            // if there are no matches, we merged the facts propogate.
            if (bmem.matchCount() == 0)
            {
                propogateAssert(linx, engine, mem);
            }
        }
        /// <summary> the function will printout one or more templates. This implementation
        /// is slightly different than CLIPS in that it can take one or more
        /// template names. The definition in CLIPS beginners guide states the
        /// function does the following: (ppdeftemplate &lt;deftemplate-name>)
        /// </summary>
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            GenericHashMap <object, object> filter = new GenericHashMap <object, object>();

            if (params_Renamed != null && params_Renamed.Length > 0)
            {
                for (int idx = 0; idx < params_Renamed.Length; idx++)
                {
                    if (params_Renamed[idx] is ValueParam)
                    {
                        Object df = ((ValueParam)params_Renamed[idx]).Value;
                        filter.Put(df, df);
                    }
                }
            }
            List <Object> templ = (List <Object>)engine.CurrentFocus.Templates;
            IEnumerator   itr   = templ.GetEnumerator();

            while (itr.MoveNext())
            {
                ITemplate tp = (ITemplate)itr.Current;
                if (filter.Get(tp.Name) != null)
                {
                    engine.writeMessage(tp.toPPString() + "\r\n", "t");
                }
            }
            return(new DefaultReturnVector());
        }
Beispiel #28
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            bool bound = false;

            if (params_Renamed.Length == 2)
            {
                String name = params_Renamed[0].StringValue;
                Object val  = null;
                if (params_Renamed[1] is ValueParam)
                {
                    val = params_Renamed[1].Value;
                }
                else if (params_Renamed[1] is FunctionParam2)
                {
                    FunctionParam2 fp2 = (FunctionParam2)params_Renamed[1];
                    fp2.Engine = engine;
                    fp2.lookUpFunction();
                    DefaultReturnVector drv = (DefaultReturnVector)fp2.Value;
                    val = drv.firstReturnValue().Value;
                }
                engine.setBindingValue(name, val);
                bound = true;
            }
            DefaultReturnVector ret = new DefaultReturnVector();
            DefaultReturnValue  rv  = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, bound);

            ret.addReturnValue(rv);
            return(ret);
        }
Beispiel #29
0
        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();
        }
        /// <summary> Assert from the right side is always going to be from an Alpha node.
        ///
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
        {
            // Get the memory for the node
            HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory)mem.getBetaRightMemory(this);
            NotEqHashIndex       inx      = new NotEqHashIndex(NodeUtils.getRightBindValues(binds, rfact));

            rightmem.addPartialMatch(inx, rfact);
            // now that we've added the facts to the list, we
            // proceed with evaluating the fact
            // else we compare the fact to all facts in the left
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            // since there may be key collisions, we iterate over the
            // values of the HashMap. If we used keySet to iterate,
            // we could encounter a ClassCastException in the case of
            // key collision.
            IEnumerator itr = leftmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                Index linx = (Index)itr.Current;
                if (evaluate(linx.Facts, rfact))
                {
                    // now we propogate
                    propogateAssert(linx.add(rfact), engine, mem);
                }
            }
        }
Beispiel #31
0
 /// <summary> Retract simply propogates it down the network
 /// </summary>
 public override void retractFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     IAlphaMemory alpha = (IAlphaMemory) mem.getAlphaMemory(this);
     if (alpha.removePartialMatch(fact) != null)
     {
         propogateRetract(fact, engine, mem);
     }
 }
Beispiel #32
0
 /// <summary>
 /// Evaluates a command given in any language that is supported by this
 /// LanguageAdapter.
 /// </summary>
 /// <param name="engine">The engine.</param>
 /// <param name="command">The command.</param>
 /// <param name="language">The language.</param>
 /// <returns>
 /// The result returned from the Rete-engine in the given
 /// language.
 /// @throws ParseException
 /// </returns>
 public virtual String Evaluate(Rete.Rete engine, String command, String language)
 {
     StringReader reader = new StringReader(command);
     StringWriter writer = new StringWriter();
     ////	CLIPSParser parser = new CLIPSParser(engine, reader, writer, false);
     //	parser.startParser();
     //	String res = writer.toString();
     //	parser = null;
     return null;
 }
        public void EvaluatorTest()
        {
            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 c = new Variable("c");
            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z));
            //prod.AddConditionToLHS(new condition("C3", z, "color", "red"));

            PositiveCondition noteq = new PositiveCondition("NOT BLUE", x, "color", "blue");
            noteq.Evaluator = EvaluatorManager.Instance.Evaluators["NotEquals"];
            prod.AddConditionToLHS(noteq);

            prod.AddConditionToRHS(new AssertCondition("C5", x, "calls", z));

            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);

            NetworkPrinter printer = new NetworkPrinter();
            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\EvaluatorTest.log", false))
            {
                writer.Write(printer.Output);
                writer.Flush();
            }

            Assert.IsTrue(prod.InferredFacts.Count == 2, "Wrong number of conclusions");
            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }
Beispiel #34
0
        public FactEditor(Rete engine)
            : base(engine)
        {
            InitBlock();
            //UPGRADE_ISSUE: Constructor 'java.awt.BorderLayout.BorderLayout' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtBorderLayout"'
            setLayout(new BorderLayout());
            setTitle("Assert new Fact");
            //UPGRADE_ISSUE: Constructor 'java.awt.CardLayout.CardLayout' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtCardLayout"'
            contentPanel = new JPanel(new CardLayout());
            //UPGRADE_ISSUE: Field 'java.awt.BorderLayout.CENTER' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtBorderLayout"'
            add(contentPanel, BorderLayout.CENTER);
            cancelButton = new JButton("Cancel", IconLoader.getImageIcon("cancel"));
            cancelButton.addActionListener(this);
            assertButton = new JButton("Assert Fact", IconLoader.getImageIcon("database_add"));
            assertButton.addActionListener(this);
            assertButton.setVisible(false);
            backButton = new JButton("Back", IconLoader.getImageIcon("resultset_previous"));
            backButton.addActionListener(this);
            backButton.setVisible(false);
            nextButton = new JButton("Next", IconLoader.getImageIcon("resultset_next"));
            nextButton.setHorizontalTextPosition(SwingConstants.LEFT);
            nextButton.addActionListener(this);
            JPanel buttonPanel = new JPanel();
            //UPGRADE_ISSUE: Constructor 'java.awt.FlowLayout.FlowLayout' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtFlowLayout"'
            //UPGRADE_ISSUE: Field 'java.awt.FlowLayout.CENTER' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtFlowLayout"'
            buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 1));
            buttonPanel.add(cancelButton);
            buttonPanel.add(backButton);
            buttonPanel.add(nextButton);
            buttonPanel.add(assertButton);
            //UPGRADE_ISSUE: Field 'java.awt.BorderLayout' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000"'
            add(buttonPanel, BorderLayout.PAGE_END);

            dumpAreaTemplate.setEditable(false);
            //UPGRADE_NOTE: If the given Font Name does not exist, a default Font instance is created. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1075"'
            //UPGRADE_TODO: Method 'java.awt.Font.Plain' was converted to 'System.Drawing.FontStyle.Regular' which has a different behavior. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1073_javaawtFontPLAIN_f"'
            dumpAreaTemplate.setFont(new System.Drawing.Font("Courier", 12, (System.Drawing.FontStyle) System.Drawing.FontStyle.Regular));
            dumpAreaTemplate.setRows(5);
            dumpAreaFact.setEditable(false);
            //UPGRADE_NOTE: If the given Font Name does not exist, a default Font instance is created. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1075"'
            //UPGRADE_TODO: Method 'java.awt.Font.Plain' was converted to 'System.Drawing.FontStyle.Regular' which has a different behavior. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1073_javaawtFontPLAIN_f"'
            dumpAreaFact.setFont(new System.Drawing.Font("Courier", 12, (System.Drawing.FontStyle) System.Drawing.FontStyle.Regular));
            dumpAreaFact.setRows(5);
        }
        public void MultiBuiltinTest()
        {
            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 c = new Variable("c");
            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            //prod.AddConditionToLHS(new condition("C2", y, "left of", z));
            prod.AddConditionToLHS(new PositiveCondition("C3", x, "color", c));

            FunctionCondition funCond = new FunctionCondition("F4", c, new FuncTerm("funcNotEquals", new funcNotEquals()), new StringTerm("blue"));
            funCond.ConditionType = ConditionType.Function;
            prod.AddConditionToLHS(funCond);

            prod.AddConditionToRHS(new AssertCondition("C4", x, "is", c));

            Rete rete = new Rete();
            rete.AddProduction(prod);

            NetworkPrinter printer = new NetworkPrinter();
            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\MultiBuiltinTest.log", false))
            {
                writer.Write(printer.Output);
                writer.WriteLine();
                writer.WriteLine();
                writer.WriteLine();
                writer.WriteLine();
                writer.WriteLine("-----------------------------------------------");
                writer.Flush();
            }

            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);

            printer = new NetworkPrinter();
            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\MultiBuiltinTest.log", true))
            {
                writer.WriteLine();
                writer.WriteLine();
                writer.WriteLine();
                writer.WriteLine();
                writer.Write(printer.Output);
                writer.Flush();
            }

            //Assert.IsTrue(rete.RulesThatFired.Count == 1, "Rule did not fire.");
            Assert.IsTrue(prod.InferredFacts.Count == 2, "Wrong number of InferredFacts");
            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }
        public void GrandParents()
        {
            Rete rete = new Rete();

            Production prod = new Production();
            prod.Label = "Whose your daddy";

            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");
            prod.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod.AddConditionToRHS(new AssertCondition(y, "child", x));

            rete.AddProduction(prod);

            Production prod1 = new Production();
            prod1.Label = "Whose your grand daddy";

            prod1.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod1.AddConditionToLHS(new PositiveCondition(y, "parent", z));
            prod1.AddConditionToRHS(new AssertCondition(x, "gparent", z));

            rete.AddProduction(prod1);

            Production prod2 = new Production();
            prod2.Label = "Whose your grand child";

            prod2.AddConditionToLHS(new PositiveCondition(x, "gparent", y));
            prod2.AddConditionToRHS(new AssertCondition(y, "gchild", x));

            rete.AddProduction(prod2);

            Production prod3 = new Production();
            prod3.Label = "Whose your great grand daddy";

            prod3.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod3.AddConditionToLHS(new PositiveCondition(y, "gparent", z));
            prod3.AddConditionToRHS(new AssertCondition(x, "ggparent", z));

            rete.AddProduction(prod3);

            List<WME> wmes = new List<WME>();

            wmes.Add(new WME("george", "gchild", "kendall"));
            wmes.Add(new WME("kendall", "parent", "immanuel"));
            wmes.Add(new WME("louis", "gparent", "richard"));
            wmes.Add(new WME("jacques", "parent", "jean-francois"));
            wmes.Add(new WME("friedrich", "parent", "louis"));
            wmes.Add(new WME("louis", "gparent", "willard"));
            wmes.Add(new WME("willard", "gchild", "ludwig"));
            wmes.Add(new WME("miles", "parent", "ludwig"));
            wmes.Add(new WME("ludwig", "gchild", "noam"));
            wmes.Add(new WME("miles", "gparent", "wilhelm"));
            wmes.Add(new WME("friedrich", "parent", "jean-francois"));
            wmes.Add(new WME("bijan", "gchild", "george"));
            wmes.Add(new WME("wilhelm", "gparent", "bertrand"));
            wmes.Add(new WME("alan", "parent", "bijan"));
            wmes.Add(new WME("kendall", "parent", "gottlob"));
            wmes.Add(new WME("david", "gchild", "bertrand"));
            wmes.Add(new WME("alan", "gchild", "henry"));
            wmes.Add(new WME("kendall", "gparent", "louis"));
            wmes.Add(new WME("willard", "gchild", "willard"));
            wmes.Add(new WME("richard", "gchild", "pat"));
            wmes.Add(new WME("miles", "parent", "friedrich"));
            wmes.Add(new WME("noam", "gparent", "ludwig"));
            wmes.Add(new WME("jean-francois", "gchild", "bertrand"));
            wmes.Add(new WME("louis", "parent", "bijan"));
            wmes.Add(new WME("willard", "parent", "kendall"));
            wmes.Add(new WME("gottlob", "gparent", "kendall"));
            wmes.Add(new WME("immanuel", "gchild", "wilhelm"));
            wmes.Add(new WME("george", "parent", "drew"));
            wmes.Add(new WME("friedrich", "gchild", "david"));
            wmes.Add(new WME("gottlob", "gchild", "bertrand"));
            wmes.Add(new WME("wilhelm", "parent", "ludwig"));
            wmes.Add(new WME("henry", "gchild", "willard"));
            wmes.Add(new WME("alan", "gchild", "richard"));
            wmes.Add(new WME("george", "gchild", "miles"));
            wmes.Add(new WME("george", "gparent", "willard"));
            wmes.Add(new WME("alasdair", "gparent", "willard"));
            wmes.Add(new WME("willard", "gparent", "immanuel"));
            wmes.Add(new WME("jacques", "gparent", "george"));
            wmes.Add(new WME("henry", "gchild", "rudolf"));
            wmes.Add(new WME("wilhelm", "gparent", "miles"));
            wmes.Add(new WME("noam", "gparent", "jean-francois"));
            wmes.Add(new WME("pat", "gchild", "friedrich"));
            wmes.Add(new WME("rudolf", "gchild", "david"));
            wmes.Add(new WME("john", "gchild", "stanley"));
            wmes.Add(new WME("jacques", "gchild", "bijan"));
            wmes.Add(new WME("george", "parent", "miles"));
            wmes.Add(new WME("louis", "parent", "drew"));
            wmes.Add(new WME("rudolf", "parent", "jacques"));
            wmes.Add(new WME("bertrand", "gparent", "ludwig"));
            wmes.Add(new WME("gottlob", "parent", "john"));
            wmes.Add(new WME("miles", "gchild", "miles"));
            wmes.Add(new WME("bijan", "gchild", "noam"));
            wmes.Add(new WME("jean-francois", "gparent", "george"));
            wmes.Add(new WME("miles", "gparent", "henry"));
            wmes.Add(new WME("kendall", "gchild", "ludwig"));
            wmes.Add(new WME("louis", "gparent", "david"));
            wmes.Add(new WME("noam", "parent", "george"));
            wmes.Add(new WME("pat", "parent", "richard"));
            wmes.Add(new WME("george", "parent", "ludwig"));
            wmes.Add(new WME("george", "gparent", "alan"));
            wmes.Add(new WME("pat", "gparent", "louis"));
            wmes.Add(new WME("david", "parent", "friedrich"));
            wmes.Add(new WME("wilhelm", "gchild", "alasdair"));
            wmes.Add(new WME("pat", "gchild", "bertrand"));
            wmes.Add(new WME("noam", "parent", "stanley"));
            wmes.Add(new WME("friedrich", "gparent", "bijan"));
            wmes.Add(new WME("willard", "gchild", "bertrand"));
            wmes.Add(new WME("kendall", "gparent", "noam"));
            wmes.Add(new WME("kendall", "gparent", "george"));
            wmes.Add(new WME("drew", "parent", "gottlob"));
            wmes.Add(new WME("david", "parent", "kendall"));
            wmes.Add(new WME("alan", "gparent", "gottlob"));
            wmes.Add(new WME("richard", "parent", "jacques"));
            wmes.Add(new WME("alasdair", "gchild", "david"));
            wmes.Add(new WME("richard", "parent", "bijan"));
            wmes.Add(new WME("jean-francois", "gparent", "alan"));
            wmes.Add(new WME("jacques", "gchild", "louis"));
            wmes.Add(new WME("miles", "gchild", "jean-francois"));
            wmes.Add(new WME("pat", "parent", "miles"));
            wmes.Add(new WME("kendall", "parent", "gottlob"));
            wmes.Add(new WME("gottlob", "parent", "willard"));
            wmes.Add(new WME("jean-francois", "parent", "willard"));
            wmes.Add(new WME("stanley", "gchild", "ludwig"));
            wmes.Add(new WME("john", "gchild", "miles"));
            wmes.Add(new WME("john", "parent", "jean-francois"));
            wmes.Add(new WME("alan", "gchild", "louis"));
            wmes.Add(new WME("stanley", "gchild", "wilhelm"));
            wmes.Add(new WME("drew", "gparent", "miles"));
            wmes.Add(new WME("jacques", "parent", "immanuel"));
            wmes.Add(new WME("david", "gparent", "willard"));
            wmes.Add(new WME("miles", "parent", "alasdair"));
            wmes.Add(new WME("immanuel", "gparent", "ludwig"));
            wmes.Add(new WME("kendall", "gparent", "henry"));
            wmes.Add(new WME("immanuel", "gparent", "ludwig"));
            wmes.Add(new WME("miles", "gchild", "john"));
            wmes.Add(new WME("louis", "gchild", "alan"));
            wmes.Add(new WME("willard", "parent", "alan"));
            wmes.Add(new WME("kendall", "parent", "ludwig"));
            wmes.Add(new WME("george", "gchild", "alasdair"));
            wmes.Add(new WME("richard", "gparent", "bertrand"));

            int i = 1;
            foreach (WME wme in wmes)
            {
                wme.Label = "W" + i++;
                rete.AddWME(wme);
            }

            NetworkPrinter printer = new NetworkPrinter();
            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\GrandParentsTest.log", false))
            {
                writer.Write(printer.Output);
                writer.Flush();
            }

            Assert.IsTrue(rete.WorkingMemory.Count == 100, "Bad");
            Assert.IsTrue(prod.InferredFacts.Count == 33, "Wrong number of conclusions");
            Assert.IsTrue(prod1.InferredFacts.Count == 42, "Wrong number of conclusions");
            Assert.IsTrue(prod2.InferredFacts.Count == 29, "Wrong number of conclusions");
            Assert.IsTrue(prod3.InferredFacts.Count == 38, "Wrong number of conclusions");
        }
        public void RemoveWME()
        {
            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");

            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z));
            prod.AddConditionToLHS(new PositiveCondition("C3", z, "color", "red"));
            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");

            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");

            rete.RemoveWME(wme9);

            Assert.IsTrue(prod.InferredFacts.Count == 0, "Wrong number of conclusions");

            Assert.IsTrue(rete.WorkingMemory.Count == 8, "Bad");
        }
        public void NegativeConditionTest()
        {
            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 c = new Variable("c");

            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z));
            //prod.AddConditionToLHS(new condition("C3", z, "color", c));

            NegativeCondition neg = new NegativeCondition("C3", z, "color", "blue");
            neg.ConditionType = ConditionType.Negative;
            //neg.IsPositive = false;
            //neg.IsNegative = true;
            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);

            //WME wme10 = new WME("W10");
            //wme10.fields[0] = "B4";
            //wme10.fields[1] = "color";
            //wme10.fields[2] = "green";
            //rete.add_wme(wme10);

            //rete_node dummy = rete.DummyTopNode;

            NetworkPrinter printer = new NetworkPrinter();
            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\NegativeConditionTest.log", false))
            {
                writer.Write(printer.Output);
                writer.Flush();
            }

            //Assert.IsTrue(rete.RulesThatFired.Count == 1, "Rule did not fire.");
            Assert.IsTrue(prod.InferredFacts.Count == 1, "Wrong number of conclusions");
            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }
Beispiel #39
0
        /// <summary> Create a GUI-Instance for Jamocha.
        /// 
        /// </summary>
        /// <param name="">engine
        /// The Jamocha-engine that will be used in the GUI.
        /// 
        /// </param>
        public JamochaGui(Rete engine)
        {
            InitBlock();

            // set up the frame
            //UPGRADE_ISSUE: Constructor 'java.awt.BorderLayout.BorderLayout' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtBorderLayout"'
            this.ContentPane.setLayout(new BorderLayout());
            this.setTitle("Jamocha");
            setSizeAndLocation();

            // show logo
            //UPGRADE_ISSUE: Constructor 'java.awt.BorderLayout.BorderLayout' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtBorderLayout"'
            JPanel logoPanel = new JPanel(new BorderLayout());
            //UPGRADE_ISSUE: Field 'java.awt.BorderLayout.EAST' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtBorderLayout"'
            logoPanel.add(new JLabel(IconLoader.getImageIcon("jamocha")), BorderLayout.EAST);
            //UPGRADE_ISSUE: Field 'java.awt.BorderLayout.NORTH' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtBorderLayout"'
            this.ContentPane.add(logoPanel, BorderLayout.NORTH);

            // create a tabbed pane
            tabbedPane = new JTabbedPane();
            //UPGRADE_ISSUE: Field 'java.awt.BorderLayout.CENTER' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtBorderLayout"'
            this.ContentPane.add(tabbedPane, BorderLayout.CENTER);

            // add MenuBar
            menuBar = new JamochaMenuBar(this);
            this.setJMenuBar(menuBar);

            // create a rete engine
            this.engine = engine;

            // create a shell tab and add it to the tabbed pane
            ShellPanel shellPanel = new ShellPanel(this);
            tabbedPane.addTab("Shell", IconLoader.getImageIcon("application_osx_terminal"), shellPanel, "Jamocha Shell");
            panels.add(shellPanel);
            FactsPanel factsPanel = new FactsPanel(this);
            tabbedPane.addTab("Facts", IconLoader.getImageIcon("database"), factsPanel, "View or modify Facts");
            panels.add(factsPanel);
            TemplatesPanel templatesPanel = new TemplatesPanel(this);
            tabbedPane.addTab("Templates", IconLoader.getImageIcon("brick"), templatesPanel, "View or modify Templates");
            panels.add(templatesPanel);
            FunctionsPanel functionsPanel = new FunctionsPanel(this);
            tabbedPane.addTab("Functions", IconLoader.getImageIcon("cog"), functionsPanel, "View Functions");
            panels.add(functionsPanel);
            RetePanel retePanel = new RetePanel(this);
            tabbedPane.addTab("Rete", IconLoader.getImageIcon("eye"), retePanel, "View the Rete-network");
            panels.add(retePanel);
            LogPanel logPanel = new LogPanel(this);
            tabbedPane.addTab("Log", IconLoader.getImageIcon("monitor"), logPanel, "View alle messages from or to the Rete-engine");
            panels.add(logPanel);
            SettingsPanel settingsPanel = new SettingsPanel(this);
            tabbedPane.addTab("Settings", IconLoader.getImageIcon("wrench"), settingsPanel, "Settings for Jamocha");
            panels.add(settingsPanel);

            // add the tab pane to the frame
            //UPGRADE_ISSUE: Field 'java.awt.BorderLayout.CENTER' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaawtBorderLayout"'
            this.ContentPane.add(tabbedPane, BorderLayout.CENTER);

            tabbedPane.addChangeListener(this);
            // add a listener to the frame to kill the engine when the GUI is closed
            addWindowListener(new AnonymousClassWindowAdapter(this));
        }
        public void ReteWithHash()
        {
            Production prod = new Production();
            prod.Label = "ReteWithHash";
            Variable a = new Variable("a");
            Variable b = new Variable("b");
            Variable c = new Variable("c");
            Variable d = new Variable("d");
            Variable s = new Variable("s");

            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");

            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z));
            prod.AddConditionToLHS(new PositiveCondition("C3", z, "color", "red"));
            prod.AddConditionToLHS(new PositiveCondition("C4", a, "color", "maize"));
            prod.AddConditionToLHS(new PositiveCondition("C5", b, "color", "blue"));
            prod.AddConditionToLHS(new PositiveCondition("C6", c, "color", "green"));
            prod.AddConditionToLHS(new PositiveCondition("C7", d, "color", "white"));
            prod.AddConditionToLHS(new PositiveCondition("C8", s, "on", "table"));
            prod.AddConditionToLHS(new PositiveCondition("C9", z, a, b));
            prod.AddConditionToLHS(new PositiveCondition("C10", a, "left of", d));

            prod.AddConditionToRHS(new AssertCondition("C4", x, "is", "on top"));

            Rete rete = new Rete();
            rete.AddProduction(prod);

            NetworkPrinter printer = new NetworkPrinter();
            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\ReteWithHash-Pre.log", false))
            {
                writer.Write(printer.Output);
                writer.Flush();
            }

            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);

            printer = new NetworkPrinter();
            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\ReteWithHash.log", false))
            {
                writer.Write(printer.Output);
                writer.Flush();
            }

            Assert.IsTrue(prod.InferredFacts.Count == 0, "Wrong number of conclusions");
        }
        public void Test4pointsTo1()
        {
            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");

            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z));
            prod.AddConditionToLHS(new PositiveCondition("C3", z, "color", "red"));
            prod.AddConditionToLHS(new PositiveCondition("C4", x, "has", "car"));
            prod.AddConditionToLHS(new PositiveCondition("C5", x, "wants", z));
            prod.AddConditionToRHS(new AssertCondition("C5", x, "gets", z));
            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);

            WME wme10 = new WME("W10");
            wme10.Fields[0] = "B2";
            wme10.Fields[1] = "has";
            wme10.Fields[2] = "bike";
            rete.AddWME(wme10);

            WME wme11 = new WME("W11");
            wme11.Fields[0] = "B1";
            wme11.Fields[1] = "has";
            wme11.Fields[2] = "car";
            rete.AddWME(wme11);

            WME wme12 = new WME("W12");
            wme12.Fields[0] = "B1";
            wme12.Fields[1] = "wants";
            wme12.Fields[2] = "B3";
            rete.AddWME(wme12);

            //Assert.IsTrue(rete.RulesThatFired.Count == 1, "Rule did not fire.");
            Assert.IsTrue(prod.InferredFacts.Count == 1, "Wrong number of conclusions");
            Assert.IsTrue(rete.WorkingMemory.Count == 12, "Bad");
        }
Beispiel #42
0
 /// <summary> the implementation just propogates the assert down the network
 /// </summary>
 public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     IAlphaMemory alpha = (IAlphaMemory) mem.getAlphaMemory(this);
     alpha.addPartialMatch(fact);
     propogateAssert(fact, engine, mem);
 }
 /// <summary>
 /// Called when [rete] is visited.
 /// </summary>
 /// <param name="rete">The rete.</param>
 public override void OnRete(Rete rete)
 {
     _sb.AppendLine(rete.ToString());
 }
        public void TestAddWME()
        {
            WME wme = new WME();
            wme.Fields[0] = "x";
            wme.Fields[1] = "y";
            wme.Fields[2] = "z";

            Rete am = new Rete();
            am.AddWME(wme);
        }
 /// <summary>
 /// Called when [rete] is visited.
 /// </summary>
 /// <param name="rete">The rete.</param>
 public virtual void OnRete(Rete rete)
 {
 }