Пример #1
0
 /// <summary>
 /// method does the actual work of creating a CLIPSParser and parsing
 /// the file.
 /// </summary>
 /// <param name="engine">The engine.</param>
 /// <param name="ins">The ins.</param>
 /// <param name="rv">The rv.</param>
 public virtual void parse(Rete engine, Stream ins, DefaultReturnVector rv)
 {
     try
     {
         CLIPSParser parser = new CLIPSParser(engine, ins);
         Object      expr   = null;
         while ((expr = parser.basicExpr()) != null)
         {
             if (expr is Defrule)
             {
                 Defrule rl = (Defrule)expr;
                 engine.RuleCompiler.addRule(rl);
             }
             else if (expr is Deftemplate)
             {
                 Deftemplate dft = (Deftemplate)expr;
                 engine.CurrentFocus.addTemplate(dft, engine, engine.WorkingMemory);
             }
             else if (expr is IFunction)
             {
                 IFunction fnc = (IFunction)expr;
                 fnc.executeFunction(engine, null);
             }
         }
         if (rv != null)
         {
             rv.addReturnValue(new DefaultReturnValue(Constants.BOOLEAN_OBJECT, true));
         }
     }
     catch (ParseException e)
     {
         engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
     }
 }
Пример #2
0
        public void executeCommand(String commandString)
        {
            Object command = null;

            try
            {
                while ((command = parser.basicExpr()) != null)
                {
                    OnCommand(new CommandEventArgs(command, ChannelId));
                }
            }
            catch (ParseException e)
            {
                OnMessage(new MessageEventArgs(EventType.PARSE_ERROR, e, ChannelId));
            }
        }
Пример #3
0
        public virtual void executeCommand(String commandString, bool blocked)
        {
            List <Object> commandMessages = blocked ? new List <Object>() : null;

            parser.ReInit(new MemoryStream(ASCIIEncoding.ASCII.GetBytes(commandString)));
            Object command = null;

            try
            {
                alreadyReceived.Clear();
                while ((command = parser.basicExpr()) != null)
                {
                    OnCommand(new CommandEventArgs(command, ChannelId));
                    if (blocked)
                    {
                        try
                        {
                            while (blocked)
                            {
                                //router.fillMessageList(ChannelId, commandMessages);
                                int count = commandMessages.Count;
                                if (count > 0)
                                {
                                    MessageEventArgs last = (MessageEventArgs)commandMessages[count - 1];
                                    if (last.Type == EventType.RESULT || last.Type == EventType.ERROR)
                                    {
                                        foreach (object message in commandMessages)
                                        {
                                            ((IList)alreadyReceived).Add(message);
                                        }
                                        commandMessages.Clear();
                                        blocked = false;
                                    }
                                }
                                if (blocked)
                                {
                                    Thread.Sleep(new TimeSpan(10000 * 10));
                                }
                            }
                        }
                        catch (ThreadInterruptedException e)
                        {
                            /* TODO for now we just ignore this case */
                        }
                        blocked = true;
                    }
                }
            }
            catch (ParseException e)
            {
                OnMessage(new MessageEventArgs(EventType.PARSE_ERROR, e, ChannelId));
            }
            catch (TokenMgrError e)
            {
                OnMessage(new MessageEventArgs(EventType.PARSE_ERROR, e, ChannelId));
                parser.ReInit(new MemoryStream(ASCIIEncoding.ASCII.GetBytes(commandString)));
            }
        }
Пример #4
0
        protected void parse(Rete engine, CLIPSParser parser, IList factlist)
        {
            Object itm = null;

            try
            {
                while ((itm = parser.basicExpr()) != null)
                {
                    // System.Console.WriteLine("obj is " + itm.getClass().Name);
                    if (itm is Defrule)
                    {
                        Defrule rule = (Defrule)itm;
                        engine.RuleCompiler.addRule(rule);
                    }
                    else if (itm is Deftemplate)
                    {
                        Deftemplate dt = (Deftemplate)itm;
                        Console.WriteLine("template=" + dt.Name);
                        engine.declareTemplate(dt);
                    }
                    else if (itm is FunctionAction)
                    {
                        FunctionAction fa = (FunctionAction)itm;
                    }
                    else if (itm is IFunction)
                    {
                        IReturnVector rv  = ((IFunction)itm).executeFunction(engine, null);
                        IEnumerator   itr = rv.Iterator;
                        while (itr.MoveNext())
                        {
                            IReturnValue rval = (IReturnValue)itr.Current;
                            Console.WriteLine(rval.StringValue);
                        }
                    }
                }
            }
            catch
            {
                // Console.WriteLine(e.Message);
                parser.ReInit(Console.OpenStandardInput());
            }
        }
Пример #5
0
        public virtual IReturnVector eval(Rete engine, String command)
        {
            IReturnVector result = null;

            try
            {
                CLIPSParser      parser      = new CLIPSParser(engine, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(command)));
                CLIPSInterpreter interpreter = new CLIPSInterpreter(engine);
                Object           expr        = null;
                while ((expr = parser.basicExpr()) != null)
                {
                    result = interpreter.executeCommand(expr);
                }
            }
            catch (ParseException e)
            {
                // we should report the error
                Trace.WriteLine(e.Message);
            }
            return(result);
        }