From 28cc69143587fbbcc3d2e2ddf9beac9c0b30904f Mon Sep 17 00:00:00 2001 From: Binh Vu Date: Mon, 6 Nov 2023 11:13:21 -0800 Subject: [PATCH] remove APP_CONFIG --- sand/extensions/export/drepr/main.py | 20 ++++++++++--------- sand/extensions/export/drepr/resources.py | 10 +++++----- sand/extensions/export/drepr/semanticmodel.py | 13 ++++++------ 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/sand/extensions/export/drepr/main.py b/sand/extensions/export/drepr/main.py index 6776ca1..abe2c6b 100644 --- a/sand/extensions/export/drepr/main.py +++ b/sand/extensions/export/drepr/main.py @@ -17,7 +17,7 @@ Resource, ResourceType, ) -from sand.config import APP_CONFIG +from sand.app import App from sand.extension_interface.export import IExport from sand.extensions.export.drepr.resources import ( get_entity_resource, @@ -34,18 +34,19 @@ from sand.models.ontology import OntPropertyAR, OntPropertyDataType from sand.models.table import Table, TableRow from slugify import slugify +from sm.misc.funcs import assert_not_null class DreprExport(IExport): - def __init__(self): - self.kgns = APP_CONFIG.get_kgns() + def __init__(self, app: App): + self.app = app def export_data_model(self, table: Table, sm: O.SemanticModel) -> DRepr: """Create a D-REPR model of the dataset.""" columns = [slugify(c).replace("-", "_") for c in table.columns] get_attr_id = lambda ci: f"{ci}__{columns[ci]}" get_ent_attr_id = lambda ci: f"{ci}__ent__{columns[ci]}" - ent_dnodes = get_entity_data_nodes(sm) + ent_dnodes = get_entity_data_nodes(self.app.cfg, sm) attrs = [ Attr( @@ -76,8 +77,7 @@ def export_data_model(self, table: Table, sm: O.SemanticModel) -> DRepr: for node in ent_dnodes ] - id2props = OntPropertyAR() - dsm = get_drepr_sm(sm, id2props, get_attr_id, get_ent_attr_id) + dsm = get_drepr_sm(sm, self.app.ontprop_ar, get_attr_id, get_ent_attr_id) datatype_transformations = [] for node in sm.nodes(): @@ -85,7 +85,7 @@ def export_data_model(self, table: Table, sm: O.SemanticModel) -> DRepr: continue datatypes: Set[OntPropertyDataType] = { - id2props[self.kgns.uri_to_id(inedge.abs_uri)].datatype + assert_not_null(self.app.ontprop_ar.get_by_uri(inedge.abs_uri)).datatype for inedge in sm.in_edges(node.id) } datatype = list(datatypes)[0] if len(datatypes) == 1 else None @@ -146,10 +146,12 @@ def export_data( # no column, no data return "" - ent_columns = {node.col_index for node in get_entity_data_nodes(sm)} + ent_columns = { + node.col_index for node in get_entity_data_nodes(self.app.cfg, sm) + } resources = { "table": get_table_resource(table, rows), - "entity": get_entity_resource(table, rows, ent_columns), + "entity": get_entity_resource(self.app.cfg, table, rows, ent_columns), } content = execute( diff --git a/sand/extensions/export/drepr/resources.py b/sand/extensions/export/drepr/resources.py index 292d38c..b93b523 100644 --- a/sand/extensions/export/drepr/resources.py +++ b/sand/extensions/export/drepr/resources.py @@ -4,7 +4,7 @@ from uuid import uuid4 from drepr.models import ResourceData, ResourceDataString -from sand.config import APP_CONFIG +from sand.config import AppConfig from sand.models.table import Table, TableRow @@ -23,12 +23,12 @@ def get_table_resource(table: Table, rows: List[TableRow]) -> ResourceData: def get_entity_resource( - table: Table, rows: List[TableRow], ent_columns: Set[int] + appcfg: AppConfig, table: Table, rows: List[TableRow], ent_columns: Set[int] ) -> ResourceData: """Return a CSV resource of matrix mapping each position in a table to a corresponding entity uri.""" - kgns = APP_CONFIG.get_kgns() - new_entity_template: str = APP_CONFIG.entity.new_entity_template + new_entity_template: str = appcfg.entity.new_entity_template ent_rows: List[List[str]] = [] + kgns = appcfg.get_kgns() for ri, row in enumerate(rows): ent_rows.append([]) @@ -44,7 +44,7 @@ def get_entity_resource( for link in links: if ( link.entity_id is not None - and link.entity_id != APP_CONFIG.entity.nil.id + and link.entity_id != appcfg.entity.nil.id ): ent = kgns.id_to_uri(link.entity_id) break diff --git a/sand/extensions/export/drepr/semanticmodel.py b/sand/extensions/export/drepr/semanticmodel.py index 7405474..4111807 100644 --- a/sand/extensions/export/drepr/semanticmodel.py +++ b/sand/extensions/export/drepr/semanticmodel.py @@ -2,7 +2,7 @@ import drepr.models.sm as drepr_sm import sm.outputs.semantic_model as O -from sand.config import APP_CONFIG +from sand.config import AppConfig from sand.models.ontology import OntProperty, OntPropertyDataType from sm.namespaces.wikidata import WikidataNamespace @@ -22,6 +22,7 @@ def get_drepr_sm( + appcfg: AppConfig, sm: O.SemanticModel, id2props: Mapping[str, OntProperty], get_attr_id: Callable[[int], str], @@ -37,7 +38,7 @@ def get_drepr_sm( """ nodes = {} edges = {} - kgns = APP_CONFIG.get_kgns() + kgns = appcfg.get_kgns() for node in sm.nodes(): if isinstance(node, O.ClassNode): @@ -90,7 +91,7 @@ def get_drepr_sm( print(edges) # add drepr:uri relationship - for node in get_entity_data_nodes(sm): + for node in get_entity_data_nodes(appcfg, sm): new_node_id = str(node.id) + ":ents" nodes[new_node_id] = drepr_sm.DataNode( node_id=new_node_id, @@ -100,7 +101,7 @@ def get_drepr_sm( inedges = [ inedge for inedge in sm.in_edges(node.id) - if inedge.abs_uri in APP_CONFIG.semantic_model.identifiers + if inedge.abs_uri in appcfg.semantic_model.identifiers ] assert len(inedges) == 1 inedge = inedges[0] @@ -119,8 +120,8 @@ def get_drepr_sm( ) -def get_entity_data_nodes(sm: O.SemanticModel) -> List[O.DataNode]: - ident_props = APP_CONFIG.semantic_model.identifiers +def get_entity_data_nodes(appcfg: AppConfig, sm: O.SemanticModel) -> List[O.DataNode]: + ident_props = appcfg.semantic_model.identifiers ent_dnodes = [] for node in sm.iter_nodes(): if not isinstance(node, O.DataNode):