Ejemplo n.º 1
0
def rewrite_loop(rule_classes, init_goals, logger, recv_msg_future_func, send_msgs_func, create_new_location_func, location, output_logger=None):
	fact_stores = new_stores()
	histories = new_histories()

	rules = map(lambda rule_class: rule_class(), rule_classes)
	if location != None:
		for rule in rules:
			rule.set_rank( loc_rank(location.value) )
	interp_rules = interpret_rules(rules, fact_stores)

	goals = HeapGoals()
	matching_funcs = generate_matching_functions(goals, fact_stores, histories, interp_rules, logger, send_msgs_func, create_new_location_func, location=location)

	for goal in init_goals:
		goals.push( make_fact_repr(goal) )

	recv_msg_func = recv_msg_future_func()
	current_steps  = default_steps
	current_factor = backoff_factor
	done = False

	while not done:
		current_steps = default_steps
		ext_fact_repr = recv_msg_func()

		if ext_fact_repr != None:
			while ext_fact_repr != None:
				goals.push(ext_fact_repr)
				recv_msg_func = recv_msg_future_func()
				ext_fact_repr = recv_msg_func()
			current_factor = backoff_factor
		else:
			current_steps  *= current_factor 
			current_factor *= 2

		while current_steps > 0:
			try:
				act_fact_repr = goals.pop()
				matching_funcs[act_fact_repr['sym_id']](act_fact_repr)
				current_steps -= 1
			except IndexError:
				ext_fact_repr = try_until(recv_msg_func, times=pause_times)
				if ext_fact_repr != None:
					goals.push(ext_fact_repr)
					recv_msg_func = recv_msg_future_func()
					current_factor = backoff_factor
					current_steps  = 0
				else:
					done = True
					break

	log_info(logger,pretty_stores( fact_stores ))

	if not (output_logger == None):
		log_info(output_logger,"Location %s:\n%s" % (location.value,pretty_stores(fact_stores,brief=True)) )
Ejemplo n.º 2
0
def add_goal(goals, fact):
	fact_repr = make_fact_repr(fact)
	# fact_repr = { 'sym_id' : fact['sym_id'], 'values':map(lambda f: f.value,fact['terms']) }
	goals.append(fact_repr)
Ejemplo n.º 3
0
def add_goal(goals, fact):
    fact_repr = make_fact_repr(fact)
    # fact_repr = { 'sym_id' : fact['sym_id'], 'values':map(lambda f: f.value,fact['terms']) }
    goals.append(fact_repr)