Beispiel #1
0
def __to_python_formula(formula):
    formula = Util.endecode_string(formula, encode_pair_logic[0],
                                   encode_pair_logic[1])
    formula = formula.replace('=', '==')
    formula = Util.endecode_string(formula, encode_pair_logic[1],
                                   encode_pair_logic[0])
    return formula.replace('!', ' not ').replace('&',
                                                 ' and ').replace('|', ' or ')
def __get_ispl_formula(formula, model, fluent_tuple_list):
	#print formula
	ispl_fluent_list = ['%s%s'%(f,'_'.join(para_list)) for f, para_list, sort in fluent_tuple_list]
	fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list]
	pred_fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list \
			if f in context_operator.get_predicates()]


	formula = Formula.transform_entailment(formula)
	formula = ____add_env(formula, fluent_list)
	#print formula
	formula = Formula.grounding(formula, model)
	#print formula
	formula = ____ispl_simplify(formula, 'Environment.Action')
	#print formula
	formula = ____to_ispl_preds(formula, pred_fluent_list)
	#print formula
	#formula = ____add_env(formula, fluent_list)
	#print formula
	formula = Util.endecode_string(formula, fluent_list, ispl_fluent_list)
	#print formula
	formula = ____to_ispl_logic(formula)
	#print formula
	#print 
	return formula
Beispiel #3
0
def transform_entailment(formula):
    old_formula = ""
    while old_formula != formula:
        old_formula = formula
        formula = Util.repeat_replace_inner_with_pattern(
            quantifier_pattern, __mrepl_entailment_quantifier, old_formula)
    return Util.endecode_string(__eliminate_bracket(formula), encode_pair[1],
                                encode_pair[0])
Beispiel #4
0
def ____add_sort_forall(rule_list):
    for e, rule in enumerate(rule_list):
        new_formula = Util.repeat_replace_inner_with_pattern(
            addsort_pattern, ______mrepl_addsort_forall, rule[2])
        new_formula = Util.endecode_string(new_formula,
                                           Util.encode_pair_forall[1],
                                           Util.encode_pair_forall[0])
        rule_list[e] = (rule[0], rule[1], new_formula)
    return rule_list
Beispiel #5
0
def ______mrepl_addsort_forall(matched):
    sort_dict = context_operator.get_symbol_sorts_dict()
    var_list = matched.group("var").split(",")
    var_sort_list = [var + ":" + sort_dict[var] for var in var_list]
    var_sort_str = ",".join(var_sort_list)
    var_sort_str = matched.group(
        "head") + "(" + var_sort_str + ")" + matched.group('body')
    return Util.endecode_string(var_sort_str, Util.encode_pair_forall[0],
                                Util.encode_pair_forall[1])
Beispiel #6
0
def __normalize_formula(formula):
	encoded_normal_str = Util.repeat_replace_inner_with_pattern(forall_pattern, __mrepl_handle_forall, formula)
	context_operator.use_local_dict() 
	encoded_normal_str = __normalize_fluents(encoded_normal_str)
	
	add_formula = __get_formula_from_local_dict()
	#add_formula = Util.endecode_string(add_formula, Util.encode_pair[1], Util.encode_pair[0])
	#--(***)---
	encoded_normal_str = add_formula+ "&( " + encoded_normal_str + ")}" if add_formula!="" else encoded_normal_str

	normal_str = Util.endecode_string(encoded_normal_str,encode_pair_forall[1],encode_pair_forall[0])
	return normal_str
Beispiel #7
0
def __mrepl_handle_forall(matched):
	context_operator.use_local_dict()   # a dict to store { fluent: var}
	encoded_str = Util.endecode_string(matched.group(), encode_pair_forall[0],encode_pair_forall[1])
	body_str = matched.group('body')
	#print "body", body_str
	normal_str = __normalize_fluents(body_str)
	#print "normal", normal_str
	add_formula = __get_formula_from_local_dict()
	#--(***)---return matched.group('head')+ '{'+ add_formula+' and '+normal_str+ '}' if add_formula!="" else encoded_str
	#print matched.group()
	#exit(0)
	return "%s{ %s&(%s)} }"% (matched.group('head'), add_formula, normal_str) if add_formula!="" else encoded_str
