to_python.alc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 explored
  16. result = "root, = yield [('RR', [])]\n"
  17. nodes = allInstances(model, "Rules/Root")
  18. while (set_len(nodes) > 0):
  19. node = set_pop(nodes)
  20. source = string_replace(node, "/", "_")
  21. result = result + source + " = root\n"
  22. // Keep following outgoing edges to find matching nodes
  23. to_explore = set_create()
  24. explored = set_create()
  25. set_add(to_explore, node)
  26. while (set_len(to_explore) > 0):
  27. // Still explore more!
  28. node = set_pop(to_explore)
  29. source = string_replace(node, "/", "_")
  30. edges = allOutgoingAssociationInstances(model, node, "Rules/Edge")
  31. while (set_len(edges) > 0):
  32. edge = set_pop(edges)
  33. new_node = readAssociationDestination(model, edge)
  34. if (value_eq(read_attribute(model, new_node, "match"), True)):
  35. // Is a match node, so fetch the value on the edge
  36. name = read_attribute(model, edge, "value")
  37. destination = string_replace(new_node, "/", "_")
  38. result = result + destination + ", = yield [('RD', [" + source + ", " + name + "])]\n"
  39. String value
  40. value = read_attribute(model, new_node, "value")
  41. if (element_neq(value, read_root())):
  42. // Match node has a value we should compare as well!
  43. // Read out the value from the Modelverse
  44. result = result + destination + "_V, = yield [('RV', [" + destination + "])]\n"
  45. set_add(to_explore, new_node)
  46. log("Got result:")
  47. log(result)
  48. return True!