Skip to content

Commit

Permalink
#184 Adding tests for FM normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasLacroix committed Feb 20, 2022
1 parent 4b6d32c commit 02e2687
Show file tree
Hide file tree
Showing 34 changed files with 829 additions and 302 deletions.
9 changes: 9 additions & 0 deletions ml2wf/src/main/java/com/ml2wf/util/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public final class FileHandler {
* TODO: manage considering used norm (e.g. BPMN)
*/
private static final String[] WF_EXTENSIONS = { "bpmn", "bpmn2" };
/**
* FeatureModel's extensions.
* TODO: manage considering used norm (e.g. BPMN)
*/
private static final String[] FM_EXTENSIONS = { "xml" };

/**
* Returns the default file name.
Expand Down Expand Up @@ -92,6 +97,10 @@ public static String[] getWfExtensions() {
return WF_EXTENSIONS.clone();
}

public static String[] getFMExtensions() {
return FM_EXTENSIONS.clone();
}

/**
* Processes the given {@code File} instance by calling the
* {@link #processExistingFile(File)} method if the given {@code outputFile}
Expand Down
1 change: 1 addition & 0 deletions ml2wf/src/main/java/com/ml2wf/util/XMLManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ public static String sanitizeName(String name) {
name = name.replaceFirst(Notation.DOCUMENTATION_VOC, "");
name = name.replaceFirst(Notation.REFERENCE_VOC, "");
name = name.replace(Notation.OPTIONALITY, "");
name = name.replace(Notation.GENERIC_VOC, "");
name = name.trim();
name = name.replace(" ", "_");
Pattern validNamePattern = RegexManager.getValidFeatureNamePattern();
Expand Down
11 changes: 4 additions & 7 deletions ml2wf/src/main/java/com/ml2wf/v2/tree/AbstractTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,17 @@
* It implements the {@link ITreeManipulable} interface defining the main tree operations
* and is {@link IObservable} to facilitate its manipulation.
*
* <p>
*
* An {@link AbstractTree} contains {@link Identifiable} children which allows it to use an
* {@link TreeInternalMemory} to improve its performances when manipulating them.
*
* @param <T> the type of the tree's nodes
*
* @see ITreeManipulable
* @see IObservable
* @see Identifiable
* @see AbstractTreeEvent
*
* @since 1.1.0
*/
@EqualsAndHashCode
@Log4j2
public abstract class AbstractTree<T extends Identifiable<I>, I> implements ITreeManipulable<T, I>,
public abstract class AbstractTree<T extends Identifiable<I>, I> implements ITreeManipulable<T, I>, // TODO: rework extend Identifiable to have access cf parent
IObservable<AbstractTreeEvent<T>> {

@NonNull protected final Set<IObserver<AbstractTreeEvent<T>>> observers = new HashSet<>();
Expand Down Expand Up @@ -74,12 +68,15 @@ public boolean hasChildren() {

@Override
public Either<String, T> appendChild(T child) {
// TODO: check if already have a parent
children.add(child);
// TODO: set parent
return Either.right(child);
}

@Override
public Optional<T> removeChild(T child) {
// TODO: set parent to null if is child of this
return (children.remove(child)) ? Optional.of(child) : Optional.empty();
}

Expand Down
17 changes: 0 additions & 17 deletions ml2wf/src/main/java/com/ml2wf/v2/tree/ITreeIterator.java

This file was deleted.

95 changes: 95 additions & 0 deletions ml2wf/src/main/java/com/ml2wf/v2/tree/TaskConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.ml2wf.v2.tree;

import com.ml2wf.util.XMLManager;
import com.ml2wf.v2.tree.fm.FeatureModel;
import com.ml2wf.v2.tree.fm.FeatureModelTask;
import com.ml2wf.v2.tree.wf.Workflow;
import com.ml2wf.v2.tree.wf.WorkflowTask;
import com.ml2wf.v2.tree.wf.util.WorkflowTaskUtil;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Utils class providing methods for converting tasks :
*
* <p>
*
* <ul>
* <li>toFeatureModelTask : {@link WorkflowTask} -> {@link FeatureModelTask}</li>
* <li>toWorkflowTask : {@link FeatureModelTask} -> {@link WorkflowTask}</li>
* </ul>
*
* @see WorkflowTask
* @see FeatureModelTask
*
* @since 1.1.0
*/
@Log4j2
public final class TaskConverter {

// TODO: support nested

// TODO: split into 2 classes implementing the same interface (+ same for cleanup ?)

@NonNull private final FeatureModel featureModel;
@NonNull private final Workflow workflow;

public TaskConverter(@NonNull FeatureModel featureModel, @NonNull Workflow workflow) {
this.featureModel = featureModel;
this.workflow = workflow;
}

private FeatureModelTask postFMConversionCleanUp(FeatureModelTask featureModelTask) {
// TODO: to improve (replace opt + ref + _Step)
featureModelTask.setName(XMLManager.sanitizeName(featureModelTask.getName()));
// TODO: sanitize documentation
return featureModelTask;
}

/**
* Returns a {@link FeatureModelTask} instance based on the given {@link WorkflowTask}.
*
* @param workflowTask the {@link WorkflowTask}'s instance
*
* @return the resulting {@link FeatureModelTask}
*/
public FeatureModelTask toFeatureModelTask(WorkflowTask workflowTask) {
log.debug("Converting WorkflowTask [{}] to FeatureModelTask.", workflowTask.getName());
List<String> descriptions = Stream.of(workflowTask.getDocumentation().getContent())
.filter(Predicate.not(String::isBlank))
.collect(Collectors.toList());
Optional<String> optReferenceName = WorkflowTaskUtil.getReferenceName(workflowTask);
var createdFmTask = FeatureModelTask.FeatureModelTaskFactory.createTask(
workflowTask.getName(), workflowTask.isAbstract(), !workflowTask.isOptional(), descriptions);
if (optReferenceName.isPresent()) {
Optional<FeatureModelTask> optReferenceFMTask = featureModel.getChildWithIdentity(optReferenceName.get());
optReferenceFMTask.ifPresentOrElse(r -> r.appendChild(createdFmTask), () -> {
log.error("Unknown reference {}. Skipping...", optReferenceName);
// TODO: attach to Unmanaged node once implemented
});
}
return postFMConversionCleanUp(createdFmTask);
}

/**
* Returns a {@link WorkflowTask} instance based on the given {@link FeatureModelTask}.
*
* @param featureModel the {@link FeatureModelTask}'s instance
*
* @return the resulting {@link WorkflowTask}
*/
public WorkflowTask toWorkflowTask(FeatureModelTask featureModel) {
log.debug("Converting WorkflowTask [{}] to WorkflowTask.", featureModel.getName());
String description = featureModel.getDescriptions().stream()
.map(FeatureModelTask.Description::toString)
.collect(Collectors.joining("\n"));
return WorkflowTask.WorkflowTaskFactory.createTask(featureModel.getName(), description);
}
}

52 changes: 0 additions & 52 deletions ml2wf/src/main/java/com/ml2wf/v2/tree/TreeInternalMemory.java

This file was deleted.

Loading

0 comments on commit 02e2687

Please sign in to comment.