# Path: etb/wrapper.py
# class Tool(object):
#     """
#     Any tool to interpret predicates. Each predicate the tool can deal
#     with should be a method with the same name as the predicate
#     itself, annotated with @Tool.predicate(argspec).
# 
#     Calling the method should return a list of substitutions that are
#     answers to the goal, and bind all the variables in it.
# 
#     the async() method should return False if the tool is very fast to
#     call, True otherwise, in which case it is run in a background
#     thread.
#     """
# 
#     def __init__(self, etb):
#         """Initialize the Tool with the given ETB instance.
#         """
#         self.etb = etb
#         self.log = logging.getLogger('etb.wrapper')
#         self.fs = ETBFS(etb)
#         self.clms = ETBClaims(etb)
#         self.path = os.path.abspath(inspect.getmodule(self).__file__) # pylint: disable=E1103
# 
#     def __repr__(self):
#         return self.__class__.__name__
# 
#     @staticmethod
#     def predicate(argspec, name=None):
#         """Decorator which, given an instance, exports the function.
#         """
#         def decorator(fun):
#             fun._argspec = argspec
#             if name is not None:
#                 fun._predicate_name = name
#             return fun
#         return decorator
# 
#     @staticmethod
#     def volatile(fun):
#         """Mark the function (hence the predicate) as volatile."""
#         fun._volatile = True
#         return fun
# 
#     @staticmethod
#     def sync(fun):
#         """Mark the function as sync, i.e. it will be interpreted
#         directly.
#         """
#         fun._async = False
#         return fun
# 
#     @staticmethod
#     def extern(fun):
#         """Mark the function as external, i.e., it will be available
#         to remote ETB nodes.
#         """
#         fun._extern = True
#         return fun
# 
#     def fail(self, reason):
#         """Report of failure of interpretation of the given goal,
#         e.g. because of wrong types, bad instantiation scheme,
#         non present tool, etc.
#         """
#         msg = "in tool wrapper {0}: {1}".format(self, reason)
#         raise Exception(msg)
# 
#     def bindResult(self, result, resultValue, current=None) :
#         """bind result to resultValue"""
#         if current is None:
#             current = {}
#         if current == []:
#             return []
#         result = terms.mk_var(result)
#         resultValue = terms.mk_term(resultValue)
#         if result.is_var():
#             current[result] = resultValue
#             return current
#         elif result == resultValue :
#             return current
#         else :
#             return []
# 
# class BatchTool(Tool) :
#     """
#     Batch tool.
#     """
# 
#     def callTool(self, *args, **kwargs) :
#         try :
#             p = subprocess.Popen(args,
#                                  shell=False,
#                                  stdout=subprocess.PIPE,
#                                  stderr=subprocess.PIPE,
#                                  env=kwargs.get('env', None))
#             (out, err) = p.communicate()
#             return (p.returncode, out, err)
#         except Exception as msg:
#             self.log.error(msg)
#             self.fail('failed to start the external tool %s' % args[0])
# 
#     def parseResult(self, (stdout, _stderr)) :
#         return stdout
# 
#     def run(self, result, *args) :
#         """
#         Run an external command, parse the result, and binds it to the
#         result variable, or check that we get the result we are
#         expecting.
#         """
#         try:
#             (ret, out, err) = self.callTool(*args)
#             if ret != 0:
#                 self.log.error(err)
#                 return  Errors(self,  [ err ] )
#             parsed = self.parseResult((out, err))
#             if result.is_var():
#                 return Substitutions(self, [ self.bindResult(result, parsed) ])
#             elif result.val == parsed:
#                 return Success(self)
#             else:
#                 return Failure(self)
#         except Exception as msg :
#             self.log.exception('wrapper.run: exception msg {0}'.format(str(msg)))
#             return Errors(self,  [ str(msg) ] ) 
# 
# Path: etb/terms.py
# def mk_term(obj):
#     """Converts a mix of python and Term to Term
#     Deals with arrays, tuples, dicts, strings, and Terms.
#     This is useful in wrappers, where it is convenient to build up Maps and Arrays
#     using dicts and lists (or tuples) in Python.
#     This will try to guess what to do with strings, but it's probably best to construct
#     the corresponding terms or call the parser directly.
#     """
#     if isinstance(obj, Term):
#         return obj
#     elif isinstance(obj, (list, tuple)):
#         return mk_array(map(lambda x: mk_term(x), obj))
#     elif isinstance(obj, dict):
#         return mk_map(map(lambda (x, y): (mk_stringconst(x), mk_term(y)), obj.iteritems()))
#     elif isinstance(obj, bool):
#         return mk_boolconst(obj)
#     elif isinstance(obj, (int, float)):
#         return mk_numberconst(obj)
#     elif isinstance(obj, basestring):
#         if obj == '':
#             return mk_stringconst(obj)
#         try:
#             float(obj)
#             return mk_numberconst(obj)
#         except:
#             pass
#         idmatch = id_match(obj)
#         if idmatch:
#             if obj[0].isupper():
#                 return mk_var(obj)
#             else:
#                 return mk_idconst(obj)
#         else:
#             sobj = obj.strip()
#             if sobj == '':
#                 return mk_stringconst(obj)
#             elif ((sobj[0] == '[' and sobj[-1] == ']') or
#                   (sobj[0] == '{' and sobj[-1] == '}')):
#                 return parser.parse_term(sobj)
#             else:
#                 return mk_stringconst(obj)


