Skip to content

Commit

Permalink
Changed RelationchangeDomain so relations in existing relations are n…
Browse files Browse the repository at this point in the history
…ot in possible relations list after loading an excel
  • Loading branch information
christiaan committed Oct 24, 2024
1 parent 4eaac29 commit 868a4ea
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 88 deletions.
60 changes: 46 additions & 14 deletions Domain/RelationChangeDomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RelationChangeDomain:
project:Project
collector:OSLOCollector
objects: list[AIMObject] = []
existing_relation: list[RelatieObject] = []
existing_relations: list[RelatieObject] = []
possible_relations_per_class: dict[str,list[OSLORelatie]] = {}
possible_object_to_object_relations: dict[str,dict[str,list[RelatieObject]]] = {}

Expand All @@ -33,7 +33,7 @@ def init_static(cls, project:Project):
cls.collector = OSLOCollector(project.subset_path)
cls.collector.collect_all()
cls.objects = []
cls.existing_relation = []
cls.existing_relations = []
cls.possible_relations_per_class = {}

@classmethod
Expand All @@ -42,12 +42,12 @@ def set_instances(cls, objects_list: List[AIMObject]):

for instance in objects_list:
if is_relation(instance):
cls.existing_relation.append(instance)
cls.existing_relations.append(instance)
else:
cls.objects.append(instance)

cls.get_screen().fill_object_list(cls.objects)
cls.get_screen().fill_existing_relations_list(cls.existing_relation)
cls.get_screen().fill_existing_relations_list(cls.existing_relations)

@classmethod
def get_object(cls,identificator:str) -> Optional[AIMObject]:
Expand All @@ -71,21 +71,33 @@ def set_possible_relations(cls, selected_object:AIMObject):
related_objects: list[AIMObject] = list(
filter(RelationChangeDomain.filter_out(selected_object), cls.objects))



if selected_object.assetId.identificator not in cls.possible_object_to_object_relations.keys():
relation_list = cls.possible_relations_per_class[selected_object.typeURI]
for relation in relation_list:



if relation.bron_uri == selected_object.typeURI:

for related_object in related_objects:
if cls.get_same_existing_relations(relation_def= relation,
selected_object=selected_object,
related_object=related_object):
continue

# if(related_object.assetId.identificator == 'dummy_TyBGmXfXC'):
# print(related_object.assetId.identificator == 'dummy_TyBGmXfXC')
if relation.doel_uri == related_object.typeURI:
cls.add_relation_between(relation, selected_object, related_object)

elif relation.doel_uri == selected_object.typeURI:
for related_object in related_objects:
# if(related_object.assetId.identificator == 'dummy_TyBGmXfXC'):
# print(related_object.assetId.identificator == 'dummy_TyBGmXfXC')
if cls.get_same_existing_relations(relation_def= relation,
selected_object=selected_object,
related_object=related_object):
continue
if relation.bron_uri == related_object.typeURI:
cls.add_relation_between(relation, selected_object,related_object,True)

Expand All @@ -96,6 +108,25 @@ def set_possible_relations(cls, selected_object:AIMObject):

cls.get_screen().fill_possible_relations_list(selected_object,possible_relations_for_this_object)

@classmethod
def get_same_existing_relations(cls, relation_def: OSLORelatie,
selected_object: AIMObject,
related_object: AIMObject):
return list(filter(
lambda existed_relation: cls.is_same_relation(existed_relation,
relation_def,
selected_object,
related_object),
cls.existing_relations))

@classmethod
def is_same_relation(cls, existing_relation:RelatieObject, relation_def: OSLORelatie, selected: AIMObject, related: AIMObject) -> bool:
return (existing_relation.typeURI == relation_def.objectUri) and ((
existing_relation.bronAssetId.identificator == selected.assetId.identificator and
existing_relation.doelAssetId.identificator == related.assetId.identificator) or(
existing_relation.bronAssetId.identificator == related.assetId.identificator and
existing_relation.doelAssetId.identificator == selected.assetId.identificator))

@classmethod
def add_relation_between(cls, relation: OSLORelatie, selected_object: AIMObject, related_object:AIMObject, reversed:bool = False) -> None:

