Search in sources :

Example 56 with RuleChain

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

the class BaseRuleChainService method loadRuleChainMetaData.

@Override
public RuleChainMetaData loadRuleChainMetaData(TenantId tenantId, RuleChainId ruleChainId) {
    Validator.validateId(ruleChainId, "Incorrect rule chain id.");
    RuleChain ruleChain = findRuleChainById(tenantId, ruleChainId);
    if (ruleChain == null) {
        return null;
    }
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(ruleChainId);
    List<RuleNode> ruleNodes = getRuleChainNodes(tenantId, ruleChainId);
    Map<RuleNodeId, Integer> ruleNodeIndexMap = new HashMap<>();
    for (RuleNode node : ruleNodes) {
        ruleNodeIndexMap.put(node.getId(), ruleNodes.indexOf(node));
    }
    ruleChainMetaData.setNodes(ruleNodes);
    if (ruleChain.getFirstRuleNodeId() != null) {
        ruleChainMetaData.setFirstNodeIndex(ruleNodeIndexMap.get(ruleChain.getFirstRuleNodeId()));
    }
    for (RuleNode node : ruleNodes) {
        int fromIndex = ruleNodeIndexMap.get(node.getId());
        List<EntityRelation> nodeRelations = getRuleNodeRelations(tenantId, node.getId());
        for (EntityRelation nodeRelation : nodeRelations) {
            String type = nodeRelation.getType();
            if (nodeRelation.getTo().getEntityType() == EntityType.RULE_NODE) {
                RuleNodeId toNodeId = new RuleNodeId(nodeRelation.getTo().getId());
                int toIndex = ruleNodeIndexMap.get(toNodeId);
                ruleChainMetaData.addConnectionInfo(fromIndex, toIndex, type);
            } else if (nodeRelation.getTo().getEntityType() == EntityType.RULE_CHAIN) {
                log.warn("[{}][{}] Unsupported node relation: {}", tenantId, ruleChainId, nodeRelation.getTo());
            }
        }
    }
    return ruleChainMetaData;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) HashMap(java.util.HashMap) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 57 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain 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 58 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain 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 59 with RuleChain

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

the class BaseRuleChainServiceTest method testFindRuleChainById.

@Test
public void testFindRuleChainById() {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setTenantId(tenantId);
    ruleChain.setName("My RuleChain");
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChain foundRuleChain = ruleChainService.findRuleChainById(tenantId, savedRuleChain.getId());
    Assert.assertNotNull(foundRuleChain);
    Assert.assertEquals(savedRuleChain, foundRuleChain);
    ruleChainService.deleteRuleChainById(tenantId, savedRuleChain.getId());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Test(org.junit.Test)

Example 60 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain 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)

Aggregations

RuleChain (org.thingsboard.server.common.data.rule.RuleChain)67 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)26 Test (org.junit.Test)20 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)19 ArrayList (java.util.ArrayList)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)15 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)14 ApiOperation (io.swagger.annotations.ApiOperation)13 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)13 Edge (org.thingsboard.server.common.data.edge.Edge)13 TenantId (org.thingsboard.server.common.data.id.TenantId)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)11 PageLink (org.thingsboard.server.common.data.page.PageLink)11 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)10 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)10 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)9 EdgeId (org.thingsboard.server.common.data.id.EdgeId)8 List (java.util.List)7 Device (org.thingsboard.server.common.data.Device)7