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.chain; 017 018import dev.tinyflow.core.util.JsCodeException; 019import dev.tinyflow.core.util.JsConditionUtil; 020import dev.tinyflow.core.util.Maps; 021 022import java.util.Map; 023 024public class JsCodeCondition implements NodeCondition, EdgeCondition { 025 private String code; 026 027 public JsCodeCondition() { 028 } 029 030 public JsCodeCondition(String code) { 031 this.code = code; 032 } 033 034 public String getCode() { 035 return code; 036 } 037 038 public void setCode(String code) { 039 this.code = code; 040 } 041 042 @Override 043 public boolean check(Chain chain, Edge edge, Map<String, Object> executeResult) { 044 try { 045 Maps map = Maps.of("_edge", edge).set("_chain", chain); 046 if (executeResult != null) { 047 map.putAll(executeResult); 048 } 049 return JsConditionUtil.eval(code, chain, map); 050 } catch (Exception e) { 051 throw new JsCodeException("edge check failed: " + e.getMessage(), e); 052 } 053 054 } 055 056 @Override 057 public boolean check(Chain chain, NodeState state, Map<String, Object> executeResult) { 058 try { 059 Maps map = Maps.of("_state", state).set("_chain", chain); 060 if (executeResult != null) { 061 map.putAll(executeResult); 062 } 063 return JsConditionUtil.eval(code, chain, map); 064 } catch (Exception e) { 065 throw new JsCodeException("node check failed: " + e.getMessage(), e); 066 } 067 } 068}