Example #1
0
    def add_precond(self, precond, c):
        """Helper function for visit_precond_stmt.

        Keyword arguments:
        precond -- 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 representation.ow_pddl.parser import Variable

        nextPredicate = None
        isNegative = False
        if c.key == "not":
            # This is a negative precond, only one child allowed.
            if len(c.children) != 1:
                raise SemanticError("Error not statement with multiple "
                                    "children in precond 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 precond "
                                "of action" % nextPredicate.key)
        if nextPredicate is None:
            raise SemanticError("Error: NoneType predicate used in precond 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 precond 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:
            precond.dellist.add(ow_pddl.Predicate(nextPredicate.key,
                                                  signature))
        else:
            precond.addlist.add(ow_pddl.Predicate(nextPredicate.key,
                                                  signature))
Example #2
0
    def add_static(self, statics, c):
        """Helper function for visit_statics_stmt.

        Keyword arguments:
        statics -- a list of statics
        c -- the formula representing a predicate we want to add to the list
        """
        from representation.ow_pddl.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 statics 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.
        statics.append(ow_pddl.Predicate(c.key, signature))
Example #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, ow_pddl.Predicate(node.name, signature))
Example #4
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
        """
        nextPredicate = None
        isNegative = False
        if c.key == "not":
            # This is a negative precond, only one child allowed.
            if len(c.children) != 1:
                raise SemanticError("Error not statement with multiple "
                                    "children in goal definition")
            nextPredicate = c.children[0]
            isNegative = True
        else:
            nextPredicate = c
        # Check whether predicate was introduced in domain file.
        if not nextPredicate.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[nextPredicate.key]
        signature = list()
        count = 0
        # Check whether the predicate uses the correct signature.
        if len(nextPredicate.children) != len(predDef.signature):
            raise SemanticError("Error: wrong number of arguments for "
                                "predicate " + c.key + " in goal")
        for v in nextPredicate.children:
            signature.append((v.key, predDef.signature[count][1]))
            count += 1
        # Add the predicate to the goal.
        # Add a new effect to the positive or negative effects respectively.
        if isNegative:
            goal.dellist.add(ow_pddl.Predicate(nextPredicate.key, signature))
        else:
            goal.addlist.add(ow_pddl.Predicate(nextPredicate.key, signature))
Example #5
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, ow_pddl.Predicate(node.name, signature))