Example #1
0
def init(shouldHalt):
    root = tk.Tk() # Build the Tkinter application
    with TypeDB.core_client("localhost:1729") as client:
        with client.session("tube_network", SessionType.DATA) as session:
            tube_gui = TubeGui(session, root)
            if shouldHalt:
                root.mainloop()
Example #2
0
    def assert_migration_results(self):
        with TypeDB.core_client("localhost:1729") as client:
            with client.session("tube_network", SessionType.DATA) as session:
                with session.transaction(TransactionType.READ) as tx:
                    number_of_stations = tx.query().match_aggregate(
                        "match $x isa station; get $x; count;").get().as_int()
                    self.assertEqual(number_of_stations, 271)

                    number_of_tube_lines = tx.query().match_aggregate(
                        "match $x isa tube-line; get $x; count;").get().as_int(
                        )
                    self.assertEqual(number_of_tube_lines, 11)

                    number_of_route_sections = tx.query().match_aggregate(
                        "match $x isa route-section; get $x; count;").get(
                        ).as_int()
                    self.assertEqual(number_of_route_sections, 885)

                    number_of_routes = tx.query().match_aggregate(
                        "match $x isa route; get $x; count;").get().as_int()
                    self.assertEqual(number_of_routes, 39)

                    number_of_tunnels = tx.query().match_aggregate(
                        "match $x isa tunnel; get $x; count;").get().as_int()
                    self.assertEqual(number_of_tunnels, 907)

                    number_of_zones = tx.query().match_aggregate(
                        "match $x isa zone; get $x; count;").get().as_int()
                    self.assertEqual(number_of_zones, 9)
def migrate_journals(uri,
                     database,
                     journal_names: list,
                     batch_size,
                     process_id=0):
    '''
    Migrate journals to TypeDB \n
    journal_names - list of journal names (strings) \n
    process_id - process id while running on multiple cores, by process_id = 0
    '''
    counter = 0
    with TypeDB.core_client(uri) as client:
        with client.session(database, SessionType.DATA) as session:
            transaction = session.transaction(TransactionType.WRITE)
            for journal_name in journal_names:
                ##Check if journal already in Knowledge Base
                try:
                    match_query = 'match $j isa journal, has journal-name "{}";'.format(
                        journal_name)
                    next(transaction.query().match(match_query))
                except StopIteration:
                    insert_query = 'insert $j isa journal, has journal-name "{}";'.format(
                        journal_name)
                    transaction.query().insert(insert_query)
                if counter % batch_size == 0:
                    transaction.commit()
                    transaction.close()
                    transaction = session.transaction(TransactionType.WRITE)
                    print("Process {} COMMITED ----- {} journals added".format(
                        process_id, counter))
                counter = counter + 1
            transaction.commit()
            transaction.close()
Example #4
0
    def test_visualisation_queries(self):
        app.init(False)

        with TypeDB.core_client("localhost:1729") as client:
            with client.session("tube_network", SessionType.DATA) as session:
                for query in app.TubeGui.ANALYTICAL_QUERIES:
                    with session.transaction(
                            TransactionType.READ) as read_transaction:
                        read_transaction.query(query)
Example #5
0
def init(from_station_name, to_station_name, selected_path_strategy):
    with TypeDB.core_client("localhost:1729") as client:
        with client.session("tube_network", SessionType.DATA) as session:
            # Get the departing station
            valid_from_name = False
            while not valid_from_name:
                if from_station_name is None:
                    from_station_name = input(
                        "Enter the name of the station you're travelling from: "
                    ).title()
                print("Verifying station name '" + from_station_name +
                      "' ...\n")
                from_station_id = get_station_by_name(session,
                                                      from_station_name)
                print(from_station_id)

                if from_station_id:
                    valid_from_name = True
                else:
                    print("No station with that name exists! Try again.\n")

            # Get the destination station
            valid_to_name = False
            while not valid_to_name:
                if to_station_name is None:
                    to_station_name = input("Where to: ")
                print("Verifying station name '" + to_station_name + "' ...\n")
                to_station_id = get_station_by_name(session, to_station_name)

                if to_station_id:
                    valid_to_name = True
                else:
                    print("No station with that name exists! Try again.\n")

            # Get the shortest path strategy
            print("Shortest path strategies: ")
            print("1. Via fewest stops")
            print("2. Via fewest route changes")
            path_strategies = ["stops", "routes"]

            if selected_path_strategy is None:
                selected_path_strategy = -1

            while selected_path_strategy < 0 or selected_path_strategy > len(
                    path_strategies) - 1:
                selected_path_strategy = int(
                    input("Select a shortest path strategy: ")) - 1
                print("")

            # Retrieve the shortest path
            print("Finding the shortest path between " + from_station_name +
                  " to " + to_station_name + " via the fewest " +
                  path_strategies[selected_path_strategy] + " ...")

            find_path(session, from_station_id, to_station_id,
                      path_strategies[selected_path_strategy])
