<s> import copy <EOL> import logging <EOL> import re <EOL> import gevent <EOL> import opencenter . backends <EOL> from opencenter . db import api as db_api <EOL> from opencenter . webapp import ast <EOL> class Solver : <EOL> def __init__ ( self , api , node_id , constraints , <EOL> parent = None , prim = None , ns = None , applied_consequences = None ) : <EOL> self . constraints = copy . deepcopy ( [ api . regularize_expression ( x ) <EOL> for x in constraints ] ) <EOL> self . node_id = node_id <EOL> self . base_api = api <EOL> self . applied_consequences = copy . deepcopy ( applied_consequences ) if applied_consequences is not None else [ ] <EOL> self . children = [ ] <EOL> classname = self . __class__ . __name__ . lower ( ) <EOL> self . logger = logging . getLogger ( '<STR_LIT>' % ( __name__ , classname ) ) <EOL> self . parent = parent <EOL> self . prim = prim <EOL> self . task_primitives = { } <EOL> self . adventures = [ ] <EOL> self . ns = ns if ns is not None else { } <EOL> self . logger . info ( '<STR_LIT>' % constraints ) <EOL> self . logger . info ( '<STR_LIT>' % applied_consequences ) <EOL> self . api = db_api . ephemeral_api_from_api ( self . base_api ) <EOL> pre_node = self . api . _model_get_by_id ( '<STR_LIT>' , self . node_id ) <EOL> for consequence in self . applied_consequences : <EOL> node = self . api . _model_get_by_id ( '<STR_LIT>' , self . node_id ) <EOL> ast . apply_expression ( node , consequence , self . api ) <EOL> self . constraints = [ x for x in self . constraints if not <EOL> self . _constraint_satisfied ( x ) ] <EOL> node = self . api . _model_get_by_id ( '<STR_LIT>' , self . node_id ) <EOL> if '<STR_LIT>' in node [ '<STR_LIT>' ] and '<STR_LIT>' in node [ '<STR_LIT>' ] and '<STR_LIT>' in node [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> for task_name in node [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> mangled_name = task_name <EOL> task = node [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ task_name ] <EOL> id = hash ( mangled_name ) & <NUM_LIT> <EOL> self . task_primitives [ id ] = { } <EOL> self . task_primitives [ id ] [ '<STR_LIT:id>' ] = id <EOL> self . task_primitives [ id ] [ '<STR_LIT:name>' ] = mangled_name <EOL> self . task_primitives [ id ] [ '<STR_LIT>' ] = task_name <EOL> self . task_primitives [ id ] [ '<STR_LIT>' ] = task [ '<STR_LIT>' ] <EOL> self . task_primitives [ id ] [ '<STR_LIT>' ] = task [ '<STR_LIT>' ] <EOL> self . task_primitives [ id ] [ '<STR_LIT:args>' ] = task [ '<STR_LIT:args>' ] <EOL> self . task_primitives [ id ] [ '<STR_LIT>' ] = <NUM_LIT:50> <EOL> self . task_primitives [ id ] [ '<STR_LIT>' ] = task [ '<STR_LIT>' ] if '<STR_LIT>' in task else <NUM_LIT:30> <EOL> self . logger . debug ( '<STR_LIT>' % pre_node ) <EOL> self . logger . debug ( '<STR_LIT>' % <EOL> self . applied_consequences ) <EOL> self . logger . debug ( '<STR_LIT>' % node ) <EOL> @ classmethod <EOL> def from_plan ( cls , api , node_id , constraints , plan , <EOL> applied_consequences = None ) : <EOL> root_solver = Solver ( api , node_id , constraints , <EOL> applied_consequences = applied_consequences ) <EOL> current_node = root_solver <EOL> for plan_item in plan : <EOL> current_node . solve_one ( plan_item ) <EOL> if len ( current_node . children ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> current_node = current_node . children [ <NUM_LIT:0> ] <EOL> if len ( current_node . children ) != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return root_solver <EOL> def _get_all_primitives ( self ) : <EOL> primitive_list = self . api . _model_get_all ( '<STR_LIT>' ) <EOL> primitive_list += self . task_primitives . values ( ) <EOL> return primitive_list <EOL> def _get_primitive_by_name ( self , name ) : <EOL> primitives = self . api . _model_query ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % name ) <EOL> if len ( primitives ) > <NUM_LIT:0> : <EOL> return primitives [ <NUM_LIT:0> ] <EOL> primitives = [ self . task_primitives [ x ] for x in <EOL> self . task_primitives if <EOL> self . task_primitives [ x ] [ '<STR_LIT:name>' ] == name ] <EOL> if len ( primitives ) > <NUM_LIT:0> : <EOL> return primitives [ <NUM_LIT:0> ] <EOL> return None <EOL> def _get_additional_constraints ( self , primitive_id , ns ) : <EOL> if primitive_id in self . task_primitives : <EOL> return [ ] <EOL> else : <EOL> return opencenter . backends . additional_constraints ( self . api , <EOL> self . node_id , <EOL> primitive_id , <EOL> ns ) <EOL> def _build_constraints ( self , constraints ) : <EOL> """<STR_LIT>""" <EOL> retval = [ ] <EOL> f_builder = ast . FilterBuilder ( ast . FilterTokenizer ( ) ) <EOL> for constraint in constraints : <EOL> f_builder . set_input ( constraint ) <EOL> constraint_node = f_builder . build ( ) <EOL> for consequence in constraint_node . invert ( ) : <EOL> f_builder . set_input ( consequence ) <EOL> consequence_ast = f_builder . build ( ) <EOL> constraint_list = consequence_ast . invert ( ) <EOL> if len ( constraint_list ) != <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> constraint = constraint_list [ <NUM_LIT:0> ] <EOL> f_builder . set_input ( constraint ) <EOL> constraint_ast = f_builder . build ( ) <EOL> retval . append ( { '<STR_LIT>' : constraint , <EOL> '<STR_LIT>' : constraint_ast , <EOL> '<STR_LIT>' : consequence , <EOL> '<STR_LIT>' : consequence_ast } ) <EOL> return retval <EOL> def _constraint_satisfied ( self , constraint ) : <EOL> """<STR_LIT>""" <EOL> full_node = self . api . _model_get_by_id ( '<STR_LIT>' , self . node_id ) <EOL> builder = ast . FilterBuilder ( ast . FilterTokenizer ( ) , <EOL> constraint , <EOL> '<STR_LIT>' , api = self . api ) <EOL> root_node = builder . build ( ) <EOL> return root_node . eval_node ( full_node ) <EOL> def _can_meet_constraints ( self , primitive ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( '<STR_LIT>' % primitive ) <EOL> can_add = True <EOL> self . logger . debug ( '<STR_LIT>' % primitive ) <EOL> if primitive [ '<STR_LIT>' ] != [ ] : <EOL> constraint_filter = '<STR_LIT>' . join ( map ( lambda x : '<STR_LIT>' % x , <EOL> primitive [ '<STR_LIT>' ] ) ) <EOL> can_add = self . _constraint_satisfied ( constraint_filter ) <EOL> self . logger . debug ( '<STR_LIT>' % can_add ) <EOL> return can_add <EOL> def can_coerce ( self , constraint_node , consequence_node ) : <EOL> self . logger . debug ( '<STR_LIT>' % consequence_node . to_s ( ) ) <EOL> self . logger . debug ( '<STR_LIT>' % constraint_node . to_s ( ) ) <EOL> if constraint_node . op == '<STR_LIT>' and constraint_node . lhs == '<STR_LIT>' and consequence_node . op == '<STR_LIT>' and consequence_node . lhs == '<STR_LIT>' : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> if constraint_node . rhs [ <NUM_LIT:0> ] . op == '<STR_LIT>' and consequence_node . rhs [ <NUM_LIT:0> ] . op == '<STR_LIT>' and constraint_node . rhs [ <NUM_LIT:0> ] . lhs == consequence_node . rhs [ <NUM_LIT:0> ] . lhs : <EOL> can_constrain , ns = self . can_coerce ( constraint_node . rhs [ <NUM_LIT:1> ] , <EOL> consequence_node . rhs [ <NUM_LIT:1> ] ) <EOL> return can_constrain , ns <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' % <EOL> ( consequence_node . rhs [ <NUM_LIT:0> ] , <EOL> constraint_node . rhs [ <NUM_LIT:0> ] ) ) <EOL> return False , { } <EOL> if constraint_node . op not in [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' % constraint_node . op ) <EOL> return False , { } <EOL> if consequence_node . op not in [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' % consequence_node . op ) <EOL> return False , { } <EOL> str_consequence = str ( consequence_node . lhs ) <EOL> str_constraint = str ( constraint_node . lhs ) <EOL> if str_consequence == str_constraint and consequence_node . op == constraint_node . op : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> return True , { } <EOL> match = re . match ( "<STR_LIT>" , str_consequence ) <EOL> if match is None : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> return False , { } <EOL> if not str_constraint . startswith ( match . group ( <NUM_LIT:1> ) ) or not str_constraint . endswith ( match . group ( <NUM_LIT:3> ) ) : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> return False , { } <EOL> key = match . group ( <NUM_LIT:2> ) <EOL> value = str_constraint [ len ( match . group ( <NUM_LIT:1> ) ) : ] <EOL> if len ( match . group ( <NUM_LIT:3> ) ) : <EOL> value = value [ : - len ( match . group ( <NUM_LIT:3> ) ) ] <EOL> return True , { key : value } <EOL> def can_solve ( self , constraint_ast , consequence_ast ) : <EOL> conseq_string = consequence_ast . to_s ( ) <EOL> constr_string = constraint_ast . to_s ( ) <EOL> self . logger . debug ( '<STR_LIT>' % <EOL> ( conseq_string , constr_string ) ) <EOL> if constraint_ast . op != consequence_ast . op : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> return False , { } <EOL> if constraint_ast . op != '<STR_LIT>' : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> return False , { } <EOL> can_coerce_lhs , ns_lhs = self . can_coerce ( constraint_ast . lhs , <EOL> consequence_ast . lhs ) <EOL> can_coerce_rhs , ns_rhs = self . can_coerce ( constraint_ast . rhs , <EOL> consequence_ast . rhs ) <EOL> if not can_coerce_rhs or not can_coerce_lhs : <EOL> return False , { } <EOL> self . logger . debug ( '<STR_LIT>' % ns_lhs ) <EOL> self . logger . debug ( '<STR_LIT>' % ns_rhs ) <EOL> key_union = [ x for x in ns_lhs . keys ( ) if x in ns_rhs . keys ( ) ] <EOL> for key in key_union : <EOL> if ns_rhs [ key ] != ns_lhs [ key ] : <EOL> self . logger . debug ( '<STR_LIT>' % <EOL> ( key , ns_rhs [ key ] , ns_lhs [ key ] ) ) <EOL> return False , { } <EOL> for key in ns_rhs . keys ( ) : <EOL> ns_lhs [ key ] = ns_rhs [ key ] <EOL> self . logger . debug ( "<STR_LIT>" % ns_lhs ) <EOL> return True , ns_lhs <EOL> def _is_forwarding_solution ( self , primitive , constraints , ns = None ) : <EOL> """<STR_LIT>""" <EOL> if ns is None : <EOL> ns = { } <EOL> potential = self . _potential_solutions ( primitive , constraints ) <EOL> if len ( potential ) == <NUM_LIT:1> : <EOL> return potential [ <NUM_LIT:0> ] <EOL> for solution in potential : <EOL> if ns == solution [ '<STR_LIT>' ] : <EOL> return solution <EOL> return None <EOL> def _potential_solutions ( self , primitive , constraints ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( '<STR_LIT>' % <EOL> ( primitive [ '<STR_LIT:name>' ] , <EOL> [ x [ '<STR_LIT>' ] for x in constraints ] ) ) <EOL> f_builder = ast . FilterBuilder ( ast . FilterTokenizer ( ) ) <EOL> valid_solutions = [ ] <EOL> for constraint in constraints : <EOL> constraint_ast = constraint [ '<STR_LIT>' ] <EOL> for consequence in primitive [ '<STR_LIT>' ] : <EOL> f_builder . set_input ( consequence ) <EOL> consequence_ast = f_builder . build ( ) <EOL> satisfies , req_ns = self . can_solve ( constraint_ast , <EOL> consequence_ast ) <EOL> if satisfies : <EOL> solution = { '<STR_LIT>' : primitive , <EOL> '<STR_LIT>' : req_ns , <EOL> '<STR_LIT>' : constraint [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : constraint [ '<STR_LIT>' ] } <EOL> valid_solutions . append ( solution ) <EOL> self . logger . debug ( '<STR_LIT>' % valid_solutions ) <EOL> return valid_solutions <EOL> def solve_one ( self , proposed_plan = None ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( "<STR_LIT>" % ( self . constraints , <EOL> proposed_plan ) ) <EOL> constraint_list = self . _build_constraints ( self . constraints ) <EOL> self . constraints = [ x [ '<STR_LIT>' ] for x in constraint_list ] <EOL> self . logger . info ( '<STR_LIT>' % <EOL> ( self . constraints , proposed_plan ) ) <EOL> primitives = [ ] <EOL> if proposed_plan : <EOL> prim_item = self . _get_primitive_by_name ( proposed_plan [ '<STR_LIT>' ] ) <EOL> if prim_item : <EOL> primitives = [ prim_item ] <EOL> else : <EOL> primitives = self . _get_all_primitives ( ) <EOL> primitives = [ x for x in primitives if <EOL> x [ '<STR_LIT>' ] != [ ] ] <EOL> primitives = [ x for x in primitives <EOL> if self . _can_meet_constraints ( x ) ] <EOL> all_solutions = [ ] <EOL> if len ( primitives ) == <NUM_LIT:0> : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> return None <EOL> if proposed_plan : <EOL> plan_ns = proposed_plan [ '<STR_LIT>' ] <EOL> primitive = primitives [ <NUM_LIT:0> ] <EOL> solution = self . _is_forwarding_solution ( primitive , <EOL> constraint_list , <EOL> plan_ns ) <EOL> if not solution : <EOL> solution = { '<STR_LIT>' : primitive , <EOL> '<STR_LIT>' : plan_ns , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None } <EOL> all_solutions = [ solution ] <EOL> else : <EOL> for primitive in primitives : <EOL> solutions = self . _potential_solutions ( primitive , <EOL> constraint_list ) <EOL> all_solutions += solutions <EOL> self . logger . info ( "<STR_LIT>" % <EOL> ( len ( all_solutions ) , <EOL> [ x [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] for x in all_solutions ] ) ) <EOL> unconstrained_solutions = [ ] <EOL> constrained_solutions = [ ] <EOL> if not proposed_plan : <EOL> for solution in all_solutions : <EOL> solution [ '<STR_LIT>' ] = self . _get_additional_constraints ( <EOL> solution [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> solution [ '<STR_LIT>' ] ) <EOL> if solution [ '<STR_LIT>' ] is None : <EOL> self . logger . debug ( <EOL> '<STR_LIT>' % <EOL> ( solution [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , ) ) <EOL> pass <EOL> elif len ( solution [ '<STR_LIT>' ] ) != <NUM_LIT:0> : <EOL> constrained_solutions . append ( solution ) <EOL> else : <EOL> unconstrained_solutions . append ( solution ) <EOL> candidate_solutions = constrained_solutions <EOL> if len ( unconstrained_solutions ) > <NUM_LIT:0> : <EOL> candidate_solutions . append ( unconstrained_solutions [ <NUM_LIT:0> ] ) <EOL> else : <EOL> candidate_solutions = all_solutions <EOL> self . logger . info ( '<STR_LIT>' % candidate_solutions ) <EOL> for solution in candidate_solutions : <EOL> gevent . sleep ( <NUM_LIT:0> ) <EOL> self . logger . info ( "<STR_LIT>" % <EOL> ( solution [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> solution [ '<STR_LIT>' ] , <EOL> solution [ '<STR_LIT>' ] ) ) <EOL> constraints = copy . deepcopy ( self . constraints ) <EOL> applied_consequences = copy . deepcopy ( self . applied_consequences ) <EOL> self . logger . debug ( "<STR_LIT>" % <EOL> ( solution [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> solution [ '<STR_LIT>' ] ) ) <EOL> if proposed_plan is not None : <EOL> new_constraints = [ ] <EOL> else : <EOL> new_constraints = solution [ '<STR_LIT>' ] <EOL> solution . pop ( '<STR_LIT>' ) <EOL> if not solution [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] in self . task_primitives : <EOL> be , prim_name = solution [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] . split ( '<STR_LIT:.>' ) <EOL> if new_constraints is not None : <EOL> if prim_name != "<STR_LIT>" : <EOL> new_expr = '<STR_LIT>' % be <EOL> if not new_expr in new_constraints : <EOL> new_constraints . append ( new_expr ) <EOL> self . logger . info ( <EOL> '<STR_LIT>' % <EOL> new_constraints ) <EOL> if proposed_plan is not None : <EOL> new_constraints = [ ] <EOL> if new_constraints : <EOL> new_constraints = [ self . api . regularize_expression ( x ) <EOL> for x in new_constraints ] <EOL> new_constraints = [ x for x in list ( set ( new_constraints ) ) <EOL> if not self . _constraint_satisfied ( x ) ] <EOL> new_solver = None <EOL> if new_constraints is None : <EOL> self . logger . info ( '<STR_LIT>' % <EOL> solution [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] ) <EOL> else : <EOL> consequences = solution [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . logger . info ( '<STR_LIT>' % constraints ) <EOL> for consequence in consequences : <EOL> concrete_consequence = ast . concrete_expression ( <EOL> consequence , solution [ '<STR_LIT>' ] ) <EOL> applied_consequences . append ( concrete_consequence ) <EOL> concrete_constraints = ast . invert_expression ( <EOL> concrete_consequence ) <EOL> self . logger . info ( <EOL> '<STR_LIT>' % <EOL> ( concrete_consequence , concrete_constraints ) ) <EOL> if solution [ '<STR_LIT>' ] in constraints : <EOL> constraints . remove ( solution [ '<STR_LIT>' ] ) <EOL> self . logger . info ( '<STR_LIT>' % <EOL> constraints ) <EOL> new_solver = Solver ( self . base_api , self . node_id , <EOL> constraints + new_constraints , self , <EOL> solution [ '<STR_LIT>' ] , ns = solution [ '<STR_LIT>' ] , <EOL> applied_consequences = applied_consequences ) <EOL> self . children . append ( new_solver ) <EOL> if new_solver . constraints == [ ] : <EOL> return new_solver <EOL> return None <EOL> def print_tree ( self , level = <NUM_LIT:0> ) : <EOL> if self . prim : <EOL> self . logger . info ( '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' * level , self . prim [ '<STR_LIT:name>' ] ) ) <EOL> self . logger . info ( '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' * level , self . ns ) ) <EOL> self . logger . info ( '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' * level , self . constraints ) ) <EOL> self . logger . info ( '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' * level , self . applied_consequences ) ) <EOL> else : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> self . logger . info ( '<STR_LIT>' % self . constraints ) <EOL> self . logger . info ( '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' * level , len ( self . children ) ) ) <EOL> for child in self . children : <EOL> child . print_tree ( level + <NUM_LIT:1> ) <EOL> def found_solution ( self ) : <EOL> if len ( self . constraints ) == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def solve ( self ) : <EOL> """<STR_LIT>""" <EOL> top_level = self <EOL> current_leaves = [ self ] <EOL> solution_node = None <EOL> while ( len ( current_leaves ) > <NUM_LIT:0> and not solution_node ) : <EOL> new_leaves = [ ] <EOL> for leaf in current_leaves : <EOL> solution_node = leaf . solve_one ( ) <EOL> gevent . sleep ( <NUM_LIT:0> ) <EOL> if solution_node : <EOL> break <EOL> for child in leaf . children : <EOL> new_child = child <EOL> while len ( new_child . children ) > <NUM_LIT:0> : <EOL> new_child = new_child . children [ <NUM_LIT:0> ] <EOL> new_leaves . append ( new_child ) <EOL> if new_child . found_solution ( ) : <EOL> solution_node = new_child <EOL> if not solution_node : <EOL> current_leaves = new_leaves <EOL> top_level . print_tree ( ) <EOL> if solution_node : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> top_level . print_tree ( ) <EOL> while solution_node . parent : <EOL> solution_node . parent . children = [ solution_node ] <EOL> solution_node = solution_node . parent <EOL> plan = solution_node . plan ( ) <EOL> self . logger . debug ( "<STR_LIT>" % plan ) <EOL> plan_solvable = True <EOL> plan_choosable = False <EOL> for step in plan : <EOL> self . logger . debug ( '<STR_LIT>' % step ) <EOL> prim = self . _get_primitive_by_name ( step [ '<STR_LIT>' ] ) <EOL> if prim is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> addl_args = { } <EOL> if prim [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> task_name = step [ '<STR_LIT>' ] [ '<STR_LIT:action>' ] <EOL> task = [ x for x in self . task_primitives . values ( ) <EOL> if x [ '<STR_LIT>' ] == task_name ] <EOL> if len ( task ) == <NUM_LIT:1> : <EOL> addl_args = task [ <NUM_LIT:0> ] [ '<STR_LIT:args>' ] <EOL> self . logger . debug ( '<STR_LIT>' % addl_args ) <EOL> addl_args . update ( prim [ '<STR_LIT:args>' ] ) <EOL> self . logger . debug ( '<STR_LIT>' % addl_args ) <EOL> eval_args = dict ( [ [ x , addl_args [ x ] ] for x in addl_args if <EOL> addl_args [ x ] [ '<STR_LIT:type>' ] == '<STR_LIT>' ] ) <EOL> other_args = dict ( [ [ x , addl_args [ x ] ] for x in addl_args if <EOL> addl_args [ x ] [ '<STR_LIT:type>' ] != '<STR_LIT>' ] ) <EOL> for arg in other_args : <EOL> solvable , msg , choices = self . solve_arg ( arg , <EOL> addl_args [ arg ] , <EOL> step [ '<STR_LIT>' ] ) <EOL> argv = addl_args [ arg ] <EOL> if solvable : <EOL> step [ '<STR_LIT>' ] [ arg ] = choices <EOL> elif choices : <EOL> plan_choosable = True <EOL> if ( argv [ '<STR_LIT>' ] ) : <EOL> plan_solvable = False <EOL> if not '<STR_LIT:args>' in step : <EOL> step [ '<STR_LIT:args>' ] = { } <EOL> step [ '<STR_LIT:args>' ] [ arg ] = argv <EOL> step [ '<STR_LIT:args>' ] [ arg ] [ '<STR_LIT>' ] = choices <EOL> step [ '<STR_LIT:args>' ] [ arg ] [ '<STR_LIT:message>' ] = msg <EOL> else : <EOL> plan_choosable = True <EOL> if argv [ '<STR_LIT>' ] is True : <EOL> plan_solvable = False <EOL> if not '<STR_LIT:args>' in step : <EOL> step [ '<STR_LIT:args>' ] = { } <EOL> step [ '<STR_LIT:args>' ] [ arg ] = argv <EOL> step [ '<STR_LIT:args>' ] [ arg ] [ '<STR_LIT:message>' ] = msg <EOL> if plan_solvable : <EOL> eval_ns = { } <EOL> for k , v in step [ '<STR_LIT>' ] . items ( ) : <EOL> eval_ns [ k ] = v <EOL> node = self . base_api . _model_get_by_id ( '<STR_LIT>' , <EOL> self . node_id ) <EOL> nodes = self . base_api . _model_get_all ( '<STR_LIT>' ) <EOL> eval_ns [ '<STR_LIT>' ] = node <EOL> eval_ns [ '<STR_LIT>' ] = dict ( [ ( str ( x [ '<STR_LIT:id>' ] ) , x ) for x in nodes ] ) <EOL> for arg , val in eval_args . items ( ) : <EOL> if arg not in step [ '<STR_LIT>' ] : <EOL> self . logger . debug ( '<STR_LIT>' % <EOL> val [ '<STR_LIT>' ] ) <EOL> step [ '<STR_LIT>' ] [ arg ] = opencenter . webapp . ast . apply_expression ( <EOL> eval_ns , val [ '<STR_LIT>' ] , self . base_api ) <EOL> self . logger . debug ( '<STR_LIT>' % ( plan_solvable , <EOL> plan_choosable , <EOL> plan ) ) <EOL> return plan_solvable , plan_choosable , plan <EOL> else : <EOL> return False , False , [ ] <EOL> def plan ( self ) : <EOL> current = [ ] <EOL> if self . prim : <EOL> current . append ( { '<STR_LIT>' : self . prim [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : self . ns , <EOL> '<STR_LIT>' : self . prim [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : self . prim [ '<STR_LIT>' ] } ) <EOL> if len ( self . children ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if len ( self . children ) == <NUM_LIT:1> : <EOL> current += self . children [ <NUM_LIT:0> ] . plan ( ) <EOL> current_sorted = sorted ( current , key = lambda x : x [ '<STR_LIT>' ] , <EOL> reverse = True ) <EOL> if self . parent is None : <EOL> return [ { '<STR_LIT>' : x [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : x [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : x [ '<STR_LIT>' ] } for x in current_sorted ] <EOL> return current_sorted <EOL> def adventure ( self , state_number = <NUM_LIT:0> ) : <EOL> if self . prim : <EOL> if len ( self . children ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> state = '<STR_LIT>' % state_number <EOL> next_state = '<STR_LIT>' % ( state_number + <NUM_LIT:1> ) <EOL> adventure = { <EOL> '<STR_LIT>' % state_number : { <EOL> '<STR_LIT>' : self . prim [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : self . ns <EOL> } <EOL> } <EOL> if len ( self . children ) == <NUM_LIT:1> : <EOL> adventure [ state ] [ '<STR_LIT>' ] = next_state <EOL> adventure . update ( self . children [ <NUM_LIT:0> ] . adventure ( state_number + <NUM_LIT:1> ) ) <EOL> return adventure <EOL> else : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . children [ <NUM_LIT:0> ] . adventure ( ) } <EOL> def dotty ( self , fd ) : <EOL> if self . parent is None : <EOL> print >> fd , '<STR_LIT>' <EOL> print >> fd , '<STR_LIT>' '<STR_LIT>' % ( <EOL> id ( self ) , self . prim [ '<STR_LIT:name>' ] if self . prim else '<STR_LIT>' , <EOL> '<STR_LIT>' . join ( [ '<STR_LIT>' % ( <EOL> x , self . ns [ x ] ) for x in self . ns ] ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' . join ( [ '<STR_LIT:%s>' % ( <EOL> x , ) for x in self . constraints ] ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) ) <EOL> for child in self . children : <EOL> print >> fd , '<STR_LIT>' % ( id ( self ) , id ( child ) ) <EOL> child . dotty ( fd ) <EOL> if self . parent is None : <EOL> print >> fd , '<STR_LIT>' <EOL> def solve_arg ( self , name , arg , ns ) : <EOL> if name in ns : <EOL> return ( True , None , ns [ name ] ) <EOL> if arg [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> iname = arg [ '<STR_LIT:name>' ] <EOL> int_query = '<STR_LIT>' % iname <EOL> iface = self . api . _model_query ( '<STR_LIT>' , int_query ) <EOL> if len ( iface ) == <NUM_LIT:0> : <EOL> return ( False , '<STR_LIT>' % iname , None ) <EOL> if len ( iface ) > <NUM_LIT:1> : <EOL> return ( False , '<STR_LIT>' % iname , <EOL> None ) <EOL> iface_query = iface [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> nodes = self . api . _model_query ( '<STR_LIT>' , iface_query ) <EOL> if len ( nodes ) == <NUM_LIT:0> : <EOL> return ( False , '<STR_LIT>' % iname , None ) <EOL> if len ( nodes ) == <NUM_LIT:1> : <EOL> return ( True , None , nodes [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] ) <EOL> return ( False , '<STR_LIT>' % arg [ '<STR_LIT:name>' ] , <EOL> [ x [ '<STR_LIT:id>' ] for x in nodes ] ) <EOL> return ( False , '<STR_LIT>' % arg [ '<STR_LIT:type>' ] , None ) </s>
