# Path: hexlite/modelcallback.py
# class JSONModelCallback:
#   def __init__(self, stringifiedFacts, config):
#     self.facts = stringifiedFacts
#     self.config = config
# 
#   def structify_r(self, tup, d=0):
#     #logging.debug("%sstructify_r %s", ' '*d, tup)
#     if isinstance(tup, (tuple,list)):
#       # tuples
#       if len(tup) == 1:
#         return self.structify_r(tup[0], d+1)
#       else:
#         return { 'name': self.structify_r(tup[0], d+1), 'args': [ self.structify_r(x,d+1) for x in tup[1:] ] }
#     else:
#       x = tup
#       assert(isinstance(x, hexlite.clingobackend.ClingoID))
#       if x.symlit.sym.type == clingo.SymbolType.Function and len(x.symlit.sym.arguments) > 0:
#         return self.structify_r(x.tuple(), d+1)
#       elif x.isInteger():
#         return x.intValue()
#       else:
#         return x.value()
# 
#   def structify(self, sym):
#     assert(isinstance(sym, dlvhex.ID))
#     atomtuple = sym.tuple()
#     return self.structify_r(atomtuple)
# 
#   def __call__(self, model):
#     assert(isinstance(model, dlvhex.Model))
#     if not model.is_optimal:
#       logging.info('not showing suboptimal answer set')
#       return
#     str_struc_syms = [ (x,str(x)) for x in model.atoms ]
#     if self.config.nofacts:
#       # filter out by matching string representation with stringified facts
#       str_struc_syms = [ (sym, strsym) for sym, strsym in str_struc_syms if strsym not in self.facts ]
#     if not self.config.auxfacts:
#       str_struc_syms = [ (sym, strsym) for sym, strsym in str_struc_syms if not strsym.startswith(aux.Aux.PREFIX) ]
#     out = {
#       'cost': [ { 'priority': p[0]+1, 'cost': p[1] } for p in enumerate(reversed(model.cost)) if p[1] != 0 ],
#       'atoms': [ self.structify(sym) for sym, strsym in str_struc_syms ],
#       'stratoms': [ strsym for sym, strsym in str_struc_syms ]
#     }
#     sys.stdout.write(json.dumps(out)+'\n')
#     sys.stdout.flush()


import dlvhex
import hexlite.ast.shallowparser as shp
import logging, sys
from hexlite.modelcallback import JSONModelCallback
		frozenset([2]):   [ ],
		frozenset([1,2]): [1,2],
	}
	pset = frozenset(pset)
	if pset not in mapping:
		raise Exception("testNonmon2 is supposed to handle only input domain {1,2}")
	for o in mapping[pset]:
		dlvhex.output( (o,) )

def rdf(uri):
	logging.warning('TODO implement &rdf (and #namespace)')
	dlvhex.output(('s', 'p', 'o'))

def issue_2_num(a):
	n = 0
	for x in dlvhex.getInputAtoms():
		if x.tuple()[0] == a and x.isTrue():
			n += 1
	dlvhex.output((n, ))

def testStoreParseable(term):
	newterm = "foo(bar({}),baz)".format(term.value())
	dlvhex.output( (dlvhex.storeParseable(newterm),) )


def register(arguments=None):
	if arguments == ['foo', '3']:
		sys.stdout.write("testplugin loaded with arguments %s" % str(arguments))

	if arguments == ['jsonout']:

# Path: hexlite/modelcallback.py
# class JSONModelCallback:
#   def __init__(self, stringifiedFacts, config):
#     self.facts = stringifiedFacts
#     self.config = config
# 
#   def structify_r(self, tup, d=0):
#     #logging.debug("%sstructify_r %s", ' '*d, tup)
#     if isinstance(tup, (tuple,list)):
#       # tuples
#       if len(tup) == 1:
#         return self.structify_r(tup[0], d+1)
#       else:
#         return { 'name': self.structify_r(tup[0], d+1), 'args': [ self.structify_r(x,d+1) for x in tup[1:] ] }
#     else:
#       x = tup
#       assert(isinstance(x, hexlite.clingobackend.ClingoID))
#       if x.symlit.sym.type == clingo.SymbolType.Function and len(x.symlit.sym.arguments) > 0:
#         return self.structify_r(x.tuple(), d+1)
#       elif x.isInteger():
#         return x.intValue()
#       else:
#         return x.value()
# 
#   def structify(self, sym):
#     assert(isinstance(sym, dlvhex.ID))
#     atomtuple = sym.tuple()
#     return self.structify_r(atomtuple)
# 
#   def __call__(self, model):
#     assert(isinstance(model, dlvhex.Model))
#     if not model.is_optimal:
#       logging.info('not showing suboptimal answer set')
#       return
#     str_struc_syms = [ (x,str(x)) for x in model.atoms ]
#     if self.config.nofacts:
#       # filter out by matching string representation with stringified facts
#       str_struc_syms = [ (sym, strsym) for sym, strsym in str_struc_syms if strsym not in self.facts ]
#     if not self.config.auxfacts:
#       str_struc_syms = [ (sym, strsym) for sym, strsym in str_struc_syms if not strsym.startswith(aux.Aux.PREFIX) ]
#     out = {
#       'cost': [ { 'priority': p[0]+1, 'cost': p[1] } for p in enumerate(reversed(model.cost)) if p[1] != 0 ],
#       'atoms': [ self.structify(sym) for sym, strsym in str_struc_syms ],
#       'stratoms': [ strsym for sym, strsym in str_struc_syms ]
#     }
#     sys.stdout.write(json.dumps(out)+'\n')
#     sys.stdout.flush()


# Path: plugins/testplugin.py
import dlvhex
import hexlite.ast.shallowparser as shp
import logging, sys
from hexlite.modelcallback import JSONModelCallback

		frozenset([2]):   [ ],
		frozenset([1,2]): [1,2],
	}
	pset = frozenset(pset)
	if pset not in mapping:
		raise Exception("testNonmon2 is supposed to handle only input domain {1,2}")
	for o in mapping[pset]:
		dlvhex.output( (o,) )

def rdf(uri):
	logging.warning('TODO implement &rdf (and #namespace)')
	dlvhex.output(('s', 'p', 'o'))

def issue_2_num(a):
	n = 0
	for x in dlvhex.getInputAtoms():
		if x.tuple()[0] == a and x.isTrue():
			n += 1
	dlvhex.output((n, ))

def testStoreParseable(term):
	newterm = "foo(bar({}),baz)".format(term.value())
	dlvhex.output( (dlvhex.storeParseable(newterm),) )


def register(arguments=None):
	if arguments == ['foo', '3']:
		sys.stdout.write("testplugin loaded with arguments %s" % str(arguments))

	if arguments == ['jsonout']:

		dlvhex.registerModelCallbackClass(JSONModelCallback)