From 6007b0d9b3d940a47b837a138d8bf1bf262db9a5 Mon Sep 17 00:00:00 2001
From: Ludovic Apvrille <ludovic.apvrille@telecom-paristech.fr>
Date: Mon, 14 Feb 2011 16:23:43 +0000
Subject: [PATCH] AVATAR: update on simulator

---
 ...atarSimulationAsynchronousTransaction.java |  8 ++-
 .../AvatarSimulationBlock.java                | 32 ++++++----
 .../AvatarSpecificationSimulation.java        | 23 ++++---
 .../AvatarSpecificationSimulationSDPanel.java | 64 +++++++++++++++++--
 .../JFrameAvatarInteractiveSimulation.java    | 10 ++-
 5 files changed, 107 insertions(+), 30 deletions(-)

diff --git a/src/avatartranslator/directsimulation/AvatarSimulationAsynchronousTransaction.java b/src/avatartranslator/directsimulation/AvatarSimulationAsynchronousTransaction.java
index a085db77cc..577e7c918c 100644
--- a/src/avatartranslator/directsimulation/AvatarSimulationAsynchronousTransaction.java
+++ b/src/avatartranslator/directsimulation/AvatarSimulationAsynchronousTransaction.java
@@ -56,10 +56,12 @@ public class AvatarSimulationAsynchronousTransaction  {
   
 	private Vector<String> parameters;
     private AvatarRelation relation;
+	private int index;
 	public AvatarSimulationTransaction firstTransaction;
 	
-    public AvatarSimulationAsynchronousTransaction(AvatarRelation _ar) {
+    public AvatarSimulationAsynchronousTransaction(AvatarRelation _ar, int _index) {
 		relation = _ar;
+		index = _index;
 		parameters = new Vector<String>();
     }
 	
@@ -78,4 +80,8 @@ public class AvatarSimulationAsynchronousTransaction  {
 	public Vector<String> getParameters() {
 		return parameters;
 	}
+	
+	public int getIndex() {
+		return index;
+	}
 }
\ No newline at end of file
diff --git a/src/avatartranslator/directsimulation/AvatarSimulationBlock.java b/src/avatartranslator/directsimulation/AvatarSimulationBlock.java
index 555a0ef889..ea7e8ff4e2 100644
--- a/src/avatartranslator/directsimulation/AvatarSimulationBlock.java
+++ b/src/avatartranslator/directsimulation/AvatarSimulationBlock.java
@@ -220,15 +220,16 @@ public class AvatarSimulationBlock  {
 				}
 				aspt.clockValue = _clockValue;
 				
+				if (aspt.hasElapsedTime) {
+					 aspt.myMinDelay = aspt.myMinDelay -aspt.elapsedTime;
+					 aspt.myMaxDelay = aspt.myMaxDelay -aspt.elapsedTime;
+				}
+				
 				if (aspt.hasDelay) {
 					aspt.myMinDelay = Math.max(0, aspt.myMinDelay);
 					aspt.myMaxDelay = Math.max(0, aspt.myMaxDelay);
 				}
 				
-				if (aspt.hasElapsedTime) {
-					 aspt.myMinDelay = aspt.myMinDelay -aspt.elapsedTime;
-					 aspt.myMaxDelay = aspt.myMaxDelay -aspt.elapsedTime;
-				}
 				
 				ll.add(aspt);
 			}
@@ -293,25 +294,25 @@ public class AvatarSimulationBlock  {
 			ast.initialClockValue = lastTransaction.clockValueWhenFinished;
 		}
 		ast.clockValueWhenFinished = _clockValue;
-		
+		ast.duration = 0;
+		ast.id = ast.setID();
 		if (_aspt != null) {
-			_aspt.clockValueAtEnd = _clockValue;
-			ast.duration = _aspt.selectedDuration;
 			_aspt.clockValueAtEnd = _clockValue;
 			if (_aspt.hasClock) {
 				if(_aspt.hasElapsedTime) {
 					ast.duration = _aspt.elapsedTime + _aspt.selectedDuration;
-					ast.duration = Math.min(_aspt.myMaxDuration, ast.duration);
-					ast.duration = Math.max(_aspt.myMinDuration, ast.duration);
+				} else {
+					ast.duration = _aspt.selectedDuration;
+					ast.duration = Math.min(_aspt.myMaxDuration+_aspt.elapsedTime, ast.duration);
+					ast.duration = Math.max(_aspt.myMinDuration+_aspt.elapsedTime, ast.duration);
 				}
 				ast.clockValueWhenFinished = _aspt.selectedDuration + _clockValue;
 				_aspt.clockValueAtEnd = ast.clockValueWhenFinished;
 			}
-		} else {
-			ast.duration = 0;
-		}
-		ast.id = ast.setID();
+			TraceManager.addDev("Id= " + ast.id + " duration=" + ast.duration + " elapsed=" + _aspt.elapsedTime + " selectedDur=" + _aspt.selectedDuration + " at end: " + _aspt.clockValueAtEnd + "clockValue=" + _clockValue);
+		} 
 		