def __get_ispl_update(fluent_tuple_list, p1_action_tuple_list, p2_action_tuple_list, model):

	universe, assignment, default_value = model
	ispl_fluent_list = ['%s%s'%(f,'_'.join(para_list)) for f, para_list, sort in fluent_tuple_list]
	fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list]

	predicates = context_operator.get_predicates()
	fun_fluent_list = [('%s(%s)'%(f,','.join(para_list)), sort) for f, para_list, sort in fluent_tuple_list if f not in predicates]
	pred_fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list if f in predicates and f!='turn']
	#print fun_fluent_list
	#print pred_fluent_list
	fluent_value_list = sum([list(itertools.product([fluent],universe[sort])) \
		for fluent, sort in fun_fluent_list] ,[])
	fluent_value_list = ['%s=%s'%(fluent,value) for fluent, value in fluent_value_list]
	fluent_value_list.extend(pred_fluent_list)


	ispl_p1_action_list = ['Player1.Action = %s%s'%(a,'_'.join(para_list)) for a, para_list in p1_action_tuple_list]
	p1_action_list = ['%s(%s)'%(a,','.join(para_list)) for a, para_list in p1_action_tuple_list]
	ispl_p2_action_list = ['Player2.Action = %s%s'%(a,'_'.join(para_list)) for a, para_list in p2_action_tuple_list]
	p2_action_list = ['%s(%s)'%(a,','.join(para_list)) for a, para_list in p2_action_tuple_list]

	action_pair_list = [(a,b) for a, b in zip(p1_action_list+p2_action_list, ispl_p1_action_list+ispl_p2_action_list)]
	update_pair_list = [ (a,b,c) for a, (b,c) in itertools.product(fluent_value_list, action_pair_list)]
	update_pair_list = [ (fluent, atomic_regress.poss_or_ssa(action, fluent), ispl_action) \
						for fluent, action, ispl_action in update_pair_list]
	# transform (=>) to (not or)
	update_pair_list = [ (a, Formula.transform_entailment(b), c) for a, b, c in update_pair_list]
	update_pair_list = [ (a, Formula.grounding(b,model), c) for a, b, c in update_pair_list]
	update_pair_list = [ (a, ____ispl_simplify(b,'Player1.Action'), c) for a, b, c in update_pair_list]
	#update_pair_list = [ (a, b, c) for a, b, c in update_pair_list if b is not None]

	pred_fluent_list = ['%s(%s)'%(f,','.join(para_list)) for f, para_list, sort in fluent_tuple_list if f in predicates]
	# add =true to each predicates
	update_pair_list = [ (fluent, ____to_ispl_preds(update, pred_fluent_list), ispl_action) for fluent, update, ispl_action in  update_pair_list]

	fun_update_pair_list = [ (fluent, update, ispl_action) for fluent, update, ispl_action in  update_pair_list if fluent not in pred_fluent_list and update !='false'] 
	pred_update_pair_list = [ (fluent, update, ispl_action) for fluent, update, ispl_action in  update_pair_list if fluent in pred_fluent_list] 

	update_list = ['%s if (%s) and %s;'%(fluent, update, ispl_action) for fluent, update, ispl_action in  fun_update_pair_list ]
	update_list += ['%s=true if (%s) and %s;'%(fluent, update, ispl_action) for fluent, update, ispl_action in  pred_update_pair_list if update !='false' ]
	update_list += ['%s=false if !(%s) and %s;'%(fluent, update, ispl_action) for fluent, update, ispl_action in  pred_update_pair_list if update !='false' ]
	update_list += ['%s=false if %s;'%(fluent, ispl_action) for fluent, update, ispl_action in  pred_update_pair_list if update =='false' ]
	update_list += turn_list
	#update_list = [____add_env(formula, fluent_list) for formula in update_list]
	update_list = [ Util.endecode_string(formula, fluent_list, ispl_fluent_list) for formula in update_list]
	update_list = [ ____to_ispl_logic(formula) for formula in update_list]

	#return '\n    '.join(update_list)
	return update_list
Beispiel #9
0
def __grounding_conjunct(var_list,
                         sorts,
                         formula,
                         universe,
                         var_constraint_dict=None):

    #vars_consts = [ (var, var_constraint_dict[var]) if var in var_constraint_dict else (var, universe[sorts[e]]) for e, var in enumerate(var_list) ]
    #print '~~~~', var_list, sorts, formula, universe
    vars_consts = [(var, universe[sorts[e]]) for e, var in enumerate(var_list)]
    #print vars_consts
    consts_list = list(itertools.product(*zip(*vars_consts)[1]))
    #print consts_list
    instances = [
        Util.endecode_string(formula, var_list, list(consts))
        for consts in consts_list
    ]
    return '|'.join(instances)
Beispiel #10
0
def __normalize_fluents(encoded_str):
	encoded_str = " "+encoded_str+" " # in order to detect fun_pre and fun_pos (sometimes fails)
	new_encoded_str = encoded_str
	while True:
		if context_operator.get_nregx_function_patterns() ==list():
			break
		#for nreg_fluent_pattern in nreg_fluent_pattern_list:
		for nreg_fluent_pattern in context_operator.get_nregx_function_patterns():
			new_encoded_str = Util.repeat_replace_inner_with_pattern(nreg_fluent_pattern, __mrepl_detect_fluent, encoded_str)
			encoded_str = new_encoded_str
			#print '---',encoded_str 
		if new_encoded_str == encoded_str:
			new_encoded_str = re.sub(pattern.inner_parenth, __mrepl_encode, encoded_str)
			#print "replace () -> []", new_encoded_str
			if encoded_str == new_encoded_str:
				break
		encoded_str = new_encoded_str
	return Util.endecode_string(encoded_str, encode_pair[1], encode_pair[0]).strip()
def ____add_env(formula, fluent_list):
	new_fleunt_list = ['Environment.%s'%(fluent) for fluent in fluent_list]
	return Util.endecode_string(formula, fluent_list, new_fleunt_list)
def ____to_ispl_preds(formula, pred_list):
	new_pred_list = ['%s=true'%(pred) for pred in pred_list] 
	return Util.endecode_string(formula, pred_list, new_pred_list)
def ____to_ispl_logic(formula):
	#print  zip(encode_ispl[0], encode_ispl[1])
	#return Util.repeat_do_function(Util.sub_lambda_exp, zip(encode_ispl[0], encode_ispl[1]), formula)
	#return Util.repeat_do_function(Util.sub_lambda_exp, zip(encode_ispl[0], encode_ispl[1]), formula)
	return Util.endecode_string(formula, encode_ispl[0], encode_ispl[1])
def ____to_ispl_vars(formula, fluent_list, ispl_fluent_list):
	return Util.endecode_string(formula, fluent_list, ispl_fluent_list)
Beispiel #15
0
def __mrepl_entailment_quantifier(matched):
    #print matched.groups()
    formula = __eliminate_bracket(matched.group(3))
    return Util.endecode_string(
        "%s(%s)[%s]" % (matched.group(1), matched.group(2), formula),
        encode_pair[0], encode_pair[1])
Beispiel #16
0
def __mrepl_encode(matched):
	match_str=matched.group()
	encoded_str=Util.endecode_string(match_str, encode_pair[0], encode_pair[1])
	return encoded_str