Пример #1
0
 def visit_DiagramTestEvent(self, event):
     self.writer.add(
         GLC.NewExpression("Event", [
             GLC.String(event.name),
             GLC.String(event.port),
             GLC.ArrayExpression(event.parameters)
         ]))
Пример #2
0
 def visit_Association(self, association):
     self.writer.addAssignment(
         GLC.MapIndexedExpression(GLC.Property("instance", "associations"),
                                  GLC.String(association.name)),
         GLC.NewExpression("Association", [
             GLC.String(association.to_class),
             str(association.min),
             str(association.max)
         ]))
Пример #3
0
    def writeTransitionCondition(self, transition, index):
        trigger = transition.getTrigger()

        self.writer.addAssignment(
            GLC.LocalVariableDeclaration("enabled_events"),
            GLC.FunctionCall(GLC.SelfProperty("getEnabledEvents")))

        if not trigger.isUC():
            self.writer.beginForLoopIterateArray("enabled_events", "e")
            condition = GLC.EqualsExpression(
                GLC.Property(GLC.ForLoopCurrentElement("enabled_events", "e"),
                             "name"), GLC.String(trigger.getEvent()))
            if trigger.getPort() != "":
                condition = GLC.AndExpression(
                    condition,
                    GLC.EqualsExpression(
                        GLC.Property(
                            GLC.ForLoopCurrentElement("enabled_events", "e"),
                            "port"), GLC.String(trigger.getPort())))
            self.writer.beginIf(condition)
        # evaluate guard
        if transition.hasGuard():
            # handle parameters for guard evaluation
            if not transition.getTrigger().isUC():
                self.writer.addAssignment(
                    GLC.LocalVariableDeclaration("parameters"),
                    GLC.Property(
                        GLC.ForLoopCurrentElement("enabled_events", "e"),
                        "parameters"))
                self.writeFormalEventParameters(transition)
            self.writer.startRecordingExpression()
            transition.getGuard().accept(self)  # --> visit_Expression
            expr = self.writer.stopRecordingExpression()
            self.writer.beginIf(expr)

        if trigger.isUC():
            params_expr = GLC.ArrayExpression()
        else:
            params_expr = GLC.Property(
                GLC.ForLoopCurrentElement("enabled_events", "e"), "parameters")
        self.writer.add(
            GLC.FunctionCall(
                GLC.Property(GLC.SelfProperty("small_step"), "addCandidate"), [
                    GLC.SelfProperty("transition_" +
                                     transition.parent_node.full_name + "_" +
                                     str(index)), params_expr
                ]))

        self.writer.add(GLC.ReturnStatement(GLC.TrueExpression()))

        if transition.hasGuard():
            self.writer.endIf()
        if not trigger.isUC():
            self.writer.endIf()
            self.writer.endForLoopIterateArray()
Пример #4
0
    def visit_Constructor(self, constructor):
        self.writer.beginConstructor()
        if constructor.parent_class.statechart:
            self.writer.addFormalParameter("controller")
        for p in constructor.getParams():
            self.writer.addFormalParameter(p.getIdent(), p.getDefault())
        self.writer.beginMethodBody()  # constructor body

        if constructor.parent_class.statechart:
            self.writer.beginSuperClassConstructorCall("RuntimeClassBase")
            self.writer.addActualParameter("controller")
            self.writer.endSuperClassConstructorCall()

            self.writer.addVSpace()

            if constructor.parent_class.statechart.big_step_maximality == "take_one":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"),
                                 "big_step_maximality"),
                    GLC.Property("StatechartSemantics", "TakeOne"))
            elif constructor.parent_class.statechart.big_step_maximality == "take_many":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"),
                                 "big_step_maximality"),
                    GLC.Property("StatechartSemantics", "TakeMany"))

            if constructor.parent_class.statechart.internal_event_lifeline == "queue":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"),
                                 "internal_event_lifeline"),
                    GLC.Property("StatechartSemantics", "Queue"))
            elif constructor.parent_class.statechart.internal_event_lifeline == "next_small_step":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"),
                                 "internal_event_lifeline"),
                    GLC.Property("StatechartSemantics", "NextSmallStep"))
            elif constructor.parent_class.statechart.internal_event_lifeline == "next_combo_step":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"),
                                 "internal_event_lifeline"),
                    GLC.Property("StatechartSemantics", "NextComboStep"))

            if constructor.parent_class.statechart.input_event_lifeline == "first_small_step":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"),
                                 "input_event_lifeline"),
                    GLC.Property("StatechartSemantics", "FirstSmallStep"))
            elif constructor.parent_class.statechart.input_event_lifeline == "first_combo_step":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"),
                                 "input_event_lifeline"),
                    GLC.Property("StatechartSemantics", "FirstComboStep"))
            elif constructor.parent_class.statechart.input_event_lifeline == "whole":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"),
                                 "input_event_lifeline"),
                    GLC.Property("StatechartSemantics", "Whole"))

            if constructor.parent_class.statechart.priority == "source_parent":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"), "priority"),
                    GLC.Property("StatechartSemantics", "SourceParent"))
            elif constructor.parent_class.statechart.priority == "source_child":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"), "priority"),
                    GLC.Property("StatechartSemantics", "SourceChild"))

            if constructor.parent_class.statechart.concurrency == "single":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"), "concurrency"),
                    GLC.Property("StatechartSemantics", "Single"))
            elif constructor.parent_class.statechart.concurrency == "many":
                self.writer.addAssignment(
                    GLC.Property(GLC.SelfProperty("semantics"), "concurrency"),
                    GLC.Property("StatechartSemantics", "Many"))

        for p in constructor.parent_class.inports:
            self.writer.addAssignment(
                GLC.MapIndexedExpression(GLC.SelfProperty("inports"),
                                         GLC.String(p)),
                GLC.FunctionCall(
                    GLC.Property("controller", "addInputPort"),
                    [GLC.String(p), GLC.SelfExpression()]))

        if constructor.parent_class.attributes:
            self.writer.addVSpace()
            self.writer.addComment("User defined attributes")
            for attribute in constructor.parent_class.attributes:
                if attribute.init_value is None:
                    self.writer.addAssignment(GLC.SelfProperty(attribute.name),
                                              GLC.NoneExpression())
                else:
                    self.writer.addAssignment(GLC.SelfProperty(attribute.name),
                                              attribute.init_value)

        self.writer.addVSpace()
        self.writer.addComment("Call user defined constructor")
        self.writer.beginSuperClassMethodCall(constructor.parent_class.name,
                                              "user_defined_constructor")
        for p in constructor.getParams():
            # we can't do p.accept(self) here because 'p' is a FormalParameter
            # and we want to write it as an actual parameter
            self.writer.addActualParameter(p.getIdent())
        self.writer.endSuperClassMethodCall()
        self.writer.endMethodBody()
        self.writer.endConstructor()
