def find_next_template(self, state, rl, previousTemplates, interactive):
        """ Find a template according to current state of
        the model and available transitions. If a fuzzer is active
        select only templates with fields. If none of them has fields,
        pick a template as usually.
        """
        # Throw KeyError if state has not id list (END state is reached)
        try:
            id_list = self.state_id[state[0]]
        except KeyError:
            return None
        # Find templates with valid rule transitions and select
        # with a similar distribution to the templates ocurrence probability
        templates_with_rules = []
        for template_id in id_list:
            new_template = self.template_dic[template_id]
            if (not new_template.has_fields()
                    or rl.transition_exists(previousTemplates, new_template)):
                templates_with_rules.append(new_template)
        if len(templates_with_rules) > 0:
            # if a fuzzer is active, choose those templates that
            # has fields. If all are completed or the fuzzer is
            # inactive, pick from all of them as normal.
            fuzz_templates = [t for t in templates_with_rules if t.has_fields()]
            if fuzz_templates:
                templates = fuzz_templates
            else:
                templates = templates_with_rules
            
            templates_ids = [t.ID for t in templates]
            if interactive:
                choice = ""
                while choice not in templates_ids:
                    print "Manually selecting from templates: {}".format(templates_ids)
                    if len(templates_ids) == 1:
                        choice = templates_ids[0]
                    else:
                        choice = raw_input('Selection: ')
                ind = templates_ids.index(choice)
            else:
                print "Probability-based selection from templates: {}".format(templates_ids)
                ind = pickIndex([t.count for t in templates])
            return templates[ind]
        else:
            return None
dataset/ETHPy150Open hgascon/pulsar/pulsar/core/lens.py/TemplateList.find_next_template/original