示例#1
0
        static void TakeTurn()
        {
            MediationTreeEdge matchingEdge = current.Outgoing.Find(x => x.ActionType.Equals(ActionType.Constituent));

            if (matchingEdge != null)
            {
                current = tree.GetNode(current.Domain, current.Problem, matchingEdge);
                computerActions.Add(matchingEdge.Action);
            }
        }
示例#2
0
        public static void Unknown()
        {
            MediationTreeEdge matchingEdge = null;

            foreach (MediationTreeEdge edge in current.Outgoing)
            {
                if (command.Equals(edge.Action.Name) && arguments.Count == edge.Action.Arity)
                {
                    bool match = true;
                    for (int i = 0; i < arguments.Count; i++)
                    {
                        if (!arguments[i].ToLower().Equals(edge.Action.Predicate.TermAt(i)))
                        {
                            if (!arguments[i].ToLower().Equals(edge.Action.TermAt(i)))
                            {
                                match = false;
                            }
                        }
                    }

                    if (match)
                    {
                        matchingEdge = edge;
                    }
                }
            }

            if (matchingEdge != null)
            {
                current = tree.GetNode(current.Domain, current.Problem, matchingEdge);
                Console.Clear();
                Look();
            }
            else
            {
                exploreFrontier = false;
                while (frontierThread.IsAlive)
                {
                    Thread.Sleep(50);
                }
                Console.Out.WriteLine();
                Random r = new Random();
                Console.Out.WriteLine(responses[r.Next(0, responses.Length)]);
                Console.Out.WriteLine("Try typing 'help'.");
            }
        }
