Search in sources :

Example 41 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project jmeter by apache.

the class Jexl2Function method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    //$NON-NLS-1$
    String str = "";
    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();
    //$NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    try {
        JexlContext jc = new MapContext();
        //$NON-NLS-1$
        jc.set("log", log);
        //$NON-NLS-1$
        jc.set("ctx", jmctx);
        //$NON-NLS-1$
        jc.set("vars", vars);
        //$NON-NLS-1$
        jc.set("props", JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        //$NON-NLS-1$
        jc.set("threadName", Thread.currentThread().getName());
        //$NON-NLS-1$ (may be null)
        jc.set("sampler", currentSampler);
        //$NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult);
        //$NON-NLS-1$
        jc.set("OUT", System.out);
        // Now evaluate the script, getting the result
        Expression e = getJexlEngine().createExpression(exp);
        Object o = e.evaluate(jc);
        if (o != null) {
            str = o.toString();
        }
        if (vars != null && varName.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varName, str);
        }
    } catch (Exception e) {
        log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
    }
    return str;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Example 42 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project dhis2-core by dhis2.

the class ExpressionUtils method evaluate.

/**
     * @param expression the expression.
     * @param vars the variables, can be null.
     * @param strict indicates whether to use strict or lenient engine mode.
     * @return the result of the evaluation.
     */
