Search in sources :

Example 26 with RunnableAction

use of org.talend.dataprep.transformation.actions.common.RunnableAction in project data-prep by Talend.

the class ActionParser method parse.

/**
 * Return the parsed actions ready to be run.
 *
 * @param actions the actions to be parsed as string.
 * @return the parsed actions.
 * @throws IllegalArgumentException if <code>actions</code> is null.
 */
public List<RunnableAction> parse(String actions) {
    if (actions == null) {
        // Actions cannot be null (but can be empty string for no op actions).
        throw new IllegalArgumentException("Actions parameter can not be null.");
    }
    if (StringUtils.isEmpty(actions)) {
        return Collections.emptyList();
    }
    try {
        // Parse action JSON
        final Actions parsedActions = mapper.reader(Actions.class).readValue(actions);
        final List<Action> allActions = parsedActions.getActions();
        // Create closures from parsed actions
        final List<RunnableAction> builtActions = new ArrayList<>(allActions.size() + 1);
        // 
        allActions.stream().filter(// 
        parsedAction -> parsedAction != null && parsedAction.getName() != null).forEach(parsedAction -> {
            String actionNameLowerCase = parsedAction.getName().toLowerCase();
            final ActionDefinition metadata = actionRegistry.get(actionNameLowerCase);
            builtActions.add(factory.create(metadata, parsedAction.getParameters()));
        });
        // all set: wraps everything and return to caller
        return builtActions;
    } catch (TalendRuntimeException tpe) {
        // leave TDPException as is
        throw tpe;
    } catch (Exception e) {
        throw new TalendRuntimeException(BaseErrorCodes.UNABLE_TO_PARSE_JSON, e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) ActionRegistry(org.talend.dataprep.transformation.pipeline.ActionRegistry) ActionFactory(org.talend.dataprep.transformation.actions.common.ActionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Actions(org.talend.dataprep.api.preparation.Actions) ArrayList(java.util.ArrayList) List(java.util.List) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) BaseErrorCodes(org.talend.dataprep.BaseErrorCodes) Action(org.talend.dataprep.api.preparation.Action) Collections(java.util.Collections) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) Action(org.talend.dataprep.api.preparation.Action) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) Actions(org.talend.dataprep.api.preparation.Actions) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) ArrayList(java.util.ArrayList) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException)

Example 27 with RunnableAction

use of org.talend.dataprep.transformation.actions.common.RunnableAction in project data-prep by Talend.

the class ActionsStaticProfiler method profile.

