File size: 2,834 Bytes
d211a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import gradio as gr
from src.utils import Utils
from swiplserver import PrologMQI
import plotly.graph_objects as go

facts_path = "./src/facts.pl"
search_algorithm_path = "./src/search_algorithms.pl"

utils = Utils()
possible_routes = []

def query(origin, destination):
    with PrologMQI() as mqi:
        with mqi.create_thread() as prolog_thread:
            result = prolog_thread.query(f'consult("{facts_path}").')
            print(f"Loaded facts: {result}")
            result = prolog_thread.query(f'consult("{search_algorithm_path}").')
            print(f"Loaded rules: {result}")
            print(f"Query Path: {origin} - - to - - {destination}")
            prolog_thread.query_async(f"a_star_search({utils.remove_spl_chars(origin)}, {utils.remove_spl_chars(destination)}, Path).", find_all=False)
            result = prolog_thread.query_async_result()
            if (result == False) or (len(result) == 0):
                return []
            print(f"Total Results: {len(result)}")
            routes = result[0]['Path']
            print(f"Result route: {routes}")
            return routes

def load_map():
    center_lat, center_lon = utils.get_map_center()
    fig = go.Figure(layout=utils.get_default_map_layout(center_lat, center_lon))
    fig.add_traces(utils.get_traces())
    fig.add_trace(utils.get_markers())
    fig = utils.filter_duplicate_traces(fig)
    return fig

def filter_map(origin, destination):
    center_lat, center_lon = utils.get_map_center()
    output_text = ""    
    traces = []
    if (len(origin) > 0) and (len(destination) > 0):
        route = query(origin, destination)
        if len(route) == 0:
            output_text = "No Route Found"
        else:
            traces, center_lat, center_lon, output_text = utils.filter_marker_and_traces(route)
    else:
        traces = utils.get_traces()

    fig = go.Figure(data=traces,layout=utils.get_default_map_layout(center_lat, center_lon))
    fig = utils.filter_duplicate_traces(fig)
    return fig, output_text

with gr.Blocks() as demo:
    with gr.Column():
        gr.Label(value="London Underground Stations Route Finder", label="")
        map = gr.Plot(label="London Underground Station Map")
        with gr.Row():
            origin = gr.Dropdown(
                choices=utils.get_all_station_names(), label="Origin 🔵")
            destination = gr.Dropdown(
                choices=utils.get_all_station_names(), label="Destination 🔴")
        with gr.Row():
            btn_clear = gr.Button(value="Clear")
            btn_search = gr.Button(value="Search")
            btn_clear.click(load_map, [], map)
        output_label = gr.Label(label="Optimal Route")
        btn_search.click(filter_map, [origin, destination], [map, output_label])
    demo.load(load_map, [], map)

demo.launch()