+				
 		// Attributes
 		Vector<String> attributeValues = new Vector<String>();
 		String s;
@@ -449,6 +450,9 @@ public class AvatarSimulationBlock  {
 							// Asynchronous Receiving 
 							String myAction = "";
 							ast.linkedTransaction = _aspt.linkedAsynchronousMessage.firstTransaction;
+							if (_aspt.linkedAsynchronousMessage.firstTransaction == null) {
+								TraceManager.addDev("NULL FIRST TRANSACTION !!!");
+							}
 							for(i=0; i<aaos.getNbOfValues(); i++) {
 								param = _aspt.linkedAsynchronousMessage.getParameters().get(i);
 								name = aaos.getValue(i);
@@ -468,6 +472,8 @@ public class AvatarSimulationBlock  {
 								ast.actions = new Vector<String>();
 								ast.actions.add(myAction);
 							}
+						} else {
+							TraceManager.addDev("ERROR TRANSACTION");
 						}
 					}
 				}
diff --git a/src/avatartranslator/directsimulation/AvatarSpecificationSimulation.java b/src/avatartranslator/directsimulation/AvatarSpecificationSimulation.java
index 07f5bf7823..a5f738bfe5 100644
--- a/src/avatartranslator/directsimulation/AvatarSpecificationSimulation.java
+++ b/src/avatartranslator/directsimulation/AvatarSpecificationSimulation.java
@@ -181,19 +181,20 @@ public class AvatarSpecificationSimulation  {
 							go = false;
 							TraceManager.addDev("Deadlock: no transaction can be selected");
 						} else {
-							TraceManager.addDev("* * * * * Nb of selected transactions: " + selectedTransactions.size());
+							//TraceManager.addDev("* * * * * Nb of selected transactions: " + selectedTransactions.size());
 							go = performSelectedTransactions(selectedTransactions);
-							TraceManager.addDev("NbOfcommands=" + nbOfCommands);
+							//TraceManager.addDev("NbOfcommands=" + nbOfCommands);
 							nbOfCommands --;
+							//TraceManager.addDev("------------- new NbOfcommands=" + nbOfCommands);
 						}
 					}
 				}
 			}
 		}
 		setMode(TERMINATED);
-		TraceManager.addDev("Simulation finished at time: " + clockValue + "\n--------------------------------------");
+		//TraceManager.addDev("Simulation finished at time: " + clockValue + "\n--------------------------------------");
 		
-		printExecutedTransactions();
+		//printExecutedTransactions();
 	}
 	
 	public void gatherPendingTransactions() {
@@ -318,11 +319,10 @@ public class AvatarSpecificationSimulation  {
 			// If synchronous, not taken into account -> taken into account at sending side
 			if (ar.isAsynchronous()) {
 				// Must check whether there is at least one element to read in the channel
-				AvatarSimulationAsynchronousTransaction asat = getAsynchronousMessage(ar);
+				AvatarSimulationAsynchronousTransaction asat = getAsynchronousMessage(ar, as);
 				if (asat != null) {
 					_aspt.linkedAsynchronousMessage = asat;
 					transactions.add(_aspt);
-					
 				}
 			} 
 		} else {
@@ -395,10 +395,12 @@ public class AvatarSpecificationSimulation  {
 		}
 	}
 	
-	public AvatarSimulationAsynchronousTransaction getAsynchronousMessage(AvatarRelation _ar) {
+	public AvatarSimulationAsynchronousTransaction getAsynchronousMessage(AvatarRelation _ar, AvatarSignal _as) {
 		for(AvatarSimulationAsynchronousTransaction asat: asynchronousMessages) {
 			if (asat.getRelation() == _ar) {
-				return asat;
+				if (_ar.getIndexOfSignal(_as) == asat.getIndex()) {
+					return asat;
+				}
 			}
 		}
 		return null;
@@ -473,12 +475,13 @@ public class AvatarSpecificationSimulation  {
 				_aspt.isSynchronous = false;
 				if (sig.isOut()) {
 					// Create the stucture to put elements
-					AvatarSimulationAsynchronousTransaction asat = new AvatarSimulationAsynchronousTransaction(rel);
+					// Get the index of the signal in the relation
+					AvatarSimulationAsynchronousTransaction asat = new AvatarSimulationAsynchronousTransaction(rel, rel.getIndexOfSignal(sig));
 					_aspt.linkedAsynchronousMessage = asat;
 					asynchronousMessages.add(asat);
 				} else {
 					// Must remove the asynchronous operation, and give the parameters
-					AvatarSimulationAsynchronousTransaction asat = getAsynchronousMessage(rel);
+					AvatarSimulationAsynchronousTransaction asat = getAsynchronousMessage(rel, sig);
 					asynchronousMessages.remove(asat);
 					_aspt.linkedAsynchronousMessage = asat;
 				}
diff --git a/src/ui/avatarinteractivesimulation/AvatarSpecificationSimulationSDPanel.java b/src/ui/avatarinteractivesimulation/AvatarSpecificationSimulationSDPanel.java
index f6d392b694..1be6299c8a 100644
--- a/src/ui/avatarinteractivesimulation/AvatarSpecificationSimulationSDPanel.java
+++ b/src/ui/avatarinteractivesimulation/AvatarSpecificationSimulationSDPanel.java
@@ -60,7 +60,7 @@ import avatartranslator.directsimulation.*;
 import myutil.*;
 import ui.*;
 
-public class AvatarSpecificationSimulationSDPanel extends JPanel  {
+public class AvatarSpecificationSimulationSDPanel extends JPanel implements MouseMotionListener  {
 	
 	private static int MAX_X = 800;
 	private static int MAX_Y = 200;
@@ -73,7 +73,7 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
     private final int limit = 10;
 	
 	// Drawing parameters
-	private int spaceBetweenLifeLines = 200;
+	private int spaceBetweenLifeLines = 150;
 	private int spaceAtEnd = 50;
 	private int spaceAtTop = 50;
 	private int verticalSpaceUnderBlocks = 15;
@@ -93,6 +93,11 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
 	private JScrollPane jsp;
 	private boolean mustScroll = true;
 	
+	// Mouse
+	private int xMouse, yMouse;
+	private boolean drawInfo = false;
+	private long clockValueMouse;
+	
 	
     public AvatarSpecificationSimulationSDPanel(AvatarSpecificationSimulation _ass) {
 		ass = _ass;
@@ -100,6 +105,8 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
 		setBackground(Color.WHITE);
 		
 		setNewSize();
+		
+		addMouseMotionListener(this);
     }
 	
 	public void setMyScrollPanel(JScrollPane _jsp) {
@@ -131,6 +138,10 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
 				mustScroll = false;
 			}
 		}
+		
+		if (drawInfo) {
+			drawInfo(g);
+		}
     }
 	
 	protected void scrollToLowerPosition() {
@@ -175,7 +186,8 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
 		if (allTransactions.size() > 0) {
 			ast = allTransactions.get(0);
 			clockValue = ast.clockValueWhenFinished-ast.duration;
-			g.drawString("@" + clockValue, 10, currentY); 
+			clockValueMouse = clockValue;
+			g.drawString("@" + clockValue, 10, currentY+g.getFontMetrics().getHeight()/2); 
 		}
 		
 		for(int i=Math.max(allTransactions.size()-drawnTransactions, 0); i<allTransactions.size(); i++) {
@@ -209,7 +221,10 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
 				}
 				if (ast.clockValueWhenFinished != clockValue) {
 					clockValue = ast.clockValueWhenFinished;
-					g.drawString("@" + clockValue, 10, newCurrentY); 
+					if (yMouse >= newCurrentY) {
+						clockValueMouse = clockValue;
+					}
+					g.drawString("@" + clockValue, 10, newCurrentY+g.getFontMetrics().getHeight()/2); 
 				}
 			}
 			
@@ -384,7 +399,6 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
 					// Vertical line of receiving block
 					g.drawLine(currentX, currentY-20, currentX, currentY);
 					return currentY;
-					
 				}
 			} else {
 				// In, asynchronous
@@ -434,6 +448,8 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
 							//g.drawLine(x + lengthAsync, y, currentX-lengthAsync, currentY);
 						}
 					}
+				} else {
+					TraceManager.addDev("No linked transaction");
 				}
 				
 				currentY += 10;
@@ -503,4 +519,42 @@ public class AvatarSpecificationSimulationSDPanel extends JPanel  {
 		revalidate();
 	}
 	
+	public void mouseDragged(MouseEvent e) {
+	}
+ 
+	public void mouseMoved(MouseEvent e) {
+		xMouse = e.getX();
+		yMouse = e.getY();
+		if ((xMouse > minLimit) && (xMouse<maxX-spaceAtEnd) && (yMouse> spaceAtTop) && (yMouse<(maxY))) {
+			drawInfo = true;
+			repaint();
+			return;
+		}
+		
+		if( drawInfo == true) {
+			drawInfo = false;
+			repaint();
+			return;
+		}
+		
+		drawInfo = false;
+	}
+	
+	private void drawInfo(Graphics g) {
+		GraphicLib.dashedLine(g, spaceAtEnd, yMouse, maxX-spaceAtEnd, yMouse);
+		g.drawString("@" + clockValueMouse, 10, yMouse+g.getFontMetrics().getHeight()/2); 
+		
+		String name;
+		int w;
+		Vector<AvatarSimulationBlock> blocks = ass.getSimulationBlocks();
+		int x = spaceAtEnd;
+		
+		for(AvatarSimulationBlock block: blocks) {
+			name = block.getBlock().getName();
+			w = g.getFontMetrics().stringWidth(name);
+			g.drawString(name, x + ((spaceBetweenLifeLines-w)/2), yMouse - spaceVerticalText);
+			x += spaceBetweenLifeLines;
+		}
+	}
+	
 }
\ No newline at end of file
diff --git a/src/ui/avatarinteractivesimulation/JFrameAvatarInteractiveSimulation.java b/src/ui/avatarinteractivesimulation/JFrameAvatarInteractiveSimulation.java
index d0a749c527..f9cdfe2ebd 100755
--- a/src/ui/avatarinteractivesimulation/JFrameAvatarInteractiveSimulation.java
+++ b/src/ui/avatarinteractivesimulation/JFrameAvatarInteractiveSimulation.java
@@ -682,7 +682,7 @@ public	class JFrameAvatarInteractiveSimulation extends JFrame implements AvatarS
 		} catch (Exception e) {
 			nb = 1;
 		}
-		ass.setNbOfCommands(1);
+		ass.setNbOfCommands(nb);
 		ass.unstop();
 	}
 	
@@ -920,6 +920,10 @@ public	class JFrameAvatarInteractiveSimulation extends JFrame implements AvatarS
 		return false;
 	}
 	
+	public void actSaveTxt() {
+		ass.printExecutedTransactions();
+	}
+	
 	
 	// Mouse management
 	public void mouseReleased(MouseEvent e) {}
@@ -978,6 +982,10 @@ public	class JFrameAvatarInteractiveSimulation extends JFrame implements AvatarS
 			close();
 			return;
 			//TraceManager.addDev("Start simulation!");
+		} else if (command.equals(actions[AvatarInteractiveSimulationActions.ACT_SAVE_TXT].getActionCommand()))  {
+			actSaveTxt();
+			return;
+			//TraceManager.addDev("Start simulation!");
 		}
 	}
 	
-- 
GitLab