| 1 | package de.uka.ipd.sdq.dsexplore.analysis.reliability; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | |
| 9 | import org.apache.log4j.Logger; |
| 10 | import org.eclipse.core.runtime.CoreException; |
| 11 | import org.eclipse.core.runtime.IProgressMonitor; |
| 12 | import org.eclipse.debug.core.ILaunchManager; |
| 13 | import org.opt4j.core.Constraint; |
| 14 | import org.opt4j.core.Criterion; |
| 15 | import org.opt4j.core.Objective; |
| 16 | |
| 17 | import de.uka.ipd.sdq.dsexplore.analysis.AnalysisFailedException; |
| 18 | import de.uka.ipd.sdq.dsexplore.analysis.IAnalysis; |
| 19 | import de.uka.ipd.sdq.dsexplore.analysis.IAnalysisResult; |
| 20 | import de.uka.ipd.sdq.dsexplore.analysis.PCMPhenotype; |
| 21 | import de.uka.ipd.sdq.dsexplore.launch.DSEWorkflowConfiguration; |
| 22 | import de.uka.ipd.sdq.dsexplore.launch.DSEConstantsContainer.QualityAttribute; |
| 23 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.EvaluationAspect; |
| 24 | import de.uka.ipd.sdq.dsexplore.qml.contracttype.QMLContractType.Dimension; |
| 25 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.EvaluationAspectWithContext; |
| 26 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.builder.InfeasibilityConstraintBuilder; |
| 27 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.builder.ObjectiveBuilder; |
| 28 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.builder.SatisfactionConstraintBuilder; |
| 29 | import de.uka.ipd.sdq.dsexplore.qml.pcm.reader.PCMDeclarationsReader; |
| 30 | import de.uka.ipd.sdq.dsexplore.qml.profile.QMLProfile.UsageScenarioRequirement; |
| 31 | import de.uka.ipd.sdq.pcm.usagemodel.UsageModel; |
| 32 | import de.uka.ipd.sdq.pcmsolver.models.PCMInstance; |
| 33 | import de.uka.ipd.sdq.pcmsolver.runconfig.PCMSolverConfigurationBasedConfigBuilder; |
| 34 | import de.uka.ipd.sdq.pcmsolver.runconfig.PCMSolverWorkflowRunConfiguration; |
| 35 | import de.uka.ipd.sdq.reliability.solver.pcm2markov.Pcm2MarkovStrategy; |
| 36 | import de.uka.ipd.sdq.reliability.solver.runconfig.PCMSolverReliabilityConfigurationBasedConfigBuilder; |
| 37 | import de.uka.ipd.sdq.reliability.solver.runconfig.RunPCMReliabilityAnalysisJob; |
| 38 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
| 39 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
| 40 | import de.uka.ipd.sdq.workflow.launchconfig.AbstractWorkflowConfigurationBuilder; |
| 41 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
| 42 | import de.uka.ipd.sdq.workflow.pcm.blackboard.PCMResourceSetPartition; |
| 43 | import de.uka.ipd.sdq.workflow.pcm.configurations.PCMWorkflowConfigurationBuilder; |
| 44 | import de.uka.ipd.sdq.workflow.pcm.jobs.LoadPCMModelsIntoBlackboardJob; |
| 45 | |
| 46 | /** |
| 47 | * Starts a reliability Solver Analysis for the design space exploration. |
| 48 | * |
| 49 | * @author pmerkle, anne |
| 50 | * |
| 51 | */ |
| 52 | public class ReliabilityAnalysis implements IAnalysis { |
| 53 | |
| 54 | /** Logger for log4j. */ |
| 55 | private static Logger logger = |
| 56 | Logger.getLogger("de.uka.ipd.sdq.dsexplore.analysis.reliability.ReliabilityAnalysis"); |
| 57 | |
| 58 | /** |
| 59 | * Store the launch parameters so that we do not have to pass them all the |
| 60 | * time. |
| 61 | */ |
| 62 | private DSEWorkflowConfiguration config; |
| 63 | |
| 64 | private PCMSolverWorkflowRunConfiguration lastPCMSolverConfiguration; |
| 65 | |
| 66 | private int iteration = -1; |
| 67 | |
| 68 | private MDSDBlackboard blackboard; |
| 69 | |
| 70 | private ReliabilitySolverQualityAttributeDeclaration reliabilityQualityAttribute = new ReliabilitySolverQualityAttributeDeclaration(); |
| 71 | |
| 72 | //Constraint handling |
| 73 | private List<Constraint> constraints = new ArrayList<Constraint>(); |
| 74 | private Map<Constraint, EvaluationAspectWithContext> constraintToAspect = new HashMap<Constraint, EvaluationAspectWithContext>(); //This is needed to determine, what THE result is (Mean, Variance, ...) |
| 75 | |
| 76 | private List<Objective> objectives = new ArrayList<Objective>(); |
| 77 | private Map<Objective, EvaluationAspectWithContext> objectiveToAspect = new HashMap<Objective, EvaluationAspectWithContext>(); |
| 78 | |
| 79 | private Map<Long, Double> previousPofodResults = new HashMap<Long, Double>(); |
| 80 | |
| 81 | /** |
| 82 | * {@inheritDoc} |
| 83 | * @throws UserCanceledException |
| 84 | * @throws JobFailedException |
| 85 | */ |
| 86 | public void analyse(PCMPhenotype pheno, IProgressMonitor monitor) |
| 87 | throws CoreException, JobFailedException, UserCanceledException { |
| 88 | |
| 89 | iteration++; |
| 90 | |
| 91 | launchReliabilitySolver(pheno, monitor); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | private IAnalysisResult retrieveReliabilitySolverResults(PCMPhenotype pheno) { |
| 96 | |
| 97 | ReliabilityAnalysisResult result = new ReliabilityAnalysisResult(this.previousPofodResults.get(pheno.getNumericID()), this.lastPCMSolverConfiguration); |
| 98 | |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Launches the LQN Solver. |
| 104 | * @param monitor |
| 105 | * |
| 106 | * @param pcmInstance the instance of PCM |
| 107 | * @throws AnalysisFailedException RunPCMAnalysisJob solver = new RunPCMAnalysisJob(configuration, true); |
| 108 | * @throws CoreException |
| 109 | * @throws UserCanceledException |
| 110 | * @throws JobFailedException |
| 111 | */ |
| 112 | private void launchReliabilitySolver(PCMPhenotype pheno, IProgressMonitor monitor) |
| 113 | throws CoreException, JobFailedException, UserCanceledException { |
| 114 | |
| 115 | PCMSolverWorkflowRunConfiguration solverConfiguration = new PCMSolverWorkflowRunConfiguration(); |
| 116 | AbstractWorkflowConfigurationBuilder builder; |
| 117 | |
| 118 | builder = new PCMWorkflowConfigurationBuilder(this.config.getRawConfiguration(), ILaunchManager.RUN_MODE); |
| 119 | builder.fillConfiguration(solverConfiguration); |
| 120 | |
| 121 | builder = new PCMSolverConfigurationBasedConfigBuilder(this.config.getRawConfiguration(), |
| 122 | ILaunchManager.RUN_MODE); |
| 123 | builder.fillConfiguration(solverConfiguration); |
| 124 | |
| 125 | builder = new PCMSolverReliabilityConfigurationBasedConfigBuilder(this.config.getRawConfiguration(), ILaunchManager.RUN_MODE); |
| 126 | builder.fillConfiguration(solverConfiguration); |
| 127 | |
| 128 | solverConfiguration.setShowHtmlResults(false); |
| 129 | |
| 130 | solverConfiguration.setInteractive(false); |
| 131 | |
| 132 | this.lastPCMSolverConfiguration = solverConfiguration; |
| 133 | |
| 134 | // Create a new Analysis job |
| 135 | RunPCMReliabilityAnalysisJob solverJob = new RunPCMReliabilityAnalysisJob(solverConfiguration); |
| 136 | solverJob.setBlackboard(blackboard); |
| 137 | |
| 138 | //execute the job |
| 139 | solverJob.execute(monitor); |
| 140 | |
| 141 | this.previousPofodResults.put(pheno.getNumericID(), 1 - ((Pcm2MarkovStrategy)solverJob.getStrategy()).getSolvedValue().getSuccessProbability()); |
| 142 | |
| 143 | logger.debug("Finished reliability solver analysis"); |
| 144 | |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * {@inheritDoc} |
| 149 | * @see de.uka.ipd.sdq.dsexplore.analysis.IAnalysis#initialise(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor) |
| 150 | */ |
| 151 | public void initialise(DSEWorkflowConfiguration configuration) throws CoreException{ |
| 152 | this.config = configuration; |
| 153 | |
| 154 | initialiseCriteria(configuration); |
| 155 | } |
| 156 | |
| 157 | private void initialiseCriteria(DSEWorkflowConfiguration configuration) throws CoreException{ |
| 158 | UsageModel usageModel = new PCMInstance((PCMResourceSetPartition)this.blackboard.getPartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID)).getUsageModel(); |
| 159 | |
| 160 | PCMDeclarationsReader reader = new PCMDeclarationsReader( |
| 161 | configuration.getRawConfiguration().getAttribute("qmlDefinitionFile", "")); |
| 162 | |
| 163 | List<Dimension> dimensions = this.reliabilityQualityAttribute.getDimensions(); |
| 164 | |
| 165 | List<EvaluationAspectWithContext> reliabilityAspects = new ArrayList<EvaluationAspectWithContext>(6); |
| 166 | for (Dimension dimension : dimensions) { |
| 167 | reliabilityAspects.addAll(reader.getDimensionConstraintContextsForUsageModel(usageModel, dimension.getId())); |
| 168 | reliabilityAspects.addAll(reader.getDimensionObjectiveContextsForUsageModel(usageModel, dimension.getId())); |
| 169 | } |
| 170 | |
| 171 | //Check constraint aspects and create Constraint-Objects for every Aspect |
| 172 | for (Iterator<EvaluationAspectWithContext> iterator = reliabilityAspects.iterator(); iterator.hasNext();) { |
| 173 | EvaluationAspectWithContext aspectContext = iterator |
| 174 | .next(); |
| 175 | |
| 176 | |
| 177 | if(aspectContext.getRequirement() instanceof UsageScenarioRequirement) { |
| 178 | |
| 179 | //Handle possible aspects here |
| 180 | if (canEvaluateAspect(aspectContext.getEvaluationAspect(), aspectContext.getDimension())) { |
| 181 | |
| 182 | if(aspectContext.getCriterion() instanceof de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.Constraint) { |
| 183 | Constraint c = reader.translateEvalAspectToInfeasibilityConstraint(aspectContext, new InfeasibilityConstraintBuilder()); |
| 184 | constraints.add(c); |
| 185 | constraintToAspect.put(c, aspectContext); |
| 186 | } else { |
| 187 | //instanceof Objective |
| 188 | Objective o = reader.translateEvalAspectToObjective(this.getQualityAttribute().getName(), aspectContext, new ObjectiveBuilder()); |
| 189 | objectives.add(o); |
| 190 | objectiveToAspect.put(o, aspectContext); |
| 191 | |
| 192 | Constraint c = reader.translateEvalAspectToSatisfactionConstraint(aspectContext, o, new SatisfactionConstraintBuilder()); |
| 193 | constraints.add(c); |
| 194 | constraintToAspect.put(c, aspectContext); |
| 195 | } |
| 196 | } else { |
| 197 | //XXX: This should never be the case if the optimization is started with the LaunchConfig the aspect is checked there as well |
| 198 | throw new RuntimeException("Evaluation aspect not supported("+aspectContext.getEvaluationAspect()+")!"); |
| 199 | } |
| 200 | |
| 201 | } else { |
| 202 | throw new RuntimeException("Unsupported Requirement!"); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | private boolean canEvaluateAspect(EvaluationAspect aspect, Dimension dimension){ |
| 208 | return reliabilityQualityAttribute.canEvaluateAspectForDimension(aspect, dimension); |
| 209 | } |
| 210 | |
| 211 | //MOVED to PCMDeclarationsReader |
| 212 | // private Objective translateEvalAspectToObjective(EvaluationAspectWithContext aspect) { |
| 213 | // //Make sure, the aspect IS an objective |
| 214 | // try { |
| 215 | // if(aspect.getDimension().getType().getRelationSemantics().getRelSem() == EnumRelationSemantics.DECREASING) { |
| 216 | // return new Objective(this.getQualityAttribute(), Objective.Sign.MIN); |
| 217 | // } else { |
| 218 | // //INCREASING |
| 219 | // return new Objective(this.getQualityAttribute(), Objective.Sign.MAX); |
| 220 | // } |
| 221 | // } catch (CoreException e) { |
| 222 | // e.printStackTrace(); |
| 223 | // throw new RuntimeException("Could not get reliability quality attribute!"); |
| 224 | // } |
| 225 | // } |
| 226 | |
| 227 | public QualityAttribute getQualityAttribute() throws CoreException { |
| 228 | //return DSEConstantsContainer.POFOD_QUALITY; |
| 229 | return reliabilityQualityAttribute.getQualityAttribute(); |
| 230 | } |
| 231 | |
| 232 | public boolean hasStatisticResults() throws CoreException { |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | // //TODO support several usage scenarios, then also change hasObjectivePerUsageScenario |
| 237 | // @Override |
| 238 | // public List<Objective> getObjectives() throws CoreException { |
| 239 | // List<Objective> objectives = new ArrayList<Objective>(1); |
| 240 | // Objective o = new Objective(this.getQualityAttribute(), Objective.Sign.MIN); |
| 241 | // objectives.add(o); |
| 242 | // |
| 243 | // return objectives; |
| 244 | // } |
| 245 | |
| 246 | @Override |
| 247 | public List<Criterion> getCriterions() throws CoreException { |
| 248 | List<Criterion> criterions = new ArrayList<Criterion>(); |
| 249 | |
| 250 | //Objective o = new Objective(this.getQualityAttribute(), Objective.Sign.MIN); |
| 251 | criterions.addAll(objectives); |
| 252 | |
| 253 | criterions.addAll(constraints); |
| 254 | |
| 255 | return criterions; |
| 256 | } |
| 257 | |
| 258 | //TODO: Support several usage scenarios. |
| 259 | @Override |
| 260 | public IAnalysisResult retrieveResultsFor(PCMPhenotype pheno, Criterion criterion) |
| 261 | throws CoreException, AnalysisFailedException { |
| 262 | IAnalysisResult result = retrieveReliabilitySolverResults(pheno); |
| 263 | |
| 264 | //It is always the pofod, i.e. objective and constraint always have to refer to the MeanValue (-> no other aspects atm) |
| 265 | //If more possible aspects are added, the criterion needs to be examined here |
| 266 | return result; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public boolean hasObjectivePerUsageScenario() throws CoreException { |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | @Override |
| 276 | public void setBlackboard(MDSDBlackboard blackboard) { |
| 277 | this.blackboard = blackboard; |
| 278 | |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |
| 283 | } |