black formatting

This commit is contained in:
Artur Meski
2023-07-11 19:48:37 +01:00
parent 8be615b293
commit a1fb5836a5
24 changed files with 965 additions and 727 deletions

View File

@@ -1,10 +1,9 @@
from enum import Enum
BagDesc_oper = Enum(
'BagDesc_oper', 'entity true l_and l_or l_not lt le eq ge gt')
BagDesc_oper = Enum("BagDesc_oper", "entity true l_and l_or l_not lt le eq ge gt")
class BagDescription(object):
def __init__(self, f_type, L_oper=None, R_oper=None, entity=""):
self.f_type = f_type
self.left_operand = L_oper
@@ -19,9 +18,13 @@ class BagDescription(object):
if self.f_type == BagDesc_oper.true:
return "TRUE"
if self.f_type == BagDesc_oper.l_and:
return "(" + repr(self.left_operand) + " & " + repr(self.right_operand) + ")"
return (
"(" + repr(self.left_operand) + " & " + repr(self.right_operand) + ")"
)
if self.f_type == BagDesc_oper.l_or:
return "(" + repr(self.left_operand) + " | " + repr(self.right_operand) + ")"
return (
"(" + repr(self.left_operand) + " | " + repr(self.right_operand) + ")"
)
if self.f_type == BagDesc_oper.l_not:
return "~(" + repr(self.left_operand) + ")"
if self.f_type == BagDesc_oper.lt:
@@ -39,12 +42,14 @@ class BagDescription(object):
"""Sanity checks"""
for operand in (self.left_operand, self.right_operand):
if operand:
if not(
isinstance(operand, BagDescription)
or isinstance(operand, int)):
if not (
isinstance(operand, BagDescription) or isinstance(operand, int)
):
raise RuntimeError(
"Unexpected operand type for a bag: {:s} (type: {:s})".format(
str(operand), str(type(operand))))
str(operand), str(type(operand))
)
)
@classmethod
def f_entity(cls, entity_name):
@@ -86,4 +91,5 @@ class BagDescription(object):
def __invert__(self):
return BagDescription(BagDesc_oper.l_not, L_oper=self)
# EOF

View File

