def list_grandfathers_of(child=None): ''' Return a dataframe with the names of the grandfathers of a child. Of course, in real life, a child might have 0,1 or 2 grandfathers. :param child: string: woql variable or literal :return: If child is None, then a dataframe with all children and their respective grandfather. Otherwise, a dataframe with the grandfathers of the given child. ''' selects = ["v:Child_Name", "v:GFather1_Name", "v:GFather2_Name" ] if child is None else ["v:GFather1_Name", "v:GFather2_Name"] # # Terminus currently has a bug with literal values in queries. Should be able to do: # WOQLQuery().triple("v:Child", "Name", child) here if child is a literal, but instead have to use @type.. # child = "v:Child_Name" if child is None else { '@type': 'xsd:string', '@value': child } q = WOQLQuery().select(*selects).woql_and( grandfathers_of("v:Child", "v:GFather1", "v:GFather2"), WOQLQuery().triple("v:Child", "Name", child), WOQLQuery().triple("v:GFather1", "Name", "v:GFather1_Name"), WOQLQuery().triple("v:GFather2", "Name", "v:GFather2_Name")) result = wary.execute_query(q, client) return pd.DataFrame( columns=selects) if is_empty(result) else wdf.query_to_df(result)
def list_parents_of(child=None): ''' Return a dataframe with the name of both parents of a child. Of course, in real life, a child might have just a single parent, or be an orphan... :param child: string: woql variable or literal :return: If child is None, then a dataframe with all children and their respective parents. Otherwise, a dataframe with the two parents of the given child ''' selects = ["v:Child_Name", "v:P1_Name", "v:P2_Name" ] if child is None else ["v:P1_Name", "v:P2_Name"] # # Terminus currently has a bug with literal values in queries. Should be able to do: # WOQLQuery().triple("v:Child", "Name", child) here if child is a literal, but instead have to use @type.. # child = "v:Child_Name" if child is None else literal_string(child) q = WOQLQuery().select(*selects).woql_and( parents_of("v:Child", "v:Parent1", "v:Parent2"), WOQLQuery().triple("v:Child", "Name", child), WOQLQuery().triple("v:Parent1", "Name", "v:P1_Name"), WOQLQuery().triple("v:Parent2", "Name", "v:P2_Name")) result = wary.execute_query(q, client) return pd.DataFrame( columns=selects) if is_empty(result) else wdf.query_to_df(result)
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 list_grandchildren_of(gParent): ''' Return a dataframe with the names of the grandchildren of a grandparent. :param gParent: string: name of a grandparent :return: a dataframe with all the grandchildren ''' selects = ["v:Child_Name"] q = WOQLQuery().select(*selects).woql_and(grandchildren_of(gParent)) result = wary.execute_query(q, client) return pd.DataFrame( columns=selects) if is_empty(result) else wdf.query_to_df(result)
def list_children_of(parent): ''' Return a dataframe with the names of the children of a parent. :param parent: string: name of a parent :return: If child is None, then a dataframe with all children and their respective mothers. Otherwise, a dataframe with the mother of the given child ''' selects = ["v:Child_Name"] q = WOQLQuery().select(*selects).woql_and(children_of(parent)) result = wary.execute_query(q, client) return pd.DataFrame( columns=selects) if is_empty(result) else wdf.query_to_df(result)
def list_people(): ''' Return a dataframe with the name of each person ''' selects = ["v:Name", "v:Sex", "v:Parent1", "v:Parent2"] # so we can return an empty dataframe if no data q = WOQLQuery().select(*selects).woql_and( WOQLQuery().triple("v:Person", "Name", "v:Name"), WOQLQuery().triple("v:Person", "Sex", "v:Sex"), WOQLQuery().triple("v:Person", "Parent1", "v:Parent1"), WOQLQuery().triple("v:Person", "Parent2", "v:Parent2")) result = wary.execute_query(q, client) return pd.DataFrame( columns=selects) if is_empty(result) else wdf.query_to_df(result)
def query_status(time): ''' Query TerminusDB about the state of the system, at a particular date/time :param time: string, date/time :return: ''' selects = ["v:Ship", "v:Start", "v:End", "v:Route", "v:Berth"] # so we can return an empty dataframe if no data q = WOQLQuery().select(*selects).woql_and( # # Look for Events with a start and end times # WOQLQuery().triple("v:Event", "start", "v:Start"), WOQLQuery().triple("v:Event", "end", "v:End"), # # Make v:Time the current date/time in which we're interested # WOQLQuery().cast(literal_string(mdt.num2date(time).strftime('%Y-%m-%d %H:%M:%S')), "xsd:dateTime", "v:Time"), # # Want the start time before the current time, and end time after the current time # WOQLQuery().less("v:Start", "v:Time"), WOQLQuery().greater("v:End", "v:Time"), # # Now, pick up the data which we actually want from the query. # Note that the v:Event will either be a Voyage or a Berth document, so # use optional query triples to pick up alternative properties.. # WOQLQuery().triple("v:Event", "ship", "v:Ship"), WOQLQuery().opt().triple("v:Event", "route", "v:Route"), WOQLQuery().opt().triple("v:Event", "berth", "v:Berth") ) result = wary.execute_query(q, client) return pd.DataFrame(columns=selects) if is_empty(result) else wdf.query_to_df(result)