Browse Source

Changes to compiler to allow for the new HUTN syntax

Yentl Van Tendeloo 8 years ago
parent
commit
5d285a35de

+ 20 - 1
bootstrap/modelling.alc

@@ -221,8 +221,27 @@ String function instantiate_link(model : Element, type : String, name : String,
 	String actual_name
 
 	actual_name = model_add_edge(model, name, source, destination)
-	retype(model, actual_name, type)
 
+	if (type == ""):
+		// Have to find the type ourselves, as it isn't defined
+		Element out
+		Element in
+		Element options
+
+		out = selectPossibleOutgoing(model, source)
+		in = selectPossibleIncoming(model, destination)
+		options = set_overlap(out, in)
+
+		if (read_nr_out(options) == 1):
+			type = set_pop(options)
+		elif (read_nr_out(options) == 0):
+			log("ERROR: cannot find possible link between entries")
+			return ""!
+		else:
+			log("ERROR: too many possible links between entries")
+			return ""!
+	
+	retype(model, actual_name, type)
 	return actual_name!
 
 Void function define_inheritance(model : Element, inheritance_name : String):

+ 31 - 24
integration/test_pn_interface.py

@@ -888,6 +888,7 @@ Void function action(host_model : Element, name : String, mapping : Element):
                 Association T2P (Transition, Place) {
                     weight : Natural
                 }
+            }
 
             export PetriNets_Runtime to models/PetriNets_Runtime
             """
@@ -925,8 +926,7 @@ Void function action(host_model : Element, name : String, mapping : Element):
                         LHS {
                             Pre_Transition {
                                 label = "1"
-                                constraint = 
-                                    $
+                                constraint = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     include "object_operations.alh"
@@ -951,8 +951,7 @@ Void function action(host_model : Element, name : String, mapping : Element):
                         RHS {
                             Post_Transition {
                                 label = "1"
-                                action = 
-                                    $
+                                action = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Void function action(host_model : Element, name : String, mapping : Element):
@@ -962,12 +961,12 @@ Void function action(host_model : Element, name : String, mapping : Element):
                                     $
                             }
                         }
+                    }
                     Atomic consume {
                         LHS {
                             Pre_Transition {
                                 label = "0"
-                                constraint = 
-                                    $
+                                constraint = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Boolean function constraint(host_model : Element, name : String):
@@ -977,8 +976,7 @@ Void function action(host_model : Element, name : String, mapping : Element):
                             }
                             Pre_Place {
                                 label = "1"
-                                constraint = 
-                                    $
+                                constraint = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Boolean function constraint(host_model : Element, name : String):
@@ -989,14 +987,14 @@ Void function action(host_model : Element, name : String, mapping : Element):
                             Pre_P2T {
                                 label = "2"
                             }
+                        }
                         RHS {
                             Post_Transition {
                                 label = "0"
                             }
                             Post_Place {
                                 label = "1"
-                                action = 
-                                    $
+                                action = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Void function action(host_model : Element, name : String, mapping : Element):
@@ -1015,12 +1013,12 @@ Void function action(host_model : Element, name : String, mapping : Element):
                                 label = "2"
                             }
                         }
+                    }
                     Atomic produce {
                         LHS {
                             Pre_Transition {
                                 label = "0"
-                                constraint = 
-                                    $
+                                constraint = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Boolean function constraint(host_model : Element, name : String):
@@ -1030,8 +1028,7 @@ Void function action(host_model : Element, name : String, mapping : Element):
                             }
                             Pre_Place {
                                 label = "1"
-                                constraint = 
-                                    $
+                                constraint = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Boolean function constraint(host_model : Element, name : String):
@@ -1042,14 +1039,14 @@ Void function action(host_model : Element, name : String, mapping : Element):
                             Pre_T2P {
                                 label = "2"
                             }
+                        }
                         RHS {
                             Post_Transition {
                                 label = "0"
                             }
                             Post_Place {
                                 label = "1"
-                                action = 
-                                    $
+                                action = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Void function action(host_model : Element, name : String, mapping : Element):
@@ -1068,12 +1065,12 @@ Void function action(host_model : Element, name : String, mapping : Element):
                                 label = "2"
                             }
                         }
+                    }
                     Atomic unmark_transition {
                         LHS {
                             Pre_Transition {
                                 label = "0"
-                                constraint = 
-                                    $
+                                constraint = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Boolean function constraint(host_model : Element, name : String):
@@ -1085,15 +1082,22 @@ Void function action(host_model : Element, name : String, mapping : Element):
                         RHS {
                             Post_Transition {
                                 label = "0"
-                                value = $$
+                                action = $
+                                    include "primitives.alh"
+                                    include "modelling.alh"
+                                    Void function action(host_model : Element, name : String, mapping : Element):
+                                            unset_attribute(host_model, name, "executingd")
+                                            instantiate_attribute(host_model, name, "executing", False)
+                                            return!
+                                    $
                             }
                         }
-                    Atomic unmark_place:
+                    }
+                    Atomic unmark_place {
                         LHS {
                             Pre_Place {
                                 label = "0"
-                                constraint = 
-                                    $
+                                constraint = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Boolean function constraint(host_model : Element, name : String):
@@ -1105,8 +1109,7 @@ Void function action(host_model : Element, name : String, mapping : Element):
                         RHS {
                             Post_Place {
                                 label = "0"
-                                value = 
-                                    $
+                                action = $
                                     include "primitives.alh"
                                     include "modelling.alh"
                                     Void function action(host_model : Element, name : String, mapping : Element):
@@ -1128,6 +1131,10 @@ Void function action(host_model : Element, name : String, mapping : Element):
                     OnSuccess (unmark_place, unmark_place) {}
                     OnFailure (unmark_place, success) {}
                     initial = mark
+                }
+            }
+
+            export s to models/pn_simulate
             """
 
         self.assertTrue(run_file(all_files,

+ 8 - 4
interface/HUTN/grammars/modelling.g

@@ -5,17 +5,20 @@ grammar{
     export: EXPORT MODEL_ID TO MV_URL;
     include_files: INCLUDE STRVALUE NEWLINE;
 
-    model: MODEL_ID MODEL_ID NEWLINE? LCURLY NEWLINE? (model_element)* RCURLY;
+    model: MODEL_ID (LPAR MODEL_ID RPAR)? MODEL_ID NEWLINE? LCURLY NEWLINE? (model_element)* RCURLY;
 
     model_element: MODEL_ID MODEL_ID? inheritance? (LPAR MODEL_ID COMMA MODEL_ID RPAR)? NEWLINE? LCURLY NEWLINE? (model_attribute)* RCURLY NEWLINE?;
 
     inheritance: COLON MODEL_ID (COMMA MODEL_ID)*;
 
-    model_attribute : (MODEL_ID COLON MODEL_ID (LCURLY NEWLINE? model_attr_instance* RCURLY)? NEWLINE?)
+    model_attribute : ((LCURLY MODEL_ID RCURLY)? model_element)
+                    | (MODEL_ID COLON MODEL_ID (LCURLY NEWLINE? model_attr_instance* RCURLY)? NEWLINE?)
                     | (model_attr_instance)
-                    | (DOLLAR ANYTHING_EXCEPT_DOLLAR DOLLAR NEWLINE?);
+                    | (NEWLINE? DOLLAR ANYTHING_EXCEPT_DOLLAR DOLLAR NEWLINE?);
 
-    model_attr_instance: MODEL_ID ASSIGN value NEWLINE?;
+    model_attr_instance: (MODEL_ID ASSIGN value NEWLINE?)
+                       | (MODEL_ID ASSIGN NEWLINE? DOLLAR ANYTHING_EXCEPT_DOLLAR DOLLAR NEWLINE?)
+                       | (MODEL_ID ASSIGN MODEL_ID NEWLINE?);
 
     value
         : DEC_NUMBER
@@ -40,6 +43,7 @@ grammar{
         ASSIGN: '=';
         DOLLAR: '\$';
         WS: '[ ]+' @Impl;
+        TAB: '[\t]+' @Impl;
         COLON : ':';
         LPAR: '\(';
         RPAR: '\)';

+ 48 - 14
interface/HUTN/hutn_compiler/model_visitor.py

@@ -46,7 +46,8 @@ class ModelVisitor(Visitor):
         model_type = children[0].get_text()
         model_name = children[1].get_text()
         self.constructors.extend(["instantiate_model", model_type, model_name])
-        self.constructors.extend(["define_inheritance", model_name, "Inheritance"])
+        if "LPAR" in tree.get_tail():
+            self.constructors.extend(["define_inheritance", model_name, tree.get_children("MODEL_ID")[1].get_text()])
         self.current_model = model_name
         for element in tree.get_children("model_element"):
             self.visit(element)
@@ -75,6 +76,8 @@ class ModelVisitor(Visitor):
         for attr in tree.get_children("model_attribute"):
             self.visit(attr)
 
+        return element_name
+
     def visit_inheritance(self, tree):
         for token in tree.get_children("MODEL_ID"):
             superclass = token.get_text()
@@ -85,6 +88,7 @@ class ModelVisitor(Visitor):
         is_definition = bool(tree.get_children("COLON"))
         is_constraint = bool(tree.get_children("DOLLAR"))
         is_assign = bool(tree.get_children("model_attr_instance"))
+        is_nested = bool(tree.get_children("model_element"))
 
         if is_definition:
             attr_name = children[0].get_text()
@@ -116,21 +120,51 @@ class ModelVisitor(Visitor):
             directory = os.path.realpath(__file__).rsplit(os.sep, 1)[0]
             compiled = do_compile(".constraint.alc", directory + "/../grammars/actionlanguage.g", "CS")
             self.constructors.extend(["add_constraint", self.current_model, self.current_element] + compiled)
+        elif is_nested:
+            if tree.get_children("MODEL_ID"):
+                contains_link = tree.get_children("MODEL_ID")[0].get_text()
+            else:
+                contains_link = ""
+            entry = self.visit(tree.get_children("model_element")[0])
+            self.constructors.extend(["instantiate_link", self.current_model, contains_link, "__%s" % self.free_id, self.current_element, entry])
+            self.free_id += 1
 
     def visit_model_attr_instance(self, tree):
+        def constructors_compile(code):
+            code_fragments = code.split("\n")
+	    code_fragments = [i for i in code_fragments if i.strip() != ""]
+	    code_fragments = [i.replace("    ", "\t") for i in code_fragments]
+	    initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
+	    code_fragments = [i[initial_tabs:] for i in code_fragments]
+	    code = "\n".join(code_fragments)
+
+            with open(".code.alc", 'w') as f:
+                f.write(code)
+                f.flush()
+            directory = os.path.realpath(__file__).rsplit(os.sep, 1)[0]
+            compiled = do_compile(".code.alc", directory + "/../grammars/actionlanguage.g", "CS")
+
         children = tree.get_children("MODEL_ID")
         attr_name = children[0].get_text()
-        attr_value = tree.get_children("value")[0].get_tail()[0]
-        if attr_value.head == "STRVALUE":
-            attr_value = attr_value.get_text()[1:-1]
-        elif attr_value.head == "TRUE":
-            attr_value = True
-        elif attr_value.head == "FALSE":
-            attr_value = False
-        elif attr_value.head == "DEC_NUMBER":
-            attr_value = int(attr_value.get_text())
-        elif attr_value.head == "FLOAT_NUMBER":
-            attr_value = float(attr_value.get_text())
+        if tree.get_children("value"):
+            # Value attribute
+            attr_value = tree.get_children("value")[0].get_tail()[0]
+            if attr_value.head == "STRVALUE":
+                attr_value = attr_value.get_text()[1:-1]
+            elif attr_value.head == "TRUE":
+                attr_value = True
+            elif attr_value.head == "FALSE":
+                attr_value = False
+            elif attr_value.head == "DEC_NUMBER":
+                attr_value = int(attr_value.get_text())
+            elif attr_value.head == "FLOAT_NUMBER":
+                attr_value = float(attr_value.get_text())
+            else:
+                raise Exception(attr_value.head)
+            self.constructors.extend(["instantiate_attribute", self.current_model, self.current_element, attr_name, attr_value])
+        elif tree.get_children("DOLLAR"):
+            # Coded attribute
+            self.constructors.extend(["instantiate_attribute_code", self.current_model, self.current_element, attr_name, constructors_compile(tree.get_children("ANYTHING_EXCEPT_DOLLAR")[1].get_text())])
         else:
-            raise Exception(attr_value.head)
-        self.constructors.extend(["instantiate_attribute", self.current_model, self.current_element, attr_name, attr_value])
+            # Assign direct reference
+            self.constructors.extend(["instantiate_attribute_ref", self.current_model, self.current_element, attr_name, tree.get_children("MODEL_ID")[1].get_text()])