@@ -1,11 +1,11 @@
from enum import Enum
ParamConstraint_oper = Enum(
'ParamConstraint_oper',
'param_entity true l_and l_or l_not lt le eq ge gt')
"ParamConstraint_oper", "param_entity true l_and l_or l_not lt le eq ge gt"
)
class ParamConstraint(object):
def __init__(self, f_type, L_oper=None, R_oper=None, param=None, entity=""):
self.f_type = f_type
self.left_operand = L_oper
@@ -21,9 +21,13 @@ class ParamConstraint(object):
if self.f_type == ParamConstraint_oper.true:
return "TRUE"
if self.f_type == ParamConstraint_oper.l_and:
return "(" + repr(self.left_operand) + " & " + repr(self.right_operand) + ")"
return (
"(" + repr(self.left_operand) + " & " + repr(self.right_operand) + ")"
)
if self.f_type == ParamConstraint_oper.l_or:
return "(" + repr(self.left_operand) + " | " + repr(self.right_operand) + ")"
return (
"(" + repr(self.left_operand) + " | " + repr(self.right_operand) + ")"
)
if self.f_type == ParamConstraint_oper.l_not:
return "~(" + repr(self.left_operand) + ")"
if self.f_type == ParamConstraint_oper.lt:
@@ -41,17 +45,18 @@ class ParamConstraint(object):
"""Sanity checks"""
for operand in (self.left_operand, self.right_operand):
if operand:
if not(
isinstance(operand, ParamConstraint)
or isinstance(operand, int)):
if not (
isinstance(operand, ParamConstraint) or isinstance(operand, int)
):
raise RuntimeError(
"Unexpected operand type for a bag: {:s} (type: {:s})".format(
str(operand), str(type(operand))))
str(operand), str(type(operand))
)
)
@classmethod
def f_param_ent(cls, param, entity_name):
return cls(ParamConstraint_oper.param_entity, param=param,
entity=entity_name)
return cls(ParamConstraint_oper.param_entity, param=param, entity=entity_name)
@classmethod
def f_TRUE(cls):
@@ -66,34 +71,28 @@ class ParamConstraint(object):
return cls(ParamConstraint_oper.l_not, L_oper=arg)
def __lt__(self, other):
return ParamConstraint(
ParamConstraint_oper.lt, L_oper=self, R_oper=other)
return ParamConstraint(ParamConstraint_oper.lt, L_oper=self, R_oper=other)
def __le__(self, other):
return ParamConstraint(
ParamConstraint_oper.le, L_oper=self, R_oper=other)
return ParamConstraint(ParamConstraint_oper.le, L_oper=self, R_oper=other)
def __eq__(self, other):
return ParamConstraint(
ParamConstraint_oper.eq, L_oper=self, R_oper=other)
return ParamConstraint(ParamConstraint_oper.eq, L_oper=self, R_oper=other)
def __ge__(self, other):
return ParamConstraint(
ParamConstraint_oper.ge, L_oper=self, R_oper=other)
return ParamConstraint(ParamConstraint_oper.ge, L_oper=self, R_oper=other)
def __gt__(self, other):
return ParamConstraint(
ParamConstraint_oper.gt, L_oper=self, R_oper=other)
return ParamConstraint(ParamConstraint_oper.gt, L_oper=self, R_oper=other)
def __and__(self, other):
return ParamConstraint(
ParamConstraint_oper.l_and, L_oper=self, R_oper=other)
return ParamConstraint(ParamConstraint_oper.l_and, L_oper=self, R_oper=other)
def __or__(self, other):
return ParamConstraint(
ParamConstraint_oper.l_or, L_oper=self, R_oper=other)
return ParamConstraint(ParamConstraint_oper.l_or, L_oper=self, R_oper=other)
def __invert__(self):
return ParamConstraint(ParamConstraint_oper.l_not, L_oper=self)
# EOF

View File

@@ -14,57 +14,60 @@ class ParamConstr_Encoder(object):
# self.loop_position = None
def load_variables(self, var_rs, var_ctx, var_loop_pos):
self.v = var_rs
self.v_ctx = var_ctx
self.loop_position = var_loop_pos
def encode(self, param_constr):
if not param_constr:
raise RuntimeError("param_constr is None")
if param_constr.f_type == ParamConstraint_oper.param_entity:
return self.smt_checker.get_enc_param(
param_constr.param.name, param_constr.entity)
param_constr.param.name, param_constr.entity
)
if param_constr.f_type == ParamConstraint_oper.true:
return True
if param_constr.f_type == ParamConstraint_oper.l_and:
return And(self.encode(param_constr.left_operand),
self.encode(param_constr.right_operand))
return And(
self.encode(param_constr.left_operand),
self.encode(param_constr.right_operand),
)
if param_constr.f_type == ParamConstraint_oper.l_or:
return Or(self.encode(param_constr.left_operand),
self.encode(param_constr.right_operand))
return Or(
self.encode(param_constr.left_operand),
self.encode(param_constr.right_operand),
)
if param_constr.f_type == ParamConstraint_oper.l_not:
return Not(self.encode(param_constr.left_operand))
if param_constr.f_type == ParamConstraint_oper.lt:
return self.encode(
param_constr.left_operand) < int(
param_constr.right_operand)
return self.encode(param_constr.left_operand) < int(
param_constr.right_operand
)
if param_constr.f_type == ParamConstraint_oper.le:
return self.encode(
param_constr.left_operand) <= int(
param_constr.right_operand)
return self.encode(param_constr.left_operand) <= int(
param_constr.right_operand
)
if param_constr.f_type == ParamConstraint_oper.eq:
return self.encode(
param_constr.left_operand) == int(
param_constr.right_operand)
return self.encode(param_constr.left_operand) == int(
param_constr.right_operand
)
if param_constr.f_type == ParamConstraint_oper.ge:
return self.encode(
param_constr.left_operand) >= int(
param_constr.right_operand)
return self.encode(param_constr.left_operand) >= int(
param_constr.right_operand
)
if param_constr.f_type == ParamConstraint_oper.gt:
return self.encode(
param_constr.left_operand) > int(
param_constr.right_operand)
return self.encode(param_constr.left_operand) > int(
param_constr.right_operand
)
assert False, "Unsupported case {:s}".format(param_constr.f_type)

