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 pddl.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
        elif c.key == 'increase':
            nextPredicate = c.children[0]
        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')
        #hack to get cost parser
        totalCost = 1
        if nextPredicate.key == 'total-cost':
            totalCost = int(nextPredicate.children[0].key)
        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.
        effect.cost = totalCost
        if isNegative:
            effect.dellist.add(pddl.Predicate(nextPredicate.key, signature))
        else:
            effect.addlist.add(pddl.Predicate(nextPredicate.key, signature))
Esempio n. 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 pddl.parser import Variable

        if (c.key != 'not'):

            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))

        else:

            isNot = True
            predDef = self._predicates[c.children[0].key]
            signature = list()
            count = 0
            # Check for correct number of arguments.
            if len(c.children[0].children) != len(predDef.signature):
                raise SemanticError('Error: wrong number of arguments for '
                                    'predicate ' + c.children[0].key +
                                    ' in precondition of '
                                    'action')

            # Apply to all arguments.
            for v in c.children[0].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.children[0].key, signature, isNot))
Esempio n. 3
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
		"""
        from pddl.parser import Variable
        nextPredicate = None
        isNegative = False

        if c.key == 'not':
            # This is a negative goal, only one child allowed.
            if len(c.children) != 1:
                raise SemanticError('Error not statement with multiple '
                                    'children in goal condition')
            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')

        if nextPredicate == None:
            raise SemanticError(
                'Error: NoneType predicate used in goal condition')

        # 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 ' + nextPredicate.key + ' in goal')
        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 the predicate to the goal.
        #goal.append(pddl.Predicate(nextPredicate.key, signature))
        if isNegative:
            goal.dellist.add(pddl.Predicate(nextPredicate.key, signature))
        else:
            goal.addlist.add(pddl.Predicate(nextPredicate.key, signature))
Esempio n. 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
        """
        # 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):
                               
            print(len(c.children))
            print(c.key)
            for child in c.children:
                print(child.key)

            print(predDef.signature)

            raise SemanticError('Error: wrong number of arguments for '
                                'predicate ' + c.key + ' in goal' +'signature and children are: ' )

        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))
Esempio n. 5
0
    def add_decomp(self, decomp, c):
        from pddl.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 decomp of action')
            nextPredicate = c.children[0]
            isNegative = True
        else:
            nextPredicate = c

        if not nextPredicate.key in self._predicates:
            raise SemanticError('Error: unknown predicate %s used in decomp '
                                'of action' % nextPredicate.key)

        if nextPredicate == None:
            raise SemanticError('Error: NoneType predicate used in decomp 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 decomp 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

        if isNegative:
            decomp.dellist.add(pddl.Predicate(nextPredicate.key, signature))
        else:
            decomp.addlist.add(pddl.Predicate(nextPredicate.key, signature))
Esempio n. 6
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))
Esempio n. 7
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))
Esempio n. 8
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 pddl.parser import Variable
        #############################################################
        nextPredicate = None
        isNegative = False
        forall = False
        if c.key in {'forall', 'for-all'}:
            forall = True
            scoped_var = c.children[0]
            c = c.children[1]

        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 precondition of action')
            nextPredicate = c.children[0]
            isNegative = True
        else:
            nextPredicate = c

        if not nextPredicate.key in self._predicates:
            raise SemanticError(
                'Error: unknown predicate %s used in precondition '
                'of action' % nextPredicate.key)

        if nextPredicate == None:
            raise SemanticError(
                'Error: NoneType predicate used in precondition of '
                'action')
        #############################################################

        # 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')

        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 precondition 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

        if forall:
            for name, sig in signature:
                if name == scoped_var.key.name:
                    scoped_var = (name, sig)
                    break

        #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(nextPredicate.key, signature))
        if forall:
            #pred = pddl.Predicate(nextPredicate.key, signature)
            p = pddl.Predicate(nextPredicate.key, signature)
            q = pddl.Quantifier('forall', scoped_var, p)
            precond.addlist.add(q)
            return
        if isNegative:
            precond.dellist.add(pddl.Predicate(nextPredicate.key, signature))
        else:
            precond.addlist.add(pddl.Predicate(nextPredicate.key, signature))
