Search in sources :

Example 26 with RuleNode

use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.

the class BaseRuleChainService method saveRuleChainMetaData.

@Override
@Transactional
public RuleChainUpdateResult saveRuleChainMetaData(TenantId tenantId, RuleChainMetaData ruleChainMetaData) {
    Validator.validateId(ruleChainMetaData.getRuleChainId(), "Incorrect rule chain id.");
    RuleChain ruleChain = findRuleChainById(tenantId, ruleChainMetaData.getRuleChainId());
    if (ruleChain == null) {
        return RuleChainUpdateResult.failed();
    }
    ConstraintValidator.validateFields(ruleChainMetaData);
    List<RuleNodeUpdateResult> updatedRuleNodes = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(ruleChainMetaData.getConnections())) {
        validateCircles(ruleChainMetaData.getConnections());
    }
    List<RuleNode> nodes = ruleChainMetaData.getNodes();
    List<RuleNode> toAddOrUpdate = new ArrayList<>();
    List<RuleNode> toDelete = new ArrayList<>();
    Map<RuleNodeId, Integer> ruleNodeIndexMap = new HashMap<>();
    if (nodes != null) {
        for (RuleNode node : nodes) {
            if (node.getId() != null) {
                ruleNodeIndexMap.put(node.getId(), nodes.indexOf(node));
            } else {
                toAddOrUpdate.add(node);
            }
        }
    }
    List<RuleNode> existingRuleNodes = getRuleChainNodes(tenantId, ruleChainMetaData.getRuleChainId());
    for (RuleNode existingNode : existingRuleNodes) {
        deleteEntityRelations(tenantId, existingNode.getId());
        Integer index = ruleNodeIndexMap.get(existingNode.getId());
        RuleNode newRuleNode = null;
        if (index != null) {
            newRuleNode = ruleChainMetaData.getNodes().get(index);
            toAddOrUpdate.add(newRuleNode);
        } else {
            updatedRuleNodes.add(new RuleNodeUpdateResult(existingNode, null));
            toDelete.add(existingNode);
        }
        updatedRuleNodes.add(new RuleNodeUpdateResult(existingNode, newRuleNode));
    }
    if (nodes != null) {
        for (RuleNode node : toAddOrUpdate) {
            node.setRuleChainId(ruleChain.getId());
            RuleNode savedNode = ruleNodeDao.save(tenantId, node);
            createRelation(tenantId, new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.RULE_CHAIN));
            int index = nodes.indexOf(node);
            nodes.set(index, savedNode);
            ruleNodeIndexMap.put(savedNode.getId(), index);
        }
    }
    for (RuleNode node : toDelete) {
        deleteRuleNode(tenantId, node.getId());
    }
    RuleNodeId firstRuleNodeId = null;
    if (nodes != null) {
        if (ruleChainMetaData.getFirstNodeIndex() != null) {
            firstRuleNodeId = nodes.get(ruleChainMetaData.getFirstNodeIndex()).getId();
        }
        if ((ruleChain.getFirstRuleNodeId() != null && !ruleChain.getFirstRuleNodeId().equals(firstRuleNodeId)) || (ruleChain.getFirstRuleNodeId() == null && firstRuleNodeId != null)) {
            ruleChain.setFirstRuleNodeId(firstRuleNodeId);
            ruleChainDao.save(tenantId, ruleChain);
        }
        if (ruleChainMetaData.getConnections() != null) {
            for (NodeConnectionInfo nodeConnection : ruleChainMetaData.getConnections()) {
                EntityId from = nodes.get(nodeConnection.getFromIndex()).getId();
                EntityId to = nodes.get(nodeConnection.getToIndex()).getId();
                String type = nodeConnection.getType();
                createRelation(tenantId, new EntityRelation(from, to, type, RelationTypeGroup.RULE_NODE));
            }
        }
        if (ruleChainMetaData.getRuleChainConnections() != null) {
            for (RuleChainConnectionInfo nodeToRuleChainConnection : ruleChainMetaData.getRuleChainConnections()) {
                RuleChainId targetRuleChainId = nodeToRuleChainConnection.getTargetRuleChainId();
                RuleChain targetRuleChain = findRuleChainById(TenantId.SYS_TENANT_ID, targetRuleChainId);
                RuleNode targetNode = new RuleNode();
                targetNode.setName(targetRuleChain != null ? targetRuleChain.getName() : "Rule Chain Input");
                targetNode.setRuleChainId(ruleChain.getId());
                targetNode.setType("org.thingsboard.rule.engine.flow.TbRuleChainInputNode");
                var configuration = JacksonUtil.newObjectNode();
                configuration.put("ruleChainId", targetRuleChainId.getId().toString());
                targetNode.setConfiguration(configuration);
                ObjectNode layout = (ObjectNode) nodeToRuleChainConnection.getAdditionalInfo();
                layout.remove("description");
                layout.remove("ruleChainNodeId");
                targetNode.setAdditionalInfo(layout);
                targetNode.setDebugMode(false);
                targetNode = ruleNodeDao.save(tenantId, targetNode);
                EntityRelation sourceRuleChainToRuleNode = new EntityRelation();
                sourceRuleChainToRuleNode.setFrom(ruleChain.getId());
                sourceRuleChainToRuleNode.setTo(targetNode.getId());
                sourceRuleChainToRuleNode.setType(EntityRelation.CONTAINS_TYPE);
                sourceRuleChainToRuleNode.setTypeGroup(RelationTypeGroup.RULE_CHAIN);
                relationService.saveRelation(tenantId, sourceRuleChainToRuleNode);
                EntityRelation sourceRuleNodeToTargetRuleNode = new EntityRelation();
                EntityId from = nodes.get(nodeToRuleChainConnection.getFromIndex()).getId();
                sourceRuleNodeToTargetRuleNode.setFrom(from);
                sourceRuleNodeToTargetRuleNode.setTo(targetNode.getId());
                sourceRuleNodeToTargetRuleNode.setType(nodeToRuleChainConnection.getType());
                sourceRuleNodeToTargetRuleNode.setTypeGroup(RelationTypeGroup.RULE_NODE);
                relationService.saveRelation(tenantId, sourceRuleNodeToTargetRuleNode);
            }
        }
    }
    return RuleChainUpdateResult.successful(updatedRuleNodes);
}
Also used : RuleNodeUpdateResult(org.thingsboard.server.common.data.rule.RuleNodeUpdateResult) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 27 with RuleNode

