Esempio n. 1
0
    def get_link(ctx, link_id, scenario_id):
        """
        Get a link using the link_id.
        optionally, scenario_id can be included if data is to be included

        Args:
            link_id (int): The link to retrieve
            scenario_id (int) (optional): Include this if you want to include data with the scenario

        Returns:
            hydra_complexmodels.Link: A link complex model, with attributes and data if requested)

        Raises:
            ResourceNotFoundError: If the link or scenario is not found

        """
        link = network.get_link(link_id, **ctx.in_header.__dict__)

        if scenario_id is not None:
            ret_link = Link(link)
            res_scens = scenario.get_resource_data('LINK', link_id,
                                                   scenario_id, None)
            rs_dict = {}
            for rs in res_scens:
                rs_dict[rs.resource_attr_id] = rs

            for ra in ret_link.attributes:
                if rs_dict.get(ra.id):
                    ra.resourcescenario = ResourceScenario(rs_dict[ra.id])

            return ret_link
        else:
            ret_link = Link(link)
            return ret_link
Esempio n. 2
0
    def get_all_group_data(ctx, network_id, scenario_id, group_ids,
                           include_metadata):
        """
        Return all the attributes for all the groups in a given network and a
        given scenario.
        Returns a list of ResourceAttr objects, each with a resourcescenario
        attribute, containing the actual value for the scenario specified.

        Args:
            network_id (int): The network to search in
            scenario_id (int): The scenario to search
            group_ids (List(int)) (optional): The specific resource groups to search for data in. If not specified, all the groups in the network will be searched.
            include_metadata: (string) ('Y' or 'N'): Default 'N'. Set to 'Y' to return metadata. This may vause a performance hit as metadata is BIG!

        Returns:
            List(ResourceAttr), each with a resourcescenario attribute, containing the actual value for the scenario specified.

        """

        group_resourcescenarios = network.get_attributes_for_resource(
            network_id, scenario_id, 'GROUP', group_ids, include_metadata)
        return_ras = []
        for grouprs in group_resourcescenarios:
            ra = ResourceAttr(grouprs.resourceattr)
            ra.resourcescenario = ResourceScenario(grouprs, ra.attr_id)
            return_ras.append(ra)

        return return_ras
Esempio n. 3
0
    def get_resourcegroup(ctx, group_id, scenario_id):
        """
        Get a resourcegroup using the group_id.
        optionally, scenario_id can be included if data is to be included

        Args:
            group_id (int): The resource group to retrieve
            scenario_id (int) (optional): Include this if you want to include data with the scenario

        Returns:
            hydra_complexmodels.ResourceGroup: A resource group complex model, with attributes and data if requested)

        Raises:
            ResourceNotFoundError: If the group or scenario is not found

        """
        group = network.get_resourcegroup(group_id, **ctx.in_header.__dict__)

        if scenario_id is not None:
            ret_group = ResourceGroup(group)
            res_scens = scenario.get_resource_data('GROUP', group_id,
                                                   scenario_id, None)
            rs_dict = {}
            for rs in res_scens:
                rs_dict[rs.resource_attr_id] = rs

            for ra in ret_group.attributes:
                if rs_dict.get(ra.id):
                    ra.resourcescenario = ResourceScenario(rs_dict[ra.id])

            return ret_group
        else:
            ret_group = ResourceGroup(group)
            return ret_group
Esempio n. 4
0
 def add_data_to_attribute(ctx, scenario_id, resource_attr_id, dataset):
     """
             Add data to a resource scenario outside of a network update
     """
     new_rs = scenario.add_data_to_attribute(scenario_id, resource_attr_id,
                                             dataset,
                                             **ctx.in_header.__dict__)
     x = ResourceScenario(new_rs)
     return x
Esempio n. 5
0
 def update_resourcedata(ctx, scenario_id, resource_scenarios):
     """
         Update the data associated with a scenario.
         Data missing from the resource scenario will not be removed
         from the scenario. Use the remove_resourcedata for this task.
     """
     res = scenario.update_resourcedata(scenario_id, resource_scenarios,
                                        **ctx.in_header.__dict__)
     ret = [ResourceScenario(r) for r in res]
     return ret