View File

@@ -4,14 +4,13 @@ from enum import Enum
from logics.bags import *
rsLTL_form_type = Enum(
'rsLTL_form_type',
'bag l_and l_or l_not l_implies t_globally t_finally t_next t_until t_release')
"rsLTL_form_type",
"bag l_and l_or l_not l_implies t_globally t_finally t_next t_until t_release",
)
class Formula_rsLTL(object):
def __init__(
self, f_type, L_oper=None, R_oper=None, sub_oper=None, bag=None):
def __init__(self, f_type, L_oper=None, R_oper=None, sub_oper=None, bag=None):
self.f_type = f_type
self.left_operand = L_oper
self.right_operand = R_oper
@@ -21,7 +20,10 @@ class Formula_rsLTL(object):
# it's silly, but it's a frequent mistake
if callable(sub_oper):
raise RuntimeError(
"sub_oper should not be a function. Missing () for {0}?".format(sub_oper))
"sub_oper should not be a function. Missing () for {0}?".format(
sub_oper
)
)
if isinstance(self.left_operand, BagDescription):
self.left_operand = Formula_rsLTL.f_bag(self.left_operand)
@@ -43,21 +45,37 @@ class Formula_rsLTL(object):
if self.f_type == rsLTL_form_type.t_next:
return "X[" + repr(self.sub_operand) + "](" + repr(self.left_operand) + ")"
if self.f_type == rsLTL_form_type.l_and:
return "(" + repr(self.left_operand) + " & " + repr(self.right_operand) + ")"
return (
"(" + repr(self.left_operand) + " & " + repr(self.right_operand) + ")"
)
if self.f_type == rsLTL_form_type.l_or:
return "(" + repr(self.left_operand) + " | " + repr(self.right_operand) + ")"
return (
"(" + repr(self.left_operand) + " | " + repr(self.right_operand) + ")"
)
if self.f_type == rsLTL_form_type.l_implies:
return "(" + repr(self.left_operand) + " => " + repr(self.right_operand) + ")"
return (
"(" + repr(self.left_operand) + " => " + repr(self.right_operand) + ")"
)
if self.f_type == rsLTL_form_type.t_until:
return "(" + repr(
self.left_operand) + " U[" + repr(
self.sub_operand) + "] " + repr(
self.right_operand) + ")"
return (
"("
+ repr(self.left_operand)
+ " U["
+ repr(self.sub_operand)
+ "] "
+ repr(self.right_operand)
+ ")"
)
if self.f_type == rsLTL_form_type.t_release:
return "(" + repr(
self.left_operand) + " R[" + repr(
self.sub_operand) + "] " + repr(
self.right_operand) + ")"
return (
"("
+ repr(self.left_operand)
+ " R["
+ repr(self.sub_operand)
+ "] "
+ repr(self.right_operand)
+ ")"
)
@property
def is_bag(self):
@@ -93,8 +111,7 @@ class Formula_rsLTL(object):
@classmethod
def f_U(cls, sub, arg_L, arg_R):
return cls(
rsLTL_form_type.t_until, L_oper=arg_L, R_oper=arg_R, sub_oper=sub)
return cls(rsLTL_form_type.t_until, L_oper=arg_L, R_oper=arg_R, sub_oper=sub)
@classmethod
def f_F(cls, sub, arg_L):
@@ -102,9 +119,7 @@ class Formula_rsLTL(object):
@classmethod
def f_R(cls, sub, arg_L, arg_R):
return cls(
rsLTL_form_type.t_release, L_oper=arg_L, R_oper=arg_R,
sub_oper=sub)
return cls(rsLTL_form_type.t_release, L_oper=arg_L, R_oper=arg_R, sub_oper=sub)
def __and__(self, other):
return Formula_rsLTL(rsLTL_form_type.l_and, L_oper=self, R_oper=other)