Example #6
0
 def setUp(self):
     with TypeDB.core_client("localhost:1729") as client:
         client.databases().create("tube_network")
         with client.session("tube_network", SessionType.SCHEMA) as session:
             with open('schemas/tube-network-schema.gql', 'r') as schema:
                 define_query = schema.read()
                 with session.transaction(
                         TransactionType.WRITE) as transaction:
                     transaction.query().define(define_query)
                     transaction.commit()
                     print("Loaded the tube_network schema")
Example #7
0
def init(qs_number):
    # create a transaction to talk to the keyspace
    with TypeDB.core_client("localhost:1729") as client:
        with client.session("tube_network", SessionType.DATA) as session:
            with session.transaction(TransactionType.READ) as transaction:
                # execute the query for the selected question
                if qs_number == 0:
                    execute_query_all(transaction)
                else:
                    question = query_examples[qs_number - 1]["question"]
                    query_function = query_examples[qs_number -
                                                    1]["query_function"]
                    query_function(question, transaction)
Example #8
0
def process_selection(qs_number, database_name):
    ## create a transaction to talk to the phone_calls database
    with TypeDB.core_client("localhost:1729") as client:
        with client.session(database_name, SessionType.DATA) as session:
            with session.transaction(TransactionType.READ) as transaction:
                ## execute the query for the selected question
                if qs_number == 0:
                    execute_query_all(transaction)
                else:
                    question = query_examples[qs_number - 1]["question"]
                    query_function = query_examples[qs_number -
                                                    1]["query_function"]
                    query_function(question, transaction)
Example #9
0
 def setUp(self):
     self._client = TypeDB.core_client("localhost:1729")
     self._client.databases().create(database_name)
     self._session = self._client.session(database_name, SessionType.SCHEMA)
     with open('telecom/phone_calls/schema.tql', 'r') as schema:
         define_query = schema.read()
         with self._session.transaction(
                 TransactionType.WRITE) as transaction:
             transaction.query().define(define_query)
             transaction.commit()
             print("Loaded the " + database_name + " schema")
     self._session.close()
     self._session = self._client.session(database_name, SessionType.DATA)
Example #10
0
def insert(queries):
    with TypeDB.core_client("localhost:1729") as client:
        with client.session("tube_network", SessionType.DATA) as session:
            transaction = session.transaction(TransactionType.WRITE)
            for i, query in enumerate(queries):
                print(query)
                print("- - - - - - - - - - - - - -")
                transaction.query().insert(query)

                if i % 500 == 0:
                    transaction.commit()
                    transaction = session.transaction(TransactionType.WRITE)
            transaction.commit()
Example #11
0
    def typedb_session(self):
        """
        Did this like this in an attempt to make it
        also work when using with a DataLoader with
        num_workers > 0.

        TODO: it does not, so look into this.
        """
        if not self._typedb_session:
            print("setting up session")
            print(self)
            client = TypeDB.core_client(self._uri)
            self._typedb_session = client.session(database=self._database, session_type=SessionType.DATA)
        return self._typedb_session
