示例#1
0
 def process_json(self):
     """
     Processing input JSON file
     :return:
     """
     if self.json_type == JSONTypes.SOLVE.value:
         self._run_all_tasks()
         if self.app_session.results_path is not None:
             # try to create directory
             dir_path = os.path.abspath(self.app_session.results_path)
             terminal.show_info_message(
                 f"Trying to save key results to: {dir_path}")
             try:
                 os.makedirs(dir_path)
             except FileExistsError:
                 terminal.show_warning_message("Folder already exists")
             # collect values
             terminal.show_info_message("Collecting results...")
             data = self._collect_values()
             # save data
             terminal.show_info_message("Saving results...")
             file = os.path.join(dir_path, "Results.json")
             JSONDataManager.dump_data(data, file)
     elif self.json_type == JSONTypes.UPDATE_TARGETS.value:
         self._change_targets()
     else:
         terminal.show_warning_message(
             f"Cannot process JSON with behaviour: {self.json_type}")
示例#2
0
    def handle_response_to_upload_submodel_request(self):
        """
        Handles response to upload submodel request
        :return: dict with keys `to_insert` and `to_delete`, or None, of some error occurred
        """
        response_json = self.__response.json()
        if response_json and isinstance(response_json, dict):
            result = {"to_insert": [], "to_delete": []}

            status = response_json.get("status")
            # if status is `success` get ids of uploaded submodels from set
            if status == "success":
                submodels = response_json.get("set")
                if submodels and isinstance(submodels, list):
                    if len(submodels) > 0:
                        if isinstance(submodels[0], dict):
                            submodel_id = submodels[0].get("id")
                            result["to_insert"].append(submodel_id)
                            return result
            # if status is `warning` get ids of original submodels from duplicates
            # assuming that we uploading a single file every time
            if status == "warning":
                duplicates = response_json.get("duplicates")
                if duplicates and isinstance(duplicates, list):
                    terminal.show_warning_message(
                        "There are duplicate submodels on server!")
                    if len(duplicates) > 0:
                        if isinstance(duplicates[0], dict):
                            created_object = duplicates[0].get("createdObject")
                            if created_object and isinstance(
                                    created_object, dict):
                                created_object_id = created_object.get("id")
                                result["to_delete"].append(created_object_id)
                            duplicate_objects = duplicates[0].get(
                                "duplicateObjects")
                            if duplicate_objects and isinstance(
                                    duplicate_objects, list):
                                if len(duplicate_objects) > 0:
                                    original_object = duplicate_objects[0]
                                    if isinstance(original_object, dict):
                                        original_object_id = original_object.get(
                                            "id")
                                        result["to_insert"].append(
                                            original_object_id)
                                        return result
        terminal.show_error_message(
            "There were some errors during uploading new submodel!")
        return None
示例#3
0
 def handle_response_to_loadcase_simulations_request(self):
     response_json = self.__response.json()
     if response_json and isinstance(response_json, dict):
         list_of_simulations = []
         content = response_json.get("content")
         if content:
             for item in content:
                 if item and isinstance(item, dict):
                     simulation_id = item.get("id")
                     if simulation_id:
                         list_of_simulations.append(simulation_id)
             return list_of_simulations
         terminal.show_warning_message("No simulations read from loadcase!")
         return []
     terminal.show_error_message(
         "There were some errors during reading loadcase simulations!")
     return None
示例#4
0
 def handle_response_to_simulation_tasks_request(self):
     """
     Handles response to simulation tasks request
     :return: list containing IDs of simulation tasks, or None, if some error occurred
     """
     response_json = self.__response.json()
     if response_json and isinstance(response_json, dict):
         list_of_tasks = []
         content = response_json.get("content")
         if content:
             for item in content:
                 if item and isinstance(item, dict):
                     task_id = item.get("id")
                     if task_id:
                         list_of_tasks.append(task_id)
             return list_of_tasks
         terminal.show_warning_message("No tasks read from simulation!")
         return []
     terminal.show_error_message(
         "There were some errors during reading simulation tasks!")
     return None
