001/*
002 *  Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.agentsflex.core.store.condition;
017
018public class Condition implements Operand {
019
020    protected ConditionType type;
021    protected Operand left;
022    protected Operand right;
023    protected boolean effective = true;
024    protected Connector connector;
025    protected Condition prev;
026    protected Condition next;
027
028    public Condition() {
029    }
030
031    public Condition(ConditionType type, Operand left, Operand right) {
032        this.type = type;
033        this.left = left;
034        this.right = right;
035
036        if (left instanceof Value) {
037            ((Value) left).setCondition(this);
038        }
039        if (right instanceof Value) {
040            ((Value) right).setCondition(this);
041        }
042    }
043
044
045    public void connect(Condition nextCondition, Connector connector) {
046        if (this.next != null) {
047            this.next.connect(nextCondition, connector);
048        } else {
049            nextCondition.connector = connector;
050            this.next = nextCondition;
051            nextCondition.prev = this;
052        }
053    }
054
055    public boolean checkEffective() {
056        return effective;
057    }
058
059    protected Condition getPrevEffectiveCondition() {
060        if (prev == null) {
061            return null;
062        }
063        return prev.checkEffective() ? prev : prev.getPrevEffectiveCondition();
064    }
065
066    protected Condition getNextEffectiveCondition() {
067        if (next == null) {
068            return null;
069        }
070        return next.checkEffective() ? next : next.getNextEffectiveCondition();
071    }
072
073
074    @Override
075    public String toExpression(ExpressionAdaptor adaptor) {
076        StringBuilder expr = new StringBuilder();
077        if (checkEffective()) {
078            Condition prevEffectiveCondition = getPrevEffectiveCondition();
079            if (prevEffectiveCondition != null && this.connector != null) {
080                expr.append(adaptor.toConnector(this.connector));
081            }
082            expr.append(adaptor.toCondition(this));
083        }
084
085        if (this.next != null) {
086            expr.append(this.next.toExpression(adaptor));
087        }
088
089        return expr.toString();
090    }
091
092
093    public ConditionType getType() {
094        return type;
095    }
096
097    public void setType(ConditionType type) {
098        this.type = type;
099    }
100
101    public Operand getLeft() {
102        return left;
103    }
104
105    public void setLeft(Operand left) {
106        this.left = left;
107    }
108
109    public Operand getRight() {
110        return right;
111    }
112
113    public void setRight(Operand right) {
114        this.right = right;
115    }
116
117    public boolean isEffective() {
118        return effective;
119    }
120
121    public void setEffective(boolean effective) {
122        this.effective = effective;
123    }
124
125    public Connector getConnector() {
126        return connector;
127    }
128
129    public void setConnector(Connector connector) {
130        this.connector = connector;
131    }
132
133    public Condition getPrev() {
134        return prev;
135    }
136
137    public void setPrev(Condition prev) {
138        this.prev = prev;
139    }
140
141    public Condition getNext() {
142        return next;
143    }
144
145    public void setNext(Condition next) {
146        this.next = next;
147    }
148
149    @Override
150    public String toString() {
151        return "Condition{" +
152            "type=" + type +
153            ", left=" + left +
154            ", right=" + right +
155            ", effective=" + effective +
156            ", connector=" + connector +
157            ", prev=" + prev +
158            ", next=" + next +
159            '}';
160    }
161}