Пример #5
0
    def visit_ClassDiagram(self, class_diagram):
        header = (
            "Generated by Statechart compiler by Glenn De Jonghe and Joeri Exelmans\n"
            "\n"
            "Date:   " + time.asctime() + "\n")
        if class_diagram.name or class_diagram.author or class_diagram.description:
            header += "\n"
        if class_diagram.author:
            header += "Model author: " + class_diagram.author + "\n"
        if class_diagram.name:
            header += "Model name:   " + class_diagram.name + "\n"
        if class_diagram.description.strip():
            header += "Model description:\n"
            header += class_diagram.description.strip()

        self.writer.addMultiLineComment(header)
        self.writer.addVSpace()
        self.writer.addInclude(
            ([GLC.RuntimeModuleIdentifier(), "statecharts_core"]))
        if class_diagram.top.strip():
            self.writer.addRawCode(class_diagram.top)
        self.writer.addVSpace()

        self.writer.beginPackage(class_diagram.name)

        #visit children
        for c in class_diagram.classes:
            c.accept(self)

        self.writer.beginClass("ObjectManager", ["ObjectManagerBase"])

        self.writer.beginConstructor()
        self.writer.addFormalParameter("controller")
        self.writer.beginMethodBody()
        self.writer.beginSuperClassConstructorCall("ObjectManagerBase")
        self.writer.addActualParameter("controller")
        self.writer.endSuperClassConstructorCall()
        self.writer.endMethodBody()
        self.writer.endConstructor()

        self.writer.beginMethod("instantiate")
        self.writer.addFormalParameter("class_name")
        self.writer.addFormalParameter("construct_params")
        self.writer.beginMethodBody()
        for index, c in enumerate(class_diagram.classes):
            self.writer.beginElseIf(
                GLC.EqualsExpression("class_name", GLC.String(c.name)))
            if c.isAbstract():
                # cannot instantiate abstract class
                self.writer.add(
                    GLC.ThrowExceptionStatement(
                        GLC.String("Cannot instantiate abstract class \"" +
                                   c.name +
                                   "\" with unimplemented methods \"" +
                                   "\", \"".join(c.abstract_method_names) +
                                   "\".")))
            else:
                new_expr = GLC.NewExpression(c.name,
                                             [GLC.SelfProperty("controller")])
                param_count = 0
                for p in c.constructors[0].parameters:
                    new_expr.getActualParameters().add(
                        GLC.ArrayIndexedExpression("construct_params",
                                                   str(param_count)))
                    param_count += 1
                self.writer.addAssignment(
                    GLC.LocalVariableDeclaration("instance"), new_expr)
                self.writer.addAssignment(
                    GLC.Property("instance", "associations"),
                    GLC.MapExpression())
                for a in c.associations:
                    a.accept(self)
            self.writer.endElseIf()
        self.writer.add(GLC.ReturnStatement("instance"))
        self.writer.endMethodBody()
        self.writer.endMethod()
        self.writer.endClass()  # ObjectManager

        if self.platform == Platforms.Threads:
            controller_sub_class = "ThreadsControllerBase"
        if self.platform == Platforms.EventLoop:
            controller_sub_class = "EventLoopControllerBase"
        elif self.platform == Platforms.GameLoop:
            controller_sub_class = "GameLoopControllerBase"

        self.writer.beginClass("Controller", [controller_sub_class])
        self.writer.beginConstructor()
        for p in class_diagram.default_class.constructors[0].parameters:
            p.accept(self)
        if self.platform == Platforms.EventLoop:
            self.writer.addFormalParameter("event_loop_callbacks")
            self.writer.addFormalParameter("finished_callback",
                                           GLC.NoneExpression())
        elif self.platform == Platforms.Threads:
            self.writer.addFormalParameter("keep_running",
                                           GLC.TrueExpression())
        self.writer.beginMethodBody()
        self.writer.beginSuperClassConstructorCall(controller_sub_class)
        self.writer.addActualParameter(
            GLC.NewExpression("ObjectManager", [GLC.SelfExpression()]))
        if self.platform == Platforms.EventLoop:
            self.writer.addActualParameter("event_loop_callbacks")
            self.writer.addActualParameter("finished_callback")
        elif self.platform == Platforms.Threads:
            self.writer.addActualParameter("keep_running")
        self.writer.endSuperClassConstructorCall()
        for i in class_diagram.inports:
            self.writer.add(
                GLC.FunctionCall(GLC.SelfProperty("addInputPort"),
                                 [GLC.String(i)]))
        for o in class_diagram.outports:
            self.writer.add(
                GLC.FunctionCall(GLC.SelfProperty("addOutputPort"),
                                 [GLC.String(o)]))
        actual_parameters = [
            p.getIdent()
            for p in class_diagram.default_class.constructors[0].parameters
        ]
        self.writer.add(
            GLC.FunctionCall(
                GLC.Property(GLC.SelfProperty("object_manager"),
                             "createInstance"), [
                                 GLC.String(class_diagram.default_class.name),
                                 GLC.ArrayExpression(actual_parameters)
                             ]))
        self.writer.endMethodBody()
        self.writer.endConstructor()
        self.writer.endClass()  # Controller

        # Visit test node if there is one
        if class_diagram.test:
            class_diagram.test.accept(self)

        self.writer.endPackage()
