If expression. @author BaseX Team, BSD License @author Christian Gruen
| 22 | * @author Christian Gruen |
| 23 | */ |
| 24 | public final class If extends Arr { |
| 25 | /** Condition. */ |
| 26 | public Expr cond; |
| 27 | |
| 28 | /** |
| 29 | * Constructor with empty 'else' branch. |
| 30 | * @param info input info (can be {@code null}) |
| 31 | * @param cond condition |
| 32 | * @param branch1 'then' branch |
| 33 | */ |
| 34 | public If(final InputInfo info, final Expr cond, final Expr branch1) { |
| 35 | this(info, cond, branch1, Empty.VALUE); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Constructor. |
| 40 | * @param info input info (can be {@code null}) |
| 41 | * @param cond condition |
| 42 | * @param branch1 'then' branch |
| 43 | * @param branch2 'else' branch |
| 44 | */ |
| 45 | public If(final InputInfo info, final Expr cond, final Expr branch1, final Expr branch2) { |
| 46 | super(info, Types.ITEM_ZM, branch1, branch2); |
| 47 | this.cond = cond; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void checkUp() throws QueryException { |
| 52 | checkNoUp(cond); |
| 53 | checkAllUp(exprs); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Expr compile(final CompileContext cc) throws QueryException { |
| 58 | cond = cond.compile(cc); |
| 59 | |
| 60 | final int el = exprs.length; |
| 61 | for(int e = 0; e < el; e++) { |
| 62 | exprs[e] = cc.compileOrError(exprs[e], false); |
| 63 | } |
| 64 | return optimize(cc); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Expr optimize(final CompileContext cc) throws QueryException { |
| 69 | if(EMPTY.is(cond)) { |
| 70 | // if(empty(A)) then B else C → if(exists(A)) then C else B |
| 71 | cond = cc.function(EXISTS, cond.info(info), cond.arg(0)); |
| 72 | swap(); |
| 73 | cc.info(QueryText.OPTSWAP_X, this); |
| 74 | } else if(NOT.is(cond)) { |
| 75 | // if(not(A)) then B else C → if(A) then C else B |
| 76 | cond = cond.arg(0); |
| 77 | swap(); |
| 78 | cc.info(QueryText.OPTSWAP_X, this); |
| 79 | } |
| 80 | // if(exists($nodes)) → if($nodes) |
| 81 | cond = cond.simplifyFor(Simplify.EBV, cc); |
nothing calls this directly
no outgoing calls
no test coverage detected