# 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,
)
            df.to_sql(rl_name, sql_conn, if_exists='replace')

        sql_conn.commit()
        sql_conn.close()

        return True

    @staticmethod
    def export_as_asp_facts(pws, rel_facts_with_arity: bool=False, attr_defs: dict=None, include_pw_ids: bool=True):
        """
        :param pws: List of PossibleWorld objects
        :param rel_facts_with_arity: Include Arity in the Relation Names
        :param attr_defs: if provided, then statements of the form
        '% schema rel_name(pw_id, attr1_name, attr2_name...)' will be included to aid future reparsing
        (pw_id only included if include_pw_ids is True).
        :param include_pw_ids: Include PW-ID information
        :return: list of asp facts as strings
        """
        # 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:

# 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,
)

            df.to_sql(rl_name, sql_conn, if_exists='replace')

        sql_conn.commit()
        sql_conn.close()

        return True

    @staticmethod
    def export_as_asp_facts(pws, rel_facts_with_arity: bool=False, attr_defs: dict=None, include_pw_ids: bool=True):
        """
        :param pws: List of PossibleWorld objects
        :param rel_facts_with_arity: Include Arity in the Relation Names
        :param attr_defs: if provided, then statements of the form
        '% schema rel_name(pw_id, attr1_name, attr2_name...)' will be included to aid future reparsing
        (pw_id only included if include_pw_ids is True).
        :param include_pw_ids: Include PW-ID information
        :return: list of asp facts as strings
        """
        # 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))