示例#5
0
 def handle_response_to_simulation_submodels_request(self):
     """
     Handles response to simulation submodels request
     :return: list containing IDs of simulation submodels, or None, if some error occurred
     """
     response_json = self.__response.json()
     list_of_submodels = []
     if response_json and isinstance(response_json, list):
         for item in response_json:
             if item and isinstance(item, dict):
                 item_id = item.get("id")
                 if item_id:
                     list_of_submodels.append(item_id)
         return list_of_submodels
     # for the case of empty response
     response_status = self.__response.status_code
     if response_status == 200:
         terminal.show_warning_message("Simulation has no submodels!")
         return list_of_submodels
     terminal.show_error_message(
         "There were some errors during reading simulation submodels!")
     return None
示例#6
0
    def download_files(self, *files):
        """
        Download chosen files to local directory
        :param files: files to be downloaded
        :return: return list of successfully downloaded files
        """

        list_of_downloaded_files = []
        simulation_files = self.get_files()
        list_of_simulation_file_ids = [
            item.get("id") for item in simulation_files
        ]
        list_of_simulation_file_names = [
            item.get("name") for item in simulation_files
        ]

        for file in files:
            # check if fileName is in simulation files
            if file in list_of_simulation_file_names:
                file_id = list_of_simulation_file_ids[
                    list_of_simulation_file_names.index(file)]

                response = self._sender.send_download_file_request(
                    self.identifier, file_id)
                Timeout.hold_your_horses()
                self._handler.set_response(response)
                file_content = self._handler.handle_response_to_download_file_request(
                )
                if file_content and isinstance(file_content, bytes):
                    with open(self._app_session.cfg.local_storage + "\\" +
                              file,
                              mode="wb") as f:
                        f.write(file_content)
                    list_of_downloaded_files.append(file)
            else:
                terminal.show_warning_message(
                    "Selected file \"{}\" not in simulation files".format(
                        file))
        return list_of_downloaded_files
示例#7
0
 def handle_response_to_loadcase_targets_request(self):
     """
     Handles response to loadcase targets request
     :return: list of dictionaries with keys:
              - `id`
              - `name`
              - `value`
              - `condition`
              - `dimension`
              containing information of loadcase targets, or None, if some error occurred
     """
     response_json = self.__response.json()
     if response_json and isinstance(response_json, dict):
         targets_data = []
         content = response_json.get("content")
         if content:
             for item in content:
                 if item and isinstance(item, dict):
                     target_id = item.get("id")
                     target_name = item.get("name")
                     target_value = item.get("value")
                     target_condition = item.get("conditionId")
                     target_dimension = item.get("dimension")
                     if target_id:
                         targets_data.append({
                             "id": target_id,
                             "name": target_name,
                             "value": target_value,
                             "condition": target_condition,
                             "dimension": target_dimension
                         })
             return targets_data
         terminal.show_warning_message("No targets read from loadcase!")
         return []
     terminal.show_error_message(
         "There were some errors during reading loadcase targets!")
     return None
