org.jetlinks.rule.engine.api.model.RuleModel Maven / Gradle / Ivy
The newest version!
package org.jetlinks.rule.engine.api.model;
import lombok.Getter;
import lombok.Setter;
import org.jetlinks.core.utils.SerializeUtils;
import org.jetlinks.rule.engine.api.scheduler.SchedulingRule;
import java.util.*;
import java.util.stream.Collectors;
/**
* 规则模型
*
* @author zhouhao
* @since 1.0.0
*/
@Getter
@Setter
public class RuleModel {
/**
* 规则模型ID
*/
private String id;
/**
* 规则名称
*/
private String name;
/**
* 模型类型
*/
private String type;
/**
* 描述
*/
private String description;
/**
* 调度规则
*/
private SchedulingRule schedulingRule;
/**
* 规则配置
*/
private Map configuration = new HashMap<>();
/**
* 规则事件连接
*/
private List events = new ArrayList<>();
/**
* 规则节点,包含所有的节点
*/
private List nodes = new ArrayList<>();
public RuleLink link(RuleNodeModel source, RuleNodeModel target) {
RuleLink link = new RuleLink();
link.setId(source.getId() + ":" + target.getId());
link.setName(source.getName() + ":" + target.getName());
link.setSource(source);
link.setTarget(target);
source.getOutputs().add(link);
target.getInputs().add(link);
return link;
}
public List getEvents(String type) {
return events.stream()
.filter(link -> type.equals(link.getType()))
.collect(Collectors.toList());
}
public Optional getNode(String nodeId) {
return nodes.stream()
.filter(model -> model.getId().equals(nodeId))
.findFirst();
}
public RuleModel addConfiguration(String key, Object value) {
configuration.put(key, SerializeUtils.convertToSafelySerializable(value, true));
return this;
}
@SuppressWarnings("all")
public void setConfiguration(Map configuration) {
this.configuration.clear();
this.configuration.putAll((Map) SerializeUtils.convertToSafelySerializable(configuration, true));
}
public Optional getStartNode() {
return nodes.stream()
.filter(RuleNodeModel::isStart)
.findFirst();
}
public List getStartNodes() {
return nodes
.stream()
.filter(RuleNodeModel::isStartNode)
.collect(Collectors.toList());
}
public List getEndNodes() {
return nodes.stream()
.filter(RuleNodeModel::isEnd)
.collect(Collectors.toList());
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
int index = 0;
for (RuleNodeModel node : getNodes()) {
if (index++ > 0) {
stringBuilder.append("\n");
}
node.appendMermaid(stringBuilder);
}
return stringBuilder.toString();
}
}