# Path: etb/terms.py
# def loads(s, *args, **kwargs):
#     """Converts a JSON string to term classes
# 
#     >>> pid = mk_idconst('p')
#     >>> loads(dumps(pid)) == pid
#     True
#     >>> dumps(loads('{"__IdConst": "p"}')) == '{"__IdConst": "p"}'
#     True
# 
#     >>> arr = mk_array([mk_idconst('a'), mk_var('V'), mk_var(1), mk_numberconst(3)])
#     >>> loads(dumps(arr)) == arr
#     True
#     >>> arstr = '{"__Array": [{"__IdConst": "a"}, {"__Var": "V"}, {"__Var": 1}, {"__NumberConst": "3"}]}'
#     >>> dumps(loads(arstr)) == arstr
#     True
#     
#     >>> fref = mk_map({mk_stringconst('file'): mk_stringconst('doc.pdf'),
#     ...                mk_stringconst('sha1'): mk_stringconst("9af")})
#     >>> loads(dumps(fref)) == fref
#     True
#     >>> fstr = '{"__Map": {"sha1": {"__StringConst": "9af"}, "file": {"__StringConst": "doc.pdf"}}}'
#     >>> dumps(loads(fstr)) == fstr
#     True
# 
#     >>> lit = mk_literal(mk_idconst('p'), [mk_idconst('a'), mk_var('V'), mk_var(1), mk_numberconst(3)])
#     >>> loads(dumps(lit)) == lit
#     True
#     >>> litstr = '{"__Literal": [{"__IdConst": "p"}, {"__IdConst": "a"}, {"__Var": "V"}, {"__Var": 1}, {"__NumberConst": "3"}]}'
#     >>> dumps(loads(litstr)) == litstr
#     True
# 
#     >>> cls = Clause(mk_literal(mk_idconst('p'), []), [])
#     >>> loads(dumps(cls)) == cls
#     True
#     >>> clstr = '{"__Clause": [{"__Literal": [{"__IdConst": "p"}]}]}'
#     >>> dumps(loads(clstr)) == clstr
#     True
#     >>> cls2 = Clause(mk_literal(mk_idconst('p'), []), [mk_var(1), mk_map({mk_stringconst('foo'): mk_numberconst('42')})])
#     >>> loads(dumps(cls2)) == cls2
#     True
#     >>> clstr2 = '{"__Clause": [{"__Literal": [{"__IdConst": "p"}]}, {"__Var": 1}, {"__Map": {"foo": {"__NumberConst": "42"}}}]}'
#     >>> dumps(loads(clstr2)) == clstr2
#     True
# 
#     >>> sbst = Subst( { mk_var(1): mk_stringconst('p'),
#     ...                 mk_var(2): mk_stringconst('u') } )
#     >>> loads(dumps(sbst)) == sbst
#     True
#     >>> sbstr = '{"__Subst": [[{"__Var": 1}, {"__StringConst": "p"}], [{"__Var": 2}, {"__StringConst": "u"}]]}'
#     >>> dumps(loads(sbstr)) == sbstr
#     True
#     """
#     return remove_unicode(json.loads(s, object_hook=term_object_hook, *args, **kwargs))


import os
import json
from etb_manager import ETBNetwork
from etb_tests import etb_test, ETBTest
from etb.terms import loads



class HandleTest(ETBTest):

    @etb_test
    def simple_handle(self):
        f = self.etb.put_file(os.path.abspath('short.sal'), 'short.sal')
        q = self.etb.etb().query('salsim_start(%s, "main", S)' % f)
        self.etb.etb().query_wait(q)
        a = self.etb.etb().query_answers(q)
        print "a = %s" % a
        if a  == "[]":
            #sal might not be installed
            return ['{}', loads('{}')]
        pa = [ loads(s) for s in loads(a) ][0]
        print "pa = %s" % pa
        q = self.etb.etb().query('salsim_current_states(%s, States)' % pa.get('S'))
        self.etb.etb().query_wait(q)
        q = self.etb.etb().query('salsim_step(%s, S)' % pa.get('S'))
        self.etb.etb().query_wait(q)
        c = self.etb.etb().query_answers(q)
        pc = [ loads(s) for s in loads(c) ][0]