Esempio n. 6
0
    def get_resource_attribute_data(ctx, ref_key, ref_id, scenario_id,
                                    attr_id):
        """
            Get the resource scenarios for a given resource
            in a given scenario. If attr_id(s) is specified, only
            return the resource scenarios for the specified attributes.
        """
        resource_data = scenario.get_resource_attribute_data(
            ref_key, ref_id, scenario_id, attr_id, **ctx.in_header.__dict__)

        ret_data = [ResourceScenario(rs) for rs in resource_data]
        return ret_data
Esempio n. 7
0
    def update_value_from_mapping(ctx, source_resource_attr_id,
                                  target_resource_attr_id, source_scenario_id,
                                  target_scenario_id):

        updated_rs = scenario.update_value_from_mapping(
            source_resource_attr_id, target_resource_attr_id,
            source_scenario_id, target_scenario_id, **ctx.in_header.__dict__)

        if updated_rs is not None:
            return ResourceScenario(updated_rs)
        else:
            return None
Esempio n. 8
0
    def set_resourcescenario_dataset(ctx, resource_attr_id, scenario_id,
                                     dataset_id):
        """
            A short-hand way of creating a resource scenario. This function says:
            assign this datset ID to this resource attribute in this scenario.
            All the IDs must already exist.
        """

        rs = scenario.set_rs_dataset(resource_attr_id, scenario_id, dataset_id,
                                     **ctx.in_header.__dict__)

        return ResourceScenario(rs)
Esempio n. 9
0
    def get_network_data(ctx, network_id, scenario_id, type_id):
        """
            Get all the resource scenarios for a given network 
            in a given scenario. If type_id is specified, only
            return the resource scenarios for the attributes
            within the type.
        """
        network_data = scenario.get_resource_data('NETWORK', network_id,
                                                  scenario_id, type_id,
                                                  **ctx.in_header.__dict__)

        ret_data = [ResourceScenario(rs) for rs in network_data]
        return ret_data
Esempio n. 10
0
    def get_resourcegroup_data(ctx, resourcegroup_id, scenario_id, type_id):
        """
            Get all the resource scenarios for a given resourcegroup 
            in a given scenario. If type_id is specified, only
            return the resource scenarios for the attributes
            within the type.
        """
        group_data = scenario.get_resource_data('GROUP', resourcegroup_id,
                                                scenario_id, type_id,
                                                **ctx.in_header.__dict__)

        ret_data = [ResourceScenario(rs) for rs in group_data]
        return ret_data
Esempio n. 11
0
    def copy_data_from_scenario(ctx, resource_attr_ids, source_scenario_id,
                                target_scenario_id):
        """
            Copy the datasets from a source scenario into the equivalent resource scenarios
            in the target scenario. Parameters are a list of resource attribute IDS, the
            ID of the source scenario and the ID of the target scenario.
        """
        updated_resourcescenarios = scenario.copy_data_from_scenario(
            resource_attr_ids, source_scenario_id, target_scenario_id,
            **ctx.in_header.__dict__)

        ret_resourcescenarios = [
            ResourceScenario(rs) for rs in updated_resourcescenarios
        ]

        return ret_resourcescenarios
Esempio n. 12
0
    def get_resourcescenarios(ctx, resource_attr_id, scenario_id):
        """
            Get all the resource scenarios from a list of scenarios and resource scenarios
            Return a list of resource scenarios.
        """

        if not isinstance(resource_attr_id, list):
            resource_attr_id = [resource_attr_id]

        if not isinstance(scenario_id, list):
            scenario_id = [scenario_id]

        resource_scenarios = scenario.get_resourcescenarios(
            resource_attr_id, scenario_id, **ctx.in_header.__dict__)

        rs_cms = []
        for rs in resource_scenarios:
            rs_cms.append(ResourceScenario(rs))

        return rs_cms
Esempio n. 13
0
    def get_attribute_datasets(ctx, attr_id, scenario_id):
        """
            Get all the datasets from resource attributes with the given attribute
            ID in the given scenario.

            Return a list of resource attributes with their associated
            resource scenarios (and values).
        """
        resource_attrs = scenario.get_attribute_datasests(
            attr_id, scenario_id, **ctx.in_header.__dict__)

        ra_cms = []
        for ra in resource_attrs:
            res_attr_cm = ResourceAttr(ra)
            for rs in ra.resourcescenarios:
                if rs.scenario_id == scenario_id:
                    res_attr_cm.resourcescenario = ResourceScenario(rs)
            ra_cms.append(res_attr_cm)

        return ra_cms