use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.

the class BaseRuleChainService method resetRuleNodeIds.

private void resetRuleNodeIds(List<RuleChainMetaData> metaData) {
    for (RuleChainMetaData md : metaData) {
        for (RuleNode node : md.getNodes()) {
            node.setId(null);
            node.setRuleChainId(null);
        }
    }
}
Also used : RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode)

Example 28 with RuleNode

use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method createRuleChainMetadata.

private RuleChainMetaData createRuleChainMetadata() throws Exception {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("My RuleChain");
    ruleChain.setTenantId(tenantId);
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(savedRuleChain.getId());
    ObjectMapper mapper = new ObjectMapper();
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("name1");
    ruleNode1.setType("type1");
    ruleNode1.setConfiguration(mapper.readTree("\"key1\": \"val1\""));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("name2");
    ruleNode2.setType("type2");
    ruleNode2.setConfiguration(mapper.readTree("\"key2\": \"val2\""));
    RuleNode ruleNode3 = new RuleNode();
    ruleNode3.setName("name3");
    ruleNode3.setType("type3");
    ruleNode3.setConfiguration(mapper.readTree("\"key3\": \"val3\""));
    List<RuleNode> ruleNodes = new ArrayList<>();
    ruleNodes.add(ruleNode1);
    ruleNodes.add(ruleNode2);
    ruleNodes.add(ruleNode3);
    ruleChainMetaData.setFirstNodeIndex(0);
    ruleChainMetaData.setNodes(ruleNodes);
    ruleChainMetaData.addConnectionInfo(0, 1, "success");
    ruleChainMetaData.addConnectionInfo(0, 2, "fail");
    ruleChainMetaData.addConnectionInfo(1, 2, "success");
    Assert.assertTrue(ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData).isSuccess());
    return ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 29 with RuleNode

