def write_doors_to_file(doors): door_set = {} if len(doors) != 0: doors.pop(0) for door in doors: door_atom = door[0].replace("#", "_door") logical_form = "objectPos(%s,%s,%s,0)" % (door_atom, door[1], door[2]) global_vars.prolog_files["objects"].write(logical_form + ".\n") global_vars.prolog_assert_handle(logical_form) logical_form = "stat_node(%s,%s,%s)" % (door_atom, door[1], door[2]) global_vars.prolog_files["graph"].write(logical_form + ".\n") for other_door in doors: if (door[0] != other_door[0] and are_same_door(door, other_door)): other_door[0] = door[0] for door in doors: if not door[0] in door_set: logical_form = "objectType(%s,door)" % door[0].replace( "#", "_door") global_vars.prolog_files["objects"].write(logical_form + ".\n") global_vars.prolog_assert_handle(logical_form) door_set[door[0]] = ""
def learn(room_tag, obj_tag, obj_par, column, row, assertions_list=[""], asserting=True): obj_atom = get_obj_atom(room_tag, obj_tag) obj_time = get_obj_time(obj_tag) obj_type = get_obj_type(obj_tag) log_to_terminal("learning %s" % obj_atom) logical_form = "objectPos(%s,%s,%s,0)" % (obj_atom, column, row) global_vars.prolog_files["objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) logical_form = "objectType(%s,%s)" % (obj_atom, obj_type) if not (logical_form in assertions_list): assertions_list.append(logical_form) global_vars.prolog_files["objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) logical_form = "objectLastSeen(%s,%s)" % (obj_atom, obj_time) if not (logical_form in assertions_list): assertions_list.append(logical_form) global_vars.prolog_files["objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) #Expecting obj1par1~obj1par1value1^obj1par1value2...~obj1par2...#obj2par1~obj2par1value1^obj2par1value2... if (obj_par != "0"): obj_par_list = obj_par.split("~") for k in xrange(int(len(obj_par_list) / 2)): par_name_index = 2 * k par_value_index = 2 * k + 1 if obj_par_list[par_name_index] != "objectAngle": logical_form = "%s(%s,%s)" % (obj_par_list[par_name_index], obj_atom, obj_par_list[par_value_index]) else: angle = float(obj_par_list[par_value_index]) * 180 / math.pi logical_form = "%s(%s,%s)" % (obj_par_list[par_name_index], obj_atom, int(round(angle, -1))) if not (logical_form in assertions_list): assertions_list.append(logical_form) global_vars.prolog_files["objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) global_vars.last_object_memorized = obj_atom
def calculate_and_assert_rooms_center_and_type(room_list, asserting=True): room_type_list = ["office", "room", "corridor", "printerRoom", "phdRoom"] for room in room_list: centroid = global_vars.prolog_service_handle("roomCellCentroid", [room, "X", "Y"]).ris[0] room_center = [int(round(float(centroid.atoms[1]))), int(round(float(centroid.atoms[2])))] room_type_in_list = False for room_type in room_type_list: if room_type in room.lower(): logical_form = "roomType(%s,%s)" % (room, room_type) room_type_in_list = True if not room_type_in_list: logical_form = "roomType(%s,%s)" % (room, room) logical_form1 = "roomCenter(%s,%s,%s)" % (room, room_center[0], room_center[1]) logical_form2 = "dyn_node(%s,%s,%s)" % (room, room_center[0], room_center[1]) global_vars.prolog_files["rooms"].write(logical_form + ".\n") global_vars.prolog_files["rooms"].write(logical_form1 + ".\n") global_vars.prolog_files["graph"].write(logical_form2 + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) global_vars.prolog_assert_handle(logical_form1) global_vars.prolog_assert_handle(logical_form2)
def calculate_and_assert_rooms_center_and_type(room_list, asserting=True): room_type_list = ["office", "room", "corridor", "printerRoom", "phdRoom"] for room in room_list: centroid = global_vars.prolog_service_handle("roomCellCentroid", [room, "X", "Y"]).ris[0] room_center = [ int(round(float(centroid.atoms[1]))), int(round(float(centroid.atoms[2]))) ] room_type_in_list = False for room_type in room_type_list: if room_type in room.lower(): logical_form = "roomType(%s,%s)" % (room, room_type) room_type_in_list = True if not room_type_in_list: logical_form = "roomType(%s,%s)" % (room, room) logical_form1 = "roomCenter(%s,%s,%s)" % (room, room_center[0], room_center[1]) logical_form2 = "dyn_node(%s,%s,%s)" % (room, room_center[0], room_center[1]) global_vars.prolog_files["rooms"].write(logical_form + ".\n") global_vars.prolog_files["rooms"].write(logical_form1 + ".\n") global_vars.prolog_files["graph"].write(logical_form2 + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) global_vars.prolog_assert_handle(logical_form1) global_vars.prolog_assert_handle(logical_form2)
def remember(old_object_atom, new_object_atom="", asserting=True): global_vars.prolog_files["objects"].close() global_vars.prolog_files["objects"] = open("objects.pl", "r") content = global_vars.prolog_files["objects"].readlines() global_vars.prolog_files["objects"].close() os.remove("objects.pl") global_vars.prolog_files["objects"] = open("objects.pl", "a") for line in content: if not old_object_atom in line: global_vars.prolog_files["objects"].write(line) else: global_vars.prolog_retract_handle(line.replace(".\n", "")) logical_form = "prev_" + line global_vars.prolog_files["old_objects"].write(logical_form) if asserting: global_vars.prolog_assert_handle(logical_form) if new_object_atom != "": logical_form = "replaced_by(%s,%s)" % (old_object_atom, new_object_atom) global_vars.prolog_files["old_objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form)
def write_doors_to_file(doors): door_set = {} if len(doors) != 0: doors.pop(0) for door in doors: door_atom = door[0].replace("#", "_door") logical_form = "objectPos(%s,%s,%s,0)" % (door_atom, door[1], door[2]) global_vars.prolog_files["objects"].write(logical_form + ".\n") global_vars.prolog_assert_handle(logical_form) logical_form = "stat_node(%s,%s,%s)" % (door_atom, door[1], door[2]) global_vars.prolog_files["graph"].write(logical_form + ".\n") for other_door in doors: if door[0] != other_door[0] and are_same_door(door, other_door): other_door[0] = door[0] for door in doors: if not door[0] in door_set: logical_form = "objectType(%s,door)" % door[0].replace("#", "_door") global_vars.prolog_files["objects"].write(logical_form + ".\n") global_vars.prolog_assert_handle(logical_form) door_set[door[0]] = ""
def remember(old_object_atom, new_object_atom="", asserting=True): global_vars.prolog_files["objects"].close() global_vars.prolog_files["objects"] = open('objects.pl', 'r') content = global_vars.prolog_files["objects"].readlines() global_vars.prolog_files["objects"].close() os.remove("objects.pl") global_vars.prolog_files["objects"] = open('objects.pl', 'a') for line in content: if not old_object_atom in line: global_vars.prolog_files["objects"].write(line) else: global_vars.prolog_retract_handle(line.replace(".\n", "")) logical_form = "prev_" + line global_vars.prolog_files["old_objects"].write(logical_form) if asserting: global_vars.prolog_assert_handle(logical_form) if (new_object_atom != ""): logical_form = "replaced_by(%s,%s)" % (old_object_atom, new_object_atom) global_vars.prolog_files["old_objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form)
def learn(room_tag, obj_tag, obj_par, column, row, assertions_list=[""], asserting=True): obj_atom = get_obj_atom(room_tag, obj_tag) obj_time = get_obj_time(obj_tag) obj_type = get_obj_type(obj_tag) log_to_terminal("learning %s" % obj_atom) logical_form = "objectPos(%s,%s,%s,0)" % (obj_atom, column, row) global_vars.prolog_files["objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) logical_form = "objectType(%s,%s)" % (obj_atom, obj_type) if not (logical_form in assertions_list): assertions_list.append(logical_form) global_vars.prolog_files["objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) logical_form = "objectLastSeen(%s,%s)" % (obj_atom, obj_time) if not (logical_form in assertions_list): assertions_list.append(logical_form) global_vars.prolog_files["objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) # Expecting obj1par1~obj1par1value1^obj1par1value2...~obj1par2...#obj2par1~obj2par1value1^obj2par1value2... if obj_par != "0": obj_par_list = obj_par.split("~") for k in xrange(int(len(obj_par_list) / 2)): par_name_index = 2 * k par_value_index = 2 * k + 1 if obj_par_list[par_name_index] != "objectAngle": logical_form = "%s(%s,%s)" % (obj_par_list[par_name_index], obj_atom, obj_par_list[par_value_index]) else: angle = float(obj_par_list[par_value_index]) * 180 / math.pi logical_form = "%s(%s,%s)" % (obj_par_list[par_name_index], obj_atom, int(round(angle, -1))) if not (logical_form in assertions_list): assertions_list.append(logical_form) global_vars.prolog_files["objects"].write(logical_form + ".\n") if asserting: global_vars.prolog_assert_handle(logical_form) global_vars.last_object_memorized = obj_atom
def save_combinedPlan_name_in_kb(kbPlanName, paramsList, pcl): paramString = "" for i in paramsList: if i != "": paramString += i[0].lower() + i[1:] + "," assertion = "combinedPlan(%s)" % kbPlanName assertion2 = "plan(%s,[%s],[])" % (kbPlanName, paramString[:-1]) assertion3 = "pcl(%s,\'%s\')" % (kbPlanName, pcl) global_vars.prolog_assert_handle(assertion) global_vars.prolog_assert_handle(assertion2) global_vars.prolog_assert_handle(assertion3) os.chdir(global_vars.prolog_directory) f = open("plans.pl", 'a') f.write(assertion + ".\n") f.write(assertion2 + ".\n") f.write(assertion3 + ".\n") f.close()
def save_combinedPlan_name_in_kb(kbPlanName, paramsList, pcl): paramString = "" for i in paramsList: if i != "": paramString += i[0].lower() + i[1:] + "," assertion = "combinedPlan(%s)"%kbPlanName assertion2 = "plan(%s,[%s],[])"%(kbPlanName, paramString[:-1]) assertion3 = "pcl(%s,\'%s\')"%(kbPlanName, pcl) global_vars.prolog_assert_handle(assertion) global_vars.prolog_assert_handle(assertion2) global_vars.prolog_assert_handle(assertion3) os.chdir(global_vars.prolog_directory) f = open("plans.pl", 'a') f.write(assertion + ".\n") f.write(assertion2 + ".\n") f.write(assertion3 + ".\n") f.close()