def test_exhaustive_consistent(): # check basic consistency assert consistent(var('a'), var('b'), sequential('a', 'b')) # see how long a causal loop can be before transitivity stops working for i in range(1, 5): clauses = [sequential(to_char(i), 'a')] + chain(i) assert not consistent(and_(*clauses), exhaustive=True)
def test_occurrence(): occ = occurrence(relationships([simultaneous('a', 'b')])) print('occurrence: ', occ) formula = and_(simultaneous('a', 'b'), not_(var('a')), *occ) print('formula: ', formula) s = formula.sat()[1] if s: print('solution: ', [k for k, v in s.items() if v]) assert not s
def test_timeline(): statements = [simultaneous('a', 'b'), sequential('b', 'a')] t = timeline(relationships(statements)) print('timeline: ', t) formula = and_(var('a'), var('b'), *(t + statements)) print('formula: ', formula) s = formula.sat()[1] if s: print('solution: ', [k for k, v in s.items() if v]) assert not s
def test_transitivity(): def trans(s): return transitivity(triples(relationships(s))) statements = [sequential('a', 'b'), sequential('b', 'c'), sequential('d', 'e')] assert len(trans(statements)) == 1 statements += sequential('a', 'c') assert trans(statements)[0].equiv( impl(and_(var("a<b"), var("b<c")), var("a<c")))
def test_consistent2(): # see how long a causal loop can be before transitivity stops working for i in range(1, 5): clauses = [sequential(to_char(i), 'a')] + chain(i) assert not consistent(and_(*clauses))
def ordered(*args): "Arguments happen in some order; not simultaneously" return and_( *pairwise(lambda a, b: ~a | ~b | sequential(a, b) | sequential(b, a), args))
def simultaneous(a, b, *rest): if rest: return and_(*pairwise(simultaneous, (a, b) + rest)) a, b = sorted((a, b)) # make sure we always use pairs in the same order return var(a + "*" + b)
def sequential(a, b, *rest): if rest: return and_(*pairwise(sequential, (a, b) + rest)) return var(a + "<" + b)
def inner(a, b, c): return and_(impl(fn(a, b) & fn(b, c), fn(a, c)), impl(simultaneous(a, b) & fn(b, c), fn(a, c)), impl(fn(a, b) & simultaneous(b, c), fn(a, c)))