2024-07-25 19:12:31 +03:00
|
|
|
''' Models: Synthesis Inheritance. '''
|
|
|
|
|
from django.db.models import CASCADE, ForeignKey, Model
|
|
|
|
|
|
2025-11-10 21:01:59 +03:00
|
|
|
from .Substitution import Substitution
|
|
|
|
|
|
2024-07-25 19:12:31 +03:00
|
|
|
|
|
|
|
|
class Inheritance(Model):
|
|
|
|
|
''' Inheritance links parent and child constituents in synthesis operation.'''
|
2024-09-12 20:56:38 +03:00
|
|
|
operation = ForeignKey(
|
2024-08-02 11:16:41 +03:00
|
|
|
verbose_name='Операция',
|
|
|
|
|
to='oss.Operation',
|
|
|
|
|
on_delete=CASCADE,
|
|
|
|
|
related_name='inheritances'
|
|
|
|
|
)
|
2024-09-12 20:56:38 +03:00
|
|
|
parent = ForeignKey(
|
2024-07-25 19:12:31 +03:00
|
|
|
verbose_name='Исходная конституента',
|
|
|
|
|
to='rsform.Constituenta',
|
|
|
|
|
on_delete=CASCADE,
|
|
|
|
|
related_name='as_parent'
|
|
|
|
|
)
|
2024-09-12 20:56:38 +03:00
|
|
|
child = ForeignKey(
|
2024-07-25 19:12:31 +03:00
|
|
|
verbose_name='Наследованная конституента',
|
|
|
|
|
to='rsform.Constituenta',
|
|
|
|
|
on_delete=CASCADE,
|
|
|
|
|
related_name='as_child'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
''' Model metadata. '''
|
|
|
|
|
verbose_name = 'Наследование синтеза'
|
|
|
|
|
verbose_name_plural = 'Отношение наследования конституент'
|
2024-07-25 21:18:44 +03:00
|
|
|
unique_together = [['parent', 'child']]
|
|
|
|
|
|
2024-07-25 19:12:31 +03:00
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
return f'{self.parent} -> {self.child}'
|
2025-11-10 21:01:59 +03:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def check_share_origin(cst1: int, cst2: int) -> bool:
|
|
|
|
|
''' Check if two constituents share origin. '''
|
|
|
|
|
inheritance1 = Inheritance.objects.filter(child_id=cst1).first()
|
|
|
|
|
if not inheritance1:
|
|
|
|
|
return False
|
|
|
|
|
inheritance2 = Inheritance.objects.filter(child_id=cst2).first()
|
|
|
|
|
if not inheritance2:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
parent1 = inheritance1.parent
|
|
|
|
|
parent2 = inheritance2.parent
|
|
|
|
|
|
|
|
|
|
origins1 = list(
|
|
|
|
|
Substitution.objects.filter(
|
|
|
|
|
substitution=parent1).values_list(
|
|
|
|
|
'original__schema_id',
|
|
|
|
|
flat=True))
|
|
|
|
|
origins1.append(parent1.schema_id)
|
|
|
|
|
|
|
|
|
|
origins2 = list(
|
|
|
|
|
Substitution.objects.filter(
|
|
|
|
|
substitution=parent2).values_list(
|
|
|
|
|
'original__schema_id',
|
|
|
|
|
flat=True))
|
|
|
|
|
origins2.append(parent2.schema_id)
|
|
|
|
|
|
|
|
|
|
return any(x in origins1 for x in origins2)
|