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
018import dev.tinyflow.core.chain.Chain;
019import dev.tinyflow.core.code.CodeRuntimeEngine;
020import dev.tinyflow.core.code.CodeRuntimeEngineManager;
021import dev.tinyflow.core.util.StringUtil;
022import dev.tinyflow.core.util.TextTemplate;
023
024import java.util.Map;
025
026public class CodeNode extends BaseNode {
027    protected String engine;
028    protected String code;
029
030    public String getEngine() {
031        return engine;
032    }
033
034    public void setEngine(String engine) {
035        this.engine = engine;
036    }
037
038    public String getCode() {
039        return code;
040    }
041
042    public void setCode(String code) {
043        this.code = code;
044    }
045
046    @Override
047    public Map<String, Object> execute(Chain chain) {
048        if (StringUtil.noText(code)) {
049            throw new IllegalArgumentException("code is empty");
050        }
051
052        Map<String, Object> formatParameters = getFormatParameters(chain);
053        String newCode = TextTemplate.of(code).formatToString(formatParameters);
054
055        CodeRuntimeEngine codeRuntimeEngine = CodeRuntimeEngineManager.getInstance().getCodeRuntimeEngine(this.engine);
056        return codeRuntimeEngine.execute(newCode, this, chain);
057    }
058
059}