示例#3
0
        // Creates a list of all possible things a character can do.
        public static List <MediationTreeEdge> GetAllPossibleActions(string character, MediationTreeNode node)
        {
            // Create a list of possible player actions in the current state.
            List <Operator> possibleActions = GetActions(character, node.Domain, node.Problem, node.State);

            // Create a list of the causal links that span the state.
            List <CausalLink> spanningLinks = (List <CausalLink>)GetSpanningLinks(node.Plan);

            // Create an empty list of exceptional actions.
            List <MediationTreeEdge> exceptionalActions = new List <MediationTreeEdge>();

            // Store the exceptional actions to be removed later.
            List <Operator> exceptions = new List <Operator>();

            // Store exceptional effects.
            Hashtable exceptionalEffects = new Hashtable();

            // Loop through every possible action.
            foreach (Operator action in possibleActions)
            {
                // Loop through every action's effects.
                foreach (Predicate effect in action.Effects)
                {
                    // Loop through every spanning link.
                    foreach (CausalLink link in spanningLinks)
                    {
                        // If the effect is the inverse of the protected predicate...
                        if (effect.IsInverse(link.Predicate))
                        {
                            // Identify the exceptional effect.
                            if (exceptionalEffects.ContainsKey(action.Predicate.ToString()))
                            {
                                List <IPredicate> list = exceptionalEffects[action.Predicate.ToString()] as List <IPredicate>;
                                list.Add(effect.Clone() as Predicate);
                                exceptionalEffects[action.Predicate.ToString()] = list;
                            }
                            else
                            {
                                exceptionalEffects[action.Predicate.ToString()] = new List <IPredicate> {
                                    effect.Clone() as Predicate
                                }
                            };

                            // Add the current action to the list of exceptional actions.
                            exceptionalActions.Add(new MediationTreeEdge(action.Clone() as Operator, ActionType.Exceptional, node.ID));

                            // Add the actual action to the exception list.
                            exceptions.Add(action);
                        }
                    }
                }


                //Loop through the action's axioms
                foreach (IAxiom conditional in node.State.ApplicableConditionals(action, node.Problem.Objects))
                {
                    // Loop through every conditional's effects.
                    foreach (IPredicate effect in conditional.Effects)
                    {
                        // Loop through every spanning link.
                        foreach (IDependency link in spanningLinks)
                        {
                            // If the effect is the inverse of the protected predicate...
                            if (effect.IsInverse(link.Predicate))
                            {
                                // Identify the exceptional effect.
                                if (exceptionalEffects.ContainsKey(action.Predicate.ToString()))
                                {
                                    List <IPredicate> list = exceptionalEffects[action.Predicate.ToString()] as List <IPredicate>;
                                    list.Add(effect.Clone() as Predicate);
                                    exceptionalEffects[action.Predicate.ToString()] = list;
                                }
                                else
                                {
                                    exceptionalEffects[action.Predicate.ToString()] = new List <IPredicate> {
                                        effect.Clone() as Predicate
                                    }
                                };

                                // Add the current action to the list of exceptional actions.
                                exceptionalActions.Add(new MediationTreeEdge(action.Clone() as Operator, ActionType.Exceptional, node.ID));

                                // Add the actual action to the exception list.
                                exceptions.Add(action);
                            }
                        }
                    }
                }
            }

            // Remove the exceptions from the possible actions.
            foreach (Operator exception in exceptions)
            {
                possibleActions.Remove(exception);
            }

            // This is a hack to remove duplicate exceptional actions with a Remove By Property method.
            List <int> remove = new List <int>();

            // Remove duplicate actions.
            for (int i = 0; i < exceptionalActions.Count - 1; i++)
            {
                for (int j = i + 1; j < exceptionalActions.Count; j++)
                {
                    if (exceptionalActions[i].Action.Equals(exceptionalActions[j].Action))
                    {
                        remove.Add(j);
                    }
                }
            }

            for (int i = exceptionalActions.Count - 1; i >= 0; i--)
            {
                if (remove.Contains(i))
                {
                    exceptionalActions.RemoveAt(i);
                }
                else
                {
                    exceptionalActions[i].Action.ExceptionalEffects = exceptionalEffects[exceptionalActions[i].Action.Predicate.ToString()] as List <IPredicate>;
                }
            }

            // Create an empty list of consistent actions.
            List <MediationTreeEdge> consistentActions = new List <MediationTreeEdge>();

            // Loop through the remaining actions.
            foreach (Operator action in possibleActions)
            {
                // Add the current action to the list of consistent actions.
                consistentActions.Add(new MediationTreeEdge(action.Clone() as Operator, ActionType.Consistent, node.ID));
            }

            // Make the set of outgoing edges.
            List <MediationTreeEdge> actions = new List <MediationTreeEdge>();

            // Find the current constituent action.
            MediationTreeEdge constituent = GetConstituentAction(character, node);

            // Add the constituent action to the possible actions.
            actions.Add(constituent);

            // Remove any exceptional actions that correspond to the constituent step.
            foreach (MediationTreeEdge exception in exceptionalActions)
            {
                if (!exception.Action.Equals(constituent.Action))
                {
                    actions.Add(exception);
                }
            }

            // Remove any exceptional actions that correspond to the constituent step.
            foreach (MediationTreeEdge consistent in consistentActions)
            {
                if (!consistent.Action.Equals(constituent.Action))
                {
                    actions.Add(consistent);
                }
            }

            // Return the outgoing edges.
            return(actions);
        }
    }
}
示例#4
0
        public static void TwoArgs()
        {
            if (arguments.Count != 2)
            {
                Unknown();
                return;
            }

            MediationTreeEdge matchingEdge = null;

            foreach (MediationTreeEdge edge in current.Outgoing)
            {
                if (edge.Action.Name.Count(x => x == '-') == 2)
                {
                    if (edge.Action.Name.Contains('-'))
                    {
                        string[] split = edge.Action.Name.Split('-');
                        if (command.Equals(split[0]))
                        {
                            if (arguments[0].ToLower().Equals(edge.Action.TermAt(1)))
                            {
                                if (arguments[1].ToLower().Equals(edge.Action.TermAt(2)))
                                {
                                    matchingEdge = edge;
                                }
                            }
                        }
                    }
                    else if (command.Equals(edge.Action.Name))
                    {
                        if (arguments[0].ToLower().Equals(edge.Action.TermAt(1)))
                        {
                            if (arguments[1].ToLower().Equals(edge.Action.TermAt(2)))
                            {
                                matchingEdge = edge;
                            }
                        }
                    }
                }
            }

            if (matchingEdge != null)
            {
                if (frontier.ContainsKey(matchingEdge))
                {
                    current = frontier[matchingEdge] as MediationTreeNode;
                }
                else
                {
                    exploreFrontier = false;
                    while (frontierThread.IsAlive)
                    {
                        Thread.Sleep(50);
                    }
                    current = tree.GetNode(current.Domain, current.Problem, matchingEdge);
                }

                Console.Clear();
                Look();
            }
            else
            {
                Console.Out.WriteLine();
                Random r = new Random();
                Console.Out.WriteLine(responses[r.Next(0, responses.Length)]);
                Console.Out.WriteLine("Try typing 'help'.");
            }
        }