private static Object evaluate(String expression, Map<String, Object> vars, boolean strict) {
    expression = expression.replaceAll(IGNORED_KEYWORDS_REGEX, StringUtils.EMPTY);
    JexlEngine engine = strict ? JEXL_STRICT : JEXL;
    Expression exp = engine.createExpression(expression);
    JexlContext context = vars != null ? new MapContext(vars) : new MapContext();
    return exp.evaluate(context);
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Example 43 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project kie-wb-common by kiegroup.

the class Bpmn2JsonMarshaller method marshallSequenceFlow.

protected void marshallSequenceFlow(SequenceFlow sequenceFlow, BPMNPlane plane, JsonGenerator generator, float xOffset, float yOffset) throws JsonGenerationException, IOException {
    // dont marshal "dangling" sequence flow..better to just omit than fail
    if (sequenceFlow.getSourceRef() == null || sequenceFlow.getTargetRef() == null) {
        return;
    }
    Map<String, Object> properties = new LinkedHashMap<String, Object>();
    // check null for sequence flow name
    if (sequenceFlow.getName() != null && !"".equals(sequenceFlow.getName())) {
        properties.put(NAME, StringEscapeUtils.unescapeXml(sequenceFlow.getName()));
    } else {
        properties.put(NAME, "");
    }
    // overwrite name if elementname extension element is present
    String elementName = Utils.getMetaDataValue(sequenceFlow.getExtensionValues(), "elementname");
    if (elementName != null) {
        properties.put(NAME, elementName);
    }
    putDocumentationProperty(sequenceFlow, properties);
    if (sequenceFlow.isIsImmediate()) {
        properties.put(ISIMMEDIATE, "true");
    } else {
        properties.put(ISIMMEDIATE, "false");
    }
    Expression conditionExpression = sequenceFlow.getConditionExpression();
    if (conditionExpression instanceof FormalExpression) {
        setConditionExpressionProperties((FormalExpression) conditionExpression, properties, "mvel");
    }
    boolean foundBgColor = false;
    boolean foundBrColor = false;
    boolean foundFontColor = false;
    boolean foundSelectable = false;
    Iterator<FeatureMap.Entry> iter = sequenceFlow.getAnyAttribute().iterator();
    while (iter.hasNext()) {
        FeatureMap.Entry entry = iter.next();
        if (entry.getEStructuralFeature().getName().equals("priority")) {
            String priorityStr = String.valueOf(entry.getValue());
            if (priorityStr != null) {
                try {
                    Integer priorityInt = Integer.parseInt(priorityStr);
                    if (priorityInt >= 1) {
                        properties.put(PRIORITY, entry.getValue());
                    } else {
                        _logger.error("Priority must be equal or greater than 1.");
                    }
                } catch (NumberFormatException e) {
                    _logger.error("Priority must be a number.");
                }
            }
        }
        if (entry.getEStructuralFeature().getName().equals("background-color") || entry.getEStructuralFeature().getName().equals("bgcolor")) {
            properties.put(BGCOLOR, entry.getValue());
            foundBgColor = true;
        }
        if (entry.getEStructuralFeature().getName().equals("border-color") || entry.getEStructuralFeature().getName().equals("bordercolor")) {
            properties.put(BORDERCOLOR, entry.getValue());
            foundBrColor = true;
        }
        if (entry.getEStructuralFeature().getName().equals("fontsize")) {
            properties.put(FONTSIZE, entry.getValue());
            foundBrColor = true;
        }
        if (entry.getEStructuralFeature().getName().equals("color") || entry.getEStructuralFeature().getName().equals("fontcolor")) {
            properties.put(FONTCOLOR, entry.getValue());
            foundFontColor = true;
        }
        if (entry.getEStructuralFeature().getName().equals("selectable")) {
            properties.put(ISSELECTABLE, entry.getValue());
            foundSelectable = true;
        }
    }
    if (!foundBgColor) {
        properties.put(BGCOLOR, defaultSequenceflowColor);
    }
    if (!foundBrColor) {
        properties.put(BORDERCOLOR, defaultSequenceflowColor);
    }
    if (!foundFontColor) {
        properties.put(FONTCOLOR, defaultSequenceflowColor);
    }
    if (!foundSelectable) {
        properties.put(ISSELECTABLE, "true");
    }
    // simulation properties
    setSimulationProperties(sequenceFlow.getId(), properties);
    // Custom attributes for Stunner's connectors - source/target auto connection flag.
    String sourcePropertyName = Bpmn2OryxManager.MAGNET_AUTO_CONNECTION + Bpmn2OryxManager.SOURCE;
    String sourceConnectorAuto = Utils.getMetaDataValue(sequenceFlow.getExtensionValues(), sourcePropertyName);
    if (sourceConnectorAuto != null && sourceConnectorAuto.trim().length() > 0) {
        properties.put(sourcePropertyName, sourceConnectorAuto);
    }
    String targetPropertyName = Bpmn2OryxManager.MAGNET_AUTO_CONNECTION + Bpmn2OryxManager.TARGET;
    String targetConnectorAuto = Utils.getMetaDataValue(sequenceFlow.getExtensionValues(), targetPropertyName);
    if (targetConnectorAuto != null && targetConnectorAuto.trim().length() > 0) {
        properties.put(targetPropertyName, targetConnectorAuto);
    }
    marshallProperties(properties, generator);
    generator.writeObjectFieldStart("stencil");
    generator.writeObjectField("id", "SequenceFlow");
    generator.writeEndObject();
    generator.writeArrayFieldStart("childShapes");
    generator.writeEndArray();
    generator.writeArrayFieldStart("outgoing");
    generator.writeStartObject();
    generator.writeObjectField("resourceId", sequenceFlow.getTargetRef().getId());
    generator.writeEndObject();
    generator.writeEndArray();
    Bounds sourceBounds = ((BPMNShape) findDiagramElement(plane, sequenceFlow.getSourceRef())).getBounds();
    Bounds targetBounds = ((BPMNShape) findDiagramElement(plane, sequenceFlow.getTargetRef())).getBounds();
    generator.writeArrayFieldStart("dockers");
    List<Point> waypoints = ((BPMNEdge) findDiagramElement(plane, sequenceFlow)).getWaypoint();
    if (waypoints.size() > 1) {
        Point waypoint = waypoints.get(0);
        writeWaypointObject(generator, waypoint.getX() - sourceBounds.getX(), waypoint.getY() - sourceBounds.getY());
    } else {
        writeWaypointObject(generator, sourceBounds.getWidth() / 2, sourceBounds.getHeight() / 2);
    }
    for (int i = 1; i < waypoints.size() - 1; i++) {
        Point waypoint = waypoints.get(i);
        writeWaypointObject(generator, waypoint.getX(), waypoint.getY());
    }
    if (waypoints.size() > 1) {
        Point waypoint = waypoints.get(waypoints.size() - 1);
        writeWaypointObject(generator, waypoint.getX() - targetBounds.getX(), waypoint.getY() - targetBounds.getY());
    } else {
        writeWaypointObject(generator, targetBounds.getWidth() / 2, targetBounds.getHeight() / 2);
    }
    generator.writeEndArray();
}
Also used : Bounds(org.eclipse.dd.dc.Bounds) Point(org.eclipse.dd.dc.Point) FormalExpression(org.eclipse.bpmn2.FormalExpression) BPMNShape(org.eclipse.bpmn2.di.BPMNShape) Point(org.eclipse.dd.dc.Point) LinkedHashMap(java.util.LinkedHashMap) FeatureMap(org.eclipse.emf.ecore.util.FeatureMap) Entry(java.util.Map.Entry) Expression(org.eclipse.bpmn2.Expression) FormalExpression(org.eclipse.bpmn2.FormalExpression) DataObject(org.eclipse.bpmn2.DataObject) BPMNEdge(org.eclipse.bpmn2.di.BPMNEdge)

Example 44 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project kie-wb-common by kiegroup.

the class RelationGridTest method testInitialiseUiModel.

@Test
public void testInitialiseUiModel() throws Exception {
    relation.getColumn().add(new InformationItem() {

        {
            getName().setValue("first column header");
        }
    });
    final String firstRowValue = "first column value 1";
    final String secondRowValue = "first column value 2";
    relation.getRow().add(new List() {

        {
            getExpression().add(new LiteralExpression() {

                {
                    setText(firstRowValue);
                }
            });
        }
    });
    relation.getRow().add(new List() {

        {
            getExpression().add(new LiteralExpression() {

                {
                    setText(secondRowValue);
                }
            });
        }
    });
    expression = Optional.of(relation);
    setupGrid(0);
    assertEquals(2, grid.getModel().getRowCount());
    assertEquals(firstRowValue, grid.getModel().getRow(0).getCells().get(1).getValue().getValue());
    assertEquals(secondRowValue, grid.getModel().getRow(1).getCells().get(1).getValue().getValue());
}
Also used : LiteralExpression(org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression) InformationItem(org.kie.workbench.common.dmn.api.definition.v1_1.InformationItem) List(org.kie.workbench.common.dmn.api.definition.v1_1.List) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 45 with Expression

use of org.kie.workbench.common.dmn.api.definition.v1_1.Expression in project kie-wb-common by kiegroup.

the class UndefinedExpressionGridTest method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() {
    definition = new UndefinedExpressionEditorDefinition(gridPanel, gridLayer, definitionUtils, sessionManager, sessionCommandManager, canvasCommandFactory, cellEditorControls, listSelector, translationService, expressionEditorDefinitionsSupplier);
    expression = definition.getModelClass();
    final ExpressionEditorDefinitions expressionEditorDefinitions = new ExpressionEditorDefinitions();
    expressionEditorDefinitions.add(definition);
    expressionEditorDefinitions.add(literalExpressionEditorDefinition);
    doReturn(expressionEditorDefinitions).when(expressionEditorDefinitionsSupplier).get();
    doReturn(ExpressionType.LITERAL_EXPRESSION).when(literalExpressionEditorDefinition).getType();
    doReturn(LiteralExpression.class.getSimpleName()).when(literalExpressionEditorDefinition).getName();
    doReturn(Optional.of(literalExpression)).when(literalExpressionEditorDefinition).getModelClass();
    doReturn(Optional.of(literalExpressionEditor)).when(literalExpressionEditorDefinition).getEditor(any(GridCellTuple.class), any(Optional.class), any(HasExpression.class), any(Optional.class), any(Optional.class), anyInt());
    doReturn(0).when(parent).getRowIndex();
    doReturn(0).when(parent).getColumnIndex();
    doReturn(parentGridWidget).when(parent).getGridWidget();
    doReturn(mock(GridData.class)).when(parentGridWidget).getModel();
    doReturn(session).when(sessionManager).getCurrentSession();
    doReturn(handler).when(session).getCanvasHandler();
    when(parentGridWidget.getModel()).thenReturn(parentGridUiModel);
    when(parent.getGridWidget()).thenReturn(parentGridWidget);
    when(parent.getRowIndex()).thenReturn(0);
    when(parent.getColumnIndex()).thenReturn(1);
}
Also used : ExpressionEditorDefinitions(org.kie.workbench.common.dmn.client.editors.expressions.types.ExpressionEditorDefinitions) HasExpression(org.kie.workbench.common.dmn.api.definition.HasExpression) GridCellTuple(org.kie.workbench.common.dmn.client.widgets.grid.model.GridCellTuple) Optional(java.util.Optional) LiteralExpression(org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression) DMNGridData(org.kie.workbench.common.dmn.client.widgets.grid.model.DMNGridData) GridData(org.uberfire.ext.wires.core.grids.client.model.GridData) Before(org.junit.Before)

Aggregations

Expression (org.kie.workbench.common.dmn.api.definition.v1_1.Expression)19 BaseExpressionGrid (org.kie.workbench.common.dmn.client.widgets.grid.BaseExpressionGrid)15 Expression (org.apache.commons.jexl2.Expression)14 GridCellTuple (org.kie.workbench.common.dmn.client.widgets.grid.model.GridCellTuple)13 JexlContext (org.apache.commons.jexl2.JexlContext)12 Test (org.junit.Test)12 HasExpression (org.kie.workbench.common.dmn.api.definition.HasExpression)12 LiteralExpression (org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression)11 HasName (org.kie.workbench.common.dmn.api.definition.HasName)10 JexlEngine (org.apache.commons.jexl2.JexlEngine)9 InformationItem (org.kie.workbench.common.dmn.api.definition.v1_1.InformationItem)9 BaseGridCellValue (org.uberfire.ext.wires.core.grids.client.model.impl.BaseGridCellValue)9 GridWidget (org.uberfire.ext.wires.core.grids.client.widget.grid.GridWidget)9 MapContext (org.apache.commons.jexl2.MapContext)8 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)7 Expression (org.eclipse.xtext.resource.bug385636.Expression)7 Optional (java.util.Optional)6 Before (org.junit.Before)6 Decision (org.kie.workbench.common.dmn.api.definition.v1_1.Decision)6 Name (org.kie.workbench.common.dmn.api.property.dmn.Name)6