Skip to content

Commit

Permalink
Merge pull request #16 from diverso-lab/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
drorganvidez authored Nov 26, 2022
2 parents 6ac541c + 1cc8bf7 commit 91e2b29
Show file tree
Hide file tree
Showing 53 changed files with 4,232 additions and 29,132 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ Thus, we have started a Drupal container populated with test data on port :8080

<div align="center">

| | Web | MySQL | PMA | ADM credentials (Web /admin & PMA database access) |
|-----------|-------|--------|-------|----------------------------------------------------|
| Drupal | :8080 | :33061 | :8081 | user=drupal pw=drupal |
| WordPress | :8082 | :3307 | :8083 | user=wordpress pw=wordpress |
| | Web | MySQL | PHPMyAdmin | Credentials (Web /admin & PHPMyAdmin database access) |
|-----------|-------|--------|------------|-------------------------------------------------------|
| Drupal | :8080 | :33061 | :8081 | user=drupal, password=drupal |
| WordPress | :8082 | :33062 | :8083 | user=wordpress, password=wordpress |

</div>
253 changes: 212 additions & 41 deletions core/models/mm/Migration.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import os

from flamapy.metamodels.fm_metamodel.models import Feature

from core.loaders.WorkspaceLoader import WorkspaceLoader
from core.models.mm import MigrationModel
from core.models.sdm.SimpleDatabaseModel import SimpleDatabaseModel
from core.models.stm.AvailableActionsExtractor import AvailableActionsExtractor
from core.models.stm.AvailableAction import AvailableAction
from core.models.stm.SimpleTransformationModel import SimpleTransformationModel
from core.models.stm.actions.AbstractAction import AbstractAction
from core.models.stm.actions.CopyAttributeAction import CopyAttributeAction
from core.mutators.SimpleDatabaseModelMutator import SimpleDatabaseModelMutator
from core.writers.MigrationWriter import MigrationWriter

Expand All @@ -14,17 +19,17 @@ class Migration:
def __init__(self, migration_model: MigrationModel, feature: Feature):

# basic
self._migration_model = migration_model
self._feature = feature
self._migration_model: MigrationModel = migration_model
self._feature: Feature = feature
self._workspace: str = WorkspaceLoader().name()

# derivative
self._migration_name = self._feature.name
self._current_sdm_source = migration_model.sdm_source()
self._migration_name: str = self._feature.name
self._current_sdm_source: SimpleDatabaseModel = self.current_sdm_source()
self._sdm_target = migration_model.sdm_target()

# for operations
self._selected_actions: list[AvailableAction] = list()
self._actions_counter: int = 0

def __str__(self):
return self._feature.__str__()
Expand All @@ -39,54 +44,118 @@ def is_root(self):
def is_abstract(self):
return self._feature.is_abstract

def define(self, opening=True) -> None:
def is_leaf(self):
return self._feature.is_leaf()

print(self._migration_model.root())
def _calculate_actions_counter(self) -> int:

extractor = AvailableActionsExtractor(sdm_source=self._current_sdm_source,
sdm_target=self._migration_model.sdm_target())
stm = self.stm()

extractor.extract_available_actions()
if stm is None:
return 0
else:
return len(stm.transformations()) - 1

if len(extractor.available_actions()) == 0:
return self._finish()
def current_sdm_source(self) -> SimpleDatabaseModel:

# show migration info
print()
stm = self.stm()

if stm is None:
return self._migration_model.sdm_source()
else:

suffix = len(stm.transformations()) - 1

sdm_file = 'workspaces/{workspace}/migrations/{migration_name}/{migration_name}_{suffix}.sdm'.format(
workspace=self._workspace,
migration_name=self._migration_name,
suffix=suffix)

if not os.path.isfile(sdm_file):
return None
else:
return SimpleDatabaseModel(filename=sdm_file)

def sdm_target(self) -> SimpleDatabaseModel:
return self._sdm_target

def _is_opening(self) -> bool:

return not os.path.isfile('workspaces/{workspace}/migrations/{migration_name}/{migration_name}.stm'.format(
workspace=self._workspace,
migration_name=self._migration_name))

def _show_basic_info(self) -> None:

os.system('clear')
print("########################################")
print("Current migration: {}".format(self._migration_name))
print("{workspace}: MIGRATION WIZARD".format(workspace=self._workspace))
print("########################################")
print()
print("-> Working on migration: {}".format(self._migration_name))
print()
print('-> Current actions')
print()

stm = self.stm()
if not stm is None:

for t in stm.transformations():
for a in t.actions():
print("\t" + a.apply().info())
print()

else:

print("No actions defined")

# show available actions
extractor.print()
def _show_and_select_abstract_action(self) -> AbstractAction | None:

abstract_action = None

print("-> Available action(s)")
print()
print("\t[0] Create entity")
print("\t[1] Rename entity")
print("\t[2] Delete entity")
print()
print("\t[3] Create attribute")
print("\t[4] Rename attribute")
print("\t[5] Retype attribute")
print("\t[6] Move attribute")
print("\t[7] Copy attribute")
print("\t[8] Delete attribute")