Esempio n. 14
0
    def get_node_attribute_data(ctx, node_ids, attr_ids):
        """
            Get the data for multiple attributes on multiple nodes
            across multiple scenarios.
            @returns a list of AttributeData objects, which consist of a list
            of ResourceAttribute objects and a list of corresponding
            ResourceScenario objects.
        """

        node_attrs, resource_scenarios = scenario.get_attribute_data(
            attr_ids, node_ids, **ctx.in_header.__dict__)

        node_ras = [ResourceAttr(na) for na in node_attrs]
        node_rs = [ResourceScenario(rs) for rs in resource_scenarios]

        ret_obj = AttributeData()
        ret_obj.resourceattrs = node_ras
        ret_obj.resourcescenarios = node_rs

        return ret_obj
Esempio n. 15
0
    def get_all_link_data(ctx, network_id, scenario_id, link_ids,
                          include_metadata):
        """
        Return all the attributes for all the links in a given network and a
        given scenario.
        Returns a list of ResourceAttr objects, each with a resourcescenario
        attribute, containing the actual value for the scenario specified.

        Args:
            network_id (int): The network to search in
            scenario_id (int): The scenario to search
            link_ids (List(int)) (optional): The specific links to search for data in. If not specified, all the links in the network will be searched.
            include_metadata: (string) ('Y' or 'N'): Default 'N'. Set to 'Y' to return metadata. This may vause a performance hit as metadata is BIG!

        Returns:
            List(ResourceAttr), each with a resourcescenario attribute, containing the actual value for the scenario specified.

        Raises:
            ResourceNotFoundError: If the network or scenario are not found


        """
        start = datetime.datetime.now()

        link_resourcescenarios = network.get_attributes_for_resource(
            network_id, scenario_id, 'LINK', link_ids, include_metadata)

        log.info("Qry done in %s", (datetime.datetime.now() - start))
        start = datetime.datetime.now()

        return_ras = []
        for linkrs in link_resourcescenarios:
            ra = ResourceAttr(linkrs.resourceattr)
            ra.resourcescenario = ResourceScenario(linkrs, ra.attr_id)
            return_ras.append(ra)

        log.info("Return vals built in %s", (datetime.datetime.now() - start))

        return return_ras
Esempio n. 16
0
    def get_nodes(ctx, node_ids, scenario_id):
        """
        Get nodes using the node_ids.
        optionally, scenario_id can be included if data is to be included

        Args:
            node_id (int): The node to retrieve
            scenario_id (int) (optional): Include this if you want to include data with the scenario

        Returns:
            hydra_complexmodels.Node: A node complex model, with attributes and data if requested)

        Raises:
            ResourceNotFoundError: If the node or scenario is not found

        """
        nodes = network.get_nodes(node_ids, **ctx.in_header.__dict__)
        ret_nodes = []
        if scenario_id is not None:
            for node in nodes:
                ret_node = Node(node)

                res_scens = scenario.get_resource_data('NODE', node.node_id,
                                                       scenario_id, None)

                rs_dict = {}
                for rs in res_scens:
                    rs_dict[rs.resource_attr_id] = rs

                for ra in ret_node.attributes:
                    if rs_dict.get(ra.id):
                        ra.resourcescenario = ResourceScenario(rs_dict[ra.id])
                ret_nodes.append(ret_node)

        else:
            ret_nodes = [Node(node) for node in nodes]

        return ret_nodes
Esempio n. 17
0
    def get_resource_attribute_datasets(ctx, resource_attr_id, scenario_id):
        """
            Get all the datasets from resource attributes with the given resource attribute
            IDs in the given scenarios.
        """

        if not isinstance(resource_attr_id, list):
            resource_attr_id = [resource_attr_id]

        if not isinstance(scenario_id, list):
            scenario_id = [scenario_id]

        ra_cms = []
        resource_attrs = scenario.get_resource_attribute_datasets(
            resource_attr_id, scenario_id, **ctx.in_header.__dict__)

        for ra in resource_attrs:
            res_attr_cm = ResourceAttr(ra)
            for rs in ra.resourcescenarios:
                if rs.scenario_id in scenario_id:
                    res_attr_cm.resourcescenario = ResourceScenario(rs)
            ra_cms.append(res_attr_cm)

        return ra_cms