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
import static org.fortiss.tooling.base.utils.ConstraintsBaseUtils.getActiveConfigurationsTransitively;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -172,8 +171,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
// Updates the current objective if any object is selected on the navigator window.
EList<ConstraintConfiguration> configs = null;
float successPercentage;
if(selection instanceof ITreeSelection) {
if(((ITreeSelection)selection).getFirstElement() != null) {
Object objectChosenElement = ((ITreeSelection)selection).getFirstElement();
......@@ -208,30 +205,23 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
currentDevelopmentProcess = process;
successPercentage = generateScoresAndUpdateUI(process);
List<String> configNames =
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()];
configNames.toArray(configNamesArray);
currentObjectiveCombo.setItems(configNamesArray);
currentObjectiveCombo.setText(currentDevelopmentProcess
.getCurrentObjective().getName());
currentObjectiveCombo.setToolTipText(new DecimalFormat("##.#")
.format(successPercentage * 100) +
"% of the constraints are satisfied");
updateObjectiveByName(currentDevelopmentProcess.getCurrentObjective()
.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
* @return returns the float score
*/
private float generateScoresAndUpdateUI(ConstraintBasedDevelopmentProcess process) {
float successPercentage = 0f;
generateConstraintsResultMap(process, true);
generateScoresForAllConfigs(process);
float successPercentage = -1f;
generateConstraintsResultMap(process);
generateScoresForConfig(process);
if(percentageMap.get(process.getCurrentObjective().getName()) != null) {
successPercentage =
percentageMap.get(process.getCurrentObjective().getName()).successCount /
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;
}
......@@ -301,6 +294,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
* The name of the objective to update to.
*/
public void updateObjectiveByName(String newObjectiveName) {
ConstraintConfiguration newObjective = getConstraintConfigByName(newObjectiveName);
final ConstraintConfiguration oldObjective =
currentDevelopmentProcess.getCurrentObjective();
......@@ -328,17 +322,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
List<String> configNames =
currentDevelopmentProcess.getConfigurations().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()];
configNames.toArray(configNamesArray);
......@@ -355,7 +338,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
currentObjectiveCombo
.setText(currentDevelopmentProcess.getCurrentObjective().getName());
currentObjectiveCombo.setToolTipText(new DecimalFormat("##.#")
currentObjectiveCombo.setToolTipText(new DecimalFormat("##")
.format(successPercentage * 100) + "% of the constraints are satisfied");
}
}
......@@ -365,11 +348,8 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
* configuration or any of its dependencies.
*
* @param process
* @param considerOnlyCurrent
* the flag indicating whether only until current objective will be computed or not
*/
private void generateConstraintsResultMap(ConstraintBasedDevelopmentProcess process,
boolean considerOnlyCurrent) {
private void generateConstraintsResultMap(ConstraintBasedDevelopmentProcess process) {
if(constraintsMap == null) {
constraintsMap = new HashMap<String, ConstraintResultHolder>();
} else
......@@ -386,7 +366,6 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
}
}
generateConstraintsStatusMapForIncluded(currentDevelopmentProcess.getCurrentObjective());
resetCurrentObjectiveAsActiveConfiguration(currentDevelopmentProcess.getCurrentObjective());
}
......@@ -423,79 +402,32 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
* @param process
* the root process
*/
private void generateScoresForAllConfigs(ConstraintBasedDevelopmentProcess process) {
boolean unrelatedConfig = false;
private void generateScoresForConfig(ConstraintBasedDevelopmentProcess process) {
if(percentageMap == null) {
percentageMap = new HashMap<String, ConstraintResultHolder>();
} else {
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) {
if(constraintsMap.get(activeConstraintName) == null) {
unrelatedConfig = true;
} else {
ConstraintResultHolder constraintResultHolder =
constraintsMap.get(activeConstraintName);
satisfiedConstraintCount += constraintResultHolder.successCount;
totalConstraintCount += constraintResultHolder.totalCount;
}
}
EList<String> activeConstraints =
currentDevelopmentProcess.getCurrentObjective().getActiveConstraints();
if(!unrelatedConfig) {
percentageMap.put(config.getName(), new ConstraintResultHolder(
satisfiedConstraintCount, totalConstraintCount));
}
for(String activeConstraintName : activeConstraints) {
resetCurrentObjectiveAsActiveConfiguration(config);
unrelatedConfig = false;
ConstraintResultHolder constraintResultHolder =
constraintsMap.get(activeConstraintName);
satisfiedConstraintCount += constraintResultHolder.successCount;
totalConstraintCount += constraintResultHolder.totalCount;
}
configurations.forEach((item) -> {
List<ConstraintConfiguration> dependentConfigs = getDependentConfigs(item);
dependentConfigs.forEach(dependent -> {
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());
});
if(totalConstraintCount > 0) {
percentageMap.put(currentDevelopmentProcess.getCurrentObjective().getName(),
new ConstraintResultHolder(satisfiedConstraintCount, totalConstraintCount));
}
}
/**
......@@ -524,7 +456,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
@Override
public void resourceChanged(IResourceChangeEvent event) {
if(event.getType() == IResourceChangeEvent.POST_CHANGE) {
if(event.getType() == IResourceChangeEvent.POST_CHANGE && currentDevelopmentProcess != null) {
float successPercentage = generateScoresAndUpdateUI(currentDevelopmentProcess);
List<String> configNames =
......@@ -534,12 +466,12 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
.forEach(item -> {
if(percentageMap.get(item) != null) {
String percentagevalue =
new DecimalFormat("##.#").format(percentageMap.get(item).successCount /
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");
configNames.set(configNames.indexOf(item), item + " (N/A)");
});
String[] configNamesArray = new String[configNames.size()];
......@@ -557,7 +489,7 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
currentObjectiveCombo
.setText(currentDevelopmentProcess.getCurrentObjective().getName());
currentObjectiveCombo.setToolTipText(new DecimalFormat("##.#")
currentObjectiveCombo.setToolTipText(new DecimalFormat("##")
.format(successPercentage * 100) + "% of the constraints are satisfied");
}
}
......@@ -580,49 +512,4 @@ class CurrentObjectiveContributionItem extends ContributionItem implements ISele
}
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