示例#8
0
        def status_based_behaviour(vertex):
            """
            Define main loop behaviour while walking through vertex basing on vertex status
            :param vertex: vertex in workflow graph
            :return terminate_loop: magic integer value:
                                    -1: error occurred and main loop shall be stopped
                                     0: current simulation is not done yet, continue
                                     1: current simulation is done
            """
            assert isinstance(vertex, Vertex)
            terminal.show_info_message("Processing vertex with ID: {}",
                                       vertex.identifier)

            # if status is "New",
            #   - clone base simulation
            #   - upload submodels
            #   - run cloned (current vertex) simulation
            #   - update vertex status from simulation task status
            if vertex.status == "New":
                terminal.show_info_message("Vertex status: {}", vertex.status)
                terminal.show_info_message("Vertex base simulation ID: {}",
                                           vertex.base_simulation.identifier)
                base_simulation = vertex.base_simulation
                terminal.show_info_message(
                    "Trying to clone base simulation...")
                current_simulation = base_simulation.clone()
                terminal.show_info_message(
                    "Modify current simulation description...")
                current_simulation.set_description(vertex.description)
                terminal.show_info_message(
                    "Update vertex current simulation...")
                vertex.current_simulation = current_simulation
                if current_simulation:
                    # if cloned successfully, upload submodels
                    terminal.show_info_message(
                        "Cloned simulation ID: {}",
                        vertex.current_simulation.identifier)
                    terminal.show_info_message(
                        "Uploading submodels for current simulation...")
                    stype = vertex.stype
                    uploaded_submodels = stype.upload_submodel(
                        *vertex.submodels)
                    # uploaded_submodels_ids = [submodel.identifier for submodel in uploaded_submodels]
                    terminal.show_info_message(
                        "Erasing current (cloned) simulation submodels...")
                    status = current_simulation.erase_submodels()
                    if status:
                        terminal.show_info_message("Done")
                    else:
                        terminal.show_error_message("Failed")
                    _ = current_simulation.add_submodels(*uploaded_submodels)
                    terminal.show_info_message(
                        "{} submodels added for current simulations",
                        len(uploaded_submodels))
                    # start with default parameters
                    terminal.show_info_message(
                        "Trying to run current simulation...")
                    # obtain default parameters to run tasks from base simulation
                    current_task = current_simulation.run(
                        bsi=base_simulation.identifier)
                    vertex.current_task = current_task
                    if current_task:
                        # if task created successfully, get status
                        terminal.show_info_message(
                            f"Created task ID: {vertex.current_task.identifier}"
                        )
                        vertex.status = current_task.get_status()
                        return 0
                    terminal.show_error_message("Task has not been created.")
                    return -1
                terminal.show_error_message("Simulation has not been cloned.")
                return -1

            # if status is "Finished",
            #   - download vertex results
            #   - save status; when all vertices will have the same status, loop can be stopped
            elif vertex.status == "Finished":
                terminal.show_info_message("Vertex status: {}", vertex.status)
                if len(vertex.results) == 0:
                    terminal.show_info_message(
                        "No results selected for download")
                else:
                    terminal.show_info_message("Downloading results...")
                    current_simulation = vertex.current_simulation
                    lst = current_simulation.download_files(*vertex.results)
                    terminal.show_info_message(
                        "Successfully downloaded {} files", len(lst))
                return 1

            # if status is "Failed",
            #   - terminate main loop
            elif vertex.status in ["Failed", "failed", "Error", "error"]:
                terminal.show_warning_message("Vertex status: {}",
                                              vertex.status)
                return -1

            # if status is unknown,
            #   - update vertex status from simulation task status
            else:
                terminal.show_info_message("Updating vertex status...")
                current_task = vertex.current_task
                if current_task:
                    current_status = current_task.get_status()
                    vertex.status = current_status
                    task_end_waiting, task_end_solving = current_task.get_time_estimation(
                    )
                    terminal.show_info_message(
                        "Current task estimated end waiting time: {}",
                        task_end_waiting)
                    terminal.show_info_message(
                        "Current task estimated end solving time: {}",
                        task_end_solving)
                terminal.show_info_message("Vertex status: {}", vertex.status)
                return 0
