# Path: hexlite/modelcallback.py
# class StandardModelCallback:
#   def __init__(self, stringifiedFacts, config):
#     self.facts = stringifiedFacts
#     self.config = config
# 
#   def __call__(self, model):
#     assert(isinstance(model, dlvhex.Model))
#     if not model.is_optimal:
#       logging.info('not showing suboptimal answer set')
#       return
#     strsyms = [ str(x) for x in model.atoms ]
#     if self.config.nofacts:
#       strsyms = [ s for s in strsyms if s not in self.facts ]
#     if not self.config.auxfacts:
#       strsyms = [ s for s in strsyms if not s.startswith(aux.Aux.PREFIX) ]
#     if len(model.cost) > 0:
#       # first entry = highest priority level
#       # last entry = lowest priority level (1)
#       #logging.debug('got cost'+repr(model.cost))
#       pairs = [ '[{}:{}]'.format(p[1], p[0]+1) for p in enumerate(reversed(model.cost)) if p[1] != 0 ]
#       costs=' <{}>'.format(','.join(pairs))
#     else:
#       costs = ''
#     logging.info('showing (optimal) answer set')
#     sys.stdout.write('{'+','.join(strsyms)+'}'+costs+'\n')


import sys
import platform
import hexlite.app as app
import hexlite
import hexlite.rewriter as rewriter
import hexlite.ast.shallowparser as shp
import hexlite.ast as ast
import dlvhex
import os, argparse, traceback, pprint, logging
from hexlite.modelcallback import StandardModelCallback

def teardownPlugins(plugins):
  for p in plugins:
    p.teardown()

def main():
  code = 1
  try:
    config = hexlite.Configuration()
    args = interpretArguments(sys.argv[1:], config)
    # import API here to fail early if clingo module does not exist
    app.importClingoAPI()
    # now set additional paths for plugin imports
    setPaths(hexlite.flatten(args.pluginpath))
    plugins = loadPlugins(args.plugin)
    program = loadProgram(hexlite.flatten(args.hexfiles))
    pcontext = hexlite.ProgramContext()
    if config.stats:
      pcontext.stats = hexlite.Statistics()
      # (per default, a dummy that does nothing is used)
    with pcontext.stats.context('rewriting'):
      rewriter.classifyEAtomsInstallRewritingHandlers(pcontext)
      pr = rewriter.ProgramRewriter(pcontext, program, plugins, config)
      rewritten, facts = pr.rewrite()
    stringifiedFacts = frozenset(ast.normalizeFacts(facts))
    modelcbs = None
    if len(dlvhex.modelCallbacks) > 0:
      # instantiate model callbacks
      modelcbs = [ cb(stringifiedFacts, config) for cb in dlvhex.modelCallbacks ]
    else:

# Path: hexlite/modelcallback.py
# class StandardModelCallback:
#   def __init__(self, stringifiedFacts, config):
#     self.facts = stringifiedFacts
#     self.config = config
# 
#   def __call__(self, model):
#     assert(isinstance(model, dlvhex.Model))
#     if not model.is_optimal:
#       logging.info('not showing suboptimal answer set')
#       return
#     strsyms = [ str(x) for x in model.atoms ]
#     if self.config.nofacts:
#       strsyms = [ s for s in strsyms if s not in self.facts ]
#     if not self.config.auxfacts:
#       strsyms = [ s for s in strsyms if not s.startswith(aux.Aux.PREFIX) ]
#     if len(model.cost) > 0:
#       # first entry = highest priority level
#       # last entry = lowest priority level (1)
#       #logging.debug('got cost'+repr(model.cost))
#       pairs = [ '[{}:{}]'.format(p[1], p[0]+1) for p in enumerate(reversed(model.cost)) if p[1] != 0 ]
#       costs=' <{}>'.format(','.join(pairs))
#     else:
#       costs = ''
#     logging.info('showing (optimal) answer set')
#     sys.stdout.write('{'+','.join(strsyms)+'}'+costs+'\n')


# Path: hexlite/main.py
import sys
import platform
import hexlite.app as app
import hexlite
import hexlite.rewriter as rewriter
import hexlite.ast.shallowparser as shp
import hexlite.ast as ast
import dlvhex
import os, argparse, traceback, pprint, logging
from hexlite.modelcallback import StandardModelCallback


def teardownPlugins(plugins):
  for p in plugins:
    p.teardown()

def main():
  code = 1
  try:
    config = hexlite.Configuration()
    args = interpretArguments(sys.argv[1:], config)
    # import API here to fail early if clingo module does not exist
    app.importClingoAPI()
    # now set additional paths for plugin imports
    setPaths(hexlite.flatten(args.pluginpath))
    plugins = loadPlugins(args.plugin)
    program = loadProgram(hexlite.flatten(args.hexfiles))
    pcontext = hexlite.ProgramContext()
    if config.stats:
      pcontext.stats = hexlite.Statistics()
      # (per default, a dummy that does nothing is used)
    with pcontext.stats.context('rewriting'):
      rewriter.classifyEAtomsInstallRewritingHandlers(pcontext)
      pr = rewriter.ProgramRewriter(pcontext, program, plugins, config)
      rewritten, facts = pr.rewrite()
    stringifiedFacts = frozenset(ast.normalizeFacts(facts))
    modelcbs = None
    if len(dlvhex.modelCallbacks) > 0:
      # instantiate model callbacks
      modelcbs = [ cb(stringifiedFacts, config) for cb in dlvhex.modelCallbacks ]
    else:

      modelcbs = [StandardModelCallback(stringifiedFacts, config)]