Skip to content

Commit

Permalink
Merge pull request #351 from meaningfy-ws/feature/MWB-857
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleanych authored Nov 22, 2024
2 parents 9a4c233 + fbd66d7 commit a50d3ce
Show file tree
Hide file tree
Showing 46 changed files with 416 additions and 671 deletions.
10 changes: 5 additions & 5 deletions mapping_workbench/backend/file_resource/models/file_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from pydantic_core.core_schema import ValidationInfo

from mapping_workbench.backend.core.models.base_mapping_package_resource_entity import \
BaseMappingPackageResourceSchemaTrait, BaseMappingPackageResourceEntityInSchema
from mapping_workbench.backend.core.models.base_project_resource_entity import BaseProjectResourceEntity, \
BaseProjectResourceEntityInSchema, BaseProjectAbleResourceEntity
BaseMappingPackageResourceEntityInSchema, BaseMappingPackagesResourceSchemaTrait
from mapping_workbench.backend.core.models.base_project_resource_entity import BaseProjectResourceEntityInSchema, \
BaseProjectAbleResourceEntity
from mapping_workbench.backend.state_manager.models.state_object import ObjectState


Expand Down Expand Up @@ -52,7 +52,7 @@ def set_content(cls, v: str, values: ValidationInfo) -> str:

