Пример #1
0
        public void Run()
        {
            reason = true;
            IMapper sensorMapper = mapper.getMapper(typeof(AdvancedSensor));

            while (reason)
            {
                if (!brain.executeReasonerOn.Equals("When Sensors are ready"))
                {
                    lock (brain.toLock)
                    {
                        MyDebugger.MyDebug("going to wait for pulse by brain");
                        brain.solverWaiting = true;
                        Monitor.Wait(brain.toLock);
                    }
                }
                try
                {
                    factsPath = Path.GetTempFileName();

                    using (StreamWriter fs = new StreamWriter(factsPath, true))
                    {
                        string toAppend = SensorsManager.GetSensorsMapping(brain);
                        if (!reason)
                        {
                            return;
                        }
                        fs.Write(toAppend);
                        fs.Close();
                    }
                }
                catch (Exception e)
                {
                    MyDebugger.MyDebug("CAUGHT EXECPTION!!!!");
                    MyDebugger.MyDebug(e.Message);
                    MyDebugger.MyDebug(e.StackTrace);
                }


                Handler handler = new DesktopHandler(new DLV2DesktopService(@".\lib\dlv2.exe"));
                //With DLV2DesktopService I get a Error during parsing: --> Invalid #show directive: setOnActuator/1--competition-output.
                //With DLVDesktopService the AS, obviously, are wrongly parsed
                InputProgram encoding = new ASPInputProgram();
                MyDebugger.MyDebug("adding encoding");
                encoding.AddFilesPath(Path.GetFullPath(brain.ASPFilePath));
                InputProgram facts = new ASPInputProgram();
                MyDebugger.MyDebug("adding facts");
                facts.AddFilesPath(factsPath);
                handler.AddProgram(encoding);
                handler.AddProgram(facts);
                handler.AddOption(new OptionDescriptor("--filter=setOnActuator/1 "));
                stopwatch.Restart();
                MyDebugger.MyDebug("starting sync");
                Output o = handler.StartSync();
                if (!o.ErrorsString.Equals(""))
                {
                    MyDebugger.MyDebug(o.ErrorsString + " " + o.OutputString);
                }
                AnswerSets answers = (AnswerSets)o;
                stopwatch.Stop();
                brain.asSteps++;
                brain.asTotalMS += stopwatch.ElapsedMilliseconds;
                MyDebugger.MyDebug("num of AS " + answers.Answersets.Count);
                if (answers.Answersets.Count > 0)
                {
                    lock (brain.toLock)
                    {
                        foreach (SimpleActuator actuator in brain.getActuators())
                        {
                            actuator.parse(answers.Answersets[0]);
                        }
                        brain.setActuatorsReady(true);
                    }
                }
                if (!brain.maintainFactFile)
                {
                    File.Delete(factsPath);
                }
            }
        }
        public void Run()
        {
            reason = true;
            IMapper sensorMapper = mapper.getMapper(typeof(AdvancedSensor));

            //Debug.Log("mapper " + sensorMapper);
            while (reason)
            {
                //Thread.Sleep(1000);
                //Debug.Log("executing thread");
                lock (brain.toLock)
                {
                    brain.solverWaiting = true;
                    Monitor.Wait(brain.toLock);
                    try
                    {
                        stopwatch.Restart();
                        factsPath = Path.GetTempFileName();

                        using (StreamWriter fs = new StreamWriter(factsPath, true))
                        {
                            //Debug.Log("creating file "+ factsPath);
                            string toAppend = "";
                            foreach (AdvancedSensor sensor in brain.getSensors())
                            {
                                //Stopwatch temp = new Stopwatch();
                                //temp.Start();
                                toAppend += sensor.Map();
                                //temp.Stop();
                                //Debug.Log(toAppend);
                                //Debug.Log(toAppend);
                            }
                            //Debug.Lof(fs.)
                            fs.Write(toAppend);
                            fs.Close();
                            //Debug.Log("closing stream");
                        }
                        stopwatch.Stop();
                        factsSteps++;
                        factsAvgTime += stopwatch.ElapsedMilliseconds;
                    }
                    catch (Exception e)
                    {
                        UnityEngine.Debug.LogError(e.Message);
                        UnityEngine.Debug.LogError(e.StackTrace);
                    }
                }
                //Debug.Log(Path.GetFullPath(@".\lib\dlv.exe"));
                Handler      handler  = new DesktopHandler(new DLVDesktopService(@".\lib\dlv2.exe"));
                InputProgram encoding = new ASPInputProgram();
                encoding.AddFilesPath(Path.GetFullPath(brain.ASPFilePath));
                InputProgram facts = new ASPInputProgram();
                facts.AddFilesPath(factsPath);
                handler.AddProgram(encoding);
                handler.AddProgram(facts);
                handler.AddOption(new OptionDescriptor("--filter=setOnActuator/1"));
                stopwatch.Restart();
                //Debug.Log("reasoning");
                Output o = handler.StartSync();
                if (!o.ErrorsString.Equals(""))
                {
                    Debug.Log(o.ErrorsString + " " + o.OutputString);
                }
                AnswerSets answers = (AnswerSets)o;
                stopwatch.Stop();
                asSteps++;
                asAvgTime += stopwatch.ElapsedMilliseconds;
                //Debug.Log("debugging answer set");
                //Debug.Log("there are "+answers.Answersets.Count);
                //Debug.Log("error: " + answers.ErrorsString);
                if (answers.Answersets.Count > 0)
                {
                    /*string asPath = Path.GetTempFileName();
                     * using (StreamWriter fs = new StreamWriter(asPath, true))
                     * {
                     *  fs.Write(o.OutputString);
                     *  fs.Close();
                     * }*/
                    lock (brain.toLock)
                    {
                        foreach (SimpleActuator actuator in brain.getActuators())
                        {
                            Debug.Log("input fact " + factsPath);
                            Debug.Log("parsing " + actuator.actuatorName);
                            if (answers.Answersets[0].GetAnswerSet().Count > 0)
                            {
                                Debug.Log(answers.Answersets[0].GetAnswerSet()[0]);
                            }
                            actuator.parse(answers.Answersets[0]);
                        }
                        brain.setActuatorsReady(true);
                    }
                }
                if (!brain.maintainFactFile)
                {
                    File.Delete(factsPath);
                }
            }
        }