Search in sources :

Example 1 with DataSetRowAction

use of org.talend.dataprep.transformation.api.action.DataSetRowAction in project data-prep by Talend.

the class PipelineTest method testCompileAction.

@Test
public void testCompileAction() throws Exception {
    // Given
    final RunnableAction mockAction = new RunnableAction() {

        @Override
        public DataSetRowAction getRowAction() {
            return new DataSetRowAction() {

                @Override
                public void compile(ActionContext actionContext) {
                    actionContext.get("ExecutedCompile", p -> true);
                }

                @Override
                public DataSetRow apply(DataSetRow dataSetRow, ActionContext context) {
                    return dataSetRow;
                }
            };
        }
    };
    final ActionContext actionContext = new ActionContext(new TransformationContext());
    final Node node = NodeBuilder.source().to(new CompileNode(mockAction, actionContext)).to(output).build();
    final RowMetadata rowMetadata = new RowMetadata();
    final DataSetRow row = new DataSetRow(rowMetadata);
    // when
    assertFalse(actionContext.has("ExecutedCompile"));
    node.exec().receive(row, rowMetadata);
    // then
    assertTrue(actionContext.has("ExecutedCompile"));
    assertTrue(actionContext.get("ExecutedCompile"));
}
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) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test)

Example 2 with DataSetRowAction

use of org.talend.dataprep.transformation.api.action.DataSetRowAction in project data-prep by Talend.

the class PipelineTest method testAction.

@Test
public void testAction() throws Exception {
    // Given
    final RunnableAction mockAction = new RunnableAction() {

        @Override
        public DataSetRowAction getRowAction() {
            return (r, context) -> {
                context.get("ExecutedApply", p -> true);
                return r;
            };
        }
    };
    final ActionContext actionContext = new ActionContext(new TransformationContext());
    final Node node = NodeBuilder.source().to(new ActionNode(mockAction, actionContext)).to(output).build();
    final RowMetadata rowMetadata = new RowMetadata();
    final DataSetRow row = new DataSetRow(rowMetadata);
    // when
    assertFalse(actionContext.has("ExecutedApply"));
    node.exec().receive(row, rowMetadata);
    // then
    assertTrue(actionContext.has("ExecutedApply"));
    assertTrue(actionContext.get("ExecutedApply"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) CoreMatchers(org.hamcrest.CoreMatchers) BasicLink(org.talend.dataprep.transformation.pipeline.link.BasicLink) HashMap(java.util.HashMap) Test(org.junit.Test) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) ArrayList(java.util.ArrayList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.talend.dataprep.transformation.pipeline.node(org.talend.dataprep.transformation.pipeline.node) END_OF_STREAM(org.talend.dataprep.transformation.pipeline.Signal.END_OF_STREAM) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) CloneLink(org.talend.dataprep.transformation.pipeline.link.CloneLink) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) DataSetRowAction(org.talend.dataprep.transformation.api.action.DataSetRowAction) Assert(org.junit.Assert) DataSet(org.talend.dataprep.api.dataset.DataSet) NodeBuilder(org.talend.dataprep.transformation.pipeline.builder.NodeBuilder) Before(org.junit.Before) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) 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) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test)

Example 3 with DataSetRowAction

use of org.talend.dataprep.transformation.api.action.DataSetRowAction in project data-prep by Talend.

the class ActionNodesBuilder method build.

/**
 * Build the actions pipeline
 */
public Node build() {
    final StatisticsNodesBuilder statisticsNodesBuilder = StatisticsNodesBuilder.builder().analyzerService(// 
    analyzerService).actionRegistry(// 
    actionRegistry).statisticsAdapter(// 
    statisticsAdapter).allowSchemaAnalysis(// 
    allowSchemaAnalysis).actions(// 
    actions).columns(initialMetadata.getColumns());
    final NodeBuilder builder = NodeBuilder.source();
    // unless we don't have initial metadata or we explicitly ask it
    if (needStatisticsBefore || initialMetadata.getColumns().isEmpty()) {
        LOGGER.debug("No initial metadata submitted for transformation, computing new one.");
        builder.to(statisticsNodesBuilder.buildPreStatistics());
    }
    // transformation context is the parent of every action context
    // it will hold all the action context
    // that makes it the perfect entry point to clean up all the contexts
    final TransformationContext context = new TransformationContext();
    // * an action node
    for (final RunnableAction nextAction : actions) {
        // some actions need fresh statistics
        // in those cases, we gather the rows in a reservoir node that triggers statistics computation
        // before dispatching each row to the next node
        final Node neededReservoir = statisticsNodesBuilder.buildIntermediateStatistics(nextAction);
        if (neededReservoir != null) {
            builder.to(neededReservoir);
        }
        final DataSetRowAction rowAction = nextAction.getRowAction();
        builder.to(new CompileNode(nextAction, context.create(rowAction, initialMetadata)));
        builder.to(new ActionNode(nextAction, context.in(rowAction)));
    }
    // when it is explicitly asked and the actions changes the columns
    if (needStatisticsAfter) {
        builder.to(statisticsNodesBuilder.buildPostStatistics());
    }
    // cleanup all contexts after all actions
    builder.to(new CleanUpNode(context));
    return builder.build();
}
Also used : DataSetRowAction(org.talend.dataprep.transformation.api.action.DataSetRowAction) CompileNode(org.talend.dataprep.transformation.pipeline.node.CompileNode) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) CleanUpNode(org.talend.dataprep.transformation.pipeline.node.CleanUpNode) ActionNode(org.talend.dataprep.transformation.pipeline.node.ActionNode) CompileNode(org.talend.dataprep.transformation.pipeline.node.CompileNode) Node(org.talend.dataprep.transformation.pipeline.Node) CleanUpNode(org.talend.dataprep.transformation.pipeline.node.CleanUpNode) ActionNode(org.talend.dataprep.transformation.pipeline.node.ActionNode) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext)

