Commit 9629dc82 authored by Levi Lucio's avatar Levi Lucio
Browse files

fixed NPE issue and simplified code

refs 3039
parent d48cf8be
...@@ -22,7 +22,6 @@ import static org.fortiss.tooling.base.ui.utils.ConstraintsBaseUIUtils.deactivat ...@@ -22,7 +22,6 @@ import static org.fortiss.tooling.base.ui.utils.ConstraintsBaseUIUtils.deactivat
import static org.fortiss.tooling.base.utils.ConstraintsBaseUtils.getActiveConfigurationsTransitively; import static org.fortiss.tooling.base.utils.ConstraintsBaseUtils.getActiveConfigurationsTransitively;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -172,8 +171,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -172,8 +171,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
// Updates the current objective if any object is selected on the navigator window. // Updates the current objective if any object is selected on the navigator window.
EList<ConstraintConfiguration> configs = null; EList<ConstraintConfiguration> configs = null;
float successPercentage;
if(selection instanceof ITreeSelection) { if(selection instanceof ITreeSelection) {
if(((ITreeSelection)selection).getFirstElement() != null) { if(((ITreeSelection)selection).getFirstElement() != null) {
Object objectChosenElement = ((ITreeSelection)selection).getFirstElement(); Object objectChosenElement = ((ITreeSelection)selection).getFirstElement();
...@@ -208,30 +205,23 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -208,30 +205,23 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
currentDevelopmentProcess = process; currentDevelopmentProcess = process;
successPercentage = generateScoresAndUpdateUI(process);
List<String> configNames = List<String> configNames =
configs.stream().map(c -> c.getName()).collect(Collectors.toList()); configs.stream().map(c -> c.getName()).collect(Collectors.toList());
configNames
.forEach(item -> {
if(percentageMap.get(item) != null) {
String percentagevalue =
new DecimalFormat("##.#").format(percentageMap
.get(item).successCount /
percentageMap.get(item).totalCount * 100);
configNames.set(configNames.indexOf(item), item + " (" +
percentagevalue + "%)");
} else
configNames.set(configNames.indexOf(item), item + " N/A");
});
String[] configNamesArray = new String[configNames.size()]; String[] configNamesArray = new String[configNames.size()];
configNames.toArray(configNamesArray); configNames.toArray(configNamesArray);
currentObjectiveCombo.setItems(configNamesArray); currentObjectiveCombo.setItems(configNamesArray);
currentObjectiveCombo.setText(currentDevelopmentProcess currentObjectiveCombo.setText(currentDevelopmentProcess
.getCurrentObjective().getName()); .getCurrentObjective().getName());
currentObjectiveCombo.setToolTipText(new DecimalFormat("##.#")
.format(successPercentage * 100) + updateObjectiveByName(currentDevelopmentProcess.getCurrentObjective()
"% of the constraints are satisfied"); .getName());
// if(successPercentage >= 0f) {
// currentObjectiveCombo.setToolTipText(new DecimalFormat("##")
// .format(successPercentage * 100) +
// "% of the constraints are satisfied");
// }
} }
} }
} }
...@@ -276,21 +266,24 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -276,21 +266,24 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
* @return returns the float score * @return returns the float score
*/ */
private float generateScoresAndUpdateUI(ConstraintBasedDevelopmentProcess process) { private float generateScoresAndUpdateUI(ConstraintBasedDevelopmentProcess process) {
float successPercentage = 0f;
generateConstraintsResultMap(process, true); float successPercentage = -1f;
generateScoresForAllConfigs(process); generateConstraintsResultMap(process);
generateScoresForConfig(process);
if(percentageMap.get(process.getCurrentObjective().getName()) != null) { if(percentageMap.get(process.getCurrentObjective().getName()) != null) {
successPercentage = successPercentage =
percentageMap.get(process.getCurrentObjective().getName()).successCount / percentageMap.get(process.getCurrentObjective().getName()).successCount /
percentageMap.get(process.getCurrentObjective().getName()).totalCount; percentageMap.get(process.getCurrentObjective().getName()).totalCount;
if(successPercentage <= .5) {
currentObjectiveCombo.setBackground(SWTResourceManager.getColor(new RGB(0,
(float).7 - (successPercentage) / 2, 1)));
} else {
currentObjectiveCombo.setBackground(SWTResourceManager.getColor(new RGB(130,
(successPercentage) - (float).3, 1)));
}
} }
if(successPercentage <= .5) {
currentObjectiveCombo.setBackground(SWTResourceManager.getColor(new RGB(0, (float).7 -
(successPercentage) / 2, 1)));
} else {
currentObjectiveCombo.setBackground(SWTResourceManager.getColor(new RGB(130,
(successPercentage) - (float).3, 1)));
}
return successPercentage; return successPercentage;
} }
...@@ -301,6 +294,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -301,6 +294,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
* The name of the objective to update to. * The name of the objective to update to.
*/ */
public void updateObjectiveByName(String newObjectiveName) { public void updateObjectiveByName(String newObjectiveName) {
ConstraintConfiguration newObjective = getConstraintConfigByName(newObjectiveName); ConstraintConfiguration newObjective = getConstraintConfigByName(newObjectiveName);
final ConstraintConfiguration oldObjective = final ConstraintConfiguration oldObjective =
currentDevelopmentProcess.getCurrentObjective(); currentDevelopmentProcess.getCurrentObjective();
...@@ -328,17 +322,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -328,17 +322,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
List<String> configNames = List<String> configNames =
currentDevelopmentProcess.getConfigurations().stream().map(c -> c.getName()) currentDevelopmentProcess.getConfigurations().stream().map(c -> c.getName())
.collect(Collectors.toList()); .collect(Collectors.toList());
configNames
.forEach(item -> {
if(percentageMap.get(item) != null) {
String percentagevalue =
new DecimalFormat("##.#").format(percentageMap.get(item).successCount /
percentageMap.get(item).totalCount * 100);
configNames.set(configNames.indexOf(item), item + " (" +
percentagevalue + "%)");
} else
configNames.set(configNames.indexOf(item), item + " N/A");
});
String[] configNamesArray = new String[configNames.size()]; String[] configNamesArray = new String[configNames.size()];
configNames.toArray(configNamesArray); configNames.toArray(configNamesArray);
...@@ -355,7 +338,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -355,7 +338,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
currentObjectiveCombo currentObjectiveCombo
.setText(currentDevelopmentProcess.getCurrentObjective().getName()); .setText(currentDevelopmentProcess.getCurrentObjective().getName());
currentObjectiveCombo.setToolTipText(new DecimalFormat("##.#") currentObjectiveCombo.setToolTipText(new DecimalFormat("##")
.format(successPercentage * 100) + "% of the constraints are satisfied"); .format(successPercentage * 100) + "% of the constraints are satisfied");
} }
} }
...@@ -365,11 +348,8 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -365,11 +348,8 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
* configuration or any of its dependencies. * configuration or any of its dependencies.
* *
* @param process * @param process
* @param considerOnlyCurrent
* the flag indicating whether only until current objective will be computed or not
*/ */
private void generateConstraintsResultMap(ConstraintBasedDevelopmentProcess process, private void generateConstraintsResultMap(ConstraintBasedDevelopmentProcess process) {
boolean considerOnlyCurrent) {
if(constraintsMap == null) { if(constraintsMap == null) {
constraintsMap = new HashMap<String, ConstraintResultHolder>(); constraintsMap = new HashMap<String, ConstraintResultHolder>();
} else } else
...@@ -386,7 +366,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -386,7 +366,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
} }
} }
generateConstraintsStatusMapForIncluded(currentDevelopmentProcess.getCurrentObjective());
resetCurrentObjectiveAsActiveConfiguration(currentDevelopmentProcess.getCurrentObjective()); resetCurrentObjectiveAsActiveConfiguration(currentDevelopmentProcess.getCurrentObjective());
} }
...@@ -423,79 +402,32 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -423,79 +402,32 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
* @param process * @param process
* the root process * the root process
*/ */
private void generateScoresForAllConfigs(ConstraintBasedDevelopmentProcess process) { private void generateScoresForConfig(ConstraintBasedDevelopmentProcess process) {
boolean unrelatedConfig = false;
if(percentageMap == null) { if(percentageMap == null) {
percentageMap = new HashMap<String, ConstraintResultHolder>(); percentageMap = new HashMap<String, ConstraintResultHolder>();
} else { } else {
percentageMap.clear(); percentageMap.clear();
} }
EList<ConstraintConfiguration> configurations = process.getConfigurations();
for(ConstraintConfiguration config : configurations) {
float totalConstraintCount = 0;
float satisfiedConstraintCount = 0;
setConfigAsActive(config);
EList<String> activeConstraints = config.getActiveConstraints(); float totalConstraintCount = 0;
float satisfiedConstraintCount = 0;
for(String activeConstraintName : activeConstraints) { EList<String> activeConstraints =
currentDevelopmentProcess.getCurrentObjective().getActiveConstraints();
if(constraintsMap.get(activeConstraintName) == null) {
unrelatedConfig = true;
} else {
ConstraintResultHolder constraintResultHolder =
constraintsMap.get(activeConstraintName);
satisfiedConstraintCount += constraintResultHolder.successCount;
totalConstraintCount += constraintResultHolder.totalCount;
}
}
if(!unrelatedConfig) { for(String activeConstraintName : activeConstraints) {
percentageMap.put(config.getName(), new ConstraintResultHolder(
satisfiedConstraintCount, totalConstraintCount));
}
resetCurrentObjectiveAsActiveConfiguration(config); ConstraintResultHolder constraintResultHolder =
unrelatedConfig = false; constraintsMap.get(activeConstraintName);
satisfiedConstraintCount += constraintResultHolder.successCount;
totalConstraintCount += constraintResultHolder.totalCount;
} }
configurations.forEach((item) -> { if(totalConstraintCount > 0) {
List<ConstraintConfiguration> dependentConfigs = getDependentConfigs(item); percentageMap.put(currentDevelopmentProcess.getCurrentObjective().getName(),
dependentConfigs.forEach(dependent -> { new ConstraintResultHolder(satisfiedConstraintCount, totalConstraintCount));
if(percentageMap.get(item.getName()) != null) { }
percentageMap.put(
item.getName(),
new ConstraintResultHolder(
percentageMap.get(item.getName()).successCount +
percentageMap.get(dependent.getName()).successCount,
percentageMap.get(item.getName()).totalCount +
percentageMap.get(dependent.getName()).totalCount));
}
});
});
}
/**
* Sets given configuration as active.
*
* @param configuration
* the configuration which has to be set active
*/
private void setConfigAsActive(ConstraintConfiguration configuration) {
ITopLevelElement modelContext =
IPersistencyService.getInstance().getTopLevelElementFor(currentDevelopmentProcess);
modelContext.runAsNonDirtyingCommand(() -> {
// deactivate current configurations
deactivateConfiguration(currentDevelopmentProcess.getCurrentObjective(),
currentDevelopmentProcess.getConstraintInstanceContainer());
// activate configuration to be checked
activateConfiguration(configuration,
currentDevelopmentProcess.getConstraintInstanceContainer());
});
} }
/** /**
...@@ -524,7 +456,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -524,7 +456,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
@Override @Override
public void resourceChanged(IResourceChangeEvent event) { public void resourceChanged(IResourceChangeEvent event) {
if(event.getType() == IResourceChangeEvent.POST_CHANGE) { if(event.getType() == IResourceChangeEvent.POST_CHANGE && currentDevelopmentProcess != null) {
float successPercentage = generateScoresAndUpdateUI(currentDevelopmentProcess); float successPercentage = generateScoresAndUpdateUI(currentDevelopmentProcess);
List<String> configNames = List<String> configNames =
...@@ -534,12 +466,12 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -534,12 +466,12 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
.forEach(item -> { .forEach(item -> {
if(percentageMap.get(item) != null) { if(percentageMap.get(item) != null) {
String percentagevalue = String percentagevalue =
new DecimalFormat("##.#").format(percentageMap.get(item).successCount / new DecimalFormat("##").format(percentageMap.get(item).successCount /
percentageMap.get(item).totalCount * 100); percentageMap.get(item).totalCount * 100);
configNames.set(configNames.indexOf(item), item + " (" + configNames.set(configNames.indexOf(item), item + " (" +
percentagevalue + "%)"); percentagevalue + "%)");
} else } else
configNames.set(configNames.indexOf(item), item + " N/A"); configNames.set(configNames.indexOf(item), item + " (N/A)");
}); });
String[] configNamesArray = new String[configNames.size()]; String[] configNamesArray = new String[configNames.size()];
...@@ -557,7 +489,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -557,7 +489,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
currentObjectiveCombo currentObjectiveCombo
.setText(currentDevelopmentProcess.getCurrentObjective().getName()); .setText(currentDevelopmentProcess.getCurrentObjective().getName());
currentObjectiveCombo.setToolTipText(new DecimalFormat("##.#") currentObjectiveCombo.setToolTipText(new DecimalFormat("##")
.format(successPercentage * 100) + "% of the constraints are satisfied"); .format(successPercentage * 100) + "% of the constraints are satisfied");
} }
} }
...@@ -580,49 +512,4 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele ...@@ -580,49 +512,4 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
} }
return " "; return " ";
} }
/**
* Generate a map holding the constraint data (satisfied versus total number of constraints) for
* a given configuration. Constraints for which data that has already been calculated for other
* objectives are ignored.
*
* @param configuration
* the configuration to be counted for
*/
private void generateConstraintsStatusMapForIncluded(ConstraintConfiguration configuration) {
for(ConstraintConfiguration config : configuration.getIncludedConfigurations()) {
setConfigAsActive(config);
for(String activeConstraintName : config.getActiveConstraints()) {
ConstraintResultHolder constraintValueByName =
getConstraintValueObjectByName(activeConstraintName);
if(!constraintsMap.containsKey(activeConstraintName)) {
constraintsMap.put(activeConstraintName, constraintValueByName);
}
}
generateConstraintsStatusMapForIncluded(config);
}
resetCurrentObjectiveAsActiveConfiguration(configuration);
}
/**
* Helper to recursively find all configurations a given configuration depends on.
*
* @param config
* @return list of dependencies
*/
private List<ConstraintConfiguration> getDependentConfigs(ConstraintConfiguration config) {
List<ConstraintConfiguration> list = new ArrayList<ConstraintConfiguration>();
for(ConstraintConfiguration constraintConfiguration : config.getIncludedConfigurations()) {
if(constraintConfiguration.getIncludedConfigurations().isEmpty()) {
list.add(constraintConfiguration);
} else {
List<ConstraintConfiguration> dependentConfigs =
getDependentConfigs(constraintConfiguration);
list.addAll(dependentConfigs);
}
}
return list;
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment