Skip to content

Commit

Permalink
yarrrml generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ensaremirerol committed Jan 3, 2025
1 parent 9a9be0f commit 0718c11
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 1 deletion.
3 changes: 3 additions & 0 deletions bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ async def bootstrap():

di["TEMP_DIR"] = di["APP_DIR"] / "temp"

if not di["TEMP_DIR"].exists():
di["TEMP_DIR"].mkdir()

# Detecting system and architecture for later use
di["SYSTEM"] = platform.system()
di["ARCH"] = platform.machine()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependencies = [
"python-multipart==0.0.12",
"pytz==2024.2",
"pywebview==5.3.1",
"PyYAML==6.0.2",
"pyyaml==6.0.2",
"rdflib==7.1.0",
"rich==13.9.2",
"setuptools==70.3.0",
Expand Down
8 changes: 8 additions & 0 deletions server/const/err_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ class ErrCodes(Enum):
# Mapping Service
MAPPING_NOT_FOUND = 120
MAPPING_ILLEGAL_UPDATE_OPERATION = 121

# YARRRML Service

MAPPING_EDGE_ID_NOT_FOUND = 140
MAPPING_NODE_ID_NOT_FOUND = 141
ENTITY_URI_PATTERN_NOT_FOUND = 142
LITERAL_VALUE_NOT_FOUND = 143
URIREF_URI_PATTERN_NOT_FOUND = 144
109 changes: 109 additions & 0 deletions server/facades/workspace/mapping/mapping_to_yarrrml_facade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from kink import inject

from server.exceptions import ErrCodes
from server.facades import (
BaseFacade,
FacadeResponse,
ServerException,
)
from server.service_protocols.mapping_service_protocol import (
MappingServiceProtocol,
)
from server.service_protocols.mapping_to_yarrrml_service_protocol import (
FSServiceProtocol,
MappingToYARRRMLServiceProtocol,
)
from server.services.core.workspace_metadata_service import (
WorkspaceMetadataServiceProtocol,
)
from server.services.local.local_source_service import (
SourceServiceProtocol,
)
from server.services.local.local_workspace_service import (
WorkspaceServiceProtocol,
)


@inject
class MappingToYARRRMLFacade(BaseFacade):
def __init__(
self,
workspace_metadata_service: WorkspaceMetadataServiceProtocol,
workspace_service: WorkspaceServiceProtocol,
mapping_service: MappingServiceProtocol,
source_service: SourceServiceProtocol,
yarrrml_service: MappingToYARRRMLServiceProtocol,
fs_service: FSServiceProtocol,
):
super().__init__()
self.workspace_metadata_service: WorkspaceMetadataServiceProtocol = workspace_metadata_service
self.workspace_service: WorkspaceServiceProtocol = (
workspace_service
)
self.mapping_service: MappingServiceProtocol = (
mapping_service
)
self.source_service: SourceServiceProtocol = (
source_service
)
self.yarrrml_service: MappingToYARRRMLServiceProtocol = yarrrml_service
self.fs_service: FSServiceProtocol = fs_service

@BaseFacade.error_wrapper
def execute(
self,
workspace_id: str,
mapping_id: str | None = None,
) -> FacadeResponse:
self.logger.info(
f"Creating YARRRML mapping for mapping {mapping_id} in workspace {workspace_id}"
)

self.logger.info("Retrieving workspace metadata")

workspace_metadata = self.workspace_metadata_service.get_workspace_metadata(
workspace_id,
)

self.logger.info("Retrieving workspace")

workspace = self.workspace_service.get_workspace(
workspace_metadata.location,
)

self.logger.info("Retrieving mapping")

if mapping_id not in workspace.mappings:
self.logger.error(
f"Mapping {mapping_id} not found in workspace {workspace_id}"
)
raise ServerException(
f"Mapping {mapping_id} not found in workspace {workspace_id}",
ErrCodes.MAPPING_NOT_FOUND,
)

mapping = self.mapping_service.get_mapping(
mapping_id
)

self.logger.info("Retrieving source")

source = self.source_service.get_source(
mapping.source_id,
)

self.logger.info("Converting mapping to YARRRML")

yarrrml = (
self.yarrrml_service.convert_mapping_to_yarrrml(
workspace.prefixes,
source,
mapping,
self.fs_service,
)
)

return self._success_response(
data=yarrrml,
message="YARRRML mapping created",
)
35 changes: 35 additions & 0 deletions server/routers/workspaces/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi.params import Depends
from fastapi.routing import APIRouter
from kink.container import di
from starlette.routing import PlainTextResponse

from server.facades import FacadeResponse
from server.facades.workspace.create_workspace_facade import (
Expand All @@ -24,6 +25,9 @@
from server.facades.workspace.mapping.get_mappings_in_workspace_facade import (
GetMappingsInWorkspaceFacade,
)
from server.facades.workspace.mapping.mapping_to_yarrrml_facade import (
MappingToYARRRMLFacade,
)
from server.facades.workspace.mapping.update_mapping_facade import (
UpdateMappingFacade,
)
Expand Down Expand Up @@ -124,6 +128,11 @@
Depends(lambda: di[UpdateMappingFacade]),
]

MappingToYARRRMLDep = Annotated[
MappingToYARRRMLFacade,
Depends(lambda: di[MappingToYARRRMLFacade]),
]


@router.get("/")
async def get_workspaces(
Expand Down Expand Up @@ -478,3 +487,29 @@ async def update_mapping(
status_code=facade_response.status,
detail=facade_response.to_dict(),
)


@router.get(
"/{workspace_id}/mapping/{mapping_id}/yarrrml",
response_class=PlainTextResponse,
)
async def mapping_to_yarrrml(
workspace_id: str,
mapping_id: str,
mapping_to_yarrrml_facade: MappingToYARRRMLDep,
) -> str:
facade_response = mapping_to_yarrrml_facade.execute(
workspace_id=workspace_id,
mapping_id=mapping_id,
)

if (
facade_response.status // 100 == 2
and facade_response.data
):
return facade_response.data

raise HTTPException(
status_code=facade_response.status,
detail=facade_response.to_dict(),
)
14 changes: 14 additions & 0 deletions server/service_protocols/fs_service_protocol/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
from pathlib import Path

from server.models.file_metadata import (
FileMetadata,
Expand Down Expand Up @@ -50,3 +51,16 @@ def download_file_with_uuid(self, uuid: str) -> bytes:
bytes: content of the file
"""
...

@abstractmethod
def provide_file_path_of_uuid(self, uuid: str) -> Path:
"""
Provide the path of a file with UUID. If file does not exist locally, implementation should first download it.
Args:
uuid (str): UUID of the file
Returns:
pathlib.Path: path of the file
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from abc import ABC, abstractmethod

from server.models.mapping import MappingGraph
from server.models.source import Source
from server.service_protocols.fs_service_protocol import (
FSServiceProtocol,
)


class MappingToYARRRMLServiceProtocol(ABC):
@abstractmethod
def convert_mapping_to_yarrrml(
self,
prefixes: dict[str, str],
source: Source,
mapping: MappingGraph,
fs_service: FSServiceProtocol,
) -> str:
"""
Convert a mapping to YARRRML
Args:
prefixes (dict): A dictionary of prefixes
source (Source): Source data of the mapping
mapping (MappingGraph): Mapping data
Returns:
str: Valid Yaml string representing the YARRRML mapping
"""
pass
4 changes: 4 additions & 0 deletions server/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from server.services.core.config_service import (
ConfigService,
)
from server.services.core.mapping_to_yarrrml_service import (
MappingToYARRRMLService,
)
from server.services.core.sqlite_db_service import DBService
from server.services.core.workspace_metadata_service import (
WorkspaceMetadataService,
Expand Down Expand Up @@ -30,4 +33,5 @@
"LocalOntologyService",
"LocalSourceService",
"LocalMappingService",
"MappingToYARRRMLService",
]
Loading

0 comments on commit 0718c11

Please sign in to comment.