Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

optimizer.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. class MigrationOptimizer:
  2. """
  3. Power the optimization process, where you provide a list of Operations
  4. and you are returned a list of equal or shorter length - operations
  5. are merged into one if possible.
  6. For example, a CreateModel and an AddField can be optimized into a
  7. new CreateModel, and CreateModel and DeleteModel can be optimized into
  8. nothing.
  9. """
  10. def optimize(self, operations, app_label):
  11. """
  12. Main optimization entry point. Pass in a list of Operation instances,
  13. get out a new list of Operation instances.
  14. Unfortunately, due to the scope of the optimization (two combinable
  15. operations might be separated by several hundred others), this can't be
  16. done as a peephole optimization with checks/output implemented on
  17. the Operations themselves; instead, the optimizer looks at each
  18. individual operation and scans forwards in the list to see if there
  19. are any matches, stopping at boundaries - operations which can't
  20. be optimized over (RunSQL, operations on the same field/model, etc.)
  21. The inner loop is run until the starting list is the same as the result
  22. list, and then the result is returned. This means that operation
  23. optimization must be stable and always return an equal or shorter list.
  24. """
  25. # Internal tracking variable for test assertions about # of loops
  26. if app_label is None:
  27. raise TypeError("app_label must be a str.")
  28. self._iterations = 0
  29. while True:
  30. result = self.optimize_inner(operations, app_label)
  31. self._iterations += 1
  32. if result == operations:
  33. return result
  34. operations = result
  35. def optimize_inner(self, operations, app_label):
  36. """Inner optimization loop."""
  37. new_operations = []
  38. for i, operation in enumerate(operations):
  39. right = True # Should we reduce on the right or on the left.
  40. # Compare it to each operation after it
  41. for j, other in enumerate(operations[i + 1 :]):
  42. result = operation.reduce(other, app_label)
  43. if isinstance(result, list):
  44. in_between = operations[i + 1 : i + j + 1]
  45. if right:
  46. new_operations.extend(in_between)
  47. new_operations.extend(result)
  48. elif all(op.reduce(other, app_label) is True for op in in_between):
  49. # Perform a left reduction if all of the in-between
  50. # operations can optimize through other.
  51. new_operations.extend(result)
  52. new_operations.extend(in_between)
  53. else:
  54. # Otherwise keep trying.
  55. new_operations.append(operation)
  56. break
  57. new_operations.extend(operations[i + j + 2 :])
  58. return new_operations
  59. elif not result:
  60. # Can't perform a right reduction.
  61. right = False
  62. else:
  63. new_operations.append(operation)
  64. return new_operations