to_python.alc 3.7 KB

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