math - java how to determine logical expression minimum values to evauate -
i have expression (an example)
(value1<15 , value2>25) or ((value3>0 , vaue4<5) or (value5<6 , value7>8))
the issue value1-7 calls external services, expensive. , need reduce cost, need make minimum number of calls evaluate expression. example if have
(value1<15 , value2>25)
evaluated true, don't need evaluate right part, don't need make useless calls external services. how determine in java (or may in math) when need stop, , further evaluations not make effect?
update
i have 5 workers works on 5 different servers.
first worker:
accept(expression) value1=calculatevalue1() setvaluetoexpression(expression, 0, value1) enough=expression.checkisitenough() if(!enough){ determinenextworker(expression) sendtonextworker() } else return expression.evaluate()
the second worker
accept(expression) value2=calculatevalue2() setvaluetoexpression(expression, 1, value2) enough=expression.checkisitenough() if(!enough){ determinenextworker(expression) sendtonextworker() } else return expression.evaluate()
.....................
as said in comments, understand if can write
evaluator.evaluate("(value1<5 , value2>6) or (value3>5 , value4>7)")
it evaluates of know, don't have ability due many reasons. can't make function calls value1()<15... it's happening synchronously, 1 one on different servers, kinda offline evaluation. hope it's clear
consider normal short-circuit intermediate code for:
(value1<15 , value2>25) or ((value3>0 , value4<5) or (value5<6 , value7>8))
if value1<15 goto value2_test else goto value3_test value2_test: if value2>25 goto success else goto value3_test value3_test: if value3>0 goto value4_test else goto value5_test value4_test: if value4<5 goto success else goto value5_test value5_test: if value5<6 goto value7_test else goto fail value7_test: if value7>8 goto success else goto fail
you simplify things substantially having tree representation of expression, , passing around subexpressions represented trees rather whole expression.
for example, first worker have 2 subexpressions: value2>25
, (value3>0 , value4<5) or (value5<6 , value7>8)
. select first 1 if test succeeds, second 1 if fails.
the value2>25 worker have 2 subexpressions: "success" , (value3>0 , value4<5) or (value5<6 , value7>8)
i not aware of library conversions. if exists, in domain of compiler construction.
i try hard change 1 worker organize job, , call on other workers evaluate 1 relational condition.
======================================================================== more detail, because seems sort of answer op looking for:
terminology:
- "term" -> comparison or boolean variable, such
value2>25
- "product" -> , of boolean expressions
- "sum" -> or of boolean expressions.
consider sum-of-products expressions, such (value1<15 , value2>25) or ((value3>0 , value4<5) or (value5<6 , value7>8))
. not significant limitation. many optimizations , simplifications used in digital logic design depend on converting arbitrary logical expressions sum-of-products.
at each step, worker leading term of expression called. depending on expression , outcome of test, can declare success, declare failure, or calculate new expression must passed worker leading term.
if worker's condition true if more terms in current product remove leading term current product pass new expression worker next term else declare success else if there product remove leading product expression pass new expression worker new leading product's leading term else declare failure
Comments
Post a Comment