to_python.alc 3.1 KB

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