예제 #1
0
파일: Client.cs 프로젝트: fangld/EDGolog
        private Planning.HighLevelProgramExecution.Program Trans(Planning.HighLevelProgramExecution.Program program)
        {
            Planning.HighLevelProgramExecution.Program remainingProgram;

            if (program is Planning.HighLevelProgramExecution.Action)
            {
                remainingProgram = Planning.HighLevelProgramExecution.Program.EmptyProgram;

                Planning.HighLevelProgramExecution.Action programAction =
                    (Planning.HighLevelProgramExecution.Action)program;
                Action action = _actionDict[programAction.FullName];
                SendMessage(action.FullName);
                string responseName = ReceiveMessage();
                Console.WriteLine("Receive response: {0}", responseName);
                Response response = action.ResponseDict[responseName];
                _mentalAttitude.Update(response);
            }
            else if (program is SequenceStructure)
            {
                SequenceStructure seq = ((SequenceStructure)program);
                int i;
                for (i = 0; i < seq.SubProgramLength - 1; i++)
                {
                    if (!Final(seq.SubPrograms[i]))
                    {
                        break;
                    }
                }

                Planning.HighLevelProgramExecution.Program[] newRemainingProgramArray =
                    new Planning.HighLevelProgramExecution.Program[seq.SubProgramLength - i];
                newRemainingProgramArray[0] = Trans(seq.SubPrograms[i]);
                Parallel.For(1, newRemainingProgramArray.Length,
                             j => newRemainingProgramArray[j] = seq.SubPrograms[j + i]);

                remainingProgram = new SequenceStructure(newRemainingProgramArray);
            }
            else if (program is ConditionalStructure)
            {
                ConditionalStructure cond = (ConditionalStructure)program;
                remainingProgram = _mentalAttitude.Implies(cond.Condition)
                    ? Trans(cond.SubProgram1)
                    : Trans(cond.SubProgram2);
            }
            else if (program is LoopStructure)
            {
                LoopStructure loop = (LoopStructure)program;
                remainingProgram = _mentalAttitude.Implies(loop.Condition)
                    ? new SequenceStructure(new[] { Trans(loop.SubProgram), program })
                    : null;
            }
            else
            {
                throw new PlanningException("Trans an empty program!");
            }

            return(remainingProgram);
        }
예제 #2
0
        public void ReceiveActions()
        {
            do
            {
                Client client = SelectAnClient();
                if (client == null)
                {
                    if (Completed != null)
                    {
                        Completed(this, null);
                    }
                    break;
                }
                Console.WriteLine("Select agent: {0}", client.AgentId);
                client.SendMessage("action");
                Action   action   = client.GetAction();
                Response response = ObjectBase.GetActualResponse(action);

                client.SendMessage(response.FullName);
                string message = client.ReceiveMessage();
                Console.WriteLine("receive message: {0}", message);
                if (message == "quit")
                {
                    Console.WriteLine("Agent {0} is terminated", client.AgentId);
                    client.IsTerminated = true;
                    if (AgentTerminated != null)
                    {
                        AgentTerminated(this, client.AgentId);
                    }
                }

                string agentName = client.AgentId;
                Event  e         = ObjectBase.GetActualEvent(response);
                foreach (var otherAgentName in _problem.AgentList)
                {
                    if (otherAgentName != agentName)
                    {
                        Agent  otherAgent  = _problem.AgentDict[otherAgentName];
                        Client otherClient = _agentClientDict[otherAgentName];
                        if (!otherClient.IsTerminated)
                        {
                            foreach (var observation in otherAgent.ObservationList)
                            {
                                if (e.ObservationList.Any(obs => obs == observation))
                                {
                                    CUDDNode objectBaseNode = ObjectBase.CurrentCuddNode;
                                    CUDD.Ref(objectBaseNode);
                                    CUDDNode obsPreNode = observation.Precondition;
                                    CUDD.Ref(obsPreNode);
                                    CUDDNode impliesNode = CUDD.Function.Implies(objectBaseNode, obsPreNode);
                                    if (impliesNode.Equals(CUDD.ONE))
                                    {
                                        Console.WriteLine("Send observation {0} to agent {1}.", observation,
                                                          otherAgentName);

                                        if (NewObservation != null)
                                        {
                                            NewObservation(this,
                                                           new Tuple <string, Observation>(otherAgentName, observation));
                                        }
                                        otherClient.SendMessage("observation");
                                        otherClient.SendMessage(observation.FullName);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                ObjectBase.Update(e);
                if (NewAction != null)
                {
                    NewAction(this,
                              new Tuple <string, Action, Response, Event, IReadOnlyDictionary <string, bool> >(client.AgentId,
                                                                                                               action, response, e, ObjectBase.PredBooleanMap));
                    //NewAction(this, new Tuple<IReadOnlyDictionary<string, bool>, Event>(ObjectBase.PredBooleanMap, e));
                }
            } while (true);
        }
예제 #3
0
        private void DrawMovingSquirrels(Graphics graphics, IReadOnlyDictionary <string, bool> predBooleanMap, Action action)
        {
            Image  edgyImage      = _edgyLastImage;
            Image  wallyImage     = _wallyLastImage;
            string actionFullName = action.FullName;

            if (actionFullName == "Left(a1)")
            {
                edgyImage      = _edgyLeftImage;
                _edgyLastImage = edgyImage;
            }
            else if (actionFullName == "Right(a1)")
            {
                edgyImage      = _edgyRightImage;
                _edgyLastImage = edgyImage;
            }
            else if (actionFullName == "Left(a2)")
            {
                wallyImage      = _wallyLeftImage;
                _wallyLastImage = wallyImage;
            }
            else if (actionFullName == "Right(a2)")
            {
                wallyImage      = _wallyRightImage;
                _wallyLastImage = wallyImage;
            }

            DrawSquirrels(graphics, edgyImage, wallyImage, predBooleanMap);
        }