Exemplo n.º 1
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            String asrt = "";

            if (params_Renamed.Length > 0)
            {
                Deffact fact = null;
                if (params_Renamed[0].Value is Deffact)
                {
                    fact = (Deffact)params_Renamed[0].Value;
                }
                else
                {
                    Deftemplate tmpl = (Deftemplate)engine.CurrentFocus.getTemplate(params_Renamed[0].StringValue);
                    if (tmpl == null)
                    {
                        throw new AssertException("Deftemplate named " + params_Renamed[0].StringValue + " was not found.");
                    }

                    fact = (Deffact)tmpl.createFact((Object[])params_Renamed[1].Value, -1);
                }
                if (fact.hasBinding())
                {
                    fact.resolveValues(engine, triggerFacts);
                    fact = fact.cloneFact();
                }
                try
                {
                    engine.assertFact(fact);
                    // if the fact id is still -1, it means it wasn't asserted
                    // if it was asserted, we return the fact id, otherwise
                    // we return "false".
                    if (fact.FactId > 0)
                    {
                        asrt = fact.FactId.ToString();
                    }
                    else
                    {
                        asrt = "false";
                    }
                }
                catch (AssertException e)
                {
                    // we should log this and output an error
                    asrt = "false";
                }
            }
            else
            {
                asrt = "false";
            }
            DefaultReturnVector ret = new DefaultReturnVector();
            DefaultReturnValue  rv  = new DefaultReturnValue(Constants.STRING_TYPE, asrt);

            ret.addReturnValue(rv);
            return(ret);
        }
Exemplo n.º 2
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            bool exec = false;

            if (engine != null && params_Renamed != null && params_Renamed.Length >= 2 && params_Renamed[0].ObjectBinding)
            {
                BoundParam bp   = (BoundParam)params_Renamed[0];
                Deffact    fact = (Deffact)bp.Fact;
                try
                {
                    // first retract the fact
                    engine.retractFact(fact);
                    // now modify the fact
                    SlotParam[] sp = new SlotParam[params_Renamed.Length - 1];
                    for (int idx = 0; idx < sp.Length; idx++)
                    {
                        IParameter p = params_Renamed[idx + 1];
                        if (p is SlotParam)
                        {
                            sp[idx] = (SlotParam)p;
                        }
                    }
                    fact.updateSlots(engine, convertToSlots(sp, fact.Deftemplate));
                    if (fact.hasBinding())
                    {
                        fact.resolveValues(engine, triggerFacts);
                        fact = fact.cloneFact();
                    }
                    // now assert the fact using the same fact-id
                    engine.assertFact(fact);
                    exec = true;
                }
                catch (RetractException e)
                {
                    engine.writeMessage(e.Message);
                }
                catch (AssertException e)
                {
                    engine.writeMessage(e.Message);
                }
            }

            DefaultReturnVector rv   = new DefaultReturnVector();
            DefaultReturnValue  rval = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, exec);

            rv.addReturnValue(rval);
            return(rv);
        }