import os, tempfile
from etb.wrapper import Tool, BatchTool
from etb.terms import mk_term

class List_batch(BatchTool):
    '''
    Simple utils.
    '''

    @Tool.predicate("+left: value, +right: value")
    def equal(self, left, right):
        if left == right:
            return [{}]
        else:
            return []


# Path: etb/wrapper.py
# class Tool(object):
#     """
#     Any tool to interpret predicates. Each predicate the tool can deal
#     with should be a method with the same name as the predicate
#     itself, annotated with @Tool.predicate(argspec).
# 
#     Calling the method should return a list of substitutions that are
#     answers to the goal, and bind all the variables in it.
# 
#     the async() method should return False if the tool is very fast to
#     call, True otherwise, in which case it is run in a background
#     thread.
#     """
# 
#     def __init__(self, etb):
#         """Initialize the Tool with the given ETB instance.
#         """
#         self.etb = etb
#         self.log = logging.getLogger('etb.wrapper')
#         self.fs = ETBFS(etb)
#         self.clms = ETBClaims(etb)
#         self.path = os.path.abspath(inspect.getmodule(self).__file__) # pylint: disable=E1103
# 
#     def __repr__(self):
#         return self.__class__.__name__
# 
#     @staticmethod
#     def predicate(argspec, name=None):
#         """Decorator which, given an instance, exports the function.
#         """
#         def decorator(fun):
#             fun._argspec = argspec
#             if name is not None:
#                 fun._predicate_name = name
#             return fun
#         return decorator
# 
#     @staticmethod
#     def volatile(fun):
#         """Mark the function (hence the predicate) as volatile."""
#         fun._volatile = True
#         return fun
# 
#     @staticmethod
#     def sync(fun):
#         """Mark the function as sync, i.e. it will be interpreted
#         directly.
#         """
#         fun._async = False
#         return fun
# 
#     @staticmethod
#     def extern(fun):
#         """Mark the function as external, i.e., it will be available
#         to remote ETB nodes.
#         """
#         fun._extern = True
#         return fun
# 
#     def fail(self, reason):
#         """Report of failure of interpretation of the given goal,
#         e.g. because of wrong types, bad instantiation scheme,
#         non present tool, etc.
#         """
#         msg = "in tool wrapper {0}: {1}".format(self, reason)
#         raise Exception(msg)
# 
#     def bindResult(self, result, resultValue, current=None) :
#         """bind result to resultValue"""
#         if current is None:
#             current = {}
#         if current == []:
#             return []
#         result = terms.mk_var(result)
#         resultValue = terms.mk_term(resultValue)
#         if result.is_var():
#             current[result] = resultValue
#             return current
#         elif result == resultValue :
#             return current
#         else :
#             return []
# 
# class BatchTool(Tool) :
#     """
#     Batch tool.
#     """
# 
#     def callTool(self, *args, **kwargs) :
#         try :
#             p = subprocess.Popen(args,
#                                  shell=False,
#                                  stdout=subprocess.PIPE,
#                                  stderr=subprocess.PIPE,
#                                  env=kwargs.get('env', None))
#             (out, err) = p.communicate()
#             return (p.returncode, out, err)
#         except Exception as msg:
#             self.log.error(msg)
#             self.fail('failed to start the external tool %s' % args[0])
# 
#     def parseResult(self, (stdout, _stderr)) :
#         return stdout
# 
#     def run(self, result, *args) :
#         """
#         Run an external command, parse the result, and binds it to the
#         result variable, or check that we get the result we are
#         expecting.
#         """
#         try:
#             (ret, out, err) = self.callTool(*args)
#             if ret != 0:
#                 self.log.error(err)
#                 return  Errors(self,  [ err ] )
#             parsed = self.parseResult((out, err))
#             if result.is_var():
#                 return Substitutions(self, [ self.bindResult(result, parsed) ])
#             elif result.val == parsed:
#                 return Success(self)
#             else:
#                 return Failure(self)
#         except Exception as msg :
#             self.log.exception('wrapper.run: exception msg {0}'.format(str(msg)))
#             return Errors(self,  [ str(msg) ] ) 
# 
# Path: etb/terms.py
# def mk_term(obj):
#     """Converts a mix of python and Term to Term
#     Deals with arrays, tuples, dicts, strings, and Terms.
#     This is useful in wrappers, where it is convenient to build up Maps and Arrays
#     using dicts and lists (or tuples) in Python.
#     This will try to guess what to do with strings, but it's probably best to construct
#     the corresponding terms or call the parser directly.
#     """
#     if isinstance(obj, Term):
#         return obj
#     elif isinstance(obj, (list, tuple)):
#         return mk_array(map(lambda x: mk_term(x), obj))
#     elif isinstance(obj, dict):
#         return mk_map(map(lambda (x, y): (mk_stringconst(x), mk_term(y)), obj.iteritems()))
#     elif isinstance(obj, bool):
#         return mk_boolconst(obj)
#     elif isinstance(obj, (int, float)):
#         return mk_numberconst(obj)
#     elif isinstance(obj, basestring):
#         if obj == '':
#             return mk_stringconst(obj)
#         try:
#             float(obj)
#             return mk_numberconst(obj)
#         except:
#             pass
#         idmatch = id_match(obj)
#         if idmatch:
#             if obj[0].isupper():
#                 return mk_var(obj)
#             else:
#                 return mk_idconst(obj)
#         else:
#             sobj = obj.strip()
#             if sobj == '':
#                 return mk_stringconst(obj)
#             elif ((sobj[0] == '[' and sobj[-1] == ']') or
#                   (sobj[0] == '{' and sobj[-1] == '}')):
#                 return parser.parse_term(sobj)
#             else:
#                 return mk_stringconst(obj)


# Path: demos/allsatlive/wrappers/list_wrapper.py
import os, tempfile
from etb.wrapper import Tool, BatchTool
from etb.terms import mk_term


class List_batch(BatchTool):
    '''
    Simple utils.
    '''

    @Tool.predicate("+left: value, +right: value")
    def equal(self, left, right):
        if left == right:
            return [{}]
        else:
            return []


    @Tool.predicate("-out: value")