Skip to content

Commit

Permalink
Fix cyclic dependency between Mapping and KnowledgeGraphForge (#362)
Browse files Browse the repository at this point in the history
* attempt fix cyclic dependency

* undo

* full imports within forge

* default mapper and mapping from store
  • Loading branch information
ssssarah authored Nov 29, 2023
1 parent 55ba0ce commit 2e616cd
Show file tree
Hide file tree
Showing 27 changed files with 54 additions and 45 deletions.
4 changes: 1 addition & 3 deletions kgforge/core/archetypes/dataset_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
from abc import abstractmethod, ABC

from typing import Optional, Union, List, Type, Dict
from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.archetypes.read_only_store import ReadOnlyStore
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.model import Model
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.archetypes.mapper import Mapper
from kgforge.core.commons.imports import import_class
from kgforge.core.conversions.json import as_json, from_json
from kgforge.core.commons.execution import not_supported
from kgforge.core.wrappings import Filter


Expand Down
2 changes: 1 addition & 1 deletion kgforge/core/archetypes/read_only_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.archetypes.model import Model
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.commons.attributes import repr_class
Expand Down
15 changes: 9 additions & 6 deletions kgforge/core/forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
# along with Blue Brain Nexus Forge. If not, see <https://choosealicense.com/licenses/lgpl-3.0/>.

from copy import deepcopy
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, Type

import numpy as np
import yaml
from pandas import DataFrame
from rdflib import Graph

from kgforge.core.resource import Resource
from kgforge.core.commons.files import load_file_as_byte
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.archetypes.model import Model
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.mapper import Mapper
from kgforge.core.archetypes.store import Store
from kgforge.core.commons.files import load_file_as_byte
from kgforge.core.commons.actions import LazyAction
from kgforge.core.commons.dictionaries import with_defaults
from kgforge.core.commons.exceptions import ResolvingError
Expand All @@ -44,8 +45,6 @@
)
from kgforge.core.reshaping import Reshaper
from kgforge.core.wrappings.paths import PathsWrapper, wrap_paths, Filter
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappings import DictionaryMapping


