to_python.alc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "random.alh"
  5. include "utils.alh"
  6. Boolean function main(model : Element):
  7. String result
  8. Element nodes
  9. String node
  10. Element edges
  11. String edge
  12. String new_node
  13. String source
  14. String destination
  15. String name
  16. Element to_explore
  17. Element rules
  18. String rule
  19. String value
  20. Element explored
  21. Element remainder_to_explore
  22. explored = set_create()
  23. result = "root, = yield [('RR', [])]\n"
  24. nodes = allInstances(model, "Rules/Root")
  25. while (set_len(nodes) > 0):
  26. node = set_pop(nodes)
  27. source = string_replace(node, "/", "_")
  28. result = result + source + " = root\n"
  29. // Keep following outgoing edges to find matching nodes
  30. to_explore = set_create()
  31. remainder_to_explore = set_create()
  32. set_add(to_explore, node)
  33. while (set_len(to_explore) > 0):
  34. // Still explore more!
  35. node = set_pop(to_explore)
  36. source = string_replace(node, "/", "_")
  37. edges = allOutgoingAssociationInstances(model, node, "Rules/Edge")
  38. while (set_len(edges) > 0):
  39. edge = set_pop(edges)
  40. new_node = readAssociationDestination(model, edge)
  41. if (value_eq(read_attribute(model, new_node, "match"), True)):
  42. // Is a match node, so fetch the value on the edge
  43. name = read_attribute(model, edge, "value")
  44. destination = string_replace(new_node, "/", "_")
  45. if (element_eq(name, read_root())):
  46. String node_key
  47. node_key = string_replace(set_pop(allAssociationDestinations(model, edge, "")), "/", "_")
  48. if (set_in(explored, node_key)):
  49. result = result + destination + ", = yield [('RDN', [" + source + ", " + node_key + "])]\n"
  50. else:
  51. set_add(remainder_to_explore, node)
  52. else:
  53. if (set_in(explored, destination)):
  54. // Already visited this one in another way, so try to merge!
  55. String rand
  56. rand = random_string(10)
  57. result = result + rand + ", = yield [('RD', [" + source + ", " + name + "])]\n"
  58. result = result + "if " + rand + " != " + destination + ":\n"
  59. // If the values don't agree, this is not a correct match, and we say that this element remains unmatched (i.e., assign None)
  60. result = result + "\t" + destination + " = None\n"
  61. else:
  62. // First visit to this element, so just assign
  63. result = result + destination + ", = yield [('RD', [" + source + ", " + name + "])]\n"
  64. set_add(explored, destination)
  65. String value
  66. value = read_attribute(model, new_node, "value")
  67. if (element_neq(value, read_root())):
  68. // Match node has a value we should compare as well!
  69. // Read out the value from the Modelverse
  70. result = result + destination + "_V, = yield [('RV', [" + destination + "])]\n"
  71. set_add(to_explore, new_node)
  72. if (bool_and(set_len(to_explore) == 0, set_len(remainder_to_explore) > 0)):
  73. to_explore = remainder_to_explore
  74. remainder_to_explore = set_create()
  75. rules = allInstances(model, "Rules/Rule")
  76. while (set_len(rules) > 0):
  77. // Check if this rule is applicable
  78. rule = set_pop(rules)
  79. // Fetch all elements with a "match" label and check that they are not None (and possibly, that they have a value)
  80. result = result + "if (True "
  81. nodes = allAssociationDestinations(model, rule, "Rules/contains")
  82. while (set_len(nodes) > 0):
  83. node = set_pop(nodes)
  84. if (value_eq(read_attribute(model, node, "match"), True)):
  85. // We should match on this node:
  86. value = read_attribute(model, node, "value")
  87. if (bool_and(read_type(model, node) == "Rules/NAC", element_eq(value, read_root()))):
  88. result = result + " and " + string_replace(node, "/", "_") + " is None "
  89. else:
  90. result = result + " and " + string_replace(node, "/", "_") + " is not None "
  91. if (element_neq(value, read_root())):
  92. // Got a value, so match that as well
  93. // But check if it is an action element (first character == !)
  94. String sign
  95. if (read_type(model, node) == "Rules/NAC"):
  96. sign = "!="
  97. else:
  98. sign = "=="
  99. if (string_get(value, 0) == "!"):
  100. result = result + " and " + string_replace(node, "/", "_") + "_V['value'] " + sign + " '" + string_replace(value, "!", "") + "'"
  101. else:
  102. result = result + " and " + string_replace(node, "/", "_") + "_V " + sign + " " + value
  103. result = result + "): pass # Execute rule " + string_replace(rule, "/", "_") + "\n"
  104. log("Got result:")
  105. log(result)
  106. return True!