Search in sources :

Example 1 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class CollectionFacadeIT method createRule.

private Rule createRule(String collectionId, String attributeId) {
    final AutoLinkRule rule = new AutoLinkRule(new Rule("rule1", Rule.RuleType.AUTO_LINK, Rule.RuleTiming.ALL, new DataDocument()));
    rule.setCollection1(collectionId);
    rule.setAttribute1(attributeId);
    rule.setCollection2("some collection to test");
    rule.setAttribute2("a1");
    rule.setLinkType("some link type");
    return rule.getRule();
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) Rule(io.lumeer.api.model.Rule) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule)

Example 2 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class TaskProcessingFacadeIT method testSyntaxExceptionBlocklyRules.

@Test
public void testSyntaxExceptionBlocklyRules() throws InterruptedException {
    final String ruleName = "blocklyRule2";
    final String syntaxException = "  syntax exception @#$~@#$~@$";
    final Collection c1 = createCollection("c1", "name1", Map.of("a0", "A", "a1", "B"));
    final Document c1d1 = documentFacade.createDocument(c1.getId(), new Document(new DataDocument("a0", "line1").append("a1", 10)));
    final Document c1d2 = documentFacade.createDocument(c1.getId(), new Document(new DataDocument("a0", "line2").append("a1", 20)));
    final BlocklyRule rule = new BlocklyRule(new Rule(ruleName, Rule.RuleType.BLOCKLY, Rule.RuleTiming.UPDATE, new DataDocument()));
    rule.setDryRun(false);
    rule.setResultTimestamp(0);
    rule.setJs("var i, newDocument;\n" + "\n" + "\n" + "var lumeer = Polyglot.import('lumeer');\n" + syntaxException);
    c1.getRules().put(ruleName, rule.getRule());
    collectionFacade.updateCollection(c1.getId(), c1);
    collectionFacade.upsertRule(c1.getId(), ruleName, rule.getRule());
    documentFacade.patchDocumentData(c1.getId(), c1d1.getId(), new DataDocument("a1", 11));
    Collection updatedCollection;
    BlocklyRule updatedRule;
    int cycles = 10;
    do {
        Thread.sleep(500);
        updatedCollection = collectionFacade.getCollection(c1.getId());
        updatedRule = new BlocklyRule(updatedCollection.getRules().get(ruleName));
    } while (updatedRule.getResultTimestamp() == 0 && cycles-- > 0);
    assertThat(updatedRule.getError()).contains(syntaxException);
    assertThat(System.currentTimeMillis() - updatedRule.getResultTimestamp()).isLessThan(5000);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Collection(io.lumeer.api.model.Collection) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) Rule(io.lumeer.api.model.Rule) BlocklyRule(io.lumeer.api.model.rule.BlocklyRule) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) BlocklyRule(io.lumeer.api.model.rule.BlocklyRule) Test(org.junit.Test)

Example 3 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class TaskProcessingFacadeIT method testAutoLinkRules.

