use of org.talend.dataprep.api.action.ActionDefinition in project data-prep by Talend.
the class TestI18nKeysForActionsTest method allActionShouldHaveTranslations.
@Test
public void allActionShouldHaveTranslations() {
for (ActionDefinition actionMetadata : allActions) {
final String name = actionMetadata.getName();
assertNotNull(name);
assertNotEquals("", name);
actionMetadata.getLabel(Locale.US);
actionMetadata.getDescription(Locale.US);
String toString = actionMetadata.getName() + "," + actionMetadata.getCategory(Locale.US) + "," + actionMetadata.getLabel(Locale.US) + "," + actionMetadata.getDescription(Locale.US);
LOGGER.info(toString);
for (Parameter param : actionMetadata.getParameters(Locale.US)) {
assertParameter(param);
}
}
}
use of org.talend.dataprep.api.action.ActionDefinition in project data-prep by Talend.
the class SimpleSuggestionEngineTest method shouldSuggestionsShouldBeSorted.
@Test
public void shouldSuggestionsShouldBeSorted() throws IOException {
final String json = IOUtils.toString(this.getClass().getResourceAsStream("sample_column.json"), UTF_8);
ObjectMapper mapper = new ObjectMapper();
final ColumnMetadata columnMetadata = mapper.readValue(json, ColumnMetadata.class);
List<ActionDefinition> actions = new ArrayList<>();
actions.add(new FillIfEmpty());
actions.add(new FillInvalid());
actions.add(new DeleteInvalid());
actions.add(new DeleteEmpty());
actions.add(new Absolute());
actions.add(new UpperCase());
final Stream<Suggestion> suggestions = engine.score(actions.stream(), columnMetadata);
int currentScore = Integer.MAX_VALUE;
for (Suggestion suggestion : suggestions.collect(Collectors.toList())) {
assertTrue(currentScore >= suggestion.getScore());
currentScore = suggestion.getScore();
}
}
use of org.talend.dataprep.api.action.ActionDefinition 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);
}
}
use of org.talend.dataprep.api.action.ActionDefinition 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);
}
use of org.talend.dataprep.api.action.ActionDefinition in project data-prep by Talend.
the class ActionMetadataValidationTest method checkScopeConsistency_should_throw_exception_on_missing_scope.
@Test
public void checkScopeConsistency_should_throw_exception_on_missing_scope() throws Exception {
// given
final Map<String, String> parameters = new HashMap<>();
parameters.put("column_id", "0001");
ActionDefinition actionMock = new ActionMetadataExtendingColumn();
// when
try {
validator.checkScopeConsistency(actionMock, parameters);
fail("should have thrown TDP exception because param scope is missing");
}// then
catch (final TalendRuntimeException e) {
assertThat(e.getCode(), is(MISSING_ACTION_SCOPE));
}
}
Aggregations