print("")

inputted = str(input("Select an available action ('q' for quit): "))
inputted = str(input("Select an action ('q' for quit): "))

if inputted == "q":
return self._finish()
return abstract_action

match int(inputted):
case 7:
abstract_action = self._define_copy_attribute_action()
case _:
pass

return abstract_action

def define(self) -> None:

self._show_basic_info()

option = None
try:
option = int(inputted)
except:
self.define(opening=True)
abstract_action = self._show_and_select_abstract_action()

# selection of action from available actions in current SDM
selected_action = extractor.available_actions()[option]
self._selected_actions.append(selected_action)
print("Selected action: \n")
print(selected_action)
if abstract_action is None:
return

# write transformation in STM file
migration_writer = MigrationWriter(
migration_model_name=self._migration_model.root(),
migration_name=self._migration_name,
available_action=selected_action,
opening=opening,
closing=False
abstract_action=abstract_action,
opening=self._is_opening()
)
migration_writer.write()
last_stm = migration_writer.stm()
Expand All @@ -95,23 +164,125 @@ def define(self, opening=True) -> None:
sdm_mutator = SimpleDatabaseModelMutator(
current_sdm_source=self._current_sdm_source,
last_stm=last_stm,
actions_counter=self._actions_counter,
actions_counter=self._calculate_actions_counter(),
migration_name=self._migration_name,
folder="{}/{}".format(self._migration_model.root(), self._migration_name))

sdm_mutator.mutate()
self._current_sdm_source = sdm_mutator.new_sdm()

# increments actions counter
self._actions_counter = self._actions_counter + 1

# recursive call
self.define(opening=False)
return self.define()

def _define_copy_attribute_action(self) -> AbstractAction:

# TODO: Refactoring

entities_source = self.current_sdm_source().entities()
entities_target = self.sdm_target().entities()

#### SELECT ENTITY FROM SOURCE
os.system('clear')
print("[**] Select entity from source")
print("[ ] Select attribute from source")
print("[ ] Select entity from target")
print("[ ] Select attribute from target")
print()

counter = 0
for e in entities_source:
print("[{counter}] {entity_name}".format(counter=counter, entity_name=e.name()))
counter = counter + 1

print()
inputted = str(input("Select entity from source database ('q' for quit):"))

if inputted == "q":
return self.define()

entity_from = entities_source[int(inputted)]

#### SELECT ATTRIBUTE
os.system('clear')
print("[OK] Select entity from source ({entity_from})".format(entity_from=entity_from))
print("[**] Select attribute from source")
print("[ ] Select entity from target")
print("[ ] Select attribute from target")
print()

counter = 0
for attr in entity_from.attributes():
print("[{counter}] {attr_name} ({type})".format(counter=counter, attr_name=attr.name(), type=attr.type()))
counter = counter + 1

print()
inputted = str(input("Select attribute from entity ('q' for quit):"))

if inputted == "q":
return self.define()

attribute_from = entity_from.attributes()[int(inputted)]

#### SELECT ENTITY FROM TARGET
os.system('clear')
print("[OK] Select entity from source ({entity_from})".format(entity_from=entity_from.name()))
print("[OK] Select attribute from source")
print("[**] Select entity from target")
print("[ ] Select attribute from target")
print()

counter = 0
for e in entities_target:
print("[{counter}] {entity_name}".format(counter=counter, entity_name=e.name()))
counter = counter + 1

print()
inputted = str(input("Select entity from target database ('q' for quit):"))

if inputted == "q":
return self.define()

entity_to = entities_target[int(inputted)]

#### SELECT ATTRIBUTE FROM TARGET
os.system('clear')
print("[OK] Select entity from source ({entity_from})".format(entity_from=entity_from.name()))
print("[OK] Select attribute from source")
print("[OK] Select entity from target")
print("[ ] Select attribute from target")
print()

counter = 0
for attr in entity_to.attributes():
print("[{counter}] {attr_name} ({type})".format(counter=counter, attr_name=attr.name(), type=attr.type()))
counter = counter + 1

print()
inputted = str(input("Select attribute from entity ('q' for quit):"))

if inputted == "q":
return self.define()

attribute_to = entity_to.attributes()[int(inputted)]

action = CopyAttributeAction(entity_from_id=entity_from.id(),
entity_to_id=entity_to.id(),
attribute_from_name=attribute_from.name(),
attribute_to_name=attribute_to.name(),
type=attribute_from.type())

return action

def _finish(self):
pass

def stm(self) -> SimpleTransformationModel:
return SimpleTransformationModel(stm_file='workspaces/{workspace}/migrations'
'/{migration_name}/{migration_name}.stm'
.format(workspace=self._workspace, migration_name=self._migration_name))
def stm(self) -> SimpleTransformationModel | None:

stm_file = 'workspaces/{workspace}/migrations/{migration_name}/{migration_name}.stm'.format(
workspace=self._workspace, migration_name=self._migration_name)

if not os.path.isfile(stm_file):
return None
else:
return SimpleTransformationModel(stm_file=stm_file)

Loading

0 comments on commit 91e2b29

Please sign in to comment.