def import_graph(self, G: nx.Graph) -> List[WorldRoom]: """ Convert Graph object to a list of `Proposition`. Args: G: Graph defining the structure of the world. """ rooms = OrderedDict((n, self.new_room(d.get("name", None))) for n, d in G.nodes.items()) for src, dest, data in G.edges(data=True): src_exit = rooms[src].exits[direction(dest, src)] dest_exit = rooms[dest].exits[direction(src, dest)] path = self.connect(src_exit, dest_exit) if data.get("has_door"): door = self.new_door(path, data['door_name']) door.add_property(data["door_state"]) return list(rooms.values())
def graph2state(G: networkx.Graph, rooms: Dict[str, Variable]) -> List[Proposition]: """ Convert Graph object to a list of `Proposition`. Args: G: Graph defining the structure of the world. rooms: information about the rooms in the world. """ state = [] for src, dest in G.edges(): d = direction(src, dest) d_r = direction(dest, src) e = G[src][dest] room_src = rooms[src] room_dest = rooms[dest] if e["has_door"]: door = Variable(e['door_name'], "d") pred1 = Proposition("{}_of".format(d), [room_dest, room_src]) pred2 = Proposition("{}_of".format(d_r), [room_src, room_dest]) state.append(Proposition(e["door_state"], [door])) state.append(Proposition("link", [room_src, door, room_dest])) state.append(Proposition("link", [room_dest, door, room_src])) if e["door_state"] == "open": state.append(Proposition("free", [room_dest, room_src])) state.append(Proposition("free", [room_src, room_dest])) else: pred1 = Proposition("{}_of".format(d), [room_dest, room_src]) pred2 = Proposition("{}_of".format(d_r), [room_src, room_dest]) state.append(Proposition("free", [room_dest, room_src])) state.append(Proposition("free", [room_src, room_dest])) state.append(pred1) state.append(pred2) return state