Example #12
0
    def test_statistics(self):
        statistics.init(0)

        query_examples = statistics.query_examples

        with TypeDB.core_client("localhost:1729") as client:
            with client.session("tube_network", SessionType.DATA) as session:
                with session.transaction(TransactionType.READ) as transaction:
                    self.assertEqual(
                        query_examples[0].get("query_function")("",
                                                                transaction),
                        271)

                    # TODO(vmax): enable when compute queries are supported again
                    """
Example #13
0
def migrate_publications(uri,
                         database,
                         publications_list: list,
                         batch_size,
                         process_id=0):
    '''
    Migrate publiations to TypeDB\n
    publications_list - list of dictionaries with publication data\n
    process_id - process id while running on multiple cores, by process_id = 0
    '''
    counter = 0
    with TypeDB.core_client(uri) as client:
        with client.session(database, SessionType.DATA) as session:
            transaction = session.transaction(TransactionType.WRITE)
            for publication_dict in publications_list:
                authors = publication_dict[
                    "authors"]  # list of authors - list of strings
                ##Check if publication already in Knowledge Base
                try:
                    match_query = 'match $p isa publication, has paper-id "{}";'.format(
                        publication_dict["paper-id"])
                    next(transaction.query().match(match_query))
                except StopIteration:
                    match_query = 'match $j isa journal, has journal-name "{}"; '.format(
                        publication_dict["journal-name"])
                    match_query = match_query + create_authorship_query(
                        authors)[0]
                    insert_query = 'insert $p isa publication, has paper-id "{}", has title "{}", has doi "{}", has publish-time "{}", has volume "{}", has issn "{}", has pmid "{}"; '.format(
                        publication_dict["paper-id"],
                        publication_dict["title"], publication_dict["doi"],
                        publication_dict["publish-time"],
                        publication_dict["volume"], publication_dict["issn"],
                        publication_dict["pmid"])
                    insert_query = insert_query + create_authorship_query(
                        authors)[1]
                    insert_query = insert_query + '(publishing-journal: $j, published-publication: $p) isa publishing;'
                    # print(match_query+insert_query)
                    transaction.query().insert(match_query + insert_query)
                if counter % batch_size == 0:
                    transaction.commit()
                    transaction.close()
                    transaction = session.transaction(TransactionType.WRITE)
                    print("Process {} COMMITED ----- {} publications added".
                          format(process_id, counter))
                counter = counter + 1
            transaction.commit()
            transaction.close()
Example #14
0
def build_phone_call_graph(inputs, data_path, database_name):
    """
      gets the job done:
      1. creates a TypeDB instance
      2. creates a session to the targeted database
      3. for each input:
        - a. constructs the full path to the data file
        - b. loads csv to TypeDB
      :param input as list of dictionaties: each dictionary contains details required to parse the data
    """
    with TypeDB.core_client("localhost:1729") as client:  # 1
        with client.session(database_name, SessionType.DATA) as session:  # 2
            for input in inputs:
                input["file"] = input["file"].replace(
                    data_path, "")  # for testing purposes
                input["file"] = data_path + input["file"]  # 3a
                print("Loading from [" + input["file"] +
                      ".csv] into TypeDB ...")
                load_data_into_typedb(input, session)  # 3b
Example #15
0
    def test_journey_planner(self):
        journey_planner.init(from_station_name="Green Park",
                             to_station_name="Holloway Road",
                             selected_path_strategy=1)

        with TypeDB.core_client("localhost:1729") as client:
            with client.session("tube_network", SessionType.DATA) as session:
                # not containing 'Underground Station'
                # not Title Case
                # fewest stops
                self.assertEqual(
                    journey_planner.find_path(
                        session,
                        journey_planner.get_station_by_name(
                            session, "Green Park"
                        ),  # not containing 'Underground Station'
                        journey_planner.get_station_by_name(
                            session, "holloway road"),  # not Title Case
                        "stops"),
                    [
                        u'Green Park Underground Station',
                        u'Oxford Circus Underground Station',
                        u'Warren Street Underground Station',
                        u'Euston Underground Station',
                        u"King's Cross St. Pancras Underground Station",
                        u'Caledonian Road Underground Station',
                        u'Holloway Road Underground Station'
                    ])

                # fewest route changes
                self.assertEqual(
                    journey_planner.find_path(
                        session,
                        journey_planner.get_station_by_name(
                            session, "Green Park"),
                        journey_planner.get_station_by_name(
                            session, "holloway road"), "routes"),
                    # TODO: make sure this is actually the right answer
                    [
                        u'Green Park Underground Station',
                        u'Holloway Road Underground Station'
                    ])
Example #16
0
def migrate_relationships(uri, database, data: list, batch_size, process_id=0):
    '''
    Migrate relations to TypeDB\n
    data - table in a form of list of lists \n
    process_id - process id while running on multiple cores, by process_id = 0
    '''
    counter = 0
    with TypeDB.core_client(uri) as client:
        with client.session(database, SessionType.DATA) as session:
            transaction = session.transaction(TransactionType.WRITE)
            for data_entity in data:
                predicate_name = data_entity[1]
                subject_name = data_entity[2]
                object_name = data_entity[3]
                relation = relationship_mapper(
                    predicate_name
                )  # add handler for situation when there is no relation implemented in a mapper
                pmid = data_entity[0]
                sentence_text = data_entity[4].replace('"', "'")

                match_query = 'match $p isa publication, has paper-id "{}"; $g1 isa gene, has gene-symbol "{}"; $g2 isa gene, has gene-symbol "{}"; '.format(
                    data_entity[0], data_entity[2], data_entity[3])
                insert_query = 'insert $r ({}: $g1, {}: $g2) isa {}, has sentence-text "{}"; $m (mentioned-genes-relation: $r, mentioning: $p) isa mention, has source "SemMed";'.format(
                    relation["active-role"], relation["passive-role"],
                    relation["relation-name"], sentence_text)
                transaction.query().insert(match_query + insert_query)
                if counter % batch_size == 0:
                    transaction.commit()
                    transaction.close()
                    transaction = session.transaction(TransactionType.WRITE)
                    print(
                        "Process {} COMMITED ----- {} relations added".format(
                            process_id, counter))
                counter = counter + 1
            transaction.commit()
            transaction.close()
def tissueNetMigrator(uri, database, num, num_threads, batch_size):
    client = TypeDB.core_client(uri)
    session = client.session(database, SessionType.DATA)
    batches_pr = []

    if num != 0:
        print('  ')
        print('Opening TissueNet dataset...')
        print('  ')

        with open('Dataset/TissueNet/HPA-Protein.tsv', 'rt',
                  encoding='utf-8') as csvfile:
            csvreader = csv.reader(csvfile, delimiter='\t')
            raw_file = []
            n = 0
            for row in csvreader:
                n = n + 1
                if n != 1:
                    raw_file.append(row)

    # // TODO this seems unfinished?

    session.close()
    client.close()
Example #18
0
 def tearDownClass(cls):
     with TypeDB.core_client("localhost:1729") as client:
         client.databases().get("tube_network").delete()
         print("Deleted the tube_network keyspace")