def getTerms(input_term): with stardog.Admin(**conn_details) as admin: with stardog.Connection('capability', **conn_details) as conn: # get all equivalent calsses query # No. 1 : get all parents query_for_parents = """ SELECT ?entity where {{ ?src {0}* <http://stardog.com/{1}> . FILTER(?src != <http://stardog.com/ManufacturingCapabilities>) BIND(?src AS ?entity) . }} """.format(PARENT_OF, input_term) result_for_parents = conn.select(query_for_parents, reasoning=True) # No. 2 : get all requires query_for_requires = """ SELECT ?entity where {{ <http://stardog.com/{1}> {0}* ?tgt . FILTER(?tgt != <http://stardog.com/ManufacturingCapabilities>) BIND(?tgt AS ?entity) . }} """.format(REQUIRES, input_term) result_for_requires = conn.select(query_for_requires, reasoning=True) stardog_responses = [result_for_parents, result_for_requires] result_list = set() for response in stardog_responses: for result in response['results']['bindings']: temp_out = result[next(iter(result))]['value'] temp_out = temp_out.rsplit("/", 1)[-1] result_list.add(temp_out) return result_list
def stardog_drop_db_if_set(self, force=False): assert self.conn_details if self.conn_details['dropdb'] or force: dbname = self.conn_details['dbname'] with stardog.Admin(**self.open_conn_details) as admin: db = admin.database(dbname) db.drop()
def stardog_create_db_if_set(self): # conn_details = { # 'endpoint': self.server_url_var.get(), # 'username': '******', # 'password': '******', # 'schemafile': self.schema_file_var.get(), # 'dbname': self.server_dbname_var.get(), # 'createdb': self.server_db_create_var.get(), # 'dropdb': self.server_db_drop_var.get(), # } assert self.conn_details if self.conn_details['createdb']: dbname = self.conn_details['dbname'] schema_file = stardog.content.File(self.conn_details['schemafile']) print('Probably schema_file OK: ', self.conn_details['schemafile']) with stardog.Admin(**self.open_conn_details) as admin: # check if database already exist and drop it try: self.stardog_drop_db_if_set(force=True) except: pass db = admin.new_database(dbname) db = None # forget pointer # init schema with stardog.Connection(dbname, **self.open_conn_details) as conn: conn.begin() conn.add(schema_file) conn.commit()
def main(): connection_details = { 'endpoint': 'http://localhost:5820', 'auth': HTTPKerberosAuth() } with stardog.Admin(**connection_details) as admin: users = admin.users() for u in users: print(u.name)
def get_databases() -> list: """ Return a list of all the databases/stores of narratives :return: List of database/store names """ logging.info('Getting a list of all databases') try: admin = stardog.Admin(**sd_conn_details) databases = admin.databases() db_names = list() except Exception as e: capture_error(f'Exception getting list of stores: {str(e)}', True) return [] for database in databases: db_names.append(database.name) return db_names
def create_delete_database(op_type: str, database: str) -> str: """ Create or delete a database. If created, add the DNA ontologies. :param op_type: A string = 'create' or 'delete' :param database: The database name :return: Empty string if successful or the details of an exception """ logging.info(f'Database {database} being {op_type}d') if op_type != 'create' and op_type != 'delete': capture_error(f'Invalid op_type {op_type} for create_delete_db', True) return '' try: admin = stardog.Admin(**sd_conn_details) if op_type == 'create': # Create database admin.new_database( database, { 'search.enabled': True, 'edge.properties': True, 'reasoning': True, 'reasoning.punning.enabled': True, 'query.timeout': '20m' }) # Load ontologies to the newly created database conn = stardog.Connection(database, **sd_conn_details) conn.begin() logging.info(f'Loading DNA ontologies to {database}') _load_directory_to_database(ontol_path, conn) _load_directory_to_database(f'{ontol_path}domain-context/', conn) conn.commit() else: # Delete database database_obj = admin.database(database) database_obj.drop() return '' except Exception as e: return f'Database ({op_type}) exception: {str(e)}'
def create_dataset_database(self, database_name, dataset_path): connection_details = { 'endpoint': self.endpoint, 'username': self.username, 'password': self.password } with stardog.Admin(**connection_details) as admin: if database_name in [db.name for db in admin.databases()]: admin.database(database_name).drop() db = admin.new_database(database_name) with stardog.Connection(database_name, **connection_details) as conn: conn.begin() conn.add(stardog.content.File(str(dataset_path))) conn.commit() if conn.size(exact=True) <= 0: admin.database(database_name).drop() raise StardogException('No triples loaded!') return database_name
conn.commit() if __name__ == '__main__': consumer = _initialise() # init time sleep needed time.sleep(30) manager = Manager() db_pool = manager.list([True for x in range(0, DATABASES)]) # create database pool for i in range(0, DATABASES): try: with stardog.Admin(**conn_details) as admin: admin.database('db'+str(i)).drop() except: print("no database to drop") print('db'+str(i)) with stardog.Admin(**conn_details) as admin: admin.new_database('db'+str(i), {'index.type': 'Memory'}) with stardog.Connection('db'+str(i), **conn_details) as conn: conn.begin() conn.add(stardog.content.File('rule.ttl')) conn.commit() message_queue = {} proccesses = [] counter = 0 start = time.time()