class KnowledgeGraphForge:
Expand Down Expand Up @@ -548,7 +547,7 @@ def mappings(

@catch
def mapping(
self, entity: str, source: str, type: Callable = DictionaryMapping
self, entity: str, source: str, type: Type[Mapping] = None
) -> Mapping:
"""
Return a Mapping object of type 'type' for a resource type 'entity' and a source.
Expand All @@ -558,14 +557,16 @@ def mapping(
:param type: a Mapping class
:return: Mapping
"""
if type is None:
type = self._store.mapping
return self._model.mapping(entity, source, type)

@catch
def map(
self,
data: Any,
mapping: Union[Mapping, List[Mapping]],
mapper: Callable = DictionaryMapper,
mapper: Type[Mapper] = None,
na: Union[Any, List[Any]] = None,
) -> Union[Resource, List[Resource]]:
"""
Expand All @@ -578,6 +579,8 @@ def map(
:param na: represents missing values
:return: Union[Resource, List[Resource]]
"""
if mapper is None:
mapper = self._store.mapper
return mapper(self).map(data, mapping, na)

# Reshaping User Interface.
Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/models/rdf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from rdflib import URIRef, Literal
from rdflib.namespace import XSD

from kgforge.core.archetypes import Mapping
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.resource import Resource
from kgforge.core.archetypes.store import Store
from kgforge.core.archetypes.model import Model
Expand Down
4 changes: 2 additions & 2 deletions kgforge/specializations/resolvers/agent_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from kgforge.core.commons.execution import not_supported
from kgforge.core.commons.sparql_query_builder import SPARQLQueryBuilder
from kgforge.core.commons.strategies import ResolvingStrategy
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappings import DictionaryMapping
from kgforge.specializations.mappers.dictionaries import DictionaryMapper
from kgforge.specializations.mappings.dictionaries import DictionaryMapping
from kgforge.specializations.resolvers.store_service import StoreService


Expand Down
4 changes: 2 additions & 2 deletions kgforge/specializations/resolvers/demo_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from kgforge.core.commons.exceptions import ConfigurationError
from kgforge.core.commons.execution import not_supported
from kgforge.core.commons.strategies import ResolvingStrategy
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappings import DictionaryMapping
from kgforge.specializations.mappers.dictionaries import DictionaryMapper
from kgforge.specializations.mappings.dictionaries import DictionaryMapping


class DemoResolver(Resolver):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from typing import Callable, Dict, Optional, Union, Any

from kgforge.core.commons.execution import not_supported
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappings import DictionaryMapping
from kgforge.specializations.mappers.dictionaries import DictionaryMapper
from kgforge.specializations.mappings.dictionaries import DictionaryMapping

from kgforge.specializations.resolvers.entity_linking import EntityLinker
from kgforge.specializations.resolvers.entity_linking.service.entity_linking_elastic_service import EntityLinkerElasticService

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from kgforge.core.conversions.json import as_json
from kgforge.core.resource import encode
from kgforge.core.wrappings import Filter, FilterOperator
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappings import DictionaryMapping
from kgforge.specializations.mappers.dictionaries import DictionaryMapper
from kgforge.specializations.mappings.dictionaries import DictionaryMapping
from kgforge.specializations.resolvers.entity_linking.service.entity_linking_service import (
EntityLinkerService,
)
Expand Down
4 changes: 2 additions & 2 deletions kgforge/specializations/resolvers/ontology_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from kgforge.core.commons.execution import not_supported
from kgforge.core.commons.sparql_query_builder import SPARQLQueryBuilder
from kgforge.core.commons.strategies import ResolvingStrategy
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappings import DictionaryMapping
from kgforge.specializations.mappers.dictionaries import DictionaryMapper
from kgforge.specializations.mappings.dictionaries import DictionaryMapping
from kgforge.specializations.resolvers.store_service import StoreService


Expand Down
4 changes: 2 additions & 2 deletions kgforge/specializations/stores/bluebrain_nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
from kgforge.core.conversions.rdf import as_jsonld
from kgforge.core.wrappings.dict import DictWrapper
from kgforge.core.wrappings.paths import Filter, create_filters_from_dict
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappings import DictionaryMapping
from kgforge.specializations.mappers.dictionaries import DictionaryMapper
from kgforge.specializations.mappings.dictionaries import DictionaryMapping
from kgforge.specializations.stores.nexus.service import BatchAction, Service, _error_message


Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/stores/demo_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pathlib import Path
from typing import Dict, List, Optional, Union, Type, Tuple, Any
from uuid import uuid4
from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.store import Store
from kgforge.core.archetypes.mapper import Mapper
Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/stores/sparql/sparql_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import copy
from typing import Dict, List, Optional, Union, Tuple

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.context import Context
from kgforge.core.wrappings.dict import wrap_dict

Expand Down
6 changes: 3 additions & 3 deletions kgforge/specializations/stores/sparql_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import requests
from typing import Dict, List, Optional, Union, Any, Type, Tuple

from kgforge.core import Resource
from kgforge.core.archetypes import Mapper
from kgforge.core.resource import Resource
from kgforge.core.archetypes.mapper import Mapper
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.model import Model
from kgforge.core.archetypes.dataset_store import DatasetStore
from kgforge.core.commons import Context
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappers.dictionaries import DictionaryMapper
from kgforge.specializations.stores.sparql.sparql_service import SPARQLService
from kgforge.core.wrappings.paths import create_filters_from_dict, Filter
from kgforge.core.wrappings.dict import DictWrapper
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import pytest
from pytest_bdd import given, parsers, then, when

from kgforge.core import Resource, KnowledgeGraphForge
from kgforge.core.resource import Resource
from kgforge.core.forge import KnowledgeGraphForge
from kgforge.core.commons.actions import Action
from kgforge.core.commons.context import Context
from kgforge.core.conversions.rdf import _merge_jsonld, Form
Expand Down
3 changes: 2 additions & 1 deletion tests/core/archetypes/test_read_only_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# Placeholder for the test suite for actions.
import pytest

from kgforge.core import Resource, KnowledgeGraphForge
from kgforge.core.resource import Resource
from kgforge.core.forge import KnowledgeGraphForge
from kgforge.core.commons.exceptions import DownloadingError


Expand Down
3 changes: 2 additions & 1 deletion tests/core/archetypes/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# Placeholder for the test suite for actions.
import pytest

from kgforge.core import Resource, KnowledgeGraphForge
from kgforge.core.resource import Resource
from kgforge.core.forge import KnowledgeGraphForge
from kgforge.specializations.resources import Dataset
from kgforge.core.wrappings.dict import wrap_dict
import json
Expand Down
2 changes: 1 addition & 1 deletion tests/core/commons/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# Placeholder for the test suite for actions.

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.actions import LazyAction, collect_lazy_actions, execute_lazy_actions


Expand Down
3 changes: 2 additions & 1 deletion tests/core/conversions/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from urllib.parse import urljoin
from urllib.request import pathname2url

from kgforge.core import KnowledgeGraphForge, Resource
from kgforge.core.resource import Resource
from kgforge.core.forge import KnowledgeGraphForge
from kgforge.core.commons.context import Context
from kgforge.core.conversions.rdf import _merge_jsonld
from kgforge.core.wrappings.dict import wrap_dict
Expand Down
2 changes: 1 addition & 1 deletion tests/core/conversions/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# Test suite for conversion of resource to / from Pandas DataFrame.

from kgforge.core import Resource
from kgforge.core.resource import Resource


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/core/conversions/test_rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from rdflib import Graph, BNode, term
from rdflib.namespace import RDF, Namespace

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.exceptions import NotSupportedError
from kgforge.core.conversions.rdf import _merge_jsonld, _resolve_iri, from_jsonld, as_jsonld, Form, as_graph, from_graph, LD_KEYS

Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import pytest

from kgforge.core import KnowledgeGraphForge
from kgforge.core.forge import KnowledgeGraphForge

SCOPE = "terms"
MODEL = "DemoModel"
Expand Down
3 changes: 2 additions & 1 deletion tests/core/test_reshaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

# Placeholder for the test suite for reshaping.
import pytest
from kgforge.core import Resource, KnowledgeGraphForge
from kgforge.core.resource import Resource
from kgforge.core.forge import KnowledgeGraphForge
from kgforge.core.reshaping import collect_values, Reshaper


Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytest
from pytest_bdd import given, scenarios, then, when

from kgforge.core import Resource
from kgforge.core.resource import Resource

# TODO To be port to the generic parameterizable test suite for resources in test_resources.py.
# DKE-135.
Expand Down
7 changes: 4 additions & 3 deletions tests/specializations/mappers/test_mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import pytest
from contextlib import nullcontext as does_not_raise

from kgforge.core import KnowledgeGraphForge, Resource
from kgforge.specializations.mappers import DictionaryMapper
from kgforge.specializations.mappings import DictionaryMapping
from kgforge.core.resource import Resource
from kgforge.core.forge import KnowledgeGraphForge
from kgforge.specializations.mappers.dictionaries import DictionaryMapper
from kgforge.specializations.mappings.dictionaries import DictionaryMapping


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/specializations/models/test_rdf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import json
import pytest

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.exceptions import ValidationError
from kgforge.specializations.models import RdfModel
from tests.specializations.models.data import *
Expand Down
3 changes: 2 additions & 1 deletion tests/specializations/resources/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# Placeholder for the generic parameterizable test suite for resources.
from typing import List

from kgforge.core import KnowledgeGraphForge, Resource
from kgforge.core.resource import Resource
from kgforge.core.forge import KnowledgeGraphForge
from kgforge.specializations.resources import Dataset


Expand Down
4 changes: 2 additions & 2 deletions tests/specializations/stores/test_bluebrain_nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import pytest
from typing import Callable, Union, List

from kgforge.core import Resource
from kgforge.core.archetypes import Store
from kgforge.core.resource import Resource
from kgforge.core.archetypes.store import Store
from kgforge.core.commons.context import Context
from kgforge.core.conversions.rdf import _merge_jsonld
from kgforge.core.wrappings.dict import wrap_dict
Expand Down

0 comments on commit 2e616cd

Please sign in to comment.