View File

@@ -24,13 +24,11 @@ class rsLTL_Encoder(object):
self.init_ncalls()
def load_variables(self, var_rs, var_ctx, var_loop_pos):
self.v = var_rs
self.v_ctx = var_ctx
self.loop_position = var_loop_pos
def get_encoding(self, formula, bound):
assert self.v is not None
assert self.v_ctx is not None
assert self.loop_position is not None
@@ -49,8 +47,8 @@ class rsLTL_Encoder(object):
Cache for formulae encodings
"""
self.cache_hits = 0
self.enc_fcache = [{} for level in range(0, bound+1)]
self.enc_fcache_approx = [{} for level in range(0, bound+1)]
self.enc_fcache = [{} for level in range(0, bound + 1)]
self.enc_fcache_approx = [{} for level in range(0, bound + 1)]
def cache_save(self, formula, level, formula_encoding):
self.enc_fcache[level][formula] = formula_encoding
@@ -83,7 +81,6 @@ class rsLTL_Encoder(object):
return (self.ncalls_encode, self.ncalls_encode_approx)
def encode_bag(self, bag_formula, level, context=False):
if not bag_formula:
raise RuntimeError("bag_formula is None")
@@ -100,41 +97,42 @@ class rsLTL_Encoder(object):
if bag_formula.f_type == BagDesc_oper.l_and:
return And(
self.encode_bag(bag_formula.left_operand, level, context),
self.encode_bag(bag_formula.right_operand, level, context))
self.encode_bag(bag_formula.right_operand, level, context),
)
if bag_formula.f_type == BagDesc_oper.l_or:
return Or(
self.encode_bag(bag_formula.left_operand, level, context),
self.encode_bag(bag_formula.right_operand, level, context))
self.encode_bag(bag_formula.right_operand, level, context),
)
if bag_formula.f_type == BagDesc_oper.l_not:
return Not(self.encode_bag(
bag_formula.left_operand, level, context))
return Not(self.encode_bag(bag_formula.left_operand, level, context))
if bag_formula.f_type == BagDesc_oper.lt:
return self.encode_bag(
bag_formula.left_operand, level, context) < int(
bag_formula.right_operand)
return self.encode_bag(bag_formula.left_operand, level, context) < int(
bag_formula.right_operand
)
if bag_formula.f_type == BagDesc_oper.le:
return self.encode_bag(
bag_formula.left_operand, level, context) <= int(
bag_formula.right_operand)
return self.encode_bag(bag_formula.left_operand, level, context) <= int(
bag_formula.right_operand
)
if bag_formula.f_type == BagDesc_oper.eq:
return self.encode_bag(
bag_formula.left_operand, level, context) == int(
bag_formula.right_operand)
return self.encode_bag(bag_formula.left_operand, level, context) == int(
bag_formula.right_operand
)
if bag_formula.f_type == BagDesc_oper.ge:
return self.encode_bag(
bag_formula.left_operand, level, context) >= int(
bag_formula.right_operand)
return self.encode_bag(bag_formula.left_operand, level, context) >= int(
bag_formula.right_operand
)
if bag_formula.f_type == BagDesc_oper.gt:
return self.encode_bag(
bag_formula.left_operand, level, context) > int(
bag_formula.right_operand)
return self.encode_bag(bag_formula.left_operand, level, context) > int(
bag_formula.right_operand
)
assert False, "Unsupported case {:s}".format(bag_formula.f_type)
@@ -149,7 +147,6 @@ class rsLTL_Encoder(object):
return res
def encode(self, formula, level, bound):
self.ncalls_encode += 1
from_cache = self.cache_query(formula, level)
@@ -159,12 +156,12 @@ class rsLTL_Encoder(object):
enc = None
if not isinstance(formula, Formula_rsLTL):
raise NotImplementedError(
"Unsupported formula type: " + str(type(formula)))
raise NotImplementedError("Unsupported formula type: " + str(type(formula)))
if level > bound:
raise RuntimeError(
"level > bound. Unexpected behaviour. The encoding does not support levels higher than a bound.")
"level > bound. Unexpected behaviour. The encoding does not support levels higher than a bound."
)
if formula.f_type == rsLTL_form_type.bag:
enc = self.encode_bag_state(formula.bag_descr, level)
@@ -179,33 +176,40 @@ class rsLTL_Encoder(object):
elif formula.f_type == rsLTL_form_type.l_and:
enc = And(
self.encode(formula.left_operand, level, bound),
self.encode(formula.right_operand, level, bound)
self.encode(formula.right_operand, level, bound),
)
elif formula.f_type == rsLTL_form_type.l_or:
enc = Or(
self.encode(formula.left_operand, level, bound),
self.encode(formula.right_operand, level, bound)
self.encode(formula.right_operand, level, bound),
)
elif formula.f_type == rsLTL_form_type.l_implies:
enc = Implies(
self.encode(formula.left_operand, level, bound),
self.encode(formula.right_operand, level, bound)
self.encode(formula.right_operand, level, bound),
)
elif formula.f_type == rsLTL_form_type.t_next:
if level < bound:
enc = And(
self.encode(formula.left_operand, level + 1, bound),
self.encode_bag_ctx(formula.sub_operand, level)
self.encode_bag_ctx(formula.sub_operand, level),
)
else:
# level == bound
enc = False
for loop_level in range(1, bound+1):
enc = simplify(Or(enc, And(self.loop_position == loop_level, self.encode(
formula.left_operand, loop_level, bound))))
for loop_level in range(1, bound + 1):
enc = simplify(
Or(
enc,
And(
self.loop_position == loop_level,
self.encode(formula.left_operand, loop_level, bound),
),
)
)
enc = And(enc, self.encode_bag_ctx(formula.sub_operand, level))
enc = simplify(enc)
@@ -214,22 +218,25 @@ class rsLTL_Encoder(object):
enc = And(
self.encode(formula.left_operand, level, bound),
self.encode_bag_ctx(formula.sub_operand, level),
self.encode(formula, level + 1, bound)
self.encode(formula, level + 1, bound),
)
else:
# level == bound
enc_loops = False
for loop_level in range(1, bound+1):
enc_loops = simplify(Or(enc_loops,
And(
self.loop_position == loop_level,
self.encode_approx(formula, loop_level, bound),
)
))
for loop_level in range(1, bound + 1):
enc_loops = simplify(
Or(
enc_loops,
And(
self.loop_position == loop_level,
self.encode_approx(formula, loop_level, bound),
),
)
)
enc = And(
self.encode(formula.left_operand, bound, bound),
enc_loops,
self.encode_bag_ctx(formula.sub_operand, level)
self.encode_bag_ctx(formula.sub_operand, level),
)
enc = simplify(enc)
@@ -239,25 +246,26 @@ class rsLTL_Encoder(object):
self.encode(formula.left_operand, level, bound),
And(
self.encode_bag_ctx(formula.sub_operand, level),
self.encode(formula, level + 1, bound)
)
self.encode(formula, level + 1, bound),
),
)
else:
# level == bound
enc_loops = False
for loop_level in range(1, bound+1):
enc_loops = simplify(Or(enc_loops,
And(
self.loop_position == loop_level,
self.encode_approx(formula, loop_level, bound),
)
))
for loop_level in range(1, bound + 1):
enc_loops = simplify(
Or(
enc_loops,
And(
self.loop_position == loop_level,
self.encode_approx(formula, loop_level, bound),
),
)
)
# print(enc)
enc = Or(self.encode(formula.left_operand, bound, bound),
And(
enc_loops,
self.encode_bag_ctx(formula.sub_operand, level)
)
enc = Or(
self.encode(formula.left_operand, bound, bound),
And(enc_loops, self.encode_bag_ctx(formula.sub_operand, level)),
)
enc = simplify(enc)
@@ -268,21 +276,24 @@ class rsLTL_Encoder(object):
else:
# level == bound
inner_enc = False
for loop_level in range(1, bound+1):
inner_enc = simplify(Or(inner_enc,
And(
self.loop_position == loop_level,
self.encode_approx(formula, loop_level, bound)
)
))
for loop_level in range(1, bound + 1):
inner_enc = simplify(
Or(
inner_enc,
And(
self.loop_position == loop_level,
self.encode_approx(formula, loop_level, bound),
),
)
)
enc = Or(
self.encode(formula.right_operand, level, bound),
And(
self.encode(formula.left_operand, level, bound),
inner_enc,
self.encode_bag_ctx(formula.sub_operand, level)
)
self.encode_bag_ctx(formula.sub_operand, level),
),
)
elif formula.f_type == rsLTL_form_type.t_release:
@@ -291,23 +302,23 @@ class rsLTL_Encoder(object):
else:
# level == bound
inner_enc = False
for loop_level in range(1, bound+1):
inner_enc = simplify(Or(inner_enc,
And(
self.loop_position == loop_level,
self.encode_approx(formula, loop_level, bound)
)
))
for loop_level in range(1, bound + 1):
inner_enc = simplify(
Or(
inner_enc,
And(
self.loop_position == loop_level,
self.encode_approx(formula, loop_level, bound),
),
)
)
enc = And(
self.encode(formula.right_operand, level, bound),
Or(
self.encode(formula.left_operand, level, bound),
And(
inner_enc,
self.encode_bag_ctx(formula.sub_operand, level)
)
)
And(inner_enc, self.encode_bag_ctx(formula.sub_operand, level)),
),
)
else:
@@ -341,8 +352,8 @@ class rsLTL_Encoder(object):
And(
self.encode(formula.left_operand, level, bound),
self.encode_approx(formula, level + 1, bound),
self.encode_bag_ctx(formula.sub_operand, level)
)
self.encode_bag_ctx(formula.sub_operand, level),
),
)
else:
# level == bound
@@ -356,9 +367,9 @@ class rsLTL_Encoder(object):
self.encode(formula.left_operand, level, bound),
And(
self.encode_approx(formula, level + 1, bound),
self.encode_bag_ctx(formula.sub_operand, level)
)
)
self.encode_bag_ctx(formula.sub_operand, level),
),
),
)
else:
# level == bound
@@ -369,7 +380,7 @@ class rsLTL_Encoder(object):
enc = And(
self.encode(formula.left_operand, level, bound),
self.encode_bag_ctx(formula.sub_operand, level),
self.encode_approx(formula, level + 1, bound)
self.encode_approx(formula, level + 1, bound),
)
else:
# level == bound
@@ -381,16 +392,15 @@ class rsLTL_Encoder(object):
self.encode(formula.left_operand, level, bound),
And(
self.encode_bag_ctx(formula.sub_operand, level),
self.encode_approx(formula, level + 1, bound)
)
self.encode_approx(formula, level + 1, bound),
),
)
else:
# level == bound
enc = self.encode(formula.left_operand, bound, bound)
else:
raise NotImplementedError(
"Unsupported operator in approximation encoding")
raise NotImplementedError("Unsupported operator in approximation encoding")
if enc is None:
raise RuntimeError("Encoding is NONE. Should never happen")