Skip to content

Commit

Permalink
remove APP_CONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
Binh Vu committed Nov 6, 2023
1 parent 0adb16d commit 28cc691
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
20 changes: 11 additions & 9 deletions sand/extensions/export/drepr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -76,16 +77,15 @@ 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():
if not isinstance(node, O.DataNode):
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
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions sand/extensions/export/drepr/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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([])
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions sand/extensions/export/drepr/semanticmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,6 +22,7 @@


def get_drepr_sm(
appcfg: AppConfig,
sm: O.SemanticModel,
id2props: Mapping[str, OntProperty],
get_attr_id: Callable[[int], str],
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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]
Expand All @@ -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):
Expand Down

0 comments on commit 28cc691

Please sign in to comment.