# Path: lightbulb/core/utils/common.py
# def save_model(save, model):
#     if not isinstance(save, basestring):
#         return
#     if save == "REGEX":
#         regex = model.to_regex()
#         return "Regular Expression", regex
#     elif save == "STRING":
#         string = model.shortest_string()
#         return "Shortest String", string
#     elif save.lower() in ['true', '1', 't', 'y', 'yes']:
#         home = expanduser("~")
#         timestr = time.strftime("%Y%m%d-%H%M%S")
#         if not os.path.exists(home+"/.LightBulb/"):
#             os.makedirs(home+"/.LightBulb/")
#         if not os.path.exists(home+"/.LightBulb/models"):
#             os.makedirs(home+"/.LightBulb/models")
#         model.save(home+"/.LightBulb/models/"+timestr+".fst")
#         ruleset_file_meta = open(home+"/.LightBulb/models/"+timestr+".py", "w")
#         ruleset_file_meta.write(
#             "META = {\n\
#                     'author': '"+getpass.getuser()+"',\n\
#                     'description': 'Saved model on "+timestr+"',\n\
#                     'type':'FST',\n\
#                     'comments': []\n\
#             }")
#         ruleset_file_meta.close()
#         return "Saved Model", home+"/.LightBulb/models/"+timestr+".fst"
#     elif save.lower() in ['False', '2', 'f', 'n', 'no']:
#         return
#     else:
#         model.save(save)
#         return "Saved Model", save
# 
# def findlibrary(path):
#     path = re.sub('.*my_saved_models/', expanduser("~") + "/.LightBulb/models/", path)
#     path = re.sub('.*my_saved_regex/', expanduser("~") + "/.LightBulb/regex/", path)
#     path = re.sub('.*my_saved_trees/', expanduser("~") + "/.LightBulb/trees/", path)
#     path = re.sub('.*my_saved_grammars/', expanduser("~") + "/.LightBulb/grammars/", path)
#     path = path.replace('{library}', imp.find_module('lightbulb')[1]+"/data/")
#     return path


from symautomata.dfa import DFA
from symautomata.flex2fst import Flexparser
from symautomata.alphabet import createalphabet
from lightbulb.core.utils.common import save_model, findlibrary
import json
        self.operation = configuration['OPERATION']
        self.result = configuration['RESULT']
        self.alphabet = createalphabet(configuration['ALPHABET'])
        pass

    def read_file(self, file, file_type):
        if file is not None and file_type is not None:
            if file_type == "FLEX":
                flex_object_m = Flexparser(self.alphabet)
                mma = flex_object_m.yyparse(file)
                mma.minimize()
            elif file_type == "FST":
                mma = DFA(self.alphabet)
                mma.load(file)
                mma.minimize()
            return mma
        return None

    def learn(self):
        if self.operation == "COMPLEMENT":
            self.mma.complement(self.alphabet)
            self.mmc = self.mma
        elif self.operation == "INTERSECT" and self.mmb != None:
            self.mmc = self.mma & self.mmb
        elif self.operation == "UNION" and self.mmb != None:
            self.mmc = self.mma | self.mmb
            self.mmc = self.mmc.determinize()
        elif self.operation == "DIFFERENCE" and self.mmb != None:
            self.mmc = self.mma.diff(self.mmb)
        else:

# Path: lightbulb/core/utils/common.py
# def save_model(save, model):
#     if not isinstance(save, basestring):
#         return
#     if save == "REGEX":
#         regex = model.to_regex()
#         return "Regular Expression", regex
#     elif save == "STRING":
#         string = model.shortest_string()
#         return "Shortest String", string
#     elif save.lower() in ['true', '1', 't', 'y', 'yes']:
#         home = expanduser("~")
#         timestr = time.strftime("%Y%m%d-%H%M%S")
#         if not os.path.exists(home+"/.LightBulb/"):
#             os.makedirs(home+"/.LightBulb/")
#         if not os.path.exists(home+"/.LightBulb/models"):
#             os.makedirs(home+"/.LightBulb/models")
#         model.save(home+"/.LightBulb/models/"+timestr+".fst")
#         ruleset_file_meta = open(home+"/.LightBulb/models/"+timestr+".py", "w")
#         ruleset_file_meta.write(
#             "META = {\n\
#                     'author': '"+getpass.getuser()+"',\n\
#                     'description': 'Saved model on "+timestr+"',\n\
#                     'type':'FST',\n\
#                     'comments': []\n\
#             }")
#         ruleset_file_meta.close()
#         return "Saved Model", home+"/.LightBulb/models/"+timestr+".fst"
#     elif save.lower() in ['False', '2', 'f', 'n', 'no']:
#         return
#     else:
#         model.save(save)
#         return "Saved Model", save
# 
# def findlibrary(path):
#     path = re.sub('.*my_saved_models/', expanduser("~") + "/.LightBulb/models/", path)
#     path = re.sub('.*my_saved_regex/', expanduser("~") + "/.LightBulb/regex/", path)
#     path = re.sub('.*my_saved_trees/', expanduser("~") + "/.LightBulb/trees/", path)
#     path = re.sub('.*my_saved_grammars/', expanduser("~") + "/.LightBulb/grammars/", path)
#     path = path.replace('{library}', imp.find_module('lightbulb')[1]+"/data/")
#     return path


# Path: lightbulb/modules/models_op.py
from symautomata.dfa import DFA
from symautomata.flex2fst import Flexparser
from symautomata.alphabet import createalphabet
from lightbulb.core.utils.common import save_model, findlibrary
import json

        self.operation = configuration['OPERATION']
        self.result = configuration['RESULT']
        self.alphabet = createalphabet(configuration['ALPHABET'])
        pass

    def read_file(self, file, file_type):
        if file is not None and file_type is not None:
            if file_type == "FLEX":
                flex_object_m = Flexparser(self.alphabet)
                mma = flex_object_m.yyparse(file)
                mma.minimize()
            elif file_type == "FST":
                mma = DFA(self.alphabet)
                mma.load(file)
                mma.minimize()
            return mma
        return None

    def learn(self):
        if self.operation == "COMPLEMENT":
            self.mma.complement(self.alphabet)
            self.mmc = self.mma
        elif self.operation == "INTERSECT" and self.mmb != None:
            self.mmc = self.mma & self.mmb
        elif self.operation == "UNION" and self.mmb != None:
            self.mmc = self.mma | self.mmb
            self.mmc = self.mmc.determinize()
        elif self.operation == "DIFFERENCE" and self.mmb != None:
            self.mmc = self.mma.diff(self.mmb)
        else:

            self.mmc = self.mma