# Path: etb/terms.py
# def loads(s, *args, **kwargs):
#     """Converts a JSON string to term classes
# 
#     >>> pid = mk_idconst('p')
#     >>> loads(dumps(pid)) == pid
#     True
#     >>> dumps(loads('{"__IdConst": "p"}')) == '{"__IdConst": "p"}'
#     True
# 
#     >>> arr = mk_array([mk_idconst('a'), mk_var('V'), mk_var(1), mk_numberconst(3)])
#     >>> loads(dumps(arr)) == arr
#     True
#     >>> arstr = '{"__Array": [{"__IdConst": "a"}, {"__Var": "V"}, {"__Var": 1}, {"__NumberConst": "3"}]}'
#     >>> dumps(loads(arstr)) == arstr
#     True
#     
#     >>> fref = mk_map({mk_stringconst('file'): mk_stringconst('doc.pdf'),
#     ...                mk_stringconst('sha1'): mk_stringconst("9af")})
#     >>> loads(dumps(fref)) == fref
#     True
#     >>> fstr = '{"__Map": {"sha1": {"__StringConst": "9af"}, "file": {"__StringConst": "doc.pdf"}}}'
#     >>> dumps(loads(fstr)) == fstr
#     True
# 
#     >>> lit = mk_literal(mk_idconst('p'), [mk_idconst('a'), mk_var('V'), mk_var(1), mk_numberconst(3)])
#     >>> loads(dumps(lit)) == lit
#     True
#     >>> litstr = '{"__Literal": [{"__IdConst": "p"}, {"__IdConst": "a"}, {"__Var": "V"}, {"__Var": 1}, {"__NumberConst": "3"}]}'
#     >>> dumps(loads(litstr)) == litstr
#     True
# 
#     >>> cls = Clause(mk_literal(mk_idconst('p'), []), [])
#     >>> loads(dumps(cls)) == cls
#     True
#     >>> clstr = '{"__Clause": [{"__Literal": [{"__IdConst": "p"}]}]}'
#     >>> dumps(loads(clstr)) == clstr
#     True
#     >>> cls2 = Clause(mk_literal(mk_idconst('p'), []), [mk_var(1), mk_map({mk_stringconst('foo'): mk_numberconst('42')})])
#     >>> loads(dumps(cls2)) == cls2
#     True
#     >>> clstr2 = '{"__Clause": [{"__Literal": [{"__IdConst": "p"}]}, {"__Var": 1}, {"__Map": {"foo": {"__NumberConst": "42"}}}]}'
#     >>> dumps(loads(clstr2)) == clstr2
#     True
# 
#     >>> sbst = Subst( { mk_var(1): mk_stringconst('p'),
#     ...                 mk_var(2): mk_stringconst('u') } )
#     >>> loads(dumps(sbst)) == sbst
#     True
#     >>> sbstr = '{"__Subst": [[{"__Var": 1}, {"__StringConst": "p"}], [{"__Var": 2}, {"__StringConst": "u"}]]}'
#     >>> dumps(loads(sbstr)) == sbstr
#     True
#     """
#     return remove_unicode(json.loads(s, object_hook=term_object_hook, *args, **kwargs))


# Path: tests/handles.py
import os
import json
from etb_manager import ETBNetwork
from etb_tests import etb_test, ETBTest
from etb.terms import loads




class HandleTest(ETBTest):

    @etb_test
    def simple_handle(self):
        f = self.etb.put_file(os.path.abspath('short.sal'), 'short.sal')
        q = self.etb.etb().query('salsim_start(%s, "main", S)' % f)
        self.etb.etb().query_wait(q)
        a = self.etb.etb().query_answers(q)
        print "a = %s" % a
        if a  == "[]":
            #sal might not be installed
            return ['{}', loads('{}')]
        pa = [ loads(s) for s in loads(a) ][0]
        print "pa = %s" % pa
        q = self.etb.etb().query('salsim_current_states(%s, States)' % pa.get('S'))
        self.etb.etb().query_wait(q)
        q = self.etb.etb().query('salsim_step(%s, S)' % pa.get('S'))
        self.etb.etb().query_wait(q)
        c = self.etb.etb().query_answers(q)
        pc = [ loads(s) for s in loads(c) ][0]

        return (json.dumps(loads(pc.get('S'))['session'] - loads(pa.get('S'))['session'] > 0),