def getSubFormulaNoParent(formula, objects): elements = set() edges = set() if formula.key == 'not': formula = next(iter(formula.children)) if formula.key == 'intends': raise NameError('no intends yet') else: lit = Literal(name=formula.key, num_args=len(formula.children), truth=False) elements.add(lit) elif formula.key == 'intends': raise NameError('no intends yet') elif formula.type > 0: raise ValueError('not sure why formula.type > 0') else: lit = Literal(name=formula.key, num_args=len(formula.children), truth=True) elements.add(lit) for i, child in enumerate(formula.children): #children are list arg = next(ob_element for ob_name, ob_element in objects.items() if child.key == ob_name) edges.add(Edge(lit, arg, ARGLABELS[i])) elements.add(arg) return (elements, edges)
def makeGoal(formula): if formula.key == 'not': formula = next(iter(formula.children)) num_children = len(formula.children) lit = Literal(name=formula.key, num_args=num_children, truth=False) else: num_children = len(formula.children) lit = Literal(name=formula.key, num_args=num_children, truth=True) return lit
def addNegativeInitStates(predicates, initAction, objects): init_tups = defaultdict(set) effects = initAction.getNeighbors(initAction.root) for eff in effects: nontup = sorted([(edge.sink, edge.label) for edge in initAction.getIncidentEdges(eff)], key=lambda x: x[1]) init_tups[eff.name].add(tuple(nontup[i][0] for i in range(len(nontup)))) objs_by_type_dict = defaultdict(set) for obj in objects: objs_by_type_dict[obj.typ].add(obj) for p in predicates: param_object_pairs = [[obj for obj in objs_by_type_dict[param.types[0]]] for param in p.parameters if not p.parameters[0].types is None] param_tuples = {i for i in itertools.product(*param_object_pairs)} pred = Literal(name=p.name, arg_name='init_effect', num_args=len( p.parameters), truth=False) for pt in param_tuples: if pt in init_tups[p.name]: continue pc = copy.deepcopy(pred) pc.ID = uuid4() for i, arg in enumerate(pt): initAction.edges.add(Edge(pc, arg, i)) if len(pt) > 0: initAction.elements.add(pc) initAction.edges.add(Edge(initAction.root, pc, 'effect-of'))
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 makeLit(formula, parent, relationship, elements, edges, bit=None, noAdd=None): if bit == None: bit = True if noAdd == None: noAdd = False num_children = len(formula.children) lit = Literal(name=formula.key, num_args=num_children, truth=bit) if not noAdd: elements.add(lit) edges.add(Edge(parent, lit, relationship)) return lit, formula
def litFromArg(arg, DG): neg = True if arg.key == 'not': neg = False arg = arg.children[0] # arg 2 is written out lit_name = arg.key lit_elm = Literal(name=lit_name, arg_name=lit_name + str(uuid4())[19:23], num_args=len(arg.children), truth=neg) for i, ch in enumerate(arg.children): e_i = whichElm(ch.key.name, DG) DG.edges.add(Edge(lit_elm, e_i, i)) return lit_elm
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 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
def __init__(self, ID=None, type_graph=None, name=None, Elements=None, root_element=None, Edges=None, Restrictions=None): if type_graph is None: type_graph = 'Condition' if ID is None: ID = root_element.ID if root_element is None: root_element = Literal() if Elements is None: Elements = {root_element} if name is None: name = root_element.name super(Condition, self).__init__(ID, type_graph, name, Elements, root_element, Edges, Restrictions) self.replaced_ID = root_element.replaced_ID
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 decorateElm(child, DG): if child.key == 'name': elm = whichElm(child.children[0].key.name, DG) elm.name = child.children[1].key elif child.key == 'nth-step-arg' or child.key == 'nth-lit-arg': args = child.children label = GC.ARGLABELS[int(args[0].key)] parent_elm = whichElm(args[1].key.name, DG) child_elm = whichElm(args[2].key.name, DG) DG.edges.add(Edge(parent_elm, child_elm, label)) elif child.key == 'includes': arg1, arg2 = child.children DG.edges.add(Edge(whichElm(arg1.key.name, DG), whichElm(arg2.key.name, DG), 'arg-of')) elif child.key == 'truth': lit, value = child.children lit = whichElm(lit.key.name, DG) if value.key in {'t', '1', 'true', 'yes', 'y'}: lit.truth = True else: lit.truth = False elif child.key == 'effect' or child.key == 'precond': label = child.key + '-of' arg1, arg2 = child.children if len(arg2.children) > 0: child_elm = litFromArg(arg2, DG) else: child_elm = whichElm(arg2.key.name, DG) DG.edges.add(Edge(whichElm(arg1.key.name, DG), child_elm, label)) elif child.key == 'linked': arg1, arg2 = child.children dep = Literal(arg_name='link-condition' + str(uuid4())[19:23]) Src = whichElm(arg1.key.name, DG) Snk = whichElm(arg2.key.name, DG) DG.CausalLinkGraph.addEdge(Src, Snk, dep) DG.edges.add(Edge(Snk, dep, 'precond-of')) DG.edges.add(Edge(Src, dep, 'effect-of')) elif child.key == '<': arg1, arg2 = child.children DG.OrderingGraph.addEdge(whichElm(arg1.key.name, DG), whichElm(arg2.key.name, DG)) elif child.key == 'linked-by': src, snk, by = child.children try: dep = whichElm(by.key.name, DG) except: dep = litFromArg(by, DG) Src = whichElm(src.key.name, DG) Snk = whichElm(snk.key.name, DG) DG.CausalLinkGraph.addEdge(Src, Snk, dep) DG.edges.add(Edge(Snk, dep, 'precond-of')) DG.edges.add(Edge(Src, dep, 'effect-of')) elif child.key == 'consents': arg1, arg2, by = child.children DG.edges.add(Edge(whichElm(arg1.key.name, arg2.key.name, 'actor-of'))) elif child.key in {'occurs', '=', 'is', 'equals'}: # then, first argument is step element name and second argument is an operator with children args stepFromArg(child, DG) elif child.key == 'is-state': litFromArg(child, DG) else: raise NameError('No definition implemented for decomp predicate {}'.format(child.key))