From 85cc0fc8a104dfdd65f83591e2d1c55bf9864595 Mon Sep 17 00:00:00 2001
From: jawher-j <92947144+jawher-j@users.noreply.github.com>
Date: Fri, 28 Jun 2024 19:28:39 +0200
Subject: [PATCH] update spec TML comparator

---
 .../tmltranslator/TMLActivityElement.java     |  7 +-
 .../TMLActivityElementChannel.java            | 15 +----
 .../TMLActivityElementEvent.java              |  5 +-
 .../TMLActivityElementWithAction.java         |  1 -
 .../TMLActivityElementWithIntervalAction.java |  1 -
 src/main/java/tmltranslator/TMLAttribute.java | 19 ++++--
 src/main/java/tmltranslator/TMLChoice.java    |  1 -
 .../tmltranslator/TMLComparingMethod.java     | 66 +++++++++++++++----
 src/main/java/tmltranslator/TMLDelay.java     |  1 -
 src/main/java/tmltranslator/TMLExecC.java     |  1 -
 src/main/java/tmltranslator/TMLForLoop.java   |  1 -
 src/main/java/tmltranslator/TMLRandom.java    |  1 -
 .../java/tmltranslator/TMLRandomSequence.java |  1 -
 .../java/tmltranslator/TMLSendRequest.java    |  3 +-
 src/main/java/tmltranslator/TMLSequence.java  |  1 -
 src/main/java/tmltranslator/TMLTask.java      |  4 +-
 src/main/java/tmltranslator/TMLType.java      | 16 +++--
 17 files changed, 89 insertions(+), 55 deletions(-)

diff --git a/src/main/java/tmltranslator/TMLActivityElement.java b/src/main/java/tmltranslator/TMLActivityElement.java
index 9b06395c10..4a7cedbfb0 100755
--- a/src/main/java/tmltranslator/TMLActivityElement.java
+++ b/src/main/java/tmltranslator/TMLActivityElement.java
@@ -41,6 +41,8 @@
 
 package tmltranslator;
 
+import myutil.TraceManager;
+
 import java.util.Objects;
 import java.util.Vector;
 
@@ -158,12 +160,11 @@ public abstract class TMLActivityElement extends TMLElement {
 
     public boolean equalSpec(Object o) {
         if (o == null || getClass() != o.getClass()) return false;
-        if (this == o) return true;
 
         TMLActivityElement tmlActEtls = (TMLActivityElement) o;
         TMLComparingMethod comp = new TMLComparingMethod();
-        return Objects.equals(value,tmlActEtls.getValue()) &&
-                comp.isTMLActivityEltListEquals(nexts,tmlActEtls.getNexts());
+        return Objects.equals(value, tmlActEtls.getValue()) && comp.isSecurityPatternEquals(securityPattern, tmlActEtls.getSecurityPattern()) &&
+                comp.isTMLActivityEltListEquals(nexts, tmlActEtls.getNexts());
     }
 
     public void setCheckableAccessibility(boolean b) {
diff --git a/src/main/java/tmltranslator/TMLActivityElementChannel.java b/src/main/java/tmltranslator/TMLActivityElementChannel.java
index 2bf1606e99..05d11a4eb9 100755
--- a/src/main/java/tmltranslator/TMLActivityElementChannel.java
+++ b/src/main/java/tmltranslator/TMLActivityElementChannel.java
@@ -127,24 +127,13 @@ public abstract class TMLActivityElementChannel extends TMLActivityElement {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLActivityElementChannel)) return false;
         if (!super.equalSpec(o)) return false;
         TMLActivityElementChannel tmlActEltChannel = (TMLActivityElementChannel) o;
-
+        // TMLComparingMethod comp = new TMLComparingMethod();
         // Check if the two TMLActivityElementChannel have the same channels list.
-        for (TMLChannel ch : channels) {
-            boolean isEqualChan = false;
-            for (TMLChannel ch2 : tmlActEltChannel.channels) {
-                if (ch.equalSpec(ch2)) {
-                    isEqualChan = true;
-                    break;
-                }
-            }
-            if (!isEqualChan) return false;
-        }
+        // if (!comp.isTMLChannelListEquals(channels, tmlActEltChannel.channels)) return false;
         return Objects.equals(nbOfSamples, tmlActEltChannel.getNbOfSamples()) &&
                 isAttacker == tmlActEltChannel.isAttacker();
-
     }
 
     public void fillValues(TMLActivityElementChannel newElt, TMLModeling tmlm) throws TMLCheckingError {
diff --git a/src/main/java/tmltranslator/TMLActivityElementEvent.java b/src/main/java/tmltranslator/TMLActivityElementEvent.java
index a2d0024aad..b5062ce352 100755
--- a/src/main/java/tmltranslator/TMLActivityElementEvent.java
+++ b/src/main/java/tmltranslator/TMLActivityElementEvent.java
@@ -146,11 +146,10 @@ public abstract class TMLActivityElementEvent extends TMLActivityElement {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLActivityElementEvent)) return false;
         if (!super.equalSpec(o)) return false;
         TMLActivityElementEvent tmlActivityElementEvent = (TMLActivityElementEvent) o;
         // Check if the two TMLActivityElementEvent have the same events list.
