"""
restrict_play.py -- Test advanced restriction

"""
# System
from collections import namedtuple

# PyRAL
from pyral.database import Database
from pyral.relvar import Relvar
from pyral.relation import Relation
from pyral.rtypes import Attribute

Shaft_i = namedtuple('Shaft_i', 'Class Speed In_service')

ev = "ev"

def play():
    Database.open_session(ev)
    Relation.create(db=ev, attrs=[Attribute(name="Class", type="string"), Attribute(name="Speed", type="double"), Attribute(name="In_service", type="boolean")],
                    tuples=[
                        Shaft_i(Class='S1', Speed=31.3, In_service=True),
                        Shaft_i(Class='S2', Speed=14.2, In_service=False),
                        Shaft_i(Class='S3', Speed=20.16, In_service=True),
                    ], svar_name="shafts_rv")
    # Relation.raw(db=ev, cmd_str="relation restrictwith ${shafts_rv} {[expr {$Speed > 14}]}",
    #              svar_name="raw_rv")
    # Relation.raw(db=ev, cmd_str="relation restrictwith ${shafts_rv} {[string match S* {S2}]}",
    #              svar_name="raw_rv")
    # Relation.raw(db=ev, cmd_str="relation restrict ${shafts_rv} t {[string match {S2} [tuple extract $t ID]]}",
    #              svar_name="raw_rv")
    # Relation.print(db=ev, variable_name="raw_rv")
    pass

    # Relation.raw(db=ev, cmd_str=r"relation restrictwith ${shafts_rv} [ [string match {S1} $ID] && [string match {True} $In_service] ]",
    #              svar_name="raw_rv")
    # R = f"In_service:<{True}>"
    # R = f"ID:<{v}>, In_service:<{True}>"
    # R = f"ID:<{v}> OR In_service:<{True}>"

    v = "S1"
    s = 14.2
    # R = f"Speed > {s}, ID:<S3>"
    # R = f"Class:{v}"
    # R = f"ID==<S1>"
    # R = f"ID:<{v}>, In_service:<True>"
    # R = f"ID:<{v}>, Speed:<{s}>"
    # R = f"In_service:<{True}>"
    # Not working: R = f"ID:<{v}>, Speed:{s}"
    # R = f"Speed:{s}"
    R = f"Speed > {s}"
    # R = f"NOT In_service:<{True}>"
    # R = f"NOT Speed:<{s}>"
    # s = 31
    # R = f"ID:<{v}> OR (In_service:<{True}> AND Speed > {s})"


    # result = Relation.restrict(db=ev, relation="shafts_rv", svar_name="restriction")
    result = Relation.restrict(db=ev, relation="shafts_rv", restriction=R, svar_name="restriction")
    Relation.print(db=ev, variable_name="restriction")

    pass

    # Relvar.create_relvar(db=ev, name='Shaft', attrs=[Attribute('ID', 'string'), Attribute('In_service', 'boolean'),],
    #                      ids={1: ['ID']})
    # Relvar.insert(db=ev, relvar='Shaft', tuples=[
    #     Shaft_i(ID='S1', In_service=True),
    #     Shaft_i(ID='S2', In_service=False),
    # ])

    # shaft_r = Relation.restrict(db=ev, relation="shafts_rv")




    Database.close_session(ev)

    pass
