|
@@ -1,4 +1,5 @@
|
|
|
from visitor import Visitor
|
|
|
+from compiler import main as do_compile
|
|
|
|
|
|
def jsonstr(s):
|
|
|
return '"%s"' % s
|
|
@@ -14,6 +15,7 @@ class ModelVisitor(Visitor):
|
|
|
self.name_maps = {}
|
|
|
self.current_model = None
|
|
|
self.current_element = None
|
|
|
+ self.includes = []
|
|
|
|
|
|
def dump(self):
|
|
|
print(self.constructors)
|
|
@@ -31,6 +33,9 @@ class ModelVisitor(Visitor):
|
|
|
self.visit(t)
|
|
|
self.constructors.append('"exit"')
|
|
|
|
|
|
+ def visit_include_files(self, tree):
|
|
|
+ self.includes.append(tree.get_children("STRVALUE")[0].get_text())
|
|
|
+
|
|
|
def visit_import(self, tree):
|
|
|
url = tree.get_children("MV_URL")[0]
|
|
|
target = tree.get_children("MODEL_ID")[0]
|
|
@@ -75,15 +80,28 @@ class ModelVisitor(Visitor):
|
|
|
def visit_model_attribute(self, tree):
|
|
|
children = tree.get_children("MODEL_ID")
|
|
|
is_definition = bool(tree.get_children("COLON"))
|
|
|
+ is_constraint = bool(tree.get_children("DOLLAR"))
|
|
|
+ is_assign = bool(tree.get_children("ASSIGN"))
|
|
|
|
|
|
if is_definition:
|
|
|
- # is definition
|
|
|
attr_name = children[0].get_text()
|
|
|
attr_type = children[1].get_text()
|
|
|
self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr("Association"), jsonstr(self.current_element + "_" + attr_name), jsonstr(self.current_element), jsonstr(attr_type)])
|
|
|
self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_model), jsonstr(self.current_element + "_" + attr_name), jsonstr("name"), jsonstr(attr_name)])
|
|
|
- else:
|
|
|
- # is assign
|
|
|
+ elif is_assign:
|
|
|
attr_name = children[0].get_text()
|
|
|
attr_value = tree.get_children("value")[0]
|
|
|
self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_model), jsonstr(self.current_element), jsonstr(attr_name), jsonstr(attr_value.get_text()) if attr_value.head == "STRVALUE" else attr_value.get_text()])
|
|
|
+ elif is_constraint:
|
|
|
+ constraint = tree.get_children("ANYTHING_EXCEPT_DOLLAR")[0].get_text()
|
|
|
+ whitespaces = len(constraint) - len(constraint.lstrip())
|
|
|
+ constraint = "\n".join(["\t" + line[whitespaces-1:].replace(" ", "\t") for line in constraint.split("\n") if len(line.strip()) != 0])
|
|
|
+ constraint = "".join(["include %s\n" % i for i in self.includes]) + \
|
|
|
+ "String function constraint(model : Element, element : String):\n" + \
|
|
|
+ "\tElement self\n" + \
|
|
|
+ '\tself = model["model"][element]\n' + \
|
|
|
+ constraint + "\n"
|
|
|
+ with open(".constraint.alc", 'w') as f:
|
|
|
+ f.write(constraint)
|
|
|
+ f.flush()
|
|
|
+ self.constructors.extend(['"add_constraint"', jsonstr(self.current_model), jsonstr(self.current_element)] + do_compile(".constraint.alc", "interface/HUTN/grammars/actionlanguage.g", "CS"))
|