@Test
public void testAutoLinkRules() throws InterruptedException {
    final String ruleName = "autoLinkRule";
    final Collection c1 = createCollection("ac1", "auto1", Map.of("a0", "A", "a1", "B"));
    final Collection c2 = createCollection("ac2", "auto2", Map.of("a0", "C", "a1", "D"));
    final LinkType l = linkTypeFacade.createLinkType(new LinkType("link1", List.of(c1.getId(), c2.getId()), Collections.emptyList(), null, null, null));
    final Document c1d1 = documentFacade.createDocument(c1.getId(), new Document(new DataDocument("a0", "line1").append("a1", 10)));
    final Document c1d2 = documentFacade.createDocument(c1.getId(), new Document(new DataDocument("a0", "line2").append("a1", 20)));
    final Document c2d1 = documentFacade.createDocument(c2.getId(), new Document(new DataDocument("a0", "subline1").append("a1", 10)));
    final Document c2d2 = documentFacade.createDocument(c2.getId(), new Document(new DataDocument("a0", "subline2").append("a1", 20)));
    final Document c2d3 = documentFacade.createDocument(c2.getId(), new Document(new DataDocument("a0", "subline3").append("a1", 20)));
    final Document c2d4 = documentFacade.createDocument(c2.getId(), new Document(new DataDocument("a0", "subline3").append("a1", 30)));
    final Document c2d5 = documentFacade.createDocument(c2.getId(), new Document(new DataDocument("a0", "subline3").append("a1", 30)));
    final Document c2d6 = documentFacade.createDocument(c2.getId(), new Document(new DataDocument("a0", "subline3").append("a1", 30)));
    final AutoLinkRule rule = new AutoLinkRule(new Rule(ruleName, Rule.RuleType.AUTO_LINK, Rule.RuleTiming.ALL, new DataDocument()));
    rule.setCollection1(c1.getId());
    rule.setAttribute1("a1");
    rule.setCollection2(c2.getId());
    rule.setAttribute2("a1");
    rule.setLinkType(l.getId());
    c1.getRules().put(ruleName, rule.getRule());
    collectionFacade.updateCollection(c1.getId(), c1);
    collectionFacade.upsertRule(c1.getId(), ruleName, rule.getRule());
    assertThat(getLinksByType(l.getId())).hasSize(0);
    documentFacade.patchDocumentData(c1.getId(), c1d1.getId(), new DataDocument("a1", 11));
    // there is no way we can detect the change :-(
    Thread.sleep(1000);
    assertThat(getLinksByType(l.getId())).hasSize(0);
    documentFacade.patchDocumentData(c1.getId(), c1d1.getId(), new DataDocument("a1", 10));
    List<LinkInstance> instances = waitForLinksByType(l.getId());
    assertThat(instances).hasSize(1);
    assertThat(instances.get(0).getDocumentIds()).contains(c1d1.getId(), c2d1.getId());
    documentFacade.patchDocumentData(c1.getId(), c1d1.getId(), new DataDocument("a1", 11));
    instances = waitForLinksByType(l.getId());
    assertThat(instances).hasSize(0);
    documentFacade.patchDocumentData(c1.getId(), c1d1.getId(), new DataDocument("a1", 20));
    instances = waitForLinksByType(l.getId());
    assertThat(instances).hasSize(2);
    assertThat(instances.get(0).getDocumentIds()).contains(c1d1.getId()).containsAnyOf(c2d2.getId(), c2d3.getId());
    assertThat(instances.get(1).getDocumentIds()).contains(c1d1.getId()).containsAnyOf(c2d2.getId(), c2d3.getId());
    final Document c1d3 = documentFacade.createDocument(c1.getId(), new Document(new DataDocument("a0", "line3").append("a1", 30)));
    instances = waitForLinksByType(l.getId());
    assertThat(instances).hasSize(5);
    documentFacade.deleteDocument(c1.getId(), c1d1.getId());
    instances = waitForLinksByType(l.getId());
    assertThat(instances).hasSize(3);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Collection(io.lumeer.api.model.Collection) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) Rule(io.lumeer.api.model.Rule) BlocklyRule(io.lumeer.api.model.rule.BlocklyRule) LinkInstance(io.lumeer.api.model.LinkInstance) LinkType(io.lumeer.api.model.LinkType) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) Test(org.junit.Test)

Example 4 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class CollectionFacade method upsertRule.

public Collection upsertRule(final String collectionId, final String ruleId, final Rule rule) {
    final Collection collection = collectionDao.getCollectionById(collectionId);
    permissionsChecker.checkRole(collection, RoleType.TechConfig);
    final Collection originalCollection = collection.copy();
    Map<String, Rule> rules = Objects.requireNonNullElse(collection.getRules(), new HashMap<>());
    Rule originalRule = rules.get(ruleId);
    rule.checkConfiguration(originalRule);
    permissionsChecker.checkRulePermissions(rule);
    rules.put(ruleId, rule);
    collection.setRules(rules);
    mapResourceUpdateValues(collection);
    final Collection updatedCollection = collectionDao.updateCollection(collection.getId(), collection, originalCollection);
    return mapCollection(updatedCollection);
}
Also used : Collection(io.lumeer.api.model.Collection) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) Rule(io.lumeer.api.model.Rule) CronRule(io.lumeer.api.model.rule.CronRule)

