001/**
002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.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.gnu.org/licenses/lgpl-3.0.txt
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 dev.tinyflow.core.node;
017
018
019import dev.tinyflow.core.chain.Chain;
020import dev.tinyflow.core.chain.ChainState;
021import dev.tinyflow.core.chain.Node;
022import dev.tinyflow.core.chain.Parameter;
023
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029public abstract class BaseNode extends Node {
030
031    protected List<Parameter> parameters;
032    protected List<Parameter> outputDefs;
033
034    public void setParameters(List<Parameter> parameters) {
035        this.parameters = parameters;
036    }
037
038    public List<Parameter> getParameters() {
039        return parameters;
040    }
041
042    public void addInputParameter(Parameter parameter) {
043        if (parameters == null) {
044            parameters = new java.util.ArrayList<>();
045        }
046        parameters.add(parameter);
047    }
048
049
050    public List<Parameter> getOutputDefs() {
051        return outputDefs;
052    }
053
054    public void setOutputDefs(List<Parameter> outputDefs) {
055        this.outputDefs = outputDefs;
056    }
057
058    public void addOutputDef(Parameter parameter) {
059        if (outputDefs == null) {
060            outputDefs = new java.util.ArrayList<>();
061        }
062        outputDefs.add(parameter);
063    }
064
065    public void addOutputDefs(Collection<Parameter> parameters) {
066        if (outputDefs == null) {
067            outputDefs = new java.util.ArrayList<>();
068        }
069        outputDefs.addAll(parameters);
070    }
071
072
073    public Map<String, Object> getFormatParameters(Chain chain) {
074        Map<String, Object> allParameters = new HashMap<>();
075        ChainState state = chain.getState();
076        Map<String, Object> parameterValues = state.resolveParameters(this);
077        if (parameterValues != null) {
078            allParameters.putAll(parameterValues);
079        }
080
081        Map<String, Object> envMap = state.getEnvMap();
082        if (envMap != null) {
083            allParameters.putAll(envMap);
084        }
085
086        allParameters.putAll(state.getStartParameters());
087        return allParameters;
088    }
089}