def problemToGraphs(problem): """ Returns a dictionary: Keys: 'arg', 'init', 'goal' Values: arg dictionary, (elements, edges), (elements, edges) """ Args = {object.name: Argument(name=object.name, typ=object.typeName) for object in problem.objects if not object.typeName.lower() in {'character', 'actor', 'person', 'agent'}} Args.update({object.name: Actor(typ=object.typeName.lower(), name=object.name) for object in problem.objects if object.typeName.lower() in {'character', 'actor', 'person', 'agent'}}) goal_elements, goal_edges = getGoalSet(problem.goal.formula, Args) goal_op = Operator(name='dummy_goal', stepnumber=1, num_args=0) goal_graph = Action(name='dummy_goal', root_element=goal_op) goal_graph.elements.update(goal_elements) goal_graph.edges.update(goal_edges) goal_graph.edges.update({Edge(goal_op, goal_lit, 'precond-of') for goal_lit in goal_elements if type(goal_lit) is Literal}) init_op = Operator(name='dummy_init', stepnumber=0, num_args=0) init_graph = Action(name='dummy_init', root_element=init_op) for condition in problem.init.predicates: lit = Literal(name=condition.name, num_args=len(condition.parameters), truth=True) init_graph.elements.add(lit) init_graph.edges.add(Edge(init_op, lit, 'effect-of')) for i, p in enumerate(condition.parameters): init_graph.edges.add(Edge(lit, Args[p], i)) return Args, init_graph, goal_graph
def problemToGraphs(problem): """ Returns a dictionary: Keys: 'arg', 'init', 'goal' Values: arg dictionary, (elements, edges), (elements, edges) """ if problem.goalAction is not None: action = problem.goalAction gop = Operator(name=action.name, num_args=len(action.parameters)) GoalAction = Action(name=action.name, root_element=gop) evalActionParams(action.parameters, GoalAction) getFormulaGraph(action.precond.formula, GoalAction, parent=gop, relationship='precond-of', elements=GoalAction.elements, edges=GoalAction.edges) decomp_graph = PlanElementGraph(name=action.name, type_graph='decomp') getDecompGraph(action.decomp.formula, decomp_graph, action.parameters) GoalAction.subgraphs.add(decomp_graph) goal_graph = GoalAction init_op = Operator(name='dummy_init', stepnumber=0, num_args=0) init_graph = Action(name='dummy_init', root_element=init_op) Args = dict() else: Args = { object.name: Argument(name=object.name, typ=object.typeName) for object in problem.objects if not object.typeName.lower() in {'character', 'actor'} } Args.update({ object.name: Actor(name=object.name) for object in problem.objects if object.typeName.lower() in {'character', 'actor'} }) goal_elements, goal_edges = getGoalSet(problem.goal.formula, Args) goal_op = Operator(name='dummy_goal', stepnumber=1, num_args=0) goal_graph = Action(name='dummy_goal', root_element=goal_op) goal_graph.elements.update(goal_elements) goal_graph.edges.update(goal_edges) goal_graph.edges.update({ Edge(goal_op, goal_lit, 'precond-of') for goal_lit in goal_elements if type(goal_lit) is Literal }) init_op = Operator(name='dummy_init', stepnumber=0, num_args=0) init_graph = Action(name='dummy_init', root_element=init_op) for condition in problem.init.predicates: lit = Literal(name=condition.name, num_args=len(condition.parameters), truth=True) init_graph.elements.add(lit) init_graph.edges.add(Edge(init_op, lit, 'effect-of')) for i, p in enumerate(condition.parameters): init_graph.edges.add(Edge(lit, Args[p], ARGLABELS[i])) return (Args, init_graph, goal_graph)
def arg_to_elm(i, arg): if arg.typ == 'character' or arg.typ == 'actor': elm = Actor(name=arg.name, arg_name=arg.arg_name) elif arg.typ == 'arg' or arg.typ == 'item' or arg.typ == 'place': elm = Argument(name=arg.name, typ=arg.typ, arg_name=arg.arg_name) elif arg.typ == 'step': elm = Operator(name=arg.name, arg_name=arg.arg_name) elif arg.typ == 'literal' or arg.typ == 'lit': elm = Literal(name=arg.name, arg_name=arg.arg_name) else: raise ValueError('whose typ is this anyway? {}'.format(arg.typ)) return elm
def evalActionParams(params, op_graph): for i, parameter in enumerate(params): # parameters are list if 'character' in parameter.types: arg = Actor(typ='character', arg_name=parameter.name) op_graph.elements.add(arg) elif 'actor' in parameter.types: arg = Actor(typ='actor', arg_name=parameter.name) op_graph.elements.add(arg) elif 'person' in parameter.types: arg = Actor(typ='person', arg_name=parameter.name) op_graph.elements.add(arg) # elif 'literal' in parameter.types or 'lit' in parameter.types: # lit = Literal(arg_name=parameter.name, typ='Condition') # op_graph.elements.add(lit) else: arg_type = next(iter(parameter.types)) if arg_type in {'lit', 'literal'}: arg_type = 'Condition' arg = Argument(typ=arg_type, arg_name=parameter.name) op_graph.elements.add(arg) op_graph.edges.add(Edge(op_graph.root, arg, i))
def createElementByType(parameter, decomp): paramtypes = GC.object_types[next(iter(parameter.types))] if 'character' in parameter.types: elm = Actor(typ='character', arg_name=parameter.name) elif 'actor' in parameter.types: elm = Actor(typ='actor', arg_name=parameter.name) elif 'person' in parameter.types: elm = Actor(typ='person', arg_name=parameter.name) elif 'arg' in paramtypes or 'item' in paramtypes or 'place' in paramtypes: arg_type = next(iter(parameter.types)) elm = Argument(typ=arg_type, arg_name=parameter.name) elif 'step' in parameter.types: elm = Operator(arg_name=parameter.name) elif 'literal' in parameter.types or 'lit' in parameter.types: elm = Literal(arg_name=parameter.name) else: # assume its type object, treat as Argument. arg_type = next(iter(parameter.types)) elm = Argument(typ=arg_type, arg_name=parameter.name) # raise ValueError('parameter {} not story element'.format(parameter.name)) decomp.elements.add(elm) return
def createElementByType(parameter, decomp): if 'character' in parameter.types or 'actor' in parameter.types: elm = Actor(arg_name=parameter.name) elif 'arg' in parameter.types or 'item' in parameter.types or 'place' in parameter.types: arg_type = next(iter(parameter.types)) elm = Argument(typ=arg_type, arg_name=parameter.name) elif 'step' in parameter.types: elm = Operator(arg_name=parameter.name) elif 'literal' in parameter.types or 'lit' in parameter.types: elm = Literal(arg_name=parameter.name) else: raise ValueError('parameter {} not story element'.format( parameter.name)) decomp.elements.add(elm) return