Example 4 with DataSetRowAction

use of org.talend.dataprep.transformation.api.action.DataSetRowAction in project data-prep by Talend.

the class PipelineTest method testRecompileAction.

@Test
public void testRecompileAction() throws Exception {
    // Given
    AtomicInteger compileCount = new AtomicInteger();
    final RunnableAction mockAction = new RunnableAction() {

        @Override
        public DataSetRowAction getRowAction() {
            return new DataSetRowAction() {

                @Override
                public void compile(ActionContext actionContext) {
                    compileCount.incrementAndGet();
                    actionContext.setActionStatus(ActionContext.ActionStatus.OK);
                }

                @Override
                public DataSetRow apply(DataSetRow dataSetRow, ActionContext context) {
                    return dataSetRow;
                }
            };
        }
    };
    final ActionContext actionContext = new ActionContext(new TransformationContext());
    final Node node = NodeBuilder.source().to(new CompileNode(mockAction, actionContext)).to(output).build();
    final RowMetadata rowMetadata = new RowMetadata();
    final DataSetRow row = new DataSetRow(rowMetadata);
    // when
    assertEquals(0, compileCount.get());
    node.exec().receive(row, rowMetadata);
    // Change row metadata in middle of the transformation (to trigger
    rowMetadata.addColumn(new ColumnMetadata());
    // new compile).
    node.exec().receive(row, rowMetadata);
    // then
    assertEquals(2, compileCount.get());
}
Also used : DataSetRowAction(org.talend.dataprep.transformation.api.action.DataSetRowAction) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test)

Example 5 with DataSetRowAction

use of org.talend.dataprep.transformation.api.action.DataSetRowAction in project data-prep by Talend.

the class PipelineTest method testCanceledAction.

@Test
public void testCanceledAction() throws Exception {
    // Given
    final RunnableAction mockAction = new RunnableAction() {

        @Override
        public DataSetRowAction getRowAction() {
            return (r, context) -> {
                context.get("Executed", p -> true);
                return r;
            };
        }
    };
    final ActionContext actionContext = new ActionContext(new TransformationContext());
    actionContext.setActionStatus(ActionContext.ActionStatus.CANCELED);
    final Node node = NodeBuilder.source().to(new ActionNode(mockAction, actionContext)).to(output).build();
    final RowMetadata rowMetadata = new RowMetadata();
    final DataSetRow row = new DataSetRow(rowMetadata);
    // when
    node.exec().receive(row, rowMetadata);
    // then
    assertFalse(actionContext.has("Executed"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) CoreMatchers(org.hamcrest.CoreMatchers) BasicLink(org.talend.dataprep.transformation.pipeline.link.BasicLink) HashMap(java.util.HashMap) Test(org.junit.Test) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) ArrayList(java.util.ArrayList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.talend.dataprep.transformation.pipeline.node(org.talend.dataprep.transformation.pipeline.node) END_OF_STREAM(org.talend.dataprep.transformation.pipeline.Signal.END_OF_STREAM) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) CloneLink(org.talend.dataprep.transformation.pipeline.link.CloneLink) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) DataSetRowAction(org.talend.dataprep.transformation.api.action.DataSetRowAction) Assert(org.junit.Assert) DataSet(org.talend.dataprep.api.dataset.DataSet) NodeBuilder(org.talend.dataprep.transformation.pipeline.builder.NodeBuilder) Before(org.junit.Before) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) 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) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test)

Aggregations

RunnableAction (org.talend.dataprep.transformation.actions.common.RunnableAction)6 DataSetRowAction (org.talend.dataprep.transformation.api.action.DataSetRowAction)6 TransformationContext (org.talend.dataprep.transformation.api.action.context.TransformationContext)6 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)5 ActionContext (org.talend.dataprep.transformation.api.action.context.ActionContext)5 Test (org.junit.Test)4 DataSetRow (org.talend.dataprep.api.dataset.row.DataSetRow)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ColumnMetadata (org.talend.dataprep.api.dataset.ColumnMetadata)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CoreMatchers (org.hamcrest.CoreMatchers)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 Assert (org.junit.Assert)2 Before (org.junit.Before)2 DataSet (org.talend.dataprep.api.dataset.DataSet)2 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)2 END_OF_STREAM (org.talend.dataprep.transformation.pipeline.Signal.END_OF_STREAM)2 NodeBuilder (org.talend.dataprep.transformation.pipeline.builder.NodeBuilder)2