def _get_referenced_models(self, rules):
        """
        Reduces the number of queries to be made to the DB by creating sets of Actions, Triggers
        and TriggerTypes.
        """
        action_refs = set()
        trigger_refs = set()
        trigger_type_refs = set()

        for rule in rules:
            action_ref = rule.get("action", {}).get("ref", None)
            trigger_ref = rule.get("trigger", {}).get("ref", None)
            trigger_type_ref = rule.get("trigger", {}).get("type", None)

            if action_ref:
                action_refs.add(action_ref)

            if trigger_ref:
                trigger_refs.add(trigger_ref)

            if trigger_type_ref:
                trigger_type_refs.add(trigger_type_ref)

        action_by_refs = {}
        trigger_by_refs = {}
        trigger_type_by_refs = {}

        # The functions that will return args that can used to query.
        def ref_query_args(ref):
            return {"ref": ref}

        def name_pack_query_args(ref):
            resource_ref = ResourceReference.from_string_reference(ref=ref)
            return {"name": resource_ref.name, "pack": resource_ref.pack}

        action_dbs = self._get_entities(
            model_persistence=Action, refs=action_refs, query_args=ref_query_args
        )
        for action_db in action_dbs:
            action_by_refs[action_db.ref] = action_db

        trigger_dbs = self._get_entities(
            model_persistence=Trigger,
            refs=trigger_refs,
            query_args=name_pack_query_args,
        )
        for trigger_db in trigger_dbs:
            trigger_by_refs[trigger_db.get_reference().ref] = trigger_db

        trigger_type_dbs = self._get_entities(
            model_persistence=TriggerType,
            refs=trigger_type_refs,
            query_args=name_pack_query_args,
        )
        for trigger_type_db in trigger_type_dbs:
            trigger_type_by_refs[trigger_type_db.get_reference().ref] = trigger_type_db

        return (action_by_refs, trigger_by_refs, trigger_type_by_refs)

        Reduces the number of queries to be made to the DB by creating sets of Actions, Triggers
        and TriggerTypes.
        
st2api/st2api/controllers/v1/rule_views.py