Expand All @@ -105,6 +136,7 @@ def add_relation_between(cls, relation: OSLORelatie, selected_object: AIMObject,
# if ( related_object_id == 'dummy_TyBGmXfXC'):
# print( related_object_id == 'dummy_TyBGmXfXC')

# the relation object is reversed from the wrong direction to the right direction
relation_object = None
if reversed:
relation_object = cls.create_relation_object(relation, related_object, selected_object)
Expand Down Expand Up @@ -204,7 +236,7 @@ def sort_nested_dict(cls,d, by='keys'):
def add_possible_relation_to_existing_relations(cls, bron_asset_id, target_asset_id,
relation_object_index):
relation_object = cls.possible_object_to_object_relations[bron_asset_id][target_asset_id].pop(relation_object_index)
cls.existing_relation.append(relation_object)
cls.existing_relations.append(relation_object)

cls.update_frontend()

Expand All @@ -214,11 +246,11 @@ def update_frontend(cls):
selected_object_id = cls.selected_object.assetId.identificator
possibleRelations = cls.possible_object_to_object_relations[selected_object_id]
cls.get_screen().fill_possible_relations_list(cls.selected_object, possibleRelations)
cls.get_screen().fill_existing_relations_list(cls.existing_relation)
cls.get_screen().fill_existing_relations_list(cls.existing_relations)

@classmethod
def remove_existing_relation(cls, index:int) -> RelatieObject:
removed_relation = cls.existing_relation.pop(index)
removed_relation = cls.existing_relations.pop(index)

source = removed_relation.bronAssetId.identificator
target = removed_relation.doelAssetId.identificator
Expand All @@ -232,8 +264,8 @@ def remove_existing_relation(cls, index:int) -> RelatieObject:

@classmethod
def if_possible_relations_list_exists_then_add(cls, source, target, removed_relation):
if cls.possible_object_to_object_relations.__contains__(source):
if cls.possible_object_to_object_relations[source].__contains__(
target):
cls.possible_object_to_object_relations[source][
target].append(removed_relation)
if source in cls.possible_object_to_object_relations:
if target in cls.possible_object_to_object_relations[source]:
cls.possible_object_to_object_relations[source][target].append(removed_relation)
else:
cls.possible_object_to_object_relations[source][target] = [removed_relation]
19 changes: 7 additions & 12 deletions GUI/Screens/RelationChangeScreen.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
from collections import namedtuple
from pathlib import Path
from typing import List, Optional, cast
from typing import List, Optional

import qtawesome as qta
from PyQt6.QtGui import QPixmap

from PyQt6.QtWidgets import QVBoxLayout, QFrame, QHBoxLayout, QPushButton, \
QWidget, QLineEdit, QLabel, QListWidget, QListWidgetItem
from otlmow_model.OtlmowModel.BaseClasses.OTLObject import OTLObject
from otlmow_model.OtlmowModel.Classes.ImplementatieElement.AIMObject import \
AIMObject
from otlmow_model.OtlmowModel.Classes.ImplementatieElement.DirectioneleRelatie import \
DirectioneleRelatie
from otlmow_model.OtlmowModel.Classes.ImplementatieElement.RelatieObject import RelatieObject
from otlmow_model.OtlmowModel.Classes.Onderdeel.HoortBij import HoortBij
from otlmow_model.OtlmowModel.Helpers.OTLObjectHelper import is_directional_relation
from otlmow_modelbuilder.SQLDataClasses.OSLORelatie import OSLORelatie

from Domain.RelationChangeDomain import RelationChangeDomain
from GUI.ButtonWidget import ButtonWidget
Expand Down Expand Up @@ -164,14 +158,15 @@ def fill_existing_relations_list(self, relations_objects: List[RelatieObject]) -
item.setData(3,val["data"].index)
self.existing_relation_list_gui.addItem(item)

def get_screen_name(self, OTL_object:AIMObject) -> str:
def get_screen_name(self, OTL_object:AIMObject) -> Optional[str]:
if OTL_object is None:
return None

screen_name = str(OTL_object.assetId.identificator)
if hasattr(OTL_object, 'naam') and OTL_object.naam:
screen_name = OTL_object.naam
return screen_name
return (
OTL_object.naam
if hasattr(OTL_object, 'naam') and OTL_object.naam
else str(OTL_object.assetId.identificator)
)

def fill_possible_relations_list(self, source_object: AIMObject,
relations: dict[str, list[RelatieObject]]) -> None:
Expand Down
34 changes: 9 additions & 25 deletions UnitTests/GUI_test/RelationChangeScreen_test.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
from pathlib import Path
from unittest.mock import Mock

import pytest
from _pytest.fixtures import fixture
from pytestqt.plugin import qtbot
from pytestqt.qtbot import QtBot

from Domain import global_vars
from Domain.InsertDataDomain import InsertDataDomain
from Domain.Project import Project
from Domain.RelationChangeDomain import RelationChangeDomain
from Domain.enums import Language
from GUI.Screens.DataVisualisationScreen import DataVisualisationScreen
from GUI.Screens.InsertDataScreen import InsertDataScreen
from GUI.Screens.RelationChangeScreen import RelationChangeScreen
from GUI.Screens.Screen import Screen
from GUI.translation.GlobalTranslate import GlobalTranslate
from UnitTests.TestClasses.Classes.Onderdeel.AllCasesTestClass import AllCasesTestClass

from UnitTests.general_fixtures.GUIFixtures import *
from UnitTests.general_fixtures.DomainFixtures import *
from UnitTests.general_fixtures.GUIFixtures import *


#################################################
# FULL TESTS #
Expand Down Expand Up @@ -66,7 +54,7 @@ def test_fill_class_list(root_directory:Path,

InsertDataDomain.add_files_to_backend_list(test_object_lists_file_path)

error_set, objects_lists = InsertDataDomain.load_and_validate_documents()
InsertDataDomain.load_and_validate_documents()

assert mock_rel_screen.object_list_gui.item(0).text() == "installatie#Verkeersbordopstelling | dummy_hxOTHWe"
assert mock_rel_screen.object_list_gui.item(1).text() == "installatie#Verkeersbordopstelling | dummy_LGG"
Expand Down Expand Up @@ -112,9 +100,7 @@ def test_full_fill_possible_relations_list(qtbot,root_directory:Path,
RelationChangeDomain.set_possible_relations(selected_object=fund1)
fund1_possible_relations_gui = [
'Bevestiging <-> dummy_FNrHuPZCWV | onderdeel#Funderingsmassief',
'LigtOp --> dummy_FNrHuPZCWV | onderdeel#Funderingsmassief',
'Bevestiging <-> dummy_C | onderdeel#Pictogram',
'Bevestiging <-> dummy_a | onderdeel#Pictogram',
'Bevestiging <-> dummy_J | onderdeel#Verkeersbordsteun',
'Bevestiging <-> dummy_s | onderdeel#Verkeersbordsteun']

Expand All @@ -135,16 +121,14 @@ def test_full_fill_possible_relations_list(qtbot,root_directory:Path,
range(RelationChangeDomain.get_screen().possible_relation_list_gui.count())]

fund1_possible_relations_gui_data2 = [
'dummy_FNrHuPZCWV',
'dummy_FNrHuPZCWV',
'dummy_C',
'dummy_a',
'dummy_vbeo',
'dummy_TjwXqP']

assert data2 == fund1_possible_relations_gui_data2

fund1_possible_relations_gui_data3 = [0,1,0,0,0,0]
fund1_possible_relations_gui_data3 = [0,0,0,0]

data3 = [RelationChangeDomain.get_screen().possible_relation_list_gui.item(x).data(5) for x in
range(RelationChangeDomain.get_screen().possible_relation_list_gui.count())]
Expand All @@ -165,12 +149,12 @@ def test_full_fill_existing_relations_list(qtbot,root_directory:Path,

InsertDataDomain.add_files_to_backend_list(test_object_lists_file_path)

error_set, objects_lists = InsertDataDomain.load_and_validate_documents()
InsertDataDomain.load_and_validate_documents()

existing_relations_gui = ['Bevestiging | dummy_a <-> dummy_TyBGmXfXC',
'Bevestiging | None <-> None',
'HoortBij | dummy_J --> dummy_LGG',
'LigtOp | dummy_FNrHuPZCWV --> dummy_TyBGmXfXC']
existing_relations_gui = [ 'Bevestiging | dummy_a <-> dummy_TyBGmXfXC',
'Bevestiging | None <-> None',
'HoortBij | dummy_J --> dummy_LGG',
'LigtOp | dummy_FNrHuPZCWV --> dummy_TyBGmXfXC']

real_existing_relations_gui =[RelationChangeDomain.get_screen().existing_relation_list_gui.item(x).text() for x in range(RelationChangeDomain.get_screen().existing_relation_list_gui.count())]

Expand Down
Loading

0 comments on commit 868a4ea

Please sign in to comment.