示例#9
0
    def upload_submodel(self, *files, **params):
        """
        :param files: paths to files to be uploaded into current s|type
        :param params: response parameters;
                       `stype` - ID of s|type for uploading submodels; optional; default is current s|type
                       `add_to_clipboard` - optional boolean parameter; default is False
        :return: list of uploaded submodels
        """
        # FIXME wtf??? create instance of SubmodelType inside its method
        if "stype" in params.keys():
            stype = SubmodelType(self._app_session, params.get("stype"))
        else:
            # stype = SubmodelType(self._app_session, self._app_session.cfg.server_storage)
            stype = self

        if "add_to_clipboard" in params.keys():
            add_to_clipboard = "on" if bool(
                params.get("add_to_clipboard")) else "off"
        else:
            add_to_clipboard = "off"

        submodels = []
        for file in files:
            response = self._sender.send_upload_submodel_request(
                file, stype.tree_id, add_to_clipboard)
            Timeout.hold_your_horses()
            self._handler.set_response(response)
            result = self._handler.handle_response_to_upload_submodel_request()
            if result is not None:
                submodel_ids_to_delete = result["to_delete"]
                submodel_ids_for_simulation = result["to_insert"]

                if len(submodel_ids_to_delete) == 0:
                    terminal.show_info_message(
                        "Uploaded submodel id to use in simulation: {}",
                        submodel_ids_for_simulation[0])
                    submodels.append(
                        Submodel(self._app_session,
                                 submodel_ids_for_simulation[0]))
                else:
                    terminal.show_warning_message(
                        "Uploaded submodel duplicates already existing submodel"
                    )
                    terminal.show_warning_message(
                        "Created submodel with id {} will be deleted",
                        submodel_ids_to_delete[0])
                    response = self._sender.send_delete_submodel_from_server_request(
                        submodel_ids_to_delete[0])
                    Timeout.hold_your_horses()
                    self._handler.set_response(response)
                    _ = self._handler.handle_response_to_delete_submodel_from_server_request(
                    )
                    terminal.show_warning_message("Duplicate was deleted")
                    terminal.show_info_message(
                        "Already existing submodel id to use in simulation: {}",
                        submodel_ids_for_simulation[0])
                    submodels.append(
                        Submodel(self._app_session,
                                 submodel_ids_for_simulation[0]))

        return submodels
示例#10
0
    def add_target(self,
                   name,
                   value,
                   condition,
                   dimension,
                   tolerance=None,
                   description=None):
        """
        Adds new target to loadcase.
        If target with same name already exists, deletes old target and adds new.
        :param name: target name
        :param value: target value
        :param condition: target condition: 1 - >, 2 - <, 3 - +/-
        :param dimension: target dimension
        :param tolerance: tolerance for condition
        :param description: description for target
        :return: target object
        """

        if condition == 1:
            condition_name = ">"
        elif condition == 2:
            condition_name = "<"
        elif condition == 3:
            condition_name = "+/-"
        else:
            terminal.show_error_message(
                "Unsupported condition for new target: {}".format(condition))
            return None

        if condition != 3:
            tolerance = None

        if tolerance is None:
            has_tolerance = False
        else:
            has_tolerance = True

        payload = {
            "conditionId": condition,
            "conditionName": condition_name,
            "description": description,
            "dimension": dimension,
            "hasTolerance": has_tolerance,
            "hierarchy": {
                "id": self.identifier,
                "objectType": {
                    "displayName": "Loadcase",
                    "iconSkin": "icon-loadcase",
                    "isLeaf": False,
                    "name": "loadcase",
                    "subType": None,
                    "tooltip": "Loadcase"
                },
                "parent": None
            },
            "name": name,
            "objectType": {
                "displayName": "Target value",
                "name": "targetValue",
                "subType": None,
                "tooltip": "Target value"
            },
            "tolerance": tolerance,
            "value": value
        }
        response = self._sender.send_add_loadcase_target_request(
            self.identifier, payload)
        Timeout.hold_your_horses()

        # if no target with that name exists, create new
        # else, need to find ID of target by name
        # delete it
        # and create new
        if response.status_code == 400:
            terminal.show_warning_message(response.json().get("message"))
            existing_targets = self.get_targets()
            for t in existing_targets:
                if t.name == name:
                    tid = self.delete_target(t)
                    if tid is not None:
                        terminal.show_info_message(
                            "Old target successfully removed")
                    else:
                        terminal.show_error_message(
                            "Failed removing old target")
                        return None
                    break
            response = self._sender.send_add_loadcase_target_request(
                self.identifier, payload)

        if response.status_code != 200:
            terminal.show_error_message("Failed adding new target")
            terminal.show_error_message(f"Response: {response.status_code}")
            return None

        terminal.show_info_message("Adding new target...")
        self._handler.set_response(response)
        target_data = self._handler.handle_response_to_add_loadcase_target_request(
        )

        if target_data:
            return Target(target_data)
        return None