Example 5 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class CollectionFacade method runRule.

public void runRule(final Collection collection, final String ruleId) {
    if (adapter.getDocumentsCountByCollection(collection.getId()) > 2_000) {
        throw new UnsuccessfulOperationException("Too many documents in the source collection");
    }
    final Rule rule = collection.getRules().get(ruleId);
    if (rule != null && rule.getType() == Rule.RuleType.AUTO_LINK) {
        final AutoLinkRule autoLinkRule = new AutoLinkRule(rule);
        final String otherCollectionId = autoLinkRule.getCollection2().equals(collection.getId()) ? autoLinkRule.getCollection1() : autoLinkRule.getCollection2();
        final String attributeId = autoLinkRule.getCollection1().equals(collection.getId()) ? autoLinkRule.getAttribute1() : autoLinkRule.getAttribute2();
        final Attribute attribute = collection.getAttributes().stream().filter(a -> a.getId().equals(attributeId)).findFirst().orElse(null);
        final Collection otherCollection = getCollection(otherCollectionId);
        final String otherAttributeId = autoLinkRule.getCollection2().equals(collection.getId()) ? autoLinkRule.getAttribute1() : autoLinkRule.getAttribute2();
        final Attribute otherAttribute = otherCollection.getAttributes().stream().filter(a -> a.getId().equals(otherAttributeId)).findFirst().orElse(null);
        final Map<String, AllowedPermissions> permissions = permissionsChecker.getCollectionsPermissions(List.of(collection, otherCollection));
        if (adapter.getDocumentsCountByCollection(otherCollectionId) > 10_000) {
            throw new UnsuccessfulOperationException("Too many documents in the target collection");
        }
        final LinkType linkType = linkTypeDao.getLinkType(autoLinkRule.getLinkType());
        final AutoLinkBatchTask task = taskFactory.getInstance(AutoLinkBatchTask.class);
        task.setupBatch(autoLinkRule, linkType, collection, attribute, otherCollection, otherAttribute, getCurrentUser(), permissions);
        taskExecutor.submitTask(task);
    } else if (rule != null && rule.getType() == Rule.RuleType.CRON) {
        final CronRule cronRule = new CronRule(rule);
        List<Document> documents = new ArrayList<>();
        if (cronRule.getViewId() != null) {
            try {
                final View view = viewDao.getViewById(cronRule.getViewId());
                final User user = AuthenticatedUser.getMachineUser();
                final AllowedPermissions allowedPermissions = AllowedPermissions.allAllowed();
                documents = DocumentUtils.getDocuments(collectionDao, documentDao, dataDao, userDao, groupDao, selectionListDao, getOrganization(), getProject(), view.getQuery(), user, cronRule.getLanguage(), allowedPermissions, null);
                documents = documents.stream().filter(document -> document.getCollectionId().equals(collection.getId())).collect(Collectors.toList());
            } catch (ResourceNotFoundException ignore) {
            }
        }
        RuleTask task = taskFactory.getInstance(RuleTask.class);
        task.setRule(rule.getName(), rule, collection, documents);
        taskExecutor.submitTask(task);
    }
}
Also used : AutoLinkBatchTask(io.lumeer.core.task.AutoLinkBatchTask) TaskExecutor(io.lumeer.core.task.TaskExecutor) CollectionUtil(io.lumeer.api.util.CollectionUtil) ZonedDateTime(java.time.ZonedDateTime) User(io.lumeer.api.model.User) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) LinkTypeDao(io.lumeer.storage.api.dao.LinkTypeDao) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) CollectionDao(io.lumeer.storage.api.dao.CollectionDao) ResourceType(io.lumeer.api.model.ResourceType) DefaultViewConfigDao(io.lumeer.storage.api.dao.DefaultViewConfigDao) Map(java.util.Map) ResourceCommentDao(io.lumeer.storage.api.dao.ResourceCommentDao) CollectionAdapter(io.lumeer.core.adapter.CollectionAdapter) Permission(io.lumeer.api.model.Permission) RoleType(io.lumeer.api.model.RoleType) View(io.lumeer.api.model.View) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) Document(io.lumeer.api.model.Document) Set(java.util.Set) NoResourcePermissionException(io.lumeer.core.exception.NoResourcePermissionException) Collectors(java.util.stream.Collectors) LinkType(io.lumeer.api.model.LinkType) Objects(java.util.Objects) AttributesResource(io.lumeer.api.model.common.AttributesResource) List(java.util.List) ResourceUtils(io.lumeer.api.util.ResourceUtils) PostConstruct(javax.annotation.PostConstruct) FavoriteItemDao(io.lumeer.storage.api.dao.FavoriteItemDao) Attribute(io.lumeer.api.model.Attribute) ContextualTaskFactory(io.lumeer.core.task.ContextualTaskFactory) UserDao(io.lumeer.storage.api.dao.UserDao) HashMap(java.util.HashMap) SelectionListDao(io.lumeer.storage.api.dao.SelectionListDao) AllowedPermissions(io.lumeer.api.model.AllowedPermissions) Function(java.util.function.Function) CollectionPurpose(io.lumeer.api.model.CollectionPurpose) GroupDao(io.lumeer.storage.api.dao.GroupDao) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) DataDao(io.lumeer.storage.api.dao.DataDao) ViewDao(io.lumeer.storage.api.dao.ViewDao) CodeGenerator(io.lumeer.core.util.CodeGenerator) Rule(io.lumeer.api.model.Rule) DocumentDao(io.lumeer.storage.api.dao.DocumentDao) Permissions(io.lumeer.api.model.Permissions) ResourceAdapter(io.lumeer.core.adapter.ResourceAdapter) DocumentUtils(io.lumeer.core.util.DocumentUtils) FileAttachment(io.lumeer.api.model.FileAttachment) Project(io.lumeer.api.model.Project) LinkInstanceDao(io.lumeer.storage.api.dao.LinkInstanceDao) UnsuccessfulOperationException(io.lumeer.engine.api.exception.UnsuccessfulOperationException) ConversionFacade(io.lumeer.core.facade.conversion.ConversionFacade) RuleTask(io.lumeer.core.task.RuleTask) RequestScoped(javax.enterprise.context.RequestScoped) CronRule(io.lumeer.api.model.rule.CronRule) AutoLinkBatchTask(io.lumeer.core.task.AutoLinkBatchTask) Collection(io.lumeer.api.model.Collection) User(io.lumeer.api.model.User) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) Attribute(io.lumeer.api.model.Attribute) View(io.lumeer.api.model.View) AllowedPermissions(io.lumeer.api.model.AllowedPermissions) UnsuccessfulOperationException(io.lumeer.engine.api.exception.UnsuccessfulOperationException) Collection(io.lumeer.api.model.Collection) List(java.util.List) ArrayList(java.util.ArrayList) RuleTask(io.lumeer.core.task.RuleTask) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) Rule(io.lumeer.api.model.Rule) CronRule(io.lumeer.api.model.rule.CronRule) LinkType(io.lumeer.api.model.LinkType) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) CronRule(io.lumeer.api.model.rule.CronRule)

Aggregations

Rule (io.lumeer.api.model.Rule)14 DataDocument (io.lumeer.engine.api.data.DataDocument)10 Collection (io.lumeer.api.model.Collection)9 AutoLinkRule (io.lumeer.api.model.rule.AutoLinkRule)8 Document (io.lumeer.api.model.Document)6 LinkType (io.lumeer.api.model.LinkType)5 BlocklyRule (io.lumeer.api.model.rule.BlocklyRule)5 Test (org.junit.Test)5 Attribute (io.lumeer.api.model.Attribute)3 CronRule (io.lumeer.api.model.rule.CronRule)3 Date (java.util.Date)3 CollectionPurpose (io.lumeer.api.model.CollectionPurpose)2 LinkInstance (io.lumeer.api.model.LinkInstance)2 Permissions (io.lumeer.api.model.Permissions)2 ZapierRule (io.lumeer.api.model.rule.ZapierRule)2 AuthenticatedUser (io.lumeer.core.auth.AuthenticatedUser)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Document (org.bson.Document)2