From bb3501f88280a8e86b02653d3470bedbc30345ab Mon Sep 17 00:00:00 2001
From: Andrea Enrici <andrea.enrici@nokia.com>
Date: Wed, 19 Nov 2014 10:25:12 +0000
Subject: [PATCH] not using TMLSDAttribute anymore. Checking message parameters
 in SD diagrams

---
 src/tmltranslator/TMLAttribute.java        |  7 +++
 src/tmltranslator/TMLCP.java               |  2 +-
 src/tmltranslator/TMLCPSyntaxChecking.java | 55 +++++++++++++---------
 src/tmltranslator/tmlcp/TMLSDEvent.java    | 18 +++++--
 src/tmltranslator/tmlcp/TMLSDMessage.java  | 30 ++++++------
 5 files changed, 69 insertions(+), 43 deletions(-)

diff --git a/src/tmltranslator/TMLAttribute.java b/src/tmltranslator/TMLAttribute.java
index 0eb7ba186a..b062a7b8a6 100755
--- a/src/tmltranslator/TMLAttribute.java
+++ b/src/tmltranslator/TMLAttribute.java
@@ -76,6 +76,13 @@ public class TMLAttribute extends DIPLOElement {
 		this.initialValue = "NULL";
     }
 
+    public TMLAttribute( String _name ) {
+  		this.name = _name;
+			this.instanceName = "NO_NAME";
+			this.type = new TMLType( TMLType.OTHER );
+			this.initialValue = "NULL";
+    }
+
 		public String getInstanceName()	{
 			return instanceName;
 		}
diff --git a/src/tmltranslator/TMLCP.java b/src/tmltranslator/TMLCP.java
index f3214b8198..ef3f4898ab 100755
--- a/src/tmltranslator/TMLCP.java
+++ b/src/tmltranslator/TMLCP.java
@@ -430,7 +430,7 @@ public class TMLCP extends TMLElement {
 
             //Print Messages
             ArrayList<TMLSDMessage> listMessages;
-            ArrayList<TMLSDAttribute> msgAttributes;
+            ArrayList<TMLAttribute> msgAttributes;
             TMLSDMessage msg;
             listMessages = tempSD.getMessages();
             System.out.println( "\tMessages:" );
diff --git a/src/tmltranslator/TMLCPSyntaxChecking.java b/src/tmltranslator/TMLCPSyntaxChecking.java
index 5dac788026..ec0ed979ed 100755
--- a/src/tmltranslator/TMLCPSyntaxChecking.java
+++ b/src/tmltranslator/TMLCPSyntaxChecking.java
@@ -256,10 +256,11 @@ public class TMLCPSyntaxChecking {
 
 		for( TMLCPSequenceDiagram diag: listSDs )	{
 			ArrayList<TMLAttribute> attributes = diag.getAttributes();
-			checkActions( diag, attributes );			// actions must be done on variables that have
-			checkMessages( diag, attributes );		// check that attributes have been declared
-																						// been declared and coherently boolean = boolean + 6 is not allowed
-			checkInstances( diag );								// instances within the same SD must all have different names
+			//checkUniquenessOfAttributesNames( diag );	// already done in the GUI when declaring attributes
+			checkActions( diag, attributes );						// actions must be done on variables that have
+			checkMessages( diag );											// check that attributes have been declared
+																									// been declared and coherently boolean = boolean + 6 is not allowed
+			checkInstances( diag );											// instances within the same SD must all have different names
 		}
 	}
 
@@ -290,29 +291,39 @@ public class TMLCPSyntaxChecking {
 		}
 	}
 
