Example #1
0
 public void addParameter(ActionParameter parameter)
 {
     if (_parameters.Contains(parameter) == false)
     {
         _parameters.Add(parameter);
     }
     else
     {
         throw new System.ArgumentException("The parameter: " + parameter.ToString() + " was already defined");
     }
 }
Example #2
0
        public List <Action> getPossibleActions()
        {
            List <Action> listActions     = new List <Action>();
            List <Action> possibleActions = new List <Action>();

            foreach (Action a in _domain.Actions)
            {
                // The idea behind this algorithm is to first generate a dictionary which maps each entity to a
                // list of possible entities suitable to be sobstituted in the action.
                // Then we compute all the possible combinations of substitutions in the form of a set of tuples
                Dictionary <ActionParameter, List <ActionParameter> >    dictSobstitution = new Dictionary <ActionParameter, List <ActionParameter> >();
                HashSet <Dictionary <ActionParameter, ActionParameter> > sobstitutions    = new HashSet <Dictionary <ActionParameter, ActionParameter> >();

                // For each parameter of the action we get all the possible entities in the
                // current worldState which could be substituted, according to their type
                foreach (ActionParameter item in a.Parameters)
                {
                    List <ActionParameter> listapp = new List <ActionParameter>();
                    foreach (Entity e in _entities)
                    {
                        if (item.Type.Equals(e.Type))
                        {
                            listapp.Add(new ActionParameter(e, item.Role));
                        }
                    }
                    dictSobstitution.Add(item, listapp);
                }

                // We initialize the set of mappings with the elements of the first list
                // so for example, if we had to substitute a list of waypoints
                //   "WAYPOINT1": [             ["WAYPOINT1": "ALPHA"]
                //     "ALPHA",                 ["WAYPOINT1": "BRAVO"]
                //     "BRAVO"
                //   ],
                ActionParameter        firstKey = dictSobstitution.Keys.First();
                List <ActionParameter> sobList  = dictSobstitution[firstKey];
                foreach (ActionParameter e in sobList)
                {
                    Dictionary <ActionParameter, ActionParameter> sobstitution = new Dictionary <ActionParameter, ActionParameter>();
                    sobstitution.Add(firstKey, e);
                    sobstitutions.Add(sobstitution);
                }
                dictSobstitution.Remove(firstKey);

                // We iterate over the remaining lists of entities and each time we combine them
                // with every element of the set of partial combinations that we already computed
                foreach (KeyValuePair <ActionParameter, List <ActionParameter> > entry in dictSobstitution)
                {
                    // entry.Key = name of the variable we are sobstituting
                    // entry.Value = list of possible entities we can make the sobstitution with
                    HashSet <Dictionary <ActionParameter, ActionParameter> > tmpSobstitutions = new HashSet <Dictionary <ActionParameter, ActionParameter> >();
                    foreach (Dictionary <ActionParameter, ActionParameter> sobstitution in sobstitutions)
                    {
                        foreach (ActionParameter e in entry.Value)
                        {
                            Dictionary <ActionParameter, ActionParameter> tmpSobstitution = new Dictionary <ActionParameter, ActionParameter>(sobstitution);
                            tmpSobstitution.Add(entry.Key, e);
                            tmpSobstitutions.Add(tmpSobstitution);
                        }
                    }
                    sobstitutions = tmpSobstitutions;
                }

                // Every sobstitution represents a possible action wich may or may not be
                // performable in the current state, so we check if its preconditions
                // are satisfied and, if so, we add it to the list of possible actions
                foreach (Dictionary <ActionParameter, ActionParameter> sobstitution in sobstitutions)
                {
                    Action action = a.sobstituteParameterInAction(sobstitution);
                    if (canPerformAction(action))
                    {
                        listActions.Add(action);
                    }
                }
            }
            return(listActions);
        }