Пример #6
0
    def visit_RaiseEvent(self, raise_event):
        self.writer.startRecordingExpression()
        self.writer.begin(GLC.NewExpression("Event"))

        self.writer.addActualParameter(GLC.String(raise_event.getEventName()))
        if raise_event.isOutput():
            self.writer.addActualParameter(GLC.String(raise_event.getPort()))
        else:
            self.writer.addActualParameter(GLC.NoneExpression())

        self.writer.end()
        new_event_expr = self.writer.stopRecordingExpression()

        self.writer.startRecordingExpression()
        self.writer.beginArray()
        if raise_event.isCD():
            self.writer.add(GLC.SelfExpression())
        for param in raise_event.getParameters():
            param.accept(
                self
            )  # -> visit_Expression will cause expressions to be added to array
        self.writer.endArray()
        parameters_array_expr = self.writer.stopRecordingExpression()
        new_event_expr.getActualParameters().add(parameters_array_expr)

        if raise_event.isNarrow():
            self.writer.add(
                GLC.FunctionCall(
                    GLC.Property(GLC.SelfProperty("big_step"),
                                 "outputEventOM"),
                    [
                        GLC.NewExpression("Event", [
                            GLC.String("narrow_cast"),
                            GLC.NoneExpression(),
                            GLC.ArrayExpression([
                                GLC.SelfExpression(),
                                raise_event.getTarget(), new_event_expr
                            ])
                        ])
                    ]))
        elif raise_event.isLocal():
            self.writer.add(
                GLC.FunctionCall(GLC.SelfProperty("raiseInternalEvent"),
                                 [new_event_expr]))
        elif raise_event.isOutput():
            self.writer.add(
                GLC.FunctionCall(
                    GLC.Property(GLC.SelfProperty("big_step"), "outputEvent"),
                    [new_event_expr]))
        elif raise_event.isCD():
            self.writer.add(
                GLC.FunctionCall(
                    GLC.Property(GLC.SelfProperty("big_step"),
                                 "outputEventOM"), [new_event_expr]))
        elif raise_event.isBroad():
            self.writer.add(
                GLC.FunctionCall(
                    GLC.Property(GLC.SelfProperty("big_step"),
                                 "outputEventOM"), [
                                     GLC.NewExpression("Event", [
                                         GLC.String("broad_cast"),
                                         GLC.NoneExpression(),
                                         GLC.ArrayExpression([new_event_expr])
                                     ])
                                 ]))