-        for (TMLEvent evt1 : events) {
+        /*for (TMLEvent evt1 : events) {
             boolean isEqualEvt = false;
             for (TMLEvent evt2 : tmlActivityElementEvent.getEvents()) {
                 if (evt1.equalSpec(evt2)) {
@@ -160,7 +159,7 @@ public abstract class TMLActivityElementEvent extends TMLActivityElement {
             }
             if (!isEqualEvt) return false;
         }
-        if (!event.equalSpec(tmlActivityElementEvent.getEvent())) return false;
+        if (!event.equalSpec(tmlActivityElementEvent.getEvent())) return false;*/
         return (new HashSet<>(datas)).equals(new HashSet<>(tmlActivityElementEvent.getDatas())) &&
                 Objects.equals(variable, tmlActivityElementEvent.getVariable());
     }
diff --git a/src/main/java/tmltranslator/TMLActivityElementWithAction.java b/src/main/java/tmltranslator/TMLActivityElementWithAction.java
index c2511b7431..513529e9b3 100755
--- a/src/main/java/tmltranslator/TMLActivityElementWithAction.java
+++ b/src/main/java/tmltranslator/TMLActivityElementWithAction.java
@@ -80,7 +80,6 @@ public abstract class TMLActivityElementWithAction extends TMLActivityElement {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLActivityElementWithAction)) return false;
         if (!super.equalSpec(o)) return false;
         TMLActivityElementWithAction tmlActivityElementWithAction = (TMLActivityElementWithAction) o;
         return Objects.equals(action, tmlActivityElementWithAction.getAction());
diff --git a/src/main/java/tmltranslator/TMLActivityElementWithIntervalAction.java b/src/main/java/tmltranslator/TMLActivityElementWithIntervalAction.java
index 2fcae17531..a1400ba132 100755
--- a/src/main/java/tmltranslator/TMLActivityElementWithIntervalAction.java
+++ b/src/main/java/tmltranslator/TMLActivityElementWithIntervalAction.java
@@ -85,7 +85,6 @@ public abstract class TMLActivityElementWithIntervalAction extends TMLActivityEl
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLActivityElementWithIntervalAction)) return false;
         if (!super.equalSpec(o)) return false;
 
         TMLActivityElementWithIntervalAction tmlAEIAction = (TMLActivityElementWithIntervalAction) o;
diff --git a/src/main/java/tmltranslator/TMLAttribute.java b/src/main/java/tmltranslator/TMLAttribute.java
index 1ff191a079..c73068f95e 100755
--- a/src/main/java/tmltranslator/TMLAttribute.java
+++ b/src/main/java/tmltranslator/TMLAttribute.java
@@ -139,17 +139,24 @@ public class TMLAttribute extends DIPLOElement {
         return "unknown";
     }
 
