# Path: PW_explorer/helper.py
# def mkdir_p(path):
#     """Make a directory if it doesn't already exist"""
#     try:
#         os.makedirs(path)
#     except OSError as exc:  # Python >2.5
#         if exc.errno == errno.EEXIST and os.path.isdir(path):
#             pass
#         else:
#             raise
# 
# def turn_list_into_str(l):
# 
#     if isinstance(l, (list,set,frozenset)):
#         if len(l) > 1:
#             l_ = [turn_list_into_str(l1) for l1 in l[1:]]
#             return "{}({})".format(l[0], ",".join(l_))
#         elif len(l) == 1:
#             return "{}".format(l[0])
#         else:
#             return ""
#     else:
#         return l
# 
# Path: PW_explorer/Input_Parsers/Meta_Data_Parser/meta_data_parser.py
# ASP_COMMENT_SYMBOL = '%'
# 
# ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD = 'schema'


import pandas as pd
import os
import sqlite3
from .helper import (
    mkdir_p,
    turn_list_into_str,
)
from .Input_Parsers.Meta_Data_Parser.meta_data_parser import (
    ASP_COMMENT_SYMBOL,
    ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD,
)
        # output the pws as rel_name(pw_id, attrs....) and optimization(pw_id, optimized_value)
        pw_rel_facts = []
        for pw in pws:
            if pw.pw_soln is not None:
                if include_pw_ids:
                    pw_rel_facts.append('optimization({}, {}).'.format(pw.pw_id, pw.pw_soln))
                else:
                    pw_rel_facts.append('optimization({}).'.format(pw.pw_soln))
            for rl_name, rl_facts in pw.rls.items():
                if not rel_facts_with_arity:
                    rl_name = rl_name.rsplit('_', maxsplit=1)[0]
                for rl_fact in rl_facts:
                    rl_fact = list(map(lambda x: turn_list_into_str(x) if isinstance(x, list) else x, rl_fact))
                    temp = []
                    if include_pw_ids:
                        temp.append(pw.pw_id)
                    temp.extend(rl_fact)
                    temp = [str(t) for t in temp]
                    pw_rel_facts.append('{}{}.'.format(rl_name,
                                                       '({})'.format(','.join(temp)) if len(temp) > 0 else ''))

        # add attr_def rules if they are provided
        attr_def_rules = []
        if attr_defs is None:
            attr_defs = {}
        for rel_name, attrs in attr_defs.items():
            if not rel_facts_with_arity:
                rel_name = rel_name.rsplit('_', maxsplit=1)[0]
            if include_pw_ids:
                attrs = ['PW_ID'] + attrs

# Path: PW_explorer/helper.py
# def mkdir_p(path):
#     """Make a directory if it doesn't already exist"""
#     try:
#         os.makedirs(path)
#     except OSError as exc:  # Python >2.5
#         if exc.errno == errno.EEXIST and os.path.isdir(path):
#             pass
#         else:
#             raise
# 
# def turn_list_into_str(l):
# 
#     if isinstance(l, (list,set,frozenset)):
#         if len(l) > 1:
#             l_ = [turn_list_into_str(l1) for l1 in l[1:]]
#             return "{}({})".format(l[0], ",".join(l_))
#         elif len(l) == 1:
#             return "{}".format(l[0])
#         else:
#             return ""
#     else:
#         return l
# 
# Path: PW_explorer/Input_Parsers/Meta_Data_Parser/meta_data_parser.py
# ASP_COMMENT_SYMBOL = '%'
# 
# ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD = 'schema'


# Path: PW_explorer/export.py
import pandas as pd
import os
import sqlite3
from .helper import (
    mkdir_p,
    turn_list_into_str,
)
from .Input_Parsers.Meta_Data_Parser.meta_data_parser import (
    ASP_COMMENT_SYMBOL,
    ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD,
)

        # output the pws as rel_name(pw_id, attrs....) and optimization(pw_id, optimized_value)
        pw_rel_facts = []
        for pw in pws:
            if pw.pw_soln is not None:
                if include_pw_ids:
                    pw_rel_facts.append('optimization({}, {}).'.format(pw.pw_id, pw.pw_soln))
                else:
                    pw_rel_facts.append('optimization({}).'.format(pw.pw_soln))
            for rl_name, rl_facts in pw.rls.items():
                if not rel_facts_with_arity:
                    rl_name = rl_name.rsplit('_', maxsplit=1)[0]
                for rl_fact in rl_facts:
                    rl_fact = list(map(lambda x: turn_list_into_str(x) if isinstance(x, list) else x, rl_fact))
                    temp = []
                    if include_pw_ids:
                        temp.append(pw.pw_id)
                    temp.extend(rl_fact)
                    temp = [str(t) for t in temp]
                    pw_rel_facts.append('{}{}.'.format(rl_name,
                                                       '({})'.format(','.join(temp)) if len(temp) > 0 else ''))

        # add attr_def rules if they are provided
        attr_def_rules = []
        if attr_defs is None:
            attr_defs = {}
        for rel_name, attrs in attr_defs.items():
            if not rel_facts_with_arity:
                rel_name = rel_name.rsplit('_', maxsplit=1)[0]
            if include_pw_ids:
                attrs = ['PW_ID'] + attrs

            attr_def_rules.append('{} {} {}({})'.format(ASP_COMMENT_SYMBOL, ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD,