Exception handlings & warning messages(ui) are added.

1. Contention's constructor null pointer exeception is handled.
2. CPURta Amalthea model null-pointer exception is handled.
3. E2ELatency Amalthea model null-pointer exception is handled.
4. RTARuntimeUtil null-pointer exception regarding the target Amalthea
model's hardware unit exception is handled (testing with democar.amxmi
has been done.)
5. APP4RTA warning messages regarding the Amalthea models with less
attributes are added.

Change-Id: I3ed6ee8af423d23ff3cd333ab7c773c705a096af
Signed-off-by: Junhyung Ki <kijoonh91@gmail.com>
diff --git a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/CPURta.java b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/CPURta.java
index 2c0a1a7..d5a0fa2 100644
--- a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/CPURta.java
+++ b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/CPURta.java
@@ -76,7 +76,14 @@
 	 * Constructor only for testing the class
 	 */
 	public CPURta() {
-		this.setModel(AmaltheaLoader.loadFromFile(this.inputFile));
+		final Logger log = Logger.getLogger(CPURta.class);
+		final Amalthea pModel = AmaltheaLoader.loadFromFile(this.inputFile);
+		if (pModel == null) {
+			log.error("Model is empty. Please check the model path.");
+			return;
+		} else {
+			this.setModel(AmaltheaLoader.loadFromFile(this.inputFile));
+		}
 		this.setTRT(this.getDefaultTRT(this.getModel()));
 		this.setIA(this.getDefaultIAMapping());
 		this.setPUl(CommonUtils.getPUs(this.getModel()));
diff --git a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/Contention.java b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/Contention.java
index c68bf5d..b31da13 100644
--- a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/Contention.java
+++ b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/Contention.java
@@ -20,7 +20,6 @@
 import java.util.Set;
 import java.util.stream.Collectors;
 
-import org.eclipse.app4mc.gsoc_rta.CommonUtils;
 import org.eclipse.app4mc.amalthea.model.Amalthea;
 import org.eclipse.app4mc.amalthea.model.CallSequenceItem;
 import org.eclipse.app4mc.amalthea.model.InterProcessStimulus;
@@ -55,7 +54,11 @@
 	public Contention(final int[] iap, final Amalthea modelp) {
 		this.ia = iap;
 		this.model = modelp;
-		this.flagArray = new int[iap.length];
+		if (iap == null) {
+			this.flagArray = null;
+		} else {
+			this.flagArray = new int[iap.length];
+		}
 	}
 
 	/**
@@ -139,6 +142,7 @@
 				}
 			}
 
+			@SuppressWarnings("unchecked")
 			final List<Label>[] aryofLabelList = new ArrayList[2];
 			aryofLabelList[0] = readLabelList;
 			aryofLabelList[1] = writeLabelList;
@@ -641,4 +645,4 @@
 
 		return contentionTime;
 	}
-}
+}
\ No newline at end of file
diff --git a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/E2ELatency.java b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/E2ELatency.java
index 69fa8ca..055ecd8 100644
--- a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/E2ELatency.java
+++ b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/E2ELatency.java
@@ -13,6 +13,7 @@
  *******************************************************************************/

 package org.eclipse.app4mc.gsoc_rta;

 

+import java.io.File;

 import java.math.BigDecimal;

 import java.math.BigInteger;

 import java.util.ArrayList;

@@ -48,6 +49,7 @@
  * 				This class is to analyze End-to-End latency with the help of CPURta class.

  */

 public class E2ELatency {

+	public final File inputFile = new File("model-input/WATERS19_release/ChallengeModel_release.amxmi");

 	public CPURta cpurta = new CPURta();

 	public static final int[] defaultIAMapping = new int[] { 4, 1, 1, 3, 4, 0, 3, 3, 3, 0, 6, 2, 5, 6 };

 	public static void main(String[] args) {

@@ -60,6 +62,10 @@
 	public void run() {

 		final Logger log = Logger.getLogger(E2ELatency.class);

 		this.setCPURta(this.cpurta);

+		if (this.cpurta.getModel() == null) {

+			log.debug("Model is empty. Please check the model path.");

+			return ;

+		}

 		final EList<EventChain> ecList = this.cpurta.getModel().getConstraintsModel().getEventChains();

 		for (int i = 0; i < ecList.size(); i++) {

 			log.debug(" ----- " + "Chain " + (i+1) + " ----- ");

@@ -100,9 +106,10 @@
 	 */

 	public void setCPURta(final CPURta cpurta) {

 		final Logger log = Logger.getLogger(E2ELatency.class);

-		cpurta.setModel(AmaltheaLoader.loadFromFile(cpurta.inputFile));

+		cpurta.setModel(AmaltheaLoader.loadFromFile(this.inputFile));

 		if (cpurta.getModel() == null) {

-			log.debug("NOT GOOD!");

+			log.debug("Model is empty. Please check the model path.");

+			return ;

 		}

 		cpurta.setTRT(cpurta.getDefaultTRT(cpurta.getModel()));

 		cpurta.setIA(defaultIAMapping);

diff --git a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/RTARuntimeUtil.java b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/RTARuntimeUtil.java
index da619aa..7d19a19 100644
--- a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/RTARuntimeUtil.java
+++ b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/RTARuntimeUtil.java
@@ -296,12 +296,10 @@
 		if (executionCase.equals(TimeType.WCET)) {

 			readLatency = pu.getAccessElements().get(0).getReadLatency().getUpperBound();

 			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getUpperBound();

-		}

-		else if (executionCase.equals(TimeType.BCET)) {

+		} else if (executionCase.equals(TimeType.BCET)) {

 			readLatency = pu.getAccessElements().get(0).getReadLatency().getLowerBound();

 			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getLowerBound();

-		}

-		else {

+		} else {

 			readLatency = pu.getAccessElements().get(0).getReadLatency().getAverage();

 			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getAverage();

 		}

@@ -349,21 +347,23 @@
 		final double freq = AmaltheaServices.convertToHertz(pu.getFrequencyDomain().getDefaultValue()).longValue();

 		double readLatency = 0;

 		double writeLatency = 0;

-		if (executionCase.equals(TimeType.WCET)) {

-			readLatency = pu.getAccessElements().get(0).getReadLatency().getUpperBound();

-			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getUpperBound();

-		}

-		else if (executionCase.equals(TimeType.BCET)) {

-			readLatency = pu.getAccessElements().get(0).getReadLatency().getLowerBound();

-			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getLowerBound();

-		}

-		else {

-			readLatency = pu.getAccessElements().get(0).getReadLatency().getAverage();

-			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getAverage();

+		if (pu.getAccessElements().size() == 0) {

+			readLatency = 30; // Default value will be assigned

+			writeLatency = 30; // Default value of will be assigned

+		} else {

+			if (executionCase.equals(TimeType.WCET)) {

+				readLatency = pu.getAccessElements().get(0).getReadLatency().getUpperBound();

+				writeLatency = pu.getAccessElements().get(0).getWriteLatency().getUpperBound();

+			} else if (executionCase.equals(TimeType.BCET)) {

+				readLatency = pu.getAccessElements().get(0).getReadLatency().getLowerBound();

+				writeLatency = pu.getAccessElements().get(0).getWriteLatency().getLowerBound();

+			} else {

+				readLatency = pu.getAccessElements().get(0).getReadLatency().getAverage();

+				writeLatency = pu.getAccessElements().get(0).getWriteLatency().getAverage();

+			}

 		}

 		/* Read & Write Memory Access Time */

 		result = result.add(getRunnableMemoryAccessTime(runnable, freq, readLatency, writeLatency));

-		

 		/* Execution (Ticks): */

 		final List<Ticks> ticksList = SoftwareUtil.getTicks(runnable, null);

 		for (final Ticks t : ticksList) {

@@ -390,17 +390,20 @@
 		final List<Runnable> runnableList = SoftwareUtil.getRunnableList(task, null);

 		double readLatency = 0;

 		double writeLatency = 0;

-		if (executionCase.equals(TimeType.WCET)) {

-			readLatency = pu.getAccessElements().get(0).getReadLatency().getUpperBound();

-			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getUpperBound();

-		}

-		else if (executionCase.equals(TimeType.BCET)) {

-			readLatency = pu.getAccessElements().get(0).getReadLatency().getLowerBound();

-			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getLowerBound();

-		}

-		else {

-			readLatency = pu.getAccessElements().get(0).getReadLatency().getAverage();

-			writeLatency = pu.getAccessElements().get(0).getWriteLatency().getAverage();

+		if (pu.getAccessElements().size() == 0) {

+			readLatency = 30; // Default value will be assigned

+			writeLatency = 30; // Default value of will be assigned

+		} else {

+			if (executionCase.equals(TimeType.WCET)) {

+				readLatency = pu.getAccessElements().get(0).getReadLatency().getUpperBound();

+				writeLatency = pu.getAccessElements().get(0).getWriteLatency().getUpperBound();

+			} else if (executionCase.equals(TimeType.BCET)) {

+				readLatency = pu.getAccessElements().get(0).getReadLatency().getLowerBound();

+				writeLatency = pu.getAccessElements().get(0).getWriteLatency().getLowerBound();

+			} else {

+				readLatency = pu.getAccessElements().get(0).getReadLatency().getAverage();

+				writeLatency = pu.getAccessElements().get(0).getWriteLatency().getAverage();

+			}

 		}

 		for(final Runnable r : runnableList ) {

 			result = result.add(getRunnableMemoryAccessTime(r, freq, readLatency, writeLatency));

@@ -435,7 +438,6 @@
 		readAccessParameter = (Math.ceil(sizeofReadLabels / 64.0) * (readLatency / frequency));

 		final Time readAccess = parameter.multiply(readAccessParameter);

 		result = result.add(readAccess); // LabelAccess(Read) added

-		

 		/* Write (LabelAccess): */

 		double writeAccessParameter = 0;

 		double sizeofWriteLabels = 0;

diff --git a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/ui/APP4RTA.java b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/ui/APP4RTA.java
index b4881f8..f71f927 100644
--- a/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/ui/APP4RTA.java
+++ b/eclipse-tools/responseTime-analyzer/plugins/org.eclipse.app4mc.gsoc_rta/src/org/eclipse/app4mc/gsoc_rta/ui/APP4RTA.java
@@ -53,6 +53,7 @@
 import org.eclipse.emf.common.util.EList;

 import javax.swing.SwingConstants;

 import javax.swing.filechooser.FileNameExtensionFilter;

+import javax.swing.JSeparator;

 

 /**

  * Date: August 21-2019 

@@ -189,9 +190,24 @@
 						jTextFieldArray[i].setText("" + defaultIA[i]);

 					}

 					return ;

+				} else if (currentPUList.size() == 1) {

+					final int numOfTasks = cpurta.getModel().getSwModel().getTasks().size();

+					for (int i = 0; i < numOfTasks; i++) {

+						jTextFieldArray[i].setText("0");

+					}

+					return ;

+				}

+				String str = "";

+				for (int i = 0; i < currentPUList.size(); i++) {

+					if (i == currentPUList.size() - 1) {

+						str = str + i; 

+						break;

+					}

+					str = str + i + ", ";

 				}

 				JOptionPane.showMessageDialog(frame, "ERROR: The length of the default integer array does not match with\n"

-						+ "the number of tasks in the target model.");

+						+ "the number of tasks in the target model.\n\n" + "Please assign the number(s) manually.\n" 

+						+ "Available PU number(s): " + str);

 			}

 		});

 		

@@ -467,6 +483,11 @@
 		        	selectedModelLbl.setText(currentFile.getName());

 		        	currentModel = loadedModel;

 					currentPUList = CommonUtils.getPUs(currentModel);

+					if (currentPUList.get(0).getAccessElements().size() == 0) {

+						JOptionPane.showMessageDialog(frame, "The selected model's processing unit(s) does(do) not have 'read & write Latency' values.\n\n"

+								+ "Therefore, the default value of 30 will be assigned for them.\n"

+								+ "This will affect the response time results.");

+					}

 					cpurta.setModel(currentModel);

 					cpurta.setTRT(cpurta.getDefaultTRT(currentModel));

 					cpurta.setPUl(currentPUList);

@@ -577,7 +598,7 @@
 						ecComboBox.setSelectedItem(null);

 					} else {

 						JOptionPane.showMessageDialog(frame, "WARNING: The selected model has no Event-Chain.\n"

-								+ "End to End EventChain Latency Calculation is impossible with this model.");

+								+ "End-to-End Event-Chain Latency Calculation is impossible with this model.");

 						// TODO: Something needs to be done here to prevent E2ELatency calculation.

 					}

 					frame.revalidate();

@@ -766,6 +787,9 @@
 				} else if (currentECList == null) {

 					JOptionPane.showMessageDialog(frame, "ERROR: Event-Chain List is empty somehow.");

 					return ;

+				} else if (currentECList.size() == 0) {

+					JOptionPane.showMessageDialog(frame, "ERROR: There is no Event-Chain in this model.");

+					return ;

 				} else if (ecComboBox.getSelectedItem() == null) {

 					JOptionPane.showMessageDialog(frame, "ERROR: Select an Event-Chain.");

 					return ;

@@ -1025,5 +1049,9 @@
 		lblApprta.setFont(new Font("Snap ITC", Font.ITALIC, 20));

 		lblApprta.setBounds(1050, 1, 120, 34);

 		frame.getContentPane().add(lblApprta);

+		

+		JSeparator separator = new JSeparator();

+		separator.setBounds(10, 557, 1160, 2);

+		frame.getContentPane().add(separator);

 	}

 }
\ No newline at end of file