class FileResource(
BaseProjectAbleResourceEntity,
BaseMappingPackageResourceSchemaTrait
BaseMappingPackagesResourceSchemaTrait
):
title: Optional[str] = None
description: Optional[str] = None
Expand All @@ -64,7 +64,7 @@ class FileResource(

class FileResourceCollection(
BaseProjectAbleResourceEntity,
BaseMappingPackageResourceSchemaTrait
BaseMappingPackagesResourceSchemaTrait
):
title: Optional[str] = None
description: Optional[str] = None
Expand Down
35 changes: 23 additions & 12 deletions mapping_workbench/backend/mapping_package/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MappingPackageIn(BaseProjectResourceEntityInSchema):
start_date: Optional[datetime] = None
end_date: Optional[datetime] = None
eforms_sdk_versions: List[str] = None
test_data_suites: Optional[List[Optional[Link[TestDataSuite]]]] = None
shacl_test_suites: Optional[List[Optional[Link[SHACLTestSuite]]]] = None
sparql_test_suites: Optional[List[Optional[Link[SPARQLTestSuite]]]] = None
resource_collections: Optional[List[Optional[Link[ResourceCollection]]]] = None
Expand Down Expand Up @@ -71,6 +72,7 @@ class MappingPackageOut(BaseProjectResourceEntityOutSchema):
# start_date: Optional[datetime] = None
# end_date: Optional[datetime] = None
eforms_sdk_versions: List[str] = None
test_data_suites: Optional[List[Link[TestDataSuite]]] = None
shacl_test_suites: Optional[List[Link[SHACLTestSuite]]] = None
sparql_test_suites: Optional[List[Link[SPARQLTestSuite]]] = None
resource_collections: Optional[List[Link[ResourceCollection]]] = None
Expand Down Expand Up @@ -169,14 +171,15 @@ class MappingPackage(BaseProjectResourceEntity, StatefulObjectABC):
end_date: Optional[str] = None
package_type: Optional[PackageType] = DEFAULT_PACKAGE_TYPE
eforms_sdk_versions: Optional[List[str]] = []
test_data_suites: Optional[List[Link[TestDataSuite]]] = None
shacl_test_suites: Optional[List[Link[SHACLTestSuite]]] = None
sparql_test_suites: Optional[List[Link[SPARQLTestSuite]]] = None
resource_collections: Optional[List[Link[ResourceCollection]]] = None

async def get_conceptual_mapping_rules_states(self) -> List[ConceptualMappingRuleState]:
conceptual_mapping_rules = await ConceptualMappingRule.find(
Eq(ConceptualMappingRule.refers_to_mapping_package_ids, self.id),
Eq(ConceptualMappingRule.project, self.project.to_ref())
Eq(ConceptualMappingRule.project, self.project)
).to_list()
conceptual_mapping_rule_states = [
await conceptual_mapping_rule.get_state() for conceptual_mapping_rule in conceptual_mapping_rules
Expand All @@ -185,7 +188,7 @@ async def get_conceptual_mapping_rules_states(self) -> List[ConceptualMappingRul

async def get_mapping_groups_states(self) -> List[MappingGroupState]:
mapping_groups = await MappingGroup.find(
Eq(MappingGroup.project, self.project.to_ref())
Eq(MappingGroup.project, self.project)
).to_list()
mapping_groups_states = [
await mapping_group.get_state() for mapping_group in mapping_groups
Expand All @@ -194,11 +197,18 @@ async def get_mapping_groups_states(self) -> List[MappingGroupState]:
return mapping_groups_states

async def get_test_data_suites_states(self) -> List[TestDataSuiteState]:
test_data_suites_states = []
test_data_suites_ids = []
if self.test_data_suites:
test_data_suites_ids = [test_data_suite.to_ref().id for test_data_suite in self.test_data_suites]
test_data_suites = await TestDataSuite.find(
TestDataSuite.mapping_package_id == self.id,
Eq(TestDataSuite.project, self.project.to_ref())
In(TestDataSuite.id, test_data_suites_ids),
Eq(TestDataSuite.project, self.project)
).to_list()
test_data_suites_states = [await test_data_suite.get_state() for test_data_suite in test_data_suites]
if test_data_suites:
for test_data_suite in test_data_suites:
test_data_suite_state = await test_data_suite.get_state()
test_data_suites_states.append(test_data_suite_state)
return test_data_suites_states

async def get_shacl_test_suites_states(self) -> List[SHACLTestSuiteState]:
Expand All @@ -208,7 +218,7 @@ async def get_shacl_test_suites_states(self) -> List[SHACLTestSuiteState]:
shacl_test_suites_ids = [shacl_test_suite.to_ref().id for shacl_test_suite in self.shacl_test_suites]
shacl_test_suites = await SHACLTestSuite.find(
In(SHACLTestSuite.id, shacl_test_suites_ids),
Eq(SHACLTestSuite.project, self.project.to_ref())
Eq(SHACLTestSuite.project, self.project)
).to_list()
if shacl_test_suites:
for shacl_test_suite in shacl_test_suites:
Expand All @@ -227,7 +237,7 @@ async def get_sparql_test_suites_states(
sparql_test_suites = await SPARQLTestSuite.find(
In(SPARQLTestSuite.id, sparql_test_suites_ids),
NE(SPARQLTestSuite.type, SPARQLQueryValidationType.CM_ASSERTION),
Eq(SPARQLTestSuite.project, self.project.to_ref())
Eq(SPARQLTestSuite.project, self.project)
).to_list()
if sparql_test_suites:
for sparql_test_suite in sparql_test_suites:
Expand All @@ -253,10 +263,11 @@ async def get_sparql_test_suites_states(

async def get_triple_map_fragments_states(self) -> List[TripleMapFragmentState]:
triple_map_fragments: List[TripleMapFragment] = (await GenericTripleMapFragment.find(
Eq(GenericTripleMapFragment.project, self.project.to_ref())
Eq(GenericTripleMapFragment.refers_to_mapping_package_ids, self.id),
Eq(GenericTripleMapFragment.project, self.project)
).to_list()) + (await SpecificTripleMapFragment.find(
SpecificTripleMapFragment.mapping_package_id == self.id,
Eq(TestDataSuite.project, self.project.to_ref())
Eq(TestDataSuite.project, self.project)
).to_list())

return [await triple_map_fragment.get_state() for triple_map_fragment in triple_map_fragments]
Expand All @@ -270,7 +281,7 @@ async def get_resources_states(self) -> List[ResourceFileState]:
]
resource_collections = await ResourceCollection.find(
In(ResourceCollection.id, resource_collections_ids),
Eq(ResourceCollection.project, self.project.to_ref())
Eq(ResourceCollection.project, self.project)
).to_list()
if resource_collections:
for resource_collection in resource_collections:
Expand All @@ -280,14 +291,14 @@ async def get_resources_states(self) -> List[ResourceFileState]:

async def get_terms_states(self) -> List[TermState]:
terms = await Term.find(
Eq(Term.project, self.project.to_ref())
Eq(Term.project, self.project)
).to_list()
terms_states = [await term.get_state() for term in terms]
return terms_states

async def get_namespaces_states(self) -> List[NamespaceState]:
namespaces = await Namespace.find(
Eq(Namespace.project, self.project.to_ref())
Eq(Namespace.project, self.project)
).to_list()
namespaces_states = [await namespace.get_state() for namespace in namespaces]
return namespaces_states
Expand Down
22 changes: 8 additions & 14 deletions mapping_workbench/backend/mapping_package/services/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from mapping_workbench.backend.mapping_package.models.entity import MappingPackage, MappingPackageCreateIn, \
MappingPackageUpdateIn, MappingPackageOut, MappingPackageStateGate
from mapping_workbench.backend.state_manager.services.object_state_manager import delete_object_state
from mapping_workbench.backend.test_data_suite.models.entity import TestDataFileResource, TestDataSuite
from mapping_workbench.backend.triple_map_fragment.models.entity import SpecificTripleMapFragment
from mapping_workbench.backend.triple_map_fragment.models.entity import SpecificTripleMapFragment, \
GenericTripleMapFragment
from mapping_workbench.backend.user.models.user import User


Expand Down Expand Up @@ -103,23 +103,17 @@ async def remove_mapping_package_resources(mapping_package: MappingPackage):
for resource_type in resources_to_delete:
await delete_mapping_package_resource_by_type(resource_type, project_link, package_id)

test_data_suites = await TestDataSuite.find(
TestDataSuite.mapping_package_id == package_id,
TestDataSuite.project == project_link
).to_list()
if test_data_suites:
for test_data_suite in test_data_suites:
await TestDataFileResource.find(
TestDataFileResource.test_data_suite == TestDataSuite.link_from_id(test_data_suite.id),
TestDataFileResource.project == project_link
).delete()
await test_data_suite.delete()

await ConceptualMappingRule.find(
ConceptualMappingRule.project == project_link
).update_many(
Pull({ConceptualMappingRule.refers_to_mapping_package_ids: package_id})
)
print(Pull({GenericTripleMapFragment.refers_to_mapping_package_ids: package_id}))
await GenericTripleMapFragment.find(
GenericTripleMapFragment.project == project_link
).update_many(
Pull({GenericTripleMapFragment.refers_to_mapping_package_ids: package_id})
)


# Mapping Package States
Expand Down
68 changes: 47 additions & 21 deletions mapping_workbench/backend/mapping_package/services/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,79 @@

from beanie import PydanticObjectId
from beanie.odm.operators.find.comparison import In
from beanie.odm.operators.update.array import Pull
from beanie.odm.operators.update.general import Set

from mapping_workbench.backend.mapping_package.models.entity import MappingPackage
from mapping_workbench.backend.project.models.entity import Project
from mapping_workbench.backend.resource_collection.models.entity import ResourceCollection
from mapping_workbench.backend.shacl_test_suite.models.entity import SHACLTestSuite
from mapping_workbench.backend.sparql_test_suite.models.entity import SPARQLTestSuite
from mapping_workbench.backend.test_data_suite.models.entity import TestDataSuite


class ResourceField(Enum):
SHACL_TEST_SUITES = "shacl_test_suites"
SPARQL_TEST_SUITES = "sparql_test_suites"
RESOURCE_COLLECTIONS = "resource_collections"
TEST_DATA_SUITES = "test_data_suites"
# GENERIC_TRIPLE_MAPS = "generic_triple_maps"
# TEST_DATA_SUITES = "test_data_suites"


async def assign_mapping_package_to_resources(
def resource_link_from_id(resources_field: ResourceField, resource_id):
if resources_field == ResourceField.TEST_DATA_SUITES:
return TestDataSuite.link_from_id(resource_id)
if resources_field == ResourceField.SPARQL_TEST_SUITES:
return SPARQLTestSuite.link_from_id(resource_id)
if resources_field == ResourceField.SHACL_TEST_SUITES:
return SHACLTestSuite.link_from_id(resource_id)
if resources_field == ResourceField.RESOURCE_COLLECTIONS:
return ResourceCollection.link_from_id(resource_id)


async def assign_resources_to_mapping_packages(
project_id: PydanticObjectId,
mapping_package_id: PydanticObjectId,
resource_model, # Document
resources_ids: List[PydanticObjectId]
# resource_model: Document,
resources_ids: List[PydanticObjectId],
resources_field: ResourceField,
mapping_packages_ids: List[PydanticObjectId]
):
await resource_model.get_motor_collection().update_many(
{
resource_model.project: Project.link_from_id(project_id).to_ref(),
resource_model.id: {In.operator: resources_ids}
},
query_filter: dict = {
MappingPackage.project: Project.link_from_id(project_id).to_ref(),
MappingPackage.id: {In.operator: mapping_packages_ids}
}

await MappingPackage.get_motor_collection().update_many(
query_filter,
{
"$set": {
resource_model.mapping_package_id: mapping_package_id
Set.operator: {
resources_field.value: [
resource_link_from_id(resources_field, resource_id).to_ref() for resource_id in resources_ids
]
}
}
)


async def assign_resources_to_mapping_packages(
async def unassign_resources_from_mapping_packages(
project_id: PydanticObjectId,
# resource_model: Document,
resources_ids: List[PydanticObjectId],
resources_field: ResourceField,
mapping_packages_ids: List[PydanticObjectId]
mapping_packages_ids: List[PydanticObjectId] = None
):
query_filter: dict = {
MappingPackage.project: Project.link_from_id(project_id).to_ref()
}
if mapping_packages_ids:
query_filter[MappingPackage.id] = {In.operator: mapping_packages_ids}

await MappingPackage.get_motor_collection().update_many(
query_filter,
{
MappingPackage.project: Project.link_from_id(project_id).to_ref(),
MappingPackage.id: {In.operator: mapping_packages_ids}
},
{
"$set": {
resources_field.value: resources_ids
Pull.operator: {
resources_field.value: {In.operator: [
resource_link_from_id(resources_field, resource_id).to_ref() for resource_id in resources_ids
]}
}
}
)
Empty file.
Empty file.
Loading

0 comments on commit a50d3ce

Please sign in to comment.