Example #1
0
    def init_parser(self):
        """Initializes the AST code parser.

        Returns:
            None
        """
        self.parser = UppaalCLanguageParser(semantics=UppaalCLanguageSemantics())
Example #2
0
    def __init__(self):
        """Initializes UppaalSimulator."""
        self.init_system_state = None
        self.system_state = None
        self.transitions = None
        self.recent_transition = None

        self.transition_trace = []
        self.dbm_op_sequence = DBMOperationSequence()
        self.transition_counts = {
            "potential": None,
            "enabled": None,
            "valid": None
        }

        self.system = None

        self.c_language_parser = UppaalCLanguageParser(
            semantics=UppaalCLanguageSemantics())
        self.c_evaluator = UppaalCEvaluator(do_log_details=False)
Example #3
0
def uppaal_dict_to_system(system_data):
    """Transforms the data dictionary of a Uppaal system into a system object.

    Args:
        system_data: The Uppaal system data dictionary.

    Returns:
        The Uppaal system object.
    """

    system = nta.System()
    uppaal_c_parser = UppaalCLanguageParser(
        semantics=UppaalCLanguageSemantics())

    system.set_declaration(system_data["global_declaration"])
    system.set_system_declaration(system_data["system_declaration"])

    ###################
    # Parse templates #
    ###################
    for template_data in system_data["templates"]:
        id_ = template_data["id"] if ("id" in template_data) else None
        name = template_data["name"] if ("name" in template_data) else ""

        template = system.new_template(name, id_)

        if template_data["parameters"] != "":
            parameter_asts = uppaal_c_parser.parse(template_data["parameters"],
                                                   rule_name='Parameters')
            for parameter_ast in parameter_asts:
                template.new_parameter(parameter_ast)

        template.set_declaration(template_data["declaration"])

        # Clock check function
        template_scope_clocks = system.declaration.clocks + template.declaration.clocks

        def get_clocks(ast,
                       acc):  # TODO: Handle clocks among template parameters
            """Adds the ast to acc if it is a clock variable.

            Args:
                ast: The AST dict.
                acc: A list of values accumulated during search.

            Returns:
                The original AST dict.
            """
            if ast["astType"] == "Variable":
                if ast["name"] in template_scope_clocks:
                    acc.append(ast["name"])
            return ast

        ###################
        # Parse locations #
        ###################
        for location_data in template_data["locations"]:
            id_ = location_data["id"] if ("id" in location_data) else ""
            name = location_data["name"] if ("name" in location_data) else ""

            location = template.new_location(name, id_)
            location.view["self"] = {"pos": location_data["pos"].copy()}

            if location_data["name_label"]:
                location.view["name_label"] = location_data["name_label"].copy(
                )

            if location_data["invariant"]:
                invariants = uppaal_c_parser.parse(location_data["invariant"],
                                                   rule_name='Invariants')
                for inv in invariants:
                    location.new_invariant(inv)
            if location_data["invariant_label"]:
                location.view["invariant_label"] = location_data[
                    "invariant_label"].copy()

            if location_data["urgent"]:
                location.set_urgent(True)

            if location_data["committed"]:
                location.set_committed(True)

        # Parse initial location
        template.set_init_location_by_id(template_data["init_loc_id"])

        ###############
        # Parse edges #
        ###############
        for edge_data in template_data["edges"]:
            id_ = edge_data["id"] if ("id" in edge_data) else ""
            source_loc_id = edge_data["source_loc_id"]
            target_loc_id = edge_data["target_loc_id"]

            edge = template.new_edge_by_loc_ids(source_loc_id, target_loc_id,
                                                id_)

            # Add guards
            if edge_data["guard"]:
                guards = uppaal_c_parser.parse(edge_data["guard"],
                                               rule_name='Guards')
                for guard in guards:
                    if len(apply_func_to_ast(guard, get_clocks)[1]) > 0:
                        edge.new_clock_guard(guard)
                    else:
                        edge.new_variable_guard(guard)
            if edge_data["guard_label"]:
                edge.view["guard_label"] = edge_data["guard_label"].copy()

            # Add updates
            if edge_data["update"]:
                updates = uppaal_c_parser.parse(edge_data["update"],
                                                rule_name='Updates')
                for update in updates:
                    if len(apply_func_to_ast(update, get_clocks)[1]) > 0:
                        edge.new_reset(update)
                    else:
                        edge.new_update(update)
            if edge_data["update_label"]:
                edge.view["update_label"] = edge_data["update_label"].copy()

            # Add synchronization
            if edge_data["synchronisation"]:
                edge.set_sync(edge_data["synchronisation"])
            if edge_data["sync_label"]:
                edge.view["sync_label"] = edge_data["sync_label"].copy()

            # Add selects
            if edge_data["select"]:
                edge.new_select(edge_data["select"])
            if edge_data["select_label"]:
                edge.view["select_label"] = edge_data["select_label"].copy()

            edge.view["nails"] = deepcopy(edge_data["nails"])

    #################
    # Parse queries #
    #################
    for query_data in system_data["queries"]:
        formula = query_data["formula"]
        comment = query_data["comment"]
        query = Query(formula, comment)
        system.add_query(query)

    # # print(json.dumps(system, indent=2))
    return system
Example #4
0
def parser():
    return UppaalCLanguageParser(semantics=UppaalCLanguageSemantics())