use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method createRuleChainMetadataWithCirclingRelation.

private RuleChainMetaData createRuleChainMetadataWithCirclingRelation() throws Exception {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("My RuleChain");
    ruleChain.setTenantId(tenantId);
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(savedRuleChain.getId());
    ObjectMapper mapper = new ObjectMapper();
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("name1");
    ruleNode1.setType("type1");
    ruleNode1.setConfiguration(mapper.readTree("\"key1\": \"val1\""));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("name2");
    ruleNode2.setType("type2");
    ruleNode2.setConfiguration(mapper.readTree("\"key2\": \"val2\""));
    RuleNode ruleNode3 = new RuleNode();
    ruleNode3.setName("name3");
    ruleNode3.setType("type3");
    ruleNode3.setConfiguration(mapper.readTree("\"key3\": \"val3\""));
    List<RuleNode> ruleNodes = new ArrayList<>();
    ruleNodes.add(ruleNode1);
    ruleNodes.add(ruleNode2);
    ruleNodes.add(ruleNode3);
    ruleChainMetaData.setFirstNodeIndex(0);
    ruleChainMetaData.setNodes(ruleNodes);
    ruleChainMetaData.addConnectionInfo(0, 1, "success");
    ruleChainMetaData.addConnectionInfo(0, 2, "fail");
    ruleChainMetaData.addConnectionInfo(1, 2, "success");
    ruleChainMetaData.addConnectionInfo(2, 2, "success");
    return ruleChainMetaData;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 30 with RuleNode

use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method createRuleChainMetadataWithCirclingRelation2.

private RuleChainMetaData createRuleChainMetadataWithCirclingRelation2() throws Exception {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("My RuleChain");
    ruleChain.setTenantId(tenantId);
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(savedRuleChain.getId());
    ObjectMapper mapper = new ObjectMapper();
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("name1");
    ruleNode1.setType("type1");
    ruleNode1.setConfiguration(mapper.readTree("\"key1\": \"val1\""));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("name2");
    ruleNode2.setType("type2");
    ruleNode2.setConfiguration(mapper.readTree("\"key2\": \"val2\""));
    RuleNode ruleNode3 = new RuleNode();
    ruleNode3.setName("name3");
    ruleNode3.setType("type3");
    ruleNode3.setConfiguration(mapper.readTree("\"key3\": \"val3\""));
    List<RuleNode> ruleNodes = new ArrayList<>();
    ruleNodes.add(ruleNode1);
    ruleNodes.add(ruleNode2);
    ruleNodes.add(ruleNode3);
    ruleChainMetaData.setFirstNodeIndex(0);
    ruleChainMetaData.setNodes(ruleNodes);
    ruleChainMetaData.addConnectionInfo(0, 1, "success");
    ruleChainMetaData.addConnectionInfo(0, 2, "fail");
    ruleChainMetaData.addConnectionInfo(1, 2, "success");
    ruleChainMetaData.addConnectionInfo(2, 0, "success");
    return ruleChainMetaData;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

RuleNode (org.thingsboard.server.common.data.rule.RuleNode)30 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)17 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)16 ArrayList (java.util.ArrayList)11 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)10 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)10 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)9 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Slf4j (lombok.extern.slf4j.Slf4j)6 TbRuleChainInputNodeConfiguration (org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5 TenantId (org.thingsboard.server.common.data.id.TenantId)5 NodeConnectionInfo (org.thingsboard.server.common.data.rule.NodeConnectionInfo)5 TbMsg (org.thingsboard.server.common.msg.TbMsg)5 QueueToRuleEngineMsg (org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg)5 Test (org.junit.Test)4 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)4