-    @Override public boolean equals( Object o )        {
-        if( !(o instanceof TMLAttribute ) )     {
+    @Override
+    public boolean equals(Object o) {
+        if (!(o instanceof TMLAttribute)) {
             return false;
         }
-        else    {
-            TMLAttribute attr = (TMLAttribute)o;
-            return ( name.equals( attr.getName() ) && ( type.equals( attr.getType()) ) );
-        }
+        TMLAttribute attr = (TMLAttribute) o;
+        return (name.equals(attr.getName()) && (type.equals(attr.getType())));
         //return ( (name.equals( _other.getName() )) && ( initialValue.equals( _other.getInitialValue() )) && (type.equals( _other.getType() )) );
     }
 
+    public boolean equalSpec(Object o) {
+        if (!(o instanceof TMLAttribute)) {
+            return false;
+        }
+        TMLAttribute attr = (TMLAttribute) o;
+        return (name.equals(attr.getName())) && (initialValue.equals(attr.getInitialValue())) && (type.equalSpec(attr.getType()));
+    }
+
     @Override public int hashCode()     {
         int result = 17;
         if( name == null )      {
diff --git a/src/main/java/tmltranslator/TMLChoice.java b/src/main/java/tmltranslator/TMLChoice.java
index 84147db3d1..9d0797e11f 100755
--- a/src/main/java/tmltranslator/TMLChoice.java
+++ b/src/main/java/tmltranslator/TMLChoice.java
@@ -353,7 +353,6 @@ public class TMLChoice extends TMLActivityElement{
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLChoice)) return false;
         if (!super.equalSpec(o)) return false;
         TMLChoice tmlChoice = (TMLChoice) o;
         return (new HashSet<>(guards)).equals(new HashSet<>(tmlChoice.getGuards()));
diff --git a/src/main/java/tmltranslator/TMLComparingMethod.java b/src/main/java/tmltranslator/TMLComparingMethod.java
index 636f4d4d21..b27ceaaacc 100644
--- a/src/main/java/tmltranslator/TMLComparingMethod.java
+++ b/src/main/java/tmltranslator/TMLComparingMethod.java
@@ -44,10 +44,10 @@ public class TMLComparingMethod {
             return true;
         }
         //Only one of them is null
-        else if(list1 == null || list2 == null) {
+        else if (list1 == null || list2 == null) {
             return false;
         }
-        else if(list1.size() != list2.size()) {
+        else if (list1.size() != list2.size()) {
             return false;
         }
 
@@ -193,14 +193,16 @@ public class TMLComparingMethod {
 
         boolean test;
         for (SecurityPattern spMap1 : map1.keySet()) {
+            boolean isEqual = false;
             for (SecurityPattern spMap2 : map2.keySet()) {
                 if (spMap1.getName().equals(spMap2.getName())) {
                     test = isHwMemoryListEquals(map1.get(spMap1), map2.get(spMap2));
                     if (!test) return false;
+                    isEqual = true;
                     break;
                 }
             }
-            return false;
+            if (!isEqual) return false;
         }
 
         return true;
@@ -240,18 +242,15 @@ public class TMLComparingMethod {
             return true;
         }
         //Only one of them is null
-        else if(list1 == null || list2 == null) {
+        else if (list1 == null || list2 == null) {
             return false;
         }
-        else if(list1.size() != list2.size()) {
+        else if (list1.size() != list2.size()) {
             return false;
         }
 
-        boolean test;
-
-        for (int i = 0; i< list1.size(); i++) {
-            test = list1.get(i).equalSpec(list2.get(i));
-            if (!test) return false;
+        for (int i = 0; i < list1.size(); i++) {
+            if (!list1.get(i).equalSpec(list2.get(i))) return false;
         }
         return true;
     }
@@ -346,7 +345,7 @@ public class TMLComparingMethod {
 
     public boolean isTMLChannelSetEquals(Set<TMLChannel> channelSet1, Set<TMLChannel> channelSet2) {
         if (channelSet1 == null && channelSet2 == null) return true;
-        if (channelSet1 == null|| channelSet2 == null) return false;
+        if (channelSet1 == null || channelSet2 == null) return false;
 
         if(channelSet1.size() != channelSet2.size()) return false;
 
@@ -384,8 +383,53 @@ public class TMLComparingMethod {
             test =  events1.get(i).equalSpec(events2.get(i));
             if (!test) return false;
         }
+        return true;
+    }
 
+    // Check equality of two security pattern specifications
+    public boolean isSecurityPatternEquals(SecurityPattern sp1, SecurityPattern sp2) {
+        if (sp1 == null && sp2 == null) return true;
+        else if (sp1 == null || sp2 == null) {
+            return false;
+        }
+        return (sp1.equalSpec(sp2));
+    }
+
+    // Check equality of two TMLAttribute Lists
+    public boolean isTMLAttributeListEquals(List<TMLAttribute> attributeList1, List<TMLAttribute> attributeList2) {
+        if (attributeList1 == null && attributeList2 == null) return true;
+        if (attributeList1 == null || attributeList2 == null) return false;
+        if(attributeList1.size() != attributeList2.size()) return false;
+
+        for (TMLAttribute attrib : attributeList1) {
+            boolean isEqualAttrib = false;
+            for (TMLAttribute attrib2 : attributeList2) {
+                if (!attrib.equalSpec(attrib2)) {
+                    isEqualAttrib = true;
+                    break;
+                }
+            }
+            if (!isEqualAttrib) return false;
+        }
         return true;
     }
 
+    // Check if the two TMLActivityElementChannel have the same channels list.
+    public boolean isTMLChannelListEquals(List<TMLChannel> channelList1, List<TMLChannel> channelList2) {
+        if (channelList1 == null && channelList2 == null) return true;
+        if (channelList1 == null || channelList2 == null) return false;
+        if(channelList1.size() != channelList2.size()) return false;
+
+        for (TMLChannel ch : channelList1) {
+            boolean isEqualChan = false;
+            for (TMLChannel ch2 : channelList2) {
+                if (!ch.equalSpec(ch2)) {
+                    isEqualChan = true;
+                    break;
+                }
+            }
+            if (!isEqualChan) return false;
+        }
+        return true;
+    }
 }
diff --git a/src/main/java/tmltranslator/TMLDelay.java b/src/main/java/tmltranslator/TMLDelay.java
index 7ae1a41031..3510ae48dc 100755
--- a/src/main/java/tmltranslator/TMLDelay.java
+++ b/src/main/java/tmltranslator/TMLDelay.java
@@ -95,7 +95,6 @@ public class TMLDelay extends TMLActivityElementWithIntervalAction {
 
 	@Override
 	public boolean equalSpec(Object o) {
-    	if (! (o instanceof TMLDelay)) return false;
 		if (!super.equalSpec(o)) return false;
 
 		TMLDelay tmlDelay = (TMLDelay) o;
diff --git a/src/main/java/tmltranslator/TMLExecC.java b/src/main/java/tmltranslator/TMLExecC.java
index b736434d6d..dddc500dd4 100755
--- a/src/main/java/tmltranslator/TMLExecC.java
+++ b/src/main/java/tmltranslator/TMLExecC.java
@@ -74,7 +74,6 @@ public class TMLExecC extends TMLActivityElementWithAction {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLExecC)) return false;
         if (!super.equalSpec(o)) return false;
         TMLExecC tmlExecC = (TMLExecC) o;
         return Objects.equals(isDecryptionProcess, tmlExecC.isDecryptionProcess());
diff --git a/src/main/java/tmltranslator/TMLForLoop.java b/src/main/java/tmltranslator/TMLForLoop.java
index 8c0ea888b0..6a030b569e 100755
--- a/src/main/java/tmltranslator/TMLForLoop.java
+++ b/src/main/java/tmltranslator/TMLForLoop.java
@@ -85,7 +85,6 @@ public class TMLForLoop extends TMLActivityElement {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLForLoop)) return false;
         if(!super.equalSpec(o)) return false;
         TMLForLoop tmlForLoop = (TMLForLoop) o;
         return Objects.equals(init, tmlForLoop.getInit()) &&
diff --git a/src/main/java/tmltranslator/TMLRandom.java b/src/main/java/tmltranslator/TMLRandom.java
index 0a5c1dc864..613db754ba 100755
--- a/src/main/java/tmltranslator/TMLRandom.java
+++ b/src/main/java/tmltranslator/TMLRandom.java
@@ -76,7 +76,6 @@ public class TMLRandom extends TMLActivityElement {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLRandom)) return false;
         if (!super.equalSpec(o)) return false;
 
         TMLRandom tmlRandom = (TMLRandom) o;
diff --git a/src/main/java/tmltranslator/TMLRandomSequence.java b/src/main/java/tmltranslator/TMLRandomSequence.java
index 07741656c5..29af794568 100755
--- a/src/main/java/tmltranslator/TMLRandomSequence.java
+++ b/src/main/java/tmltranslator/TMLRandomSequence.java
@@ -108,7 +108,6 @@ public class TMLRandomSequence extends TMLActivityElement {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLRandomSequence)) return false;
         if (!super.equalSpec(o)) return false;
 
         TMLRandomSequence tmlRS = (TMLRandomSequence) o;
diff --git a/src/main/java/tmltranslator/TMLSendRequest.java b/src/main/java/tmltranslator/TMLSendRequest.java
index 77de3b26ac..4ec2e96281 100755
--- a/src/main/java/tmltranslator/TMLSendRequest.java
+++ b/src/main/java/tmltranslator/TMLSendRequest.java
@@ -125,11 +125,10 @@ public class TMLSendRequest extends TMLActivityElement  {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLSendRequest)) return false;
         if (!super.equalSpec(o)) return false;
 
         TMLSendRequest tmlSendRequest = (TMLSendRequest) o;
-        if (!request.equalSpec(tmlSendRequest.getRequest())) return false;
+        //if (!request.equalSpec(tmlSendRequest.getRequest())) return false;
         return (new HashSet<>(datas)).equals(new HashSet<>(tmlSendRequest.getDatas()));
     }
 
diff --git a/src/main/java/tmltranslator/TMLSequence.java b/src/main/java/tmltranslator/TMLSequence.java
index 8f50cdc68d..90255352d4 100755
--- a/src/main/java/tmltranslator/TMLSequence.java
+++ b/src/main/java/tmltranslator/TMLSequence.java
@@ -103,7 +103,6 @@ public class TMLSequence extends TMLActivityElement {
 
     @Override
     public boolean equalSpec(Object o) {
-        if (!(o instanceof TMLSequence)) return false;
         if (!super.equalSpec(o)) return false;
 
         TMLSequence tmlSequence = (TMLSequence) o;
diff --git a/src/main/java/tmltranslator/TMLTask.java b/src/main/java/tmltranslator/TMLTask.java
index 1fdc66ec15..8bf7e31152 100755
--- a/src/main/java/tmltranslator/TMLTask.java
+++ b/src/main/java/tmltranslator/TMLTask.java
@@ -583,9 +583,7 @@ public class TMLTask extends TMLElement {
         }
 
         //TraceManager.addDev("Going to compare attributes");
-
-        if (!(new HashSet<>(attributes).equals(new HashSet<>(tmlTask.attributes))))
-            return false;
+        if (!comp.isTMLAttributeListEquals(attributes, tmlTask.getAttributes())) return false;
 
         //TraceManager.addDev("HashSet of attributes ok");
 
diff --git a/src/main/java/tmltranslator/TMLType.java b/src/main/java/tmltranslator/TMLType.java
index 2b3bff733a..9f0d452183 100755
--- a/src/main/java/tmltranslator/TMLType.java
+++ b/src/main/java/tmltranslator/TMLType.java
@@ -162,14 +162,20 @@ public class TMLType {
         return getStringType(type);
     }
 
-    public boolean equals( Object o )   {
-        if( !( o instanceof TMLType ) ) {
+    public boolean equals(Object o) {
+        if (!(o instanceof TMLType)) {
             return false;
         }
-        else    {
-            TMLType tt = (TMLType)o;
-            return ( getType() == tt.getType() );
+        TMLType tt = (TMLType) o;
+        return (getType() == tt.getType());
+    }
+
+    public boolean equalSpec(Object o) {
+        if (!(o instanceof TMLType)) {
+            return false;
         }
+        TMLType tt = (TMLType) o;
+        return (getType() == tt.getType()) && typeOther.equals(tt.getTypeOther());
     }
 
     @Override public int hashCode()     {
-- 
GitLab