| 1 | package de.uka.ipd.sdq.dsexplore.analysis; |
| 2 | |
| 3 | import java.util.List; |
| 4 | |
| 5 | import org.apache.log4j.Logger; |
| 6 | import org.eclipse.core.runtime.CoreException; |
| 7 | import org.eclipse.core.runtime.IConfigurationElement; |
| 8 | import org.eclipse.core.runtime.IExtension; |
| 9 | import org.eclipse.core.runtime.IProgressMonitor; |
| 10 | import org.eclipse.core.runtime.Status; |
| 11 | import org.opt4j.core.Criterion; |
| 12 | |
| 13 | import de.uka.ipd.sdq.dsexplore.helper.ExtensionHelper; |
| 14 | import de.uka.ipd.sdq.dsexplore.launch.DSELaunch; |
| 15 | import de.uka.ipd.sdq.dsexplore.launch.DSEWorkflowConfiguration; |
| 16 | import de.uka.ipd.sdq.dsexplore.launch.DSEConstantsContainer.QualityAttribute; |
| 17 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
| 18 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
| 19 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
| 20 | |
| 21 | /** Singleton */ |
| 22 | public class AnalysisProxy implements IAnalysis { |
| 23 | |
| 24 | private DSEWorkflowConfiguration configuration; |
| 25 | |
| 26 | private QualityAttribute qualityAttribute; |
| 27 | |
| 28 | IAnalysis ana = null; |
| 29 | |
| 30 | //TODO: Quickfix to reset the Loggers. Refactor! |
| 31 | private DSELaunch dseLaunch; |
| 32 | |
| 33 | /**Name of the analysis extension to load, e.g. LQN solver. */ |
| 34 | private String methodName; |
| 35 | |
| 36 | private MDSDBlackboard blackboard; |
| 37 | |
| 38 | /** Logger for log4j. */ |
| 39 | private static Logger logger = |
| 40 | Logger.getLogger("de.uka.ipd.sdq.dsexplore.analysis.AnalysisProxy"); |
| 41 | |
| 42 | public AnalysisProxy(DSEWorkflowConfiguration configuration, QualityAttribute qualityAttribute, DSELaunch dseLaunch, String methodName) { |
| 43 | this.initialise(configuration); |
| 44 | this.qualityAttribute = qualityAttribute; |
| 45 | this.dseLaunch = dseLaunch; |
| 46 | this.methodName = methodName; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void analyse(PCMPhenotype pheno, IProgressMonitor monitor) throws CoreException, UserCanceledException, AnalysisFailedException, JobFailedException { |
| 51 | IAnalysis ana = getDecoratedAnalysis(); |
| 52 | |
| 53 | logger.debug("Starting analysis of "+qualityAttribute.getPrettyName()); |
| 54 | ana.analyse(pheno, monitor); |
| 55 | logger.debug("Finished analysis of "+qualityAttribute.getPrettyName()); |
| 56 | dseLaunch.resetLoggers(); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | private IAnalysis getDecoratedAnalysis() throws CoreException { |
| 61 | if (ana == null){ |
| 62 | ana = this.loadDecoratedAnalysis(); |
| 63 | ana.setBlackboard(blackboard); |
| 64 | ana.initialise(configuration); |
| 65 | |
| 66 | } |
| 67 | return ana; |
| 68 | } |
| 69 | |
| 70 | private IAnalysis loadDecoratedAnalysis() throws CoreException{ |
| 71 | // obtain the extension of the choosen analysis method |
| 72 | IExtension[] extensions = ExtensionHelper.loadAnalysisExtensions(); |
| 73 | for (IExtension ext : extensions) { |
| 74 | IConfigurationElement[] elements = ext.getConfigurationElements(); |
| 75 | for (IConfigurationElement element : elements) { |
| 76 | if (element.getName().equals("analysis")) { |
| 77 | // if extension fits to analysis method |
| 78 | if (element.getAttribute("name").equals(methodName)) { |
| 79 | // obtain an analysis method instance |
| 80 | ana = (IAnalysis)ExtensionHelper.loadExecutableAttribute(element, "delegate"); |
| 81 | return ana; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | throw new CoreException(new Status(Status.ERROR, "de.uka.ipd.sdq.dsexplore", "Cannot load analysis extension for "+methodName)); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void initialise(DSEWorkflowConfiguration configuration) { |
| 91 | this.configuration = configuration; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | @Override |
| 96 | public QualityAttribute getQualityAttribute() throws CoreException { |
| 97 | return getDecoratedAnalysis().getQualityAttribute(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean hasStatisticResults() throws CoreException { |
| 102 | return this.getDecoratedAnalysis().hasStatisticResults(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public List<Criterion> getCriterions() throws CoreException { |
| 107 | return getDecoratedAnalysis().getCriterions(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public IAnalysisResult retrieveResultsFor(PCMPhenotype pheno, Criterion criterion) |
| 112 | throws CoreException, AnalysisFailedException { |
| 113 | return getDecoratedAnalysis().retrieveResultsFor(pheno, criterion); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean hasObjectivePerUsageScenario() throws CoreException { |
| 118 | if (ana != null){ |
| 119 | ana = loadDecoratedAnalysis(); |
| 120 | } |
| 121 | //ana may be not initialised |
| 122 | return ana.hasObjectivePerUsageScenario(); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void setBlackboard(MDSDBlackboard blackboard) { |
| 127 | this.blackboard = blackboard; |
| 128 | } |
| 129 | |
| 130 | } |