def __init__(self, by, max, per, where=None): # XX: Hack, need actual rate limit implementation. self._state = defaultdict(int) where = where or Literal.List([]) self.by = expect(Entity, by) self.max = expect(Literal.Number, max).unwrap() self.per = expect(Interval, per).unwrap() self.where = expect(Literal.List.Of(BaseNode), where)
def generatePredCompatAx(p, arity): """ Generate axioms for the form X1!=Y1|...|Xn!=Yn|~p(X1,...,Xn)|p(Y1,...Yn) for f with the given arity. """ res = generateEqPremise(arity) negp = list([p]) negp.extend(generateVarList("X", arity)) res.append(Literal(negp, True)) posp = list([p]) posp.extend(generateVarList("Y", arity)) res.append(Literal(posp, False)) resclause = Clause(res) resclause.setDerivation(Derivation("eq_axiom")) return resclause
def generateEqPremise(arity): """ Generate a list of literals of the form X1!=Y1|...|Xn!=Yn. """ res = [ Literal(list(["=", vars[0], vars[1]]), True) for vars in zip( generateVarList("X", arity), generateVarList("Y", arity)) ] return res
def generateFunCompatAx(f, arity): """ Generate axioms for the form X1!=Y1|...|Xn!=Yn|f(X1,...,Xn)=f(Y1,...Yn) for f with the given arity. """ res = generateEqPremise(arity) lterm = list([f]) lterm.extend(generateVarList("X", arity)) rterm = list([f]) rterm.extend(generateVarList("Y", arity)) concl = Literal(["=", lterm, rterm], False) res.append(concl) resclause = Clause(res) resclause.setDerivation(Derivation("eq_axiom")) return resclause
def wrapped(seconds): value = expect(Literal.Number, seconds).unwrap() value *= magnitude return Interval(Literal.Number(value))
def formulaTopSimplify(f): """ Try to apply the following simplification rules to f at the top level. Return (f',m), where f' is the result of simplification, and m indicates if f'!=f. """ if f.op == "~": if f.child1.isLiteral(): # Push ~ into literals if possible. This covers the case # of ~~P -> P if one of the negations is in the literal. return Formula("", f.child1.child1.negate()), True elif f.op == "|": if f.child1.isPropConst(True): # T | P -> T. Note that child1 is $true or # equivalent. This applies to several other cases where we # can reuse a $true or $false child instead of creating a # new formula. return f.child1, True elif f.child2.isPropConst(True): # P | T -> T return f.child2, True elif f.child1.isPropConst(False): # F | P -> P return f.child2, True elif f.child2.isPropConst(False): # P | F -> P return f.child1, True elif f.child1.isEqual(f.child2): # P | P -> P return f.child2, True elif f.op == "&": if f.child1.isPropConst(True): # T & P -> P return f.child2, True elif f.child2.isPropConst(True): # P & T -> P return f.child1, True elif f.child1.isPropConst(False): # F & P -> F return f.child1, True elif f.child2.isPropConst(False): # P & F -> F return f.child2, True elif f.child1.isEqual(f.child2): # P & P -> P return f.child2, True elif f.op == "<=>": if f.child1.isPropConst(True): # T <=> P -> P return f.child2, True elif f.child2.isPropConst(True): # P <=> T -> P return f.child1, True elif f.child1.isPropConst(False): # P <=> F -> ~P newform = Formula("~", f.child2) newform, m = formulaSimplify(newform) return newform, True elif f.child2.isPropConst(False): # F <=> P -> ~P newform = Formula("~", f.child1) newform, m = formulaSimplify(newform) return newform, True elif f.child1.isEqual(f.child2): # P <=> P -> T return Formula("", Literal(["$true"])), True elif f.op == "=>": if f.child1.isPropConst(True): # T => P -> P return f.child2, True elif f.child1.isPropConst(False): # F => P -> T return Formula("", Literal(["$true"])), True elif f.child2.isPropConst(True): # P => T -> T return Formula("", Literal(["$true"])), True elif f.child2.isPropConst(False): # P => F -> ~P newform = Formula("~", f.child1) newform, m = formulaSimplify(newform) return newform, True elif f.child1.isEqual(f.child2): # P => P -> T return Formula("", Literal(["$true"])), True elif f.op in ["!", "?"]: # ![X] F -> F if X is not free in F # ?[X] F -> F if X is not free in F vars = f.child2.collectFreeVars() if not f.child1 in vars: return f.child2, True else: assert f.op == "" or "Unexpected op" return f, False
def has_label_removed(entity, label): return HasLabel(entity, label, Literal.String('REMOVED'))
def has_label_added(entity, label): return HasLabel(entity, label, Literal.String('ADDED'))