-	private void checkMessages( TMLCPSequenceDiagram diag, ArrayList<TMLAttribute> attributes )	{
-		ArrayList<TMLSDMessage> messages = diag.getMessages();
-		boolean foundAttribute = false;
-		for( TMLSDMessage msg: messages )	{
-			ArrayList<TMLSDAttribute> attributesMsg = msg.getAttributes();
-			TraceManager.addDev( "PRINTING ATTRIBUTES OF MSG " + msg + attributesMsg.toString() );
-			for( TMLSDAttribute attr: attributesMsg )	{
-				for( TMLAttribute attr2: attributes )	{	//attributes of SD diagram
-					if( attr.getName().equals( attr2.getName() ) )	{
-						TraceManager.addDev( "FOUND MATCHING ATTRIBUTES " + attr.getName() + " " + attr2.getName() );
-						foundAttribute = true;
-						break;
-					}
+	// Check that for each message parameter, the corresponding attribute has been declared in both the sender
+	// and the receiver instances with the same name
+	private void checkMessages( TMLCPSequenceDiagram diag )	{
+
+		ArrayList<TMLSDMessage> messagesList = diag.getMessages();
+
+		for( TMLSDMessage message: messagesList )	{
+			String senderInstance = message.getSenderName();
+			String receiverInstance = message.getReceiverName();
+			ArrayList<TMLAttribute> parametersList = message.getAttributes();
+			for( TMLAttribute parameter: parametersList )	{
+				if( !isParameterDeclared( parameter, senderInstance, diag ) )	{
+				addError( "Parameter <<" + parameter.getName() + ">> has not been declared in instance <<" + senderInstance + ">> in diagram <<" + diag.getName() + ">>", TMLCPError.ERROR_STRUCTURE );
 				}
-				if( !foundAttribute )	{
-					addError( " Attribute <<" + attr.getName() + ">> has not been declared in diagram <<" + diag.getName() + ">>",
-										TMLCPError.ERROR_STRUCTURE );
+				if( !isParameterDeclared( parameter, receiverInstance, diag ) )	{
+				addError( "Parameter <<" + parameter.getName() + ">> has not been declared in instance <<" + receiverInstance + ">> in diagram <<" + diag.getName() + ">>", TMLCPError.ERROR_STRUCTURE );
 				}
-				else	{
-					foundAttribute = false;
+			}
+		}
+	}
+
+	private boolean isParameterDeclared( TMLAttribute parameter, String instanceName, TMLCPSequenceDiagram diag )	{
+
+		for( TMLSDInstance instance: diag.getInstances() )	{
+			if( instance.getName().equals( instanceName ) )	{
+				for( TMLAttribute attribute: instance.getAttributes() )	{	//don't use contains() as parameter is created with some partial attributes
+					if( attribute.getName().equals( parameter.getName() ) )	{
+						return true;
+					}
 				}
 			}
 		}
+		return false;
 	}
 
 	private void checkInstances( TMLCPSequenceDiagram diag )	{
diff --git a/src/tmltranslator/tmlcp/TMLSDEvent.java b/src/tmltranslator/tmlcp/TMLSDEvent.java
index 75a7a70c66..840c8e55ab 100755
--- a/src/tmltranslator/tmlcp/TMLSDEvent.java
+++ b/src/tmltranslator/tmlcp/TMLSDEvent.java
@@ -64,10 +64,10 @@ public class TMLSDEvent implements Comparable<TMLSDEvent>  {
     private final static String ERROR = "ERROR_IN_EVENT";
     private int type;
     private int yCoord;
-    private Object ref;
+    private Object referenceObject;
 
     public TMLSDEvent( Object _referenceObject, int _type, int _yCoord ) {
-        this.ref = _referenceObject;
+        this.referenceObject = _referenceObject;
         this.yCoord = _yCoord;
         this.type = _type;
     }
@@ -76,6 +76,14 @@ public class TMLSDEvent implements Comparable<TMLSDEvent>  {
         return yCoord;
     }
 
+		public int getType()	{
+			return this.type;
+		}
+
+		public Object getReferenceObject()	{
+			return this.referenceObject;
+		}
+
     @Override public int compareTo( TMLSDEvent _event ) {
 	//TraceManager.addDev("Comparing events");
         int compareValue = ((TMLSDEvent) _event).getYCoord();
@@ -98,13 +106,13 @@ public class TMLSDEvent implements Comparable<TMLSDEvent>  {
 
 			switch( type )	{
 				case 0:	//send message
-            msg = ( (TMLSDMessage) ref);
+            msg = ( (TMLSDMessage) referenceObject );
         		return SEND_MESSAGE_LABEL + msg.toString();
 				case 1:	//receive message
-            msg = ( (TMLSDMessage) ref);
+            msg = ( (TMLSDMessage) referenceObject );
         		return RECEIVE_MESSAGE_LABEL + msg.toString();
 				case 2:	//action
-     	      TMLSDAction action = ( (TMLSDAction) ref);
+     	      TMLSDAction action = ( (TMLSDAction) referenceObject );
             return ACTION_LABEL + action.toString();
 				default:
 						return ERROR;
diff --git a/src/tmltranslator/tmlcp/TMLSDMessage.java b/src/tmltranslator/tmlcp/TMLSDMessage.java
index 7e0ee49214..dc27c633cf 100755
--- a/src/tmltranslator/tmlcp/TMLSDMessage.java
+++ b/src/tmltranslator/tmlcp/TMLSDMessage.java
@@ -53,27 +53,27 @@ import myutil.*;
 
 public class TMLSDMessage extends TMLElement  {
 
-	//mind the difference between TMLSDAttribute and TMLAttribute!
-	private ArrayList<TMLSDAttribute> attributeList;	
+	//mind the difference between TMLAttribute and TMLAttribute!
+	private ArrayList<TMLAttribute> attributeList;	
 	private String senderName = "";
 	private String receiverName = "";
 	private int yCoord;
 
-  public TMLSDMessage( String _name, /*String _senderName, String _receiverName,*/ Object _referenceObject ) {
-  	super( _name, _referenceObject );
+  //public TMLSDMessage( String _name, /*String _senderName, String _receiverName,*/ Object _referenceObject ) {
+  	/*super( _name, _referenceObject );
 		this.yCoord = -1;
 		this.senderName = "";//_senderName;
 		this.receiverName = "";//_receiverName;
-		attributeList = new ArrayList<TMLSDAttribute>();
-	}
+		attributeList = new ArrayList<TMLAttribute>();
+	}*/
 	
-  public TMLSDMessage( String _name, /*String _senderName, String _receiverName,*/ int _yCoord, Object _referenceObject ) {
-  	super( _name, _referenceObject );
+  //public TMLSDMessage( String _name, /*String _senderName, String _receiverName,*/ int _yCoord, Object _referenceObject ) {
+  	/*super( _name, _referenceObject );
 		this.senderName = "";//_senderName;
 		this.receiverName = "";//_receiverName;
 		this.yCoord = _yCoord;
-		attributeList = new ArrayList<TMLSDAttribute>();
-	}
+		attributeList = new ArrayList<TMLAttribute>();
+	}*/
 
 	public TMLSDMessage( String _name, String _senderName, String _receiverName, int _yCoord,
 												Object _referenceObject, ArrayList<String> _params )	{
@@ -81,9 +81,9 @@ public class TMLSDMessage extends TMLElement  {
 		this.yCoord = _yCoord;
 		this.senderName = _senderName;
 		this.receiverName = _receiverName;
-		attributeList = new ArrayList<TMLSDAttribute>();
+		attributeList = new ArrayList<TMLAttribute>();
 		for( String p: _params )	{
-			attributeList.add( new TMLSDAttribute(p) );
+			attributeList.add( new TMLAttribute(p) );
 		}
 	}
 
@@ -95,12 +95,12 @@ public class TMLSDMessage extends TMLElement  {
 		return receiverName;
 	}
     
-	public void addAttribute( TMLSDAttribute _attribute )	{
+	public void addAttribute( TMLAttribute _attribute )	{
 		if( _attribute != null )
 			attributeList.add( _attribute );
 	}
 
-	public ArrayList<TMLSDAttribute> getAttributes()	{
+	public ArrayList<TMLAttribute> getAttributes()	{
 		return attributeList;
 	}
 
@@ -116,7 +116,7 @@ public class TMLSDMessage extends TMLElement  {
 
 		String s = this.name + "(";
 		if( attributeList.size() > 0 )	{
-			for( TMLSDAttribute attribute: attributeList )	{
+			for( TMLAttribute attribute: attributeList )	{
 				s += attribute.getName() + ",";
 			}
 			String newS = s.substring( 0, s.length() - 1 );
-- 
GitLab