Esempio n. 9
0
def add_not_allowed_predicates(domain_file_name,action_names,add_action_constraints,cur_grd_task):


        pddl_parser = parser.Parser( domain_file_name )
        planning_domain = pddl_parser.parse_domain()


        for action_name in action_names :
            #add the relevant predicate to the predicate list
            dis_predicate_name = 'not_allowed_%s'%action_name
            action = planning_domain.actions[action_name]
            predicate = pddl.Predicate(dis_predicate_name,action.signature)
            planning_domain.predicates[dis_predicate_name]=predicate

            #add the precondition to the relevant action
            predicate.IsNot = True
            action.precondition.append(predicate)


        if add_action_constraints is True:

            actionsToConstrain_names = action_names#grd_utils.get_constraints_actions(cur_grd_task)
            actionsToConstrain = []
            if actionsToConstrain_names is None:
                actionsToConstrain_names = []
                for action_name in planning_domain.actions.keys():
                    actionsToConstrain.append(planning_domain.actions[action_name])
                    actionsToConstrain_names.append(action_name)
            else:
                for action_name in action_names:#actionsToConstrain_names:
                    actionsToConstrain.append(planning_domain.actions[action_name])

            for constraint_action_def in actionsToConstrain:

                # add predicate to domain
                param_count = len(constraint_action_def.signature)
                #(constrained_obj)
                predicate_name_constraint = 'constraint_%d'%param_count
                signature_full = copy.deepcopy(constraint_action_def.signature)
                for param in constraint_action_def.signature:
                    param_name = '%s-d'%param[0]
                    signature_full.append(('%s'%param_name,('object')))
                predicate_constraint = pddl.Predicate(predicate_name_constraint,signature_full)
                planning_domain.predicates[predicate_name_constraint]=predicate_constraint

                # if act is in the list add the constraints
                realActionsToConstrain_names = grd_utils.get_constraints_actions(cur_grd_task)
                if constraint_action_def.name in realActionsToConstrain_names:
                    # token assignment
                    constraint_action_def.precondition.append(predicate_constraint)

                    # token performed
                    #for act in actionsToConstrain_names:
                    actionsToConstrain_names = grd_utils.get_constraints_actions(cur_grd_task)
                    for act in actionsToConstrain_names:
                        preidcate_name_cur = 'not_allowed_%s'%act
                        signature_dis = []
                        for param in constraint_action_def.signature:
                            param_name = '%s-d'%param[0]
                            signature_dis.append(('%s'%param_name,('%s'%param[1])))
                        predicate_add_dissallow = pddl.Predicate(preidcate_name_cur,signature_dis)
                        constraint_action_def.effect.addlist.add(predicate_add_dissallow)

                    # add constraints params to signature
                    constraint_action_def.signature = copy.deepcopy(signature_full)


        return planning_domain
Esempio n. 10
0
    def visit_constraints_stmt(self, node):
        formula = node.formula
        constr = list()
        constraints = {}
        agents = {}
        changed = {}
        predicates = node.predicates
        init_predicates = node.init_predicates

        if formula.key == 'and':
            for child in formula.children:
                formula_for_changing = self.get_formula(child, 'forall')
                changed_key = formula_for_changing.children[0].children[
                    1].key + formula_for_changing.children[0].key
                formula = self.get_formula(child, 'implies')
                blocks = []
                predDef = []
                agent = None
                for c in formula.children:
                    signature = []
                    sig = []
                    blocknames = {}
                    if c.key == "or":
                        for child in c.children:
                            predDef.append(self._domain.predicates[child.key])
                            self.blocks_blocknames(predicates, init_predicates,
                                                   child, blocknames, blocks,
                                                   constr)
                    else:
                        predDef.append(self._domain.predicates[c.key])
                        self.blocks_blocknames(predicates, init_predicates, c,
                                               blocknames, blocks, constr)

                    if len(blocknames):
                        unique = []
                        for predD in predDef:
                            for bltype in blocknames:
                                if predD.name == bltype:
                                    for name in blocknames.get(bltype):
                                        count = 0
                                        for v in name:
                                            sig.append(
                                                (v, predD.signature[count][1]))
                                            count += 1
                                        sig.insert(0, predD.name)
                                        signature.append(sig)
                                        sig = []
                            if c.key == "or":

                                for child in c.children:
                                    for sign in signature:
                                        if child.key == sign[0]:
                                            predsign = [
                                                s for s in sign
                                                if not s == sign[0]
                                            ]
                                            if predsign not in unique:
                                                constr.append(
                                                    pddl.Predicate(
                                                        child.key, predsign))
                                                unique.append(predsign)

                            else:
                                for sign in signature:
                                    if c.key == sign[0]:
                                        predsignature = [
                                            s for s in sign if not s == sign[0]
                                        ]
                                        agent = predsignature[0][0]
                                        constr.append(
                                            pddl.Predicate(
                                                c.key, predsignature))
                        predDef = []

                    elif len(blocks):
                        # if holding predicate
                        for predD in predDef:
                            for block in blocks:
                                key = None
                                child_place = 0
                                for child in c.children:
                                    if isinstance(child.key, str):
                                        key = child.key
                                        child_place = c.children.index(child)
                                lists = [block]
                                lists.insert(child_place, key)
                                #lists = [c.children[0].key, block]
                                blocknames.setdefault(predD.name,
                                                      []).append(lists)
                                agent = key
                                for name in blocknames.get(predD.name):
                                    count = 0
                                    for v in name:
                                        sig.append(
                                            (v, predD.signature[count][1]))
                                        count += 1
                                    signature.append(sig)
                                    sig = []
                                for sign in signature:
                                    constr.append(pddl.Predicate(c.key, sign))
                                signature = []
                                blocknames = {}
                    else:
                        raise SemanticError('unknown predicate!')
                #constraints[agent] = constr
                changed[changed_key] = constr
                constraints.setdefault(agent, {}).update(changed)
                constr = []
                changed = {}

            self.set_in(node, constraints)