Пример #1
0
    def add_effect(self, effect, c):
        """Helper function for visit_effect_stmt.

        Keyword arguments:
        effect -- instance of the effect data structure
        c -- the formula representing the effect that we want to add to the
             addlist or dellist
        """
        # Needed for instance check.
        from .parser import Variable

        nextPredicate = None
        isNegative = False
        if c.key == "not":
            # This is a negative effect, only one child allowed.
            if len(c.children) != 1:
                raise SemanticError("Error not statement with multiple "
                                    "children in effect of action")
            nextPredicate = c.children[0]
            isNegative = True
        else:
            nextPredicate = c
        # Check whether predicate was defined previously.
        if not nextPredicate.key in self._predicates:
            raise SemanticError("Error: unknown predicate %s used in effect "
                                "of action" % nextPredicate.key)
        if nextPredicate == None:
            raise SemanticError("Error: NoneType predicate used in effect of "
                                "action")
        predDef = self._predicates[nextPredicate.key]
        signature = list()
        count = 0
        # Check whether predicate is used with the correct signature.
        if len(nextPredicate.children) != len(predDef.signature):
            raise SemanticError("Error: wrong number of arguments for "
                                "predicate " + nextPredicate.key +
                                " in effect of action")
        # Apply to all parameters.
        for v in nextPredicate.children:
            if isinstance(v.key, Variable):
                signature.append((v.key.name, predDef.signature[count][1]))
            else:
                signature.append((v.key, predDef.signature[count][1]))
            count += 1

        # Add a new effect to the positive or negative effects respectively.
        if isNegative:
            effect.dellist.add(pddl.Predicate(nextPredicate.key, signature))
        else:
            effect.addlist.add(pddl.Predicate(nextPredicate.key, signature))
Пример #2
0
    def add_precond(self, precond, c):
        """Helper function for visit_precondition_stmt.

        Keyword arguments:
        precond -- a list of preconditions
        c -- the formula representing a precondition we want to add to the list
        """
        from .parser import Variable

        predDef = self._predicates[c.key]
        signature = list()
        count = 0
        # Check for correct number of arguments.
        if len(c.children) != len(predDef.signature):
            raise SemanticError("Error: wrong number of arguments for "
                                "predicate " + c.key + " in precondition of "
                                "action")
        # Apply to all arguments.
        for v in c.children:
            if isinstance(v.key, Variable):
                signature.append((v.key.name, predDef.signature[count][1]))
            else:
                signature.append((v.key, predDef.signature[count][1]))
            count += 1

        # Add predicate to precondition list.
        precond.append(pddl.Predicate(c.key, signature))
Пример #3
0
 def visit_predicate(self, node):
     """Visits a PDDL predicate."""
     signature = list()
     # Visit all predicate parameters.
     for v in node.parameters:
         v.accept(self)
         signatureTuple = self.get_in(v)
         # Append each parameter to the predicate signature.
         signature.append(signatureTuple)
     # Create new PDDL predicate and store it in node.
     self.set_in(node, pddl.Predicate(node.name, signature))
Пример #4
0
 def visit_predicate_instance(self, node):
     """ Visits a PDDL-problem predicate instance."""
     signature = list()
     # Visit all parameters.
     for o in node.parameters:
         o_type = None
         # Check whether predicate was introduced in objects or domain
         # constants.
         if not (o in self._objects or o in self._domain.constants):
             raise SemanticError("Error: object " + o + " referenced in "
                                 "problem definition - but not defined")
         elif o in self._objects:
             o_type = self._objects[o]
         elif o in self._domain.constants:
             o_type = self._domain.constants[o]
         signature.append((o, (o_type)))
     self.set_in(node, pddl.Predicate(node.name, signature))
Пример #5
0
    def add_goal(self, goal, c):
        """Helper function for visit_goal_stmt.

        Keyword arguments:
        goal -- a list of goals
        c -- a formula representing a goal we want to add to the goal list
        """
        # Check whether predicate was introduced in domain file.
        if not c.key in self._domain.predicates:
            raise SemanticError("Error: unknown predicate " + c.key +
                                " in goal definition")
        # Get predicate from the domain data structure.
        predDef = self._domain.predicates[c.key]
        signature = list()
        count = 0
        # Check whether the predicate uses the correct signature.
        if len(c.children) != len(predDef.signature):
            raise SemanticError("Error: wrong number of arguments for "
                                "predicate " + c.key + " in goal")
        for v in c.children:
            signature.append((v.key, predDef.signature[count][1]))
            count += 1
        # Add the predicate to the goal.
        goal.append(pddl.Predicate(c.key, signature))