public ActionsProfile profile(final List<ColumnMetadata> columns, final List<RunnableAction> actions) {
    final Map<Action, ActionDefinition> metadataByAction = getActionMetadataByAction(actions);
    // Compile actions
    final Set<String> originalColumns = columns.stream().map(ColumnMetadata::getId).collect(toSet());
    final Set<String> valueModifiedColumns = new HashSet<>();
    final Set<String> metadataModifiedColumns = new HashSet<>();
    int createColumnActions = 0;
    // Analyze what columns to look at during analysis
    for (Map.Entry<Action, ActionDefinition> entry : metadataByAction.entrySet()) {
        final ActionDefinition actionMetadata = entry.getValue();
        final Action action = entry.getKey();
        Set<ActionDefinition.Behavior> behavior = actionMetadata.getBehavior();
        boolean createColumn = false;
        for (ActionDefinition.Behavior currentBehavior : behavior) {
            switch(currentBehavior) {
                case VALUES_ALL:
                    // All values are going to be changed, and all original columns are going to be modified.
                    valueModifiedColumns.addAll(originalColumns);
                    break;
                case METADATA_CHANGE_TYPE:
                    valueModifiedColumns.add(action.getParameters().get(COLUMN_ID.getKey()));
                    metadataModifiedColumns.add(action.getParameters().get(COLUMN_ID.getKey()));
                    break;
                case VALUES_COLUMN:
                    valueModifiedColumns.add(action.getParameters().get(COLUMN_ID.getKey()));
                    break;
                case VALUES_MULTIPLE_COLUMNS:
                    // Add the action's source column
                    valueModifiedColumns.add(action.getParameters().get(COLUMN_ID.getKey()));
                    // ... then add all column parameter (COLUMN_ID is string, not column)
                    final List<Parameter> parameters = actionMetadata.getParameters(Locale.US);
                    valueModifiedColumns.addAll(// 
                    parameters.stream().filter(// 
                    parameter -> ParameterType.valueOf(parameter.getType().toUpperCase()) == ParameterType.COLUMN).map(// 
                    parameter -> action.getParameters().get(parameter.getName())).collect(Collectors.toList()));
                    break;
                case METADATA_COPY_COLUMNS:
                case METADATA_CREATE_COLUMNS:
                    createColumn = true;
                    break;
                case METADATA_DELETE_COLUMNS:
                case METADATA_CHANGE_NAME:
                    // Do nothing: no need to re-analyze where only name was changed.
                    break;
                default:
                    break;
            }
        }
        if (createColumn || isCreateColumnParameterOn(action)) {
            createColumnActions++;
        }
    }
    // when values are modified, we need to do a full analysis (schema + invalid + stats)
    boolean needFullAnalysis = !valueModifiedColumns.isEmpty() || createColumnActions > 0;
    // when only metadata is modified, we need to re-evaluate the invalids entries
    boolean needOnlyInvalidAnalysis = !needFullAnalysis && !metadataModifiedColumns.isEmpty();
    // only the columns with modified values or new columns need the schema + stats analysis
    SerializablePredicate<ColumnMetadata> filterForFullAnalysis = new FilterForFullAnalysis(originalColumns, valueModifiedColumns);
    // only the columns with metadata change or value changes need to re-evaluate invalids
    Predicate<ColumnMetadata> filterForInvalidAnalysis = new FilterForInvalidAnalysis(filterForFullAnalysis, metadataModifiedColumns);
    return new ActionsProfile(needFullAnalysis, needOnlyInvalidAnalysis, filterForFullAnalysis, filterForInvalidAnalysis, filterForInvalidAnalysis, metadataByAction);
}
Also used : ImplicitParameters(org.talend.dataprep.transformation.actions.common.ImplicitParameters) java.util(java.util) ActionRegistry(org.talend.dataprep.transformation.pipeline.ActionRegistry) Parameter(org.talend.dataprep.parameters.Parameter) Predicate(java.util.function.Predicate) ParameterType(org.talend.dataprep.parameters.ParameterType) Collectors(java.util.stream.Collectors) ScopeCategory(org.talend.dataprep.transformation.actions.category.ScopeCategory) COLUMN_ID(org.talend.dataprep.transformation.actions.common.ImplicitParameters.COLUMN_ID) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) Action(org.talend.dataprep.api.preparation.Action) Collectors.toSet(java.util.stream.Collectors.toSet) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) Action(org.talend.dataprep.api.preparation.Action) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) Parameter(org.talend.dataprep.parameters.Parameter)

Example 28 with RunnableAction

use of org.talend.dataprep.transformation.actions.common.RunnableAction in project data-prep by Talend.

the class MetadataChangesOnActionsGenerator method compileActionsOnMetadata.

/**
 * Compile the given actions, hence updating the given row metadata and return the later.
 *
 * @param actions the actions to compile.
 * @param startingRowMetadata the row metadata to start from.
 * @return the updated row metadata from the actions compilation.
 */
private RowMetadata compileActionsOnMetadata(List<RunnableAction> actions, final RowMetadata startingRowMetadata) {
    final RowMetadata updatedRowMetadata = startingRowMetadata.clone();
    TransformationContext transformationContext = new TransformationContext();
    // compile every action within the transformation context
    for (RunnableAction action : actions) {
        final DataSetRowAction rowAction = action.getRowAction();
        final ActionContext actionContext = transformationContext.create(rowAction, updatedRowMetadata);
        rowAction.compile(actionContext);
    }
    // the cleanup is REALLY important (as it can close http connection in case of a lookup for instance)
    transformationContext.cleanup();
    return updatedRowMetadata;
}
Also used : DataSetRowAction(org.talend.dataprep.transformation.api.action.DataSetRowAction) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext)

Example 29 with RunnableAction

use of org.talend.dataprep.transformation.actions.common.RunnableAction in project data-prep by Talend.

the class MakeLineHeaderTest method should_keep_header_after_modifications_with_concat.

