def exec_bulk_country_query(country_queries, priority): airport, country_queries_unzip = list(zip(*country_queries)) query = WOQLQuery().woql_and(*country_queries_unzip) try: # with wary.suppress_Terminus_diagnostics(): result = query.execute(client) df_result = pd.DataFrame() if is_empty(result) else wdf.query_to_df( result) if not df_result.empty: df_result['id'] = df_result.index columns = [ "All", "Paren1", "Country", "country_id", getPriority(priority), "country_name" ] df_result = pd.wide_to_long(df_result, columns, i="id", j="val").reset_index() return (zip(airport, df_result["country_id"])) ## TODO: Waiting that opt() work to catch bad query and rerun them. except Exception as e: print("EXCEPTION") wary.diagnose(e)
def create_schema(client): # # We first create an abstract class to represent ephemeral entities - things that have lifespans # schema = WOQLQuery().when(True).woql_and( WOQLQuery().doctype("EphemeralEntity").label("Ephemeral Entity"). description("An entity that has a lifespan").abstract().property( "lifespan_start", "dateTime").label("Existed From").property( "lifespan_end", "dateTime").label("Existed To"), # # This allows us to attach existence start and end times to entities, in a consistent way. # Then we create our four actual concrete classes, each as a subclass of this one: # # That gives you the meta-structure - the major relationships between airports, airlines, countries and flights - # you can then add whatever other properties you want. All the entities also have lifespan_start and lifespan_end # properties inherited from EphemeralEntity WOQLQuery().add_class("Country").label("Country").description( "A nation state").parent("EphemeralEntity").property( "country_id", "string").label("Id").property( "country_name", "string").label("Name").property( "country_iso_code", "string").label("ISO Code").property( "country_fips_code", "string").label("FIPS Code"), #primary key WOQLQuery().add_class("Airline").label("Airline").description( "An operator of airplane flights").parent( "EphemeralEntity").property("icao_code", "string").label( "ICAO Code") # primary key .property("registered_in", "Country").label("Registered In"), WOQLQuery().add_class("Airport").label("Airport").description( "An airport where flights terminate").parent( "EphemeralEntity").property( "situated_in", "Country").label("Situated In").property( "icao_code", "string").label("ICAO Code") # primary key .property("iata_code", "string").label("IATA Code").property( "name", "string").label("Name"), WOQLQuery().add_class("Flight").label("Flight").description( "A flight between airports").parent("EphemeralEntity").property( "departs", "Airport").label("Departs").property( "arrives", "Airport").label("Arrives").property( "operated_by", "Airline").label("Operated By")) try: print("[Building schema..]") with wary.suppress_Terminus_diagnostics(): schema.execute(client) except Exception as e: wary.diagnose(e)
def insert_countries(countries): c_generator = generate_bulk_insert(countries, country_query) for Matches, Inserts in c_generator: query = WOQLQuery().when(WOQLQuery().woql_and(*Matches), WOQLQuery().woql_and(*Inserts)) try: #with wary.suppress_Terminus_diagnostics(): query.execute(client) except Exception as e: print("EXCEPTION") wary.diagnose(e)
def insert_airports(airports): c_generator = generate_bulk_insert(airports, airport_query) for Matches, Inserts in c_generator: query = WOQLQuery().when( WOQLQuery().woql_and(*Matches), WOQLQuery().woql_and(*Inserts), ) try: print("INSERT NEXT TEN") with wary.suppress_Terminus_diagnostics(): query.execute(client) except Exception as e: print("EXCEPTION") wary.diagnose(e)
def create_schema(client): ''' Build the schema. For this example, it is very simple: just a doctype for a Person with various attributes :param client: TerminusDB server handle ''' schema = WOQLQuery().when(True).woql_and(WOQLQuery().doctype( "Person").label("Person").description("Somebody").property( "Name", "string").property("Sex", "string").property( "Parent1", "string").property("Parent2", "string")) try: print("[Building schema..]") with wary.suppress_Terminus_diagnostics(): schema.execute(client) except Exception as e: wary.diagnose(e)
def load_csv(client, url, voyages): ''' Read a .csv file and use its raw data to initialise a graph in the TerminusDB server. In the case of a local file, it should be the file path relative to the value of the TERMINUS_LOCAL environment variable set when the TerminusDB server was started... :param client: handle on the TerminusDB server :param url: string, eiher a local file name or http-style url :param voyages: boolean, whether a Voyage or Berth document set are to be created :return: None ''' csv = get_csv_variables(url, voyages) wrangles = get_wrangles(voyages) inputs = WOQLQuery().woql_and(csv, *wrangles) inserts = get_inserts(voyages) answer = WOQLQuery().when(inputs, inserts) try: print("[Loading raw data from '{}'..]".format(url)) with wary.suppress_Terminus_diagnostics(): answer.execute(client) except woqlError.APIError as e: wary.diagnose(e)
def create_schema(client): ''' Build the schema. For this demo, there is a base document to capture ephemeral events. There is then a derived document for Voyage events, and Berth events. Note especially that ANY property common to all the derived documents MUST be put in the base: Here, the 'ship' property is common to both 'Voyage' and 'Berth' and so must be in the 'Ship_Event' document. If instead it is placed in 'Voyage' and also in 'Base', then TerminusDB will complain with a "class subsumption" error... :param client: TerminusDB server handle ''' base = WOQLQuery().doctype("Ship_Event").label("Ship Event").description("An ephemeral") base.property("ship", "string").label("Ship Name") base.property("start", "dateTime").label("Existed From") # try "dateTime rather than string? base.property("end", "dateTime").label("Existed To") voyage = WOQLQuery().add_class("Voyage").label("Voyage").description("Ship movement").parent("Ship_Event") voyage.property("route", "string").label("Route") docking = WOQLQuery().add_class("Docking").label("Docking").description("A ship docked at a berth").parent("Ship_Event") docking.property("berth", "string").label("Berth") schema = WOQLQuery().when(True).woql_and( base, docking, voyage ) try: print("[Building schema..]") with wary.suppress_Terminus_diagnostics(): schema.execute(client) except Exception as e: wary.diagnose(e)
####################################################################################################################### if __name__ == "__main__": # # Connect to TerminusDB, clean out any previous version of the charities database # and build a new version using the raw .csv data # client = woql.WOQLClient() try: print("[Connecting to the TerminusDB server..]") with wary.suppress_Terminus_diagnostics(): client.connect(server_url, key) except Exception as e: wary.diagnose(e) try: print("[Removing prior version of the database, if it exists..]") with wary.suppress_Terminus_diagnostics(): client.deleteDatabase(dbId) except Exception as e: print("[No prior database to delete]") try: print("[Creating new schema..]") with wary.suppress_Terminus_diagnostics(): client.createDatabase(dbId, "People", key=None, comment="People graphbase") except Exception as e: wary.diagnose(e)