Search in sources :

Example 1 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class PermissionFilters method getReferenceBaseFilter.

/**
 * Returns a filter for records referenced by the given field.
 */
public Optional<FormulaNode> getReferenceBaseFilter(ResourceId fieldId) {
    FormulaNode filter = fieldFilters.get(fieldId);
    if (filter == null) {
        return Optional.absent();
    }
    SymbolNode fieldExpr = new SymbolNode(fieldId);
    return Optional.of(filter.transform(x -> {
        if (x.equals(fieldExpr)) {
            return new SymbolNode(ColumnModel.ID_SYMBOL);
        } else {
            return x;
        }
    }));
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode) AndFunction(org.activityinfo.model.formula.functions.AndFunction) java.util(java.util) FormulaParser(org.activityinfo.model.formula.FormulaParser) FormulaValidator(org.activityinfo.analysis.FormulaValidator) Formulas(org.activityinfo.model.formula.Formulas) Multimap(com.google.common.collect.Multimap) FormPermissions(org.activityinfo.model.form.FormPermissions) FormulaNode(org.activityinfo.model.formula.FormulaNode) HashMultimap(com.google.common.collect.HashMultimap) FieldReference(org.activityinfo.analysis.FieldReference) ResourceId(org.activityinfo.model.resource.ResourceId) Optional(com.google.common.base.Optional) ColumnModel(org.activityinfo.model.query.ColumnModel) Operation(org.activityinfo.model.database.Operation) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FormTree(org.activityinfo.model.formTree.FormTree) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode)

Example 2 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class FormInputViewModelBuilder method buildRelevanceCalculator.

private void buildRelevanceCalculator(FormTree.Node node) {
    String formula = node.getField().getRelevanceConditionExpression();
    FormulaNode rootNode;
    try {
        rootNode = FormulaParser.parse(formula);
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Invalid relevance formula: " + formula, e);
        return;
    }
    relevanceCalculators.put(node.getFieldId(), instance -> {
        evalContext.setInstance(instance);
        try {
            return rootNode.evaluateAsBoolean(evalContext);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Failed to evaluate relevance condition", e);
            return true;
        }
    });
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode)

Example 3 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class LookupKeySetTest method multipleTextKeysTest.

@Test
public void multipleTextKeysTest() {
    TestingStorageProvider catalog = setup.getCatalog();
    SimpleReferenceForm refForm = catalog.getSimpleReferenceForm();
    MultipleTextKeysForm multTextKeyForm = catalog.getMultipleTextKeysForm();
    FormField refField = refForm.getRefField();
    CompoundExpr pathToKey1 = new CompoundExpr(refField.getId(), MultipleTextKeysForm.FIRST_TEXT_KEY_ID.asString());
    CompoundExpr pathToKey2 = new CompoundExpr(refField.getId(), MultipleTextKeysForm.SECOND_TEXT_KEY_ID.asString());
    FormTree formTree = catalog.getFormTree(multTextKeyForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, refField);
    Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(refField.getId());
    assertThat(formulas.values(), containsInAnyOrder(pathToKey1, pathToKey2));
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree) LookupKey(org.activityinfo.model.formTree.LookupKey) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 4 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class RelevanceHandler method applyRelevanceLogic.

private void applyRelevanceLogic(final FormField field) {
    if (field.hasRelevanceConditionExpression()) {
        try {
            FormulaLexer lexer = new FormulaLexer(field.getRelevanceConditionExpression());
            FormulaParser parser = new FormulaParser(lexer);
            FormulaNode expr = parser.parse();
            FieldContainer fieldContainer = simpleFormPanel.getWidgetCreator().get(field.getId());
            if (fieldContainer != null) {
                FormModel model = simpleFormPanel.getModel();
                Optional<FormInstance> instance = model.getWorkingInstance(field.getId(), simpleFormPanel.getSelectedKey(field));
                FormClass formClass = model.getClassByField(field.getId());
                boolean relevant;
                if (instance.isPresent()) {
                    relevant = expr.evaluateAsBoolean(new FormEvalContext(formClass, instance.get()));
                } else {
                    relevant = expr.evaluateAsBoolean(new FormEvalContext(formClass, new FormInstance(ResourceId.generateSubmissionId(formClass), formClass.getId())));
                }
                fieldContainer.getFieldWidget().setReadOnly(!relevant);
                if (!relevant) {
                    if (resettingValues) {
                        // we are in resetting state -> handle nested relevance
                        fieldContainer.getFieldWidget().clearValue();
                        fieldContainer.getFieldWidget().fireValueChanged();
                    }
                    fieldsWithAppliedRelevance.add(fieldContainer);
                } else {
                    fieldsWithAppliedRelevance.remove(fieldContainer);
                }
            } else {
                Log.error("Can't find container for fieldId: " + field.getId() + ", fieldName: " + field.getLabel() + ", expression: " + field.getRelevanceConditionExpression());
            }
        } catch (Exception e) {
            Log.error("Error: Unable to apply relevance logic. FieldId: " + field.getId() + ", fieldName: " + field.getLabel() + ", expression: " + field.getRelevanceConditionExpression(), e);
        }
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) FormulaLexer(org.activityinfo.model.formula.FormulaLexer) FormClass(org.activityinfo.model.form.FormClass) FormulaParser(org.activityinfo.model.formula.FormulaParser) FormInstance(org.activityinfo.model.form.FormInstance) FormEvalContext(org.activityinfo.model.form.FormEvalContext)

Example 5 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class CalculatedTypeEditor method validateExpression.

private void validateExpression() {
    String expression = expressionTextBox.getValue();
    if (Strings.isNullOrEmpty(expression)) {
        expressionGroup.setShowValidationMessage(false);
        expressionGroup.validationStateType(ValidationStateType.NONE);
        return;
    }
    try {
        FormulaLexer lexer = new FormulaLexer(expression);
        FormulaParser parser = new FormulaParser(lexer);
        FormulaNode expr = parser.parse();
    } catch (Exception e) {
        expressionGroup.setShowValidationMessage(true);
        expressionGroup.setValidationMessage(I18N.CONSTANTS.calculationExpressionIsInvalid());
        expressionGroup.validationStateType(ValidationStateType.ERROR);
        return;
    }
    // Ok
    expressionGroup.setShowValidationMessage(false);
    expressionGroup.validationStateType(ValidationStateType.SUCCESS);
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) FormulaLexer(org.activityinfo.model.formula.FormulaLexer) FormulaParser(org.activityinfo.model.formula.FormulaParser)

Aggregations

FormulaNode (org.activityinfo.model.formula.FormulaNode)27 SymbolNode (org.activityinfo.model.formula.SymbolNode)8 FormTree (org.activityinfo.model.formTree.FormTree)7 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)7 Test (org.junit.Test)7 LookupKey (org.activityinfo.model.formTree.LookupKey)5 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)5 ColumnView (org.activityinfo.model.query.ColumnView)4 FormulaParser (org.activityinfo.model.formula.FormulaParser)3 ResourceId (org.activityinfo.model.resource.ResourceId)3 ForeignKeyId (org.activityinfo.store.query.shared.join.ForeignKeyId)3 FormEvalContext (org.activityinfo.model.form.FormEvalContext)2 FormField (org.activityinfo.model.form.FormField)2 FormPermissions (org.activityinfo.model.form.FormPermissions)2 FormulaLexer (org.activityinfo.model.formula.FormulaLexer)2 FieldValue (org.activityinfo.model.type.FieldValue)2 JoinNode (org.activityinfo.store.query.shared.join.JoinNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 HashMultimap (com.google.common.collect.HashMultimap)1