@Test
public void should_keep_header_after_modifications_with_concat() {
    // given
    Long rowId = 0L;
    // row 1
    Map<String, String> rowContent = new HashMap<>();
    rowContent.put("0000", "David");
    rowContent.put("0001", "Bowie");
    final DataSetRow row1 = new DataSetRow(rowContent);
    row1.setTdpId(rowId++);
    // row 2
    rowContent = new HashMap<>();
    rowContent.put("0000", "John");
    rowContent.put("0001", "Lennon");
    final DataSetRow row2 = new DataSetRow(rowContent);
    row2.setTdpId(rowId++);
    // row 3
    rowContent = new HashMap<>();
    rowContent.put("0000", "Johnny");
    rowContent.put("0001", "Lennon");
    final DataSetRow row3 = new DataSetRow(rowContent);
    row3.setTdpId(rowId++);
    // when
    final Map<String, String> makeHeaderParameters = new HashMap<>();
    makeHeaderParameters.put(ImplicitParameters.SCOPE.getKey().toLowerCase(), "line");
    makeHeaderParameters.put("row_id", row2.getTdpId().toString());
    final RunnableAction makeHeader = factory.create(action, makeHeaderParameters);
    final Map<String, String> concatColumnParameters = new HashMap<>();
    concatColumnParameters.put(ImplicitParameters.SCOPE.getKey().toLowerCase(), "column");
    concatColumnParameters.put(ImplicitParameters.COLUMN_ID.getKey().toLowerCase(), "0000");
    concatColumnParameters.put(OtherColumnParameters.MODE_PARAMETER, "other_column_mode");
    concatColumnParameters.put(OtherColumnParameters.SELECTED_COLUMN_PARAMETER, "0001");
    concatColumnParameters.put(ActionsUtils.CREATE_NEW_COLUMN, "true");
    final RunnableAction concat = factory.create(new Concat(), concatColumnParameters);
    ActionTestWorkbench.test(Arrays.asList(row1, row2, row3), actionRegistry, makeHeader, concat);
    // then
    // assertEquals(3, row3.getRowMetadata().getColumns().size());
    assertEquals("John", row1.getRowMetadata().getById("0000").getName());
    assertEquals("Lennon", row1.getRowMetadata().getById("0001").getName());
    assertEquals("John", row2.getRowMetadata().getById("0000").getName());
    assertEquals("Lennon", row2.getRowMetadata().getById("0001").getName());
    assertEquals("John", row3.getRowMetadata().getById("0000").getName());
    assertEquals("Lennon", row3.getRowMetadata().getById("0001").getName());
}
Also used : Concat(org.talend.dataprep.transformation.actions.column.Concat) HashMap(java.util.HashMap) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test) AbstractMetadataBaseTest(org.talend.dataprep.transformation.actions.AbstractMetadataBaseTest)

Example 30 with RunnableAction

use of org.talend.dataprep.transformation.actions.common.RunnableAction in project data-prep by Talend.

the class ActionParserTest method empty_string_should_return_noop_actions.

@Test
public void empty_string_should_return_noop_actions() {
    // given
    DataSetRow actualRow = getDataSetRow();
    DataSetRow expectedRow = actualRow.clone();
    RowMetadata expectedMetadata = getRowMetadata();
    List<RunnableAction> actualActions = actionParser.parse("");
    // when
    final RunnableAction[] actions = actualActions.toArray(new RunnableAction[actualActions.size()]);
    ActionTestWorkbench.test(actualRow, actionRegistry, actions);
    // then
    assertEquals(expectedRow, actualRow);
    assertEquals(expectedMetadata, actualRow.getRowMetadata());
}
Also used : RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test)

Aggregations

RunnableAction (org.talend.dataprep.transformation.actions.common.RunnableAction)32 Test (org.junit.Test)23 TransformationContext (org.talend.dataprep.transformation.api.action.context.TransformationContext)22 ActionContext (org.talend.dataprep.transformation.api.action.context.ActionContext)21 DataSetRow (org.talend.dataprep.api.dataset.row.DataSetRow)20 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)18 HashMap (java.util.HashMap)15 AbstractMetadataBaseTest (org.talend.dataprep.transformation.actions.AbstractMetadataBaseTest)14 DataSetRowAction (org.talend.dataprep.transformation.api.action.DataSetRowAction)6 ActionDefinition (org.talend.dataprep.api.action.ActionDefinition)4 ColumnMetadata (org.talend.dataprep.api.dataset.ColumnMetadata)4 Action (org.talend.dataprep.api.preparation.Action)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Before (org.junit.Before)3 DataSet (org.talend.dataprep.api.dataset.DataSet)3 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)3 CoreMatchers (org.hamcrest.CoreMatchers)2