From 545d9c69d03bc8cfa880f1ed855c0e436dc42da0 Mon Sep 17 00:00:00 2001
From: Ludovic Apvrille <ludovic.apvrille@telecom-paris.fr>
Date: Tue, 23 May 2023 17:55:27 +0200
Subject: [PATCH] Adding system block identification with AI

---
 modeling/SysMLSec/AliceAndBobHW.xml           |  72 +--
 .../avatartranslator/AvatarSpecification.java | 472 ++++++++++++++++++
 .../java/avatartranslator/AvatarType.java     |   2 +-
 src/main/java/ui/JToolBarMainTurtle.java      |  15 +-
 src/main/java/ui/MainGUI.java                 |   1 +
 src/main/java/ui/ModeManager.java             |   2 +-
 src/main/java/ui/TGComponent.java             |   2 +-
 src/main/java/ui/avatarbd/AvatarBDPanel.java  | 427 ++++++++--------
 src/main/java/ui/avatarrd/AvatarRDPanel.java  |  59 ++-
 src/main/java/ui/window/JFrameAI.java         |  87 +++-
 10 files changed, 878 insertions(+), 261 deletions(-)

diff --git a/modeling/SysMLSec/AliceAndBobHW.xml b/modeling/SysMLSec/AliceAndBobHW.xml
index 4bbc4bde31..fb0a6e640a 100644
--- a/modeling/SysMLSec/AliceAndBobHW.xml
+++ b/modeling/SysMLSec/AliceAndBobHW.xml
@@ -862,8 +862,8 @@
 <cdparam x="578" y="303" />
 <sizeparam width="0" height="0" minWidth="0" minHeight="0" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <infoparam name="connector" value="Connector between ports" />
-<P1  x="577" y="306" id="371" />
-<P2  x="671" y="306" id="358" />
+<P1  x="577" y="306" id="369" />
+<P2  x="671" y="306" id="356" />
 <AutomaticDrawing  data="true" />
 <new d="false" />
 </CONNECTOR>
@@ -871,8 +871,8 @@
 <cdparam x="578" y="246" />
 <sizeparam width="0" height="0" minWidth="0" minHeight="0" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <infoparam name="connector" value="Connector between ports" />
-<P1  x="577" y="263" id="369" />
-<P2  x="671" y="263" id="356" />
+<P1  x="577" y="263" id="371" />
+<P2  x="671" y="263" id="358" />
 <AutomaticDrawing  data="true" />
 <new d="false" />
 </CONNECTOR>
@@ -895,17 +895,17 @@
 <Data isAttacker="No" daemon="false" periodic="false" periodValue="" unit="ns" Operation="" />
 </extraparam>
 </COMPONENT>
-<SUBCOMPONENT type="1203" id="357" index="4" uid="88d5d3ae-02e6-45d4-939c-f4e23df82248" >
+<SUBCOMPONENT type="1203" id="357" index="4" uid="b6a9696d-2351-4e1d-889e-8e28ef415911" >
 <father id="368" num="0" />
-<cdparam x="659" y="263" />
+<cdparam x="659" y="306" />
 <sizeparam width="25" height="25" minWidth="1" minHeight="1" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <hidden value="false" />
 <cdrectangleparam minX="-12" maxX="147" minY="-12" maxY="108" />
-<infoparam name="Primitive port" value="Channel comm" />
+<infoparam name="Primitive port" value="Channel nonce_ch" />
 <new d="false" />
 <TGConnectingPoint num="0" id="356" />
 <extraparam>
-<Prop commName="comm" commType="0" origin="false" finite="false" blocking="false" maxSamples="8" widthSamples="4" isLossy="false" isPrex="false" isPostex="false" lossPercentage="0" maxNbOfLoss="0" dataFlowType="null" associatedEvent="" checkConf="false" checkConfStatus="0" checkAuth="true" checkWeakAuthStatus="0" checkStrongAuthStatus="0" vc="0" />
+<Prop commName="nonce_ch" commType="0" origin="true" finite="false" blocking="false" maxSamples="8" widthSamples="4" isLossy="false" isPrex="false" isPostex="false" lossPercentage="0" maxNbOfLoss="0" dataFlowType="int16_t" associatedEvent="" checkConf="false" checkConfStatus="0" checkAuth="false" checkWeakAuthStatus="0" checkStrongAuthStatus="0" vc="0" />
 <Type type="0" typeOther="" />
 <Type type="0" typeOther="" />
 <Type type="0" typeOther="" />
@@ -913,17 +913,17 @@
 <Type type="0" typeOther="" />
 </extraparam>
 </SUBCOMPONENT>
-<SUBCOMPONENT type="1203" id="359" index="5" uid="b6a9696d-2351-4e1d-889e-8e28ef415911" >
+<SUBCOMPONENT type="1203" id="359" index="5" uid="88d5d3ae-02e6-45d4-939c-f4e23df82248" >
 <father id="368" num="1" />
-<cdparam x="659" y="306" />
+<cdparam x="659" y="263" />
 <sizeparam width="25" height="25" minWidth="1" minHeight="1" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <hidden value="false" />
 <cdrectangleparam minX="-12" maxX="147" minY="-12" maxY="108" />
-<infoparam name="Primitive port" value="Channel nonce_ch" />
+<infoparam name="Primitive port" value="Channel comm" />
 <new d="false" />
 <TGConnectingPoint num="0" id="358" />
 <extraparam>
-<Prop commName="nonce_ch" commType="0" origin="true" finite="false" blocking="false" maxSamples="8" widthSamples="4" isLossy="false" isPrex="false" isPostex="false" lossPercentage="0" maxNbOfLoss="0" dataFlowType="int16_t" associatedEvent="" checkConf="false" checkConfStatus="0" checkAuth="false" checkWeakAuthStatus="0" checkStrongAuthStatus="0" vc="0" />
+<Prop commName="comm" commType="0" origin="false" finite="false" blocking="false" maxSamples="8" widthSamples="4" isLossy="false" isPrex="false" isPostex="false" lossPercentage="0" maxNbOfLoss="0" dataFlowType="null" associatedEvent="" checkConf="false" checkConfStatus="0" checkAuth="true" checkWeakAuthStatus="0" checkStrongAuthStatus="0" vc="0" />
 <Type type="0" typeOther="" />
 <Type type="0" typeOther="" />
 <Type type="0" typeOther="" />
@@ -951,17 +951,17 @@
 <Data isAttacker="No" daemon="false" periodic="false" periodValue="" unit="" Operation="" />
 </extraparam>
 </COMPONENT>
-<SUBCOMPONENT type="1203" id="370" index="6" uid="4b3c553b-8c45-4729-8a22-92683c78ab19" >
+<SUBCOMPONENT type="1203" id="370" index="6" uid="3a57071f-7f06-425a-a0a5-d6c487afbd10" >
 <father id="381" num="0" />
-<cdparam x="565" y="263" />
+<cdparam x="565" y="306" />
 <sizeparam width="25" height="25" minWidth="1" minHeight="1" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <hidden value="false" />
 <cdrectangleparam minX="-12" maxX="147" minY="-12" maxY="107" />
-<infoparam name="Primitive port" value="Channel comm" />
+<infoparam name="Primitive port" value="Channel nonce_ch" />
 <new d="false" />
 <TGConnectingPoint num="0" id="369" />
 <extraparam>
-<Prop commName="comm" commType="0" origin="true" finite="false" blocking="false" maxSamples="8" widthSamples="4" isLossy="false" isPrex="false" isPostex="false" lossPercentage="0" maxNbOfLoss="0" dataFlowType="int16_t" associatedEvent="" checkConf="true" checkConfStatus="1" checkAuth="false" checkWeakAuthStatus="0" checkStrongAuthStatus="0" vc="0" />
+<Prop commName="nonce_ch" commType="0" origin="false" finite="false" blocking="false" maxSamples="8" widthSamples="4" isLossy="false" isPrex="false" isPostex="false" lossPercentage="0" maxNbOfLoss="0" dataFlowType="int16_t" associatedEvent="" checkConf="false" checkConfStatus="0" checkAuth="false" checkWeakAuthStatus="0" checkStrongAuthStatus="0" vc="0" />
 <Type type="0" typeOther="" />
 <Type type="0" typeOther="" />
 <Type type="0" typeOther="" />
@@ -969,17 +969,17 @@
 <Type type="0" typeOther="" />
 </extraparam>
 </SUBCOMPONENT>
-<SUBCOMPONENT type="1203" id="372" index="7" uid="3a57071f-7f06-425a-a0a5-d6c487afbd10" >
+<SUBCOMPONENT type="1203" id="372" index="7" uid="4b3c553b-8c45-4729-8a22-92683c78ab19" >
 <father id="381" num="1" />
-<cdparam x="565" y="306" />
+<cdparam x="565" y="263" />
 <sizeparam width="25" height="25" minWidth="1" minHeight="1" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <hidden value="false" />
 <cdrectangleparam minX="-12" maxX="147" minY="-12" maxY="107" />
-<infoparam name="Primitive port" value="Channel nonce_ch" />
+<infoparam name="Primitive port" value="Channel comm" />
 <new d="false" />
 <TGConnectingPoint num="0" id="371" />
 <extraparam>
-<Prop commName="nonce_ch" commType="0" origin="false" finite="false" blocking="false" maxSamples="8" widthSamples="4" isLossy="false" isPrex="false" isPostex="false" lossPercentage="0" maxNbOfLoss="0" dataFlowType="int16_t" associatedEvent="" checkConf="false" checkConfStatus="0" checkAuth="false" checkWeakAuthStatus="0" checkStrongAuthStatus="0" vc="0" />
+<Prop commName="comm" commType="0" origin="true" finite="false" blocking="false" maxSamples="8" widthSamples="4" isLossy="false" isPrex="false" isPostex="false" lossPercentage="0" maxNbOfLoss="0" dataFlowType="int16_t" associatedEvent="" checkConf="true" checkConfStatus="1" checkAuth="false" checkWeakAuthStatus="0" checkStrongAuthStatus="0" vc="0" />
 <Type type="0" typeOther="" />
 <Type type="0" typeOther="" />
 <Type type="0" typeOther="" />
@@ -1407,13 +1407,13 @@
 <attributes byteDataSize="4"  memorySize="1024"  clockRatio="1"  bufferType="0" />
 </extraparam>
 </COMPONENT>
-<SUBCOMPONENT type="1103" id="483" index="19" uid="758629cb-5cec-476e-9490-89ba05e4cf75" >
+<SUBCOMPONENT type="1103" id="483" index="19" uid="aa788f80-fc04-45c8-b0ec-86a79bb52b6c" >
 <father id="517" num="0" />
-<cdparam x="468" y="418" />
-<sizeparam width="130" height="40" minWidth="75" minHeight="1" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
+<cdparam x="480" y="371" />
+<sizeparam width="112" height="40" minWidth="75" minHeight="1" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <hidden value="false" />
-<cdrectangleparam minX="0" maxX="40" minY="0" maxY="95" />
-<infoparam name="TGComponent" value="nonce::nonce_ch" />
+<cdrectangleparam minX="0" maxX="58" minY="0" maxY="95" />
+<infoparam name="TGComponent" value="nonce::comm" />
 <new d="false" />
 <TGConnectingPoint num="0" id="475" />
 <TGConnectingPoint num="1" id="476" />
@@ -1424,16 +1424,16 @@
 <TGConnectingPoint num="6" id="481" />
 <TGConnectingPoint num="7" id="482" />
 <extraparam>
-<info value="nonce::nonce_ch" communicationName="nonce_ch" referenceCommunicationName="nonce" mappedElements="ExternalBus BusAlice BusBob" priority="0" typeName="channel" />
+<info value="nonce::comm" communicationName="comm" referenceCommunicationName="nonce" mappedElements="ExternalBus BusAlice BusBob" priority="0" typeName="channel" />
 </extraparam>
 </SUBCOMPONENT>
-<SUBCOMPONENT type="1103" id="492" index="20" uid="aa788f80-fc04-45c8-b0ec-86a79bb52b6c" >
+<SUBCOMPONENT type="1103" id="492" index="20" uid="758629cb-5cec-476e-9490-89ba05e4cf75" >
 <father id="517" num="1" />
-<cdparam x="480" y="371" />
-<sizeparam width="112" height="40" minWidth="75" minHeight="1" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
+<cdparam x="468" y="418" />
+<sizeparam width="130" height="40" minWidth="75" minHeight="1" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <hidden value="false" />
-<cdrectangleparam minX="0" maxX="58" minY="0" maxY="95" />
-<infoparam name="TGComponent" value="nonce::comm" />
+<cdrectangleparam minX="0" maxX="40" minY="0" maxY="95" />
+<infoparam name="TGComponent" value="nonce::nonce_ch" />
 <new d="false" />
 <TGConnectingPoint num="0" id="484" />
 <TGConnectingPoint num="1" id="485" />
@@ -1444,7 +1444,7 @@
 <TGConnectingPoint num="6" id="490" />
 <TGConnectingPoint num="7" id="491" />
 <extraparam>
-<info value="nonce::comm" communicationName="comm" referenceCommunicationName="nonce" mappedElements="ExternalBus BusAlice BusBob" priority="0" typeName="channel" />
+<info value="nonce::nonce_ch" communicationName="nonce_ch" referenceCommunicationName="nonce" mappedElements="ExternalBus BusAlice BusBob" priority="0" typeName="channel" />
 </extraparam>
 </SUBCOMPONENT>
 
@@ -1853,8 +1853,8 @@
 <cdparam x="404" y="206" />
 <sizeparam width="0" height="0" minWidth="0" minHeight="0" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <infoparam name="connector" value="Connector between ports" />
-<P1  x="391" y="193" id="745" />
-<P2  x="493" y="193" id="734" />
+<P1  x="404" y="206" id="745" />
+<P2  x="480" y="206" id="734" />
 <AutomaticDrawing  data="true" />
 <new d="false" />
 </CONNECTOR>
@@ -3657,8 +3657,8 @@
 <cdparam x="255" y="121" />
 <sizeparam width="0" height="0" minWidth="0" minHeight="0" maxWidth="2000" maxHeight="2000" minDesiredWidth="0" minDesiredHeight="0" />
 <infoparam name="connector" value="Connector between ports" />
-<P1  x="275" y="136" id="1467" />
-<P2  x="329" y="136" id="1456" />
+<P1  x="262" y="123" id="1467" />
+<P2  x="342" y="123" id="1456" />
 <AutomaticDrawing  data="true" />
 <new d="false" />
 </CONNECTOR>
diff --git a/src/main/java/avatartranslator/AvatarSpecification.java b/src/main/java/avatartranslator/AvatarSpecification.java
index 81ec04d553..632f52c641 100644
--- a/src/main/java/avatartranslator/AvatarSpecification.java
+++ b/src/main/java/avatartranslator/AvatarSpecification.java
@@ -40,6 +40,9 @@ package avatartranslator;
 
 import myutil.NameChecker;
 import myutil.TraceManager;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import ui.TAttribute;
 
 import java.util.*;
 
@@ -1044,6 +1047,475 @@ public class AvatarSpecification extends AvatarElement {
         return lne;
     }
 
+    /*
+    * Typical JSON:
+    *
+    * AI:
+{
+"blocks": [
+{
+"name": "Battery",
+"attributes": [
+{
+ "name": "chargeLevel",
+ "type": "int"
+},
+{
+ "name": "capacity",
+ "type": "int"
+}
+],
+"methods": [
+{
+ "name": "loadBattery",
+ "parameters": [
+  {
+   "name": "rechargeTime",
+   "type": "int"
+  }
+ ],
+ "returnType": "nothing"
+ },
+ {
+ "name": "unloadBattery",
+ "parameters": [
+  {
+  "name": "dischargeThreshold",
+  "type": "int"
+  }
+ ],
+ "returnType": "nothing"
+ }
+],
+"signals": [
+{
+ "name": "batteryLoaded",
+ "parameters": [
+  {
+   "name": "loadAmount",
+   "type": "int"
+  }
+ ],
+ "type": "output"
+},
+{
+ "name": "batteryUnloaded",
+ "parameters": [
+  {
+   "name": "unloadAmount",
+   "type": "int"
+  }
+ ],
+ "type": "output"
+}
+]
+},
+{
+"name": "MainEngine",
+"attributes": [
+{
+ "name": "isTurnedOn",
+ "type": "boolean"
+},
+{
+ "name": "currentRpm",
+ "type": "int"
+}
+],
+"methods": [
+{
+ "name": "toggleEngine",
+ "parameters": [
+ {
+  "name": "turnOn",
+  "type": "boolean"
+ }
+],
+ "returnType": "nothing"
+},
+{
+ "name": "setRpm",
+ "parameters": [
+  {
+   "name": "rpm",
+   "type": "int"
+ }
+],
+ "returnType": "nothing"
+}
+],
+"signals": [
+{
+ "name": "engineToggled",
+ "parameters": [
+  {
+   "name": "turnOn",
+   "type": "boolean"
+  }
+ ],
+ "type": "output"
+},
+{
+ "name": "rpmSet",
+ "parameters": [
+  {
+   "name": "rpm",
+   "type": "int"
+  }
+ ],
+ "type": "output"
+}
+]
+},
+{
+"name": "ElectricEngines",
+"attributes": [
+{
+ "name": "hasPower",
+ "type": "boolean"
+},
+{
+ "name": "powerLevel",
+ "type": "int"
+}
+],
+"methods": [
+{
+ "name": "togglePower",
+ "parameters": [
+  {
+   "name": "hasPower",
+   "type": "boolean"
+  }
+ ],
+ "returnType": "nothing"
+},
+{
+ "name": "setPowerLevel",
+ "parameters": [
+ {
+  "name": "powerLevel",
+  "type": "int"
+ }
+],
+ "returnType": "nothing"
+}
+],
+"signals": [
+{
+ "name": "powerToggled",
+ "parameters": [
+  {
+   "name": "hasPower",
+   "type": "boolean"
+  }
+ ],
+ "type": "output"
+},
+{
+ "name": "powerLevelSet",
+ "parameters": [
+  {
+   "name": "powerLevel",
+   "type": "int"
+  }
+ ],
+ "type": "output"
+}
+]
+},
+{
+"name": "SystemController",
+"attributes": [
+{
+ "name": "batteryRechargeTime",
+ "type": "int"
+},
+{
+ "name": "dischargeThreshold",
+ "type": "int"
+}
+],
+"methods": [
+{
+ "name": "combineEngines",
+ "parameters": [
+  {
+   "name": "needMorePower",
+   "type": "boolean"
+  }
+ ],
+ "returnType": "nothing"
+},
+{
+ "name": "toggleMainEngine",
+ "parameters": [
+  {
+   "name": "turnOn",
+   "type": "boolean"
+  }
+ ],
+ "returnType": "nothing"
+},
+{
+ "name": "stopVehicle",
+ "parameters": [],
+ "returnType": "nothing"
+}
+],
+"signals": [
+{
+ "name": "enginesCombined",
+ "parameters": [
+  {
+   "name": "needMorePower",
+   "type": "boolean"
+  }
+ ],
+ "type": "output"
+},
+{
+ "name": "mainEngineToggled",
+ "parameters": [
+  {
+   "name": "turnOn",
+   "type": "boolean"
+  }
+ ],
+ "type": "output"
+},
+{
+ "name": "vehicleStopped",
+ "parameters": [],
+ "type": "output"
+},
+{
+ "name": "rechargeBattery",
+ "parameters": [
+  {
+   "name": "rechargeTime",
+   "type": "int"
+  }
+ ],
+ "type": "input",
+ "communicationType": "synchronous"
+},
+{
+ "name": "unloadBattery",
+ "parameters": [
+  {
+   "name": "dischargeThreshold",
+   "type": "int"
+  }
+ ],
+ "type": "input",
+ "communicationType": "synchronous"
+},
+{
+ "name": "setMainEngineRpm",
+ "parameters": [
+  {
+   "name": "rpm",
+   "type": "int"
+  }
+ ],
+ "type": "input",
+ "communicationType": "synchronous"
+},
+{
+ "name": "toggleElectricEnginesPower",
+ "parameters": [
+ {
+  "name": "hasPower",
+  "type": "boolean"
+  }
+ ],
+ "type": "input",
+ "communicationType": "asynchronous"
+},
+{
+ "name": "setElectricEnginesPowerLevel",
+ "parameters": [
+  {
+   "name": "powerLevel",
+   "type": "int"
+  }
+ ],
+ "type": "input",
+ "communicationType": "asynchronous"
+}
+]
+}
+],
+"connections": [
+{
+ "sourceBlock": "Battery",
+ "sourceSignal": "batteryLoaded",
+ "destinationBlock": "SystemController",
+ "destinationSignal": "rechargeBattery",
+ "communicationType": "synchronous"
+},
+{
+ "sourceBlock": "Battery",
+ "sourceSignal": "batteryUnloaded",
+ "destinationBlock": "SystemController",
+ "destinationSignal": "unloadBattery",
+ "communicationType": "synchronous"
+},
+{
+ "sourceBlock": "MainEngine",
+ "sourceSignal": "engineToggled",
+ "destinationBlock": "SystemController",
+ "destinationSignal": "mainEngineToggled",
+ "communicationType": "synchronous"
+},
+{
+ "sourceBlock": "MainEngine",
+ "sourceSignal": "rpmSet",
+ "destinationBlock": "SystemController",
+ "destinationSignal": "setMainEngineRpm",
+ "communicationType": "synchronous"
+},
+{
+ "sourceBlock": "ElectricEngines",
+ "sourceSignal": "powerToggled",
+ "destinationBlock": "SystemController",
+ "destinationSignal": "toggleElectricEnginesPower",
+ "communicationType": "asynchronous"
+},
+{
+ "sourceBlock": "ElectricEngines",
+ "sourceSignal": "powerLevelSet",
+ "destinationBlock": "SystemController",
+ "destinationSignal": "setElectricEnginesPowerLevel",
+ "communicationType": "asynchronous"
+},
+{
+ "sourceBlock": "SystemController",
+ "sourceSignal": "rechargeBattery",
+ "destinationBlock": "Battery",
+ "destinationSignal": "loadBattery",
+ "communicationType": "synchronous"
+},
+{
+ "sourceBlock": "SystemController",
+ "sourceSignal": "unloadBattery",
+ "destinationBlock": "Battery",
+ "destinationSignal": "unloadBattery",
+ "communicationType": "synchronous"
+},
+{
+ "sourceBlock": "SystemController",
+ "destinationBlock": "MainEngine",
+ "destinationSignal": "engineToggled",
+ "sourceSignal": "mainEngineToggled",
+ "communicationType": "synchronous"
+},
+{
+ "sourceBlock": "SystemController",
+ "destinationBlock": "MainEngine",
+ "destinationSignal": "rpmSet",
+ "sourceSignal": "setMainEngineRpm",
+ "communicationType": "synchronous"
+},
+{
+ "sourceBlock": "SystemController",
+ "destinationBlock": "ElectricEngines",
+ "destinationSignal": "powerToggled",
+ "sourceSignal": "toggleElectricEnginesPower",
+ "communicationType": "asynchronous"
+},
+{
+ "sourceBlock": "SystemController",
+ "destinationBlock": "ElectricEngines",
+ "destinationSignal": "powerLevelSet",
+ "sourceSignal": "setElectricEnginesPowerLevel",
+ "communicationType": "asynchronous"
+},
+{
+ "sourceBlock": "SystemController",
+ "destinationBlock": "ElectricEngines",
+ "destinationSignal": "powerToggled",
+ "sourceSignal": "enginesCombined",
+ "communicationType": "asynchronous"
+}
+]
+}
+    *
+    *
+    *
+    *
+    *
+    *
+    *
+    *
+    *
+    *
+     */
+    public static AvatarSpecification fromJSON(String _spec, String _name, Object _referenceObject) {
+        AvatarSpecification spec = new AvatarSpecification(_name, _referenceObject);
+
+        int indexStart = _spec.indexOf('{');
+        int indexStop = _spec.lastIndexOf('}');
+
+        if ((indexStart == -1) || (indexStop == -1) || (indexStart > indexStop)) {
+            throw new org.json.JSONException("Invalid JSON object");
+        }
+
+        _spec = _spec.substring(indexStart, indexStop+1);
+
+        TraceManager.addDev("Cut spec: " + _spec);
 
+        JSONObject mainObject = new JSONObject(_spec);
+
+        JSONArray blocksA = mainObject.getJSONArray("blocks");
+
+        if (blocksA == null) {
+            TraceManager.addDev("No blocks in json");
+            return spec;
+        }
+
+        for (int i=0; i<blocksA.length(); i++) {
+            JSONObject blockO = blocksA.getJSONObject(i);
+            String name = blockO.getString("name");
+            if (name != null) {
+                AvatarBlock newBlock = new AvatarBlock(name, spec, _referenceObject);
+                spec.addBlock(newBlock);
+
+                JSONArray attributesA = blockO.getJSONArray("attributes");
+
+                for (int j=0; j<attributesA.length(); j++) {
+                    String nameA = attributesA.getJSONObject(j).getString("name");
+                    String typeA = attributesA.getJSONObject(j).getString("type");
+                    AvatarAttribute aa = new AvatarAttribute(nameA, AvatarType.getType(typeA), newBlock, _referenceObject);
+                    newBlock.addAttribute(aa);
+                }
+
+                JSONArray methodsA = blockO.getJSONArray("methods");
+                for (int j=0; j<methodsA.length(); j++) {
+                    String nameM = methodsA.getJSONObject(j).getString("name");
+                    AvatarMethod am = new AvatarMethod(nameM, _referenceObject);
+                    JSONArray params = methodsA.getJSONObject(j).getJSONArray("paremeters");
+                    for(int k=0; k<params.length(); k++) {
+                        String nameA = params.getJSONObject(k).getString("name");
+                        String typeA = params.getJSONObject(k).getString("type");
+                        AvatarAttribute aa = new AvatarAttribute(nameA, AvatarType.getType(typeA), newBlock, _referenceObject);
+                        am.addParameter(aa);
+                    }
+                    String returnT = methodsA.getJSONObject(j).getString("returnType");
+                    AvatarType at = AvatarType.getType(returnT);
+                    if (at != AvatarType.UNDEFINED) {
+                        am.addReturnParameter(new AvatarAttribute("returnType", at, newBlock, _referenceObject));
+                    }
+
+                    newBlock.addMethod(am);
+                }
+
+            }
+        }
+
+        return spec;
+
+    }
 
 }
diff --git a/src/main/java/avatartranslator/AvatarType.java b/src/main/java/avatartranslator/AvatarType.java
index 6c3c09afb0..0810e18eab 100644
--- a/src/main/java/avatartranslator/AvatarType.java
+++ b/src/main/java/avatartranslator/AvatarType.java
@@ -66,7 +66,7 @@ public enum AvatarType {
         this.name = name;
         this.defaultValue = defaultValue;
         this.defaultValueTF = defaultValueTF;
-	this.defaultValueInInt = defaultValueInInt;
+	    this.defaultValueInInt = defaultValueInInt;
     }
 
     public static AvatarType getType (String s) {
diff --git a/src/main/java/ui/JToolBarMainTurtle.java b/src/main/java/ui/JToolBarMainTurtle.java
index 92dcb2b9ed..8ca91acc97 100644
--- a/src/main/java/ui/JToolBarMainTurtle.java
+++ b/src/main/java/ui/JToolBarMainTurtle.java
@@ -56,7 +56,6 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
-import java.util.concurrent.CancellationException;
 
 
 /**
@@ -85,7 +84,7 @@ public  class JToolBarMainTurtle extends JToolBar implements ActionListener
     private JMenuItem oneClickrtlotosMI, onclicklotosMI, gensystemcMI, simusystemcMI, gentmlMI, genCMI, genjavaMI, ncMI;
 
     // IA
-    private JButton ia;
+    private JButton ai;
 
     // Main button
     private JButton menuButton;
@@ -356,8 +355,8 @@ public  class JToolBarMainTurtle extends JToolBar implements ActionListener
 
         addSeparator();
 
-        ia = add(mgui.actions[TGUIAction.ACT_IA]);
-        ia.addMouseListener(mgui.mouseHandler);
+        ai = add(mgui.actions[TGUIAction.ACT_IA]);
+        ai.addMouseListener(mgui.mouseHandler);
 
         addSeparator();
 
@@ -399,7 +398,10 @@ public  class JToolBarMainTurtle extends JToolBar implements ActionListener
 
     public void showAvatarActions(boolean b) {
 
+        ai.setVisible(true);
+
         //TraceManager.addDev("Show avatar options with b = " + b);
+
         genMapping.setVisible(!b);
         genMappingMI.setVisible(!b);
         dse.setVisible(!b);
@@ -556,6 +558,8 @@ public  class JToolBarMainTurtle extends JToolBar implements ActionListener
 
         //TraceManager.addDev("Show diplodocus options with b = " + b);
 
+        ai.setVisible(true);
+
         genMapping.setVisible(b);
         dse.setVisible(b);
 
@@ -668,9 +672,6 @@ public  class JToolBarMainTurtle extends JToolBar implements ActionListener
             ncMI.setVisible(!b);
         }
 
-        if (ia != null) {
-            ia.setVisible(!b);
-        }
 
         if (gendesign != null) {
             gendesign.setVisible(!b);
diff --git a/src/main/java/ui/MainGUI.java b/src/main/java/ui/MainGUI.java
index 6cc9d5c69e..64dff6d38c 100644
--- a/src/main/java/ui/MainGUI.java
+++ b/src/main/java/ui/MainGUI.java
@@ -3089,6 +3089,7 @@ public class MainGUI implements ActionListener, WindowListener, KeyListener, Per
 
         if (dir != null)
             loadAUTGraphsDir();
+
     }
 
     public void saveAsLibrary(String data) {
diff --git a/src/main/java/ui/ModeManager.java b/src/main/java/ui/ModeManager.java
index 86c1c187b9..d7d6df2f5c 100644
--- a/src/main/java/ui/ModeManager.java
+++ b/src/main/java/ui/ModeManager.java
@@ -98,7 +98,7 @@ public class ModeManager {
             if (mainBar != null) {
                 mainBar.activateSearch(false);
             }
-            actions[TGUIAction.ACT_IA].setEnabled(false);
+            actions[TGUIAction.ACT_IA].setEnabled(true);
             break;
         case MainGUI.OPENED:
             actions[TGUIAction.ACT_MERGE].setEnabled(true);
diff --git a/src/main/java/ui/TGComponent.java b/src/main/java/ui/TGComponent.java
index 4fadb8e32a..edd492b17a 100644
--- a/src/main/java/ui/TGComponent.java
+++ b/src/main/java/ui/TGComponent.java
@@ -2290,7 +2290,7 @@ public abstract class TGComponent extends AbstractCDElement implements /*CDEleme
         double d1, d2;
         int i;
         int ref = 0;
-        //compare currentcloser to my points.
+        //compare current closer to my points.
         for (i = 0; i < nbConnectingPoint; i++) {
             if (connectingPoint[i] instanceof TGConnectingPointComment) {
                 continue;
diff --git a/src/main/java/ui/avatarbd/AvatarBDPanel.java b/src/main/java/ui/avatarbd/AvatarBDPanel.java
index f7a3a5d4b0..6439d3281e 100644
--- a/src/main/java/ui/avatarbd/AvatarBDPanel.java
+++ b/src/main/java/ui/avatarbd/AvatarBDPanel.java
@@ -38,8 +38,10 @@
 
 package ui.avatarbd;
 
+import avatartranslator.AvatarSpecification;
 import myutil.Conversion;
 import myutil.TraceManager;
+import org.json.JSONArray;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -57,8 +59,8 @@ import java.util.*;
    * @author Ludovic APVRILLE
  */
 public class AvatarBDPanel extends TDiagramPanel {
-    
-	private List<AvatarBDStateMachineOwner> validated, ignored;
+
+    private List<AvatarBDStateMachineOwner> validated, ignored;
     private String val = null, ign = null;
     private boolean optimized = true;
     private boolean considerTimingOperators = true;
@@ -68,7 +70,7 @@ public class AvatarBDPanel extends TDiagramPanel {
     private String mainCode;
 
 
-    public  AvatarBDPanel(MainGUI mgui, TToolBar _ttb) {
+    public AvatarBDPanel(MainGUI mgui, TToolBar _ttb) {
         super(mgui, _ttb);
         mainCode = DEFAULT_MAIN;
         /*TDiagramMouseManager tdmm = new TDiagramMouseManager(this);
@@ -80,9 +82,9 @@ public class AvatarBDPanel extends TDiagramPanel {
     public boolean actionOnDoubleClick(TGComponent tgc) {
         //
         if (tgc instanceof AvatarBDBlock) {
-            AvatarBDBlock b = (AvatarBDBlock)tgc;
+            AvatarBDBlock b = (AvatarBDBlock) tgc;
             //
-            return this.changeStateMachineTabName (b.oldValue, b.getValue());
+            return this.changeStateMachineTabName(b.oldValue, b.getValue());
         } else if (tgc instanceof AvatarBDLibraryFunction) {
             return true;
         } else if (tgc instanceof AvatarBDDataType) {
@@ -96,15 +98,15 @@ public class AvatarBDPanel extends TDiagramPanel {
     @Override
     public boolean actionOnAdd(TGComponent tgc) {
         if (tgc instanceof AvatarBDBlock) {
-            AvatarBDBlock abdb = (AvatarBDBlock)(tgc);
+            AvatarBDBlock abdb = (AvatarBDBlock) (tgc);
             //TraceManager.addDev(" *** add Avatar block *** name=" + abdb.getBlockName());
             ((AvatarDesignPanel) this.tp).addAvatarStateMachineDiagramPanel(abdb.getBlockName());
-            this.mgui.setPanelMode ();
+            this.mgui.setPanelMode();
             return true;
         } else if (tgc instanceof AvatarBDLibraryFunction) {
             //TraceManager.addDev(" *** add Avatar function *** name=" + ((AvatarBDLibraryFunction) tgc).getFunctionName ());
-            ((AvatarDesignPanel) this.tp).addAvatarStateMachineDiagramPanel(((AvatarBDLibraryFunction) tgc).getFunctionName ());
-            this.mgui.setPanelMode ();
+            ((AvatarDesignPanel) this.tp).addAvatarStateMachineDiagramPanel(((AvatarBDLibraryFunction) tgc).getFunctionName());
+            this.mgui.setPanelMode();
             return true;
         }
         return false;
@@ -114,14 +116,14 @@ public class AvatarBDPanel extends TDiagramPanel {
     public boolean actionOnRemove(TGComponent tgc) {
         if (tgc instanceof AvatarBDStateMachineOwner) {
             AvatarBDStateMachineOwner abdb = (AvatarBDStateMachineOwner) tgc;
-            this.mgui.removeAvatarBlock (tp, abdb.getOwnerName());
+            this.mgui.removeAvatarBlock(tp, abdb.getOwnerName());
             if (tgc instanceof AvatarBDBlock) {
-                List<AvatarBDBlock> list  = ((AvatarBDBlock) abdb).getFullBlockList();
-                for(AvatarBDBlock b: list)
+                List<AvatarBDBlock> list = ((AvatarBDBlock) abdb).getFullBlockList();
+                for (AvatarBDBlock b : list)
                     mgui.removeAvatarBlock(tp, b.getBlockName());
 
-                for(AvatarBDLibraryFunction b: ((AvatarBDBlock) abdb).getFullLibraryFunctionList())
-                    mgui.removeAvatarBlock (tp, b.getFunctionName());
+                for (AvatarBDLibraryFunction b : ((AvatarBDBlock) abdb).getFullLibraryFunctionList())
+                    mgui.removeAvatarBlock(tp, b.getFunctionName());
             }
 
             return true;
@@ -133,43 +135,43 @@ public class AvatarBDPanel extends TDiagramPanel {
     public void updateSignalAttachement(AvatarBDBlock _b, AvatarBDPortConnector _conn) {
         //TraceManager.addDev("Update signal attachment for block " + _b.getBlockName());
 
-    	// Set all signals of B as non connected
-    	_b.setSignalsAsNonAttached();
-
-    	Iterator<TGComponent> iterator = getComponentList().listIterator();
-
-    	while(iterator.hasNext()) {
-    		TGComponent tgc = iterator.next();
-    		
-    		if (tgc instanceof AvatarBDPortConnector) {
-    			AvatarBDPortConnector port = (AvatarBDPortConnector)tgc;
-    			AvatarBDBlock block1 = port.getAvatarBDBlock1();
-    			AvatarBDBlock block2 = port.getAvatarBDBlock2();
-    			
-    			if ((block1 != null) && (block2 != null)) {
-    				if ((_b == block1) || (_b == block2)) {
-    					//TraceManager.addDev("Relation found");
-    					List<String> l;
-    					
-    					if (_b == block1) {
-    						l = port.getListOfSignalsOrigin();
-    					} else {
-    						l = port.getListOfSignalsDestination();
-    					}
-
-    					for(String name: l) {
-    						name = AvatarSignal.getSignalNameFromFullSignalString(name);
-    						//TraceManager.addDev("Searching for " + name + " in block " + _b);
-    						AvatarSignal sig = _b.getAvatarSignalFromName(name);
-    						if (sig != null) {
-    							//TraceManager.addDev("Sig " + name + " is attached to a relation");
-    							sig.attachedToARelation = true;
-    						}
-    					}
-    				}
-    			}
-    		}
-    	}
+        // Set all signals of B as non connected
+        _b.setSignalsAsNonAttached();
+
+        Iterator<TGComponent> iterator = getComponentList().listIterator();
+
+        while (iterator.hasNext()) {
+            TGComponent tgc = iterator.next();
+
+            if (tgc instanceof AvatarBDPortConnector) {
+                AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
+                AvatarBDBlock block1 = port.getAvatarBDBlock1();
+                AvatarBDBlock block2 = port.getAvatarBDBlock2();
+
+                if ((block1 != null) && (block2 != null)) {
+                    if ((_b == block1) || (_b == block2)) {
+                        //TraceManager.addDev("Relation found");
+                        List<String> l;
+
+                        if (_b == block1) {
+                            l = port.getListOfSignalsOrigin();
+                        } else {
+                            l = port.getListOfSignalsDestination();
+                        }
+
+                        for (String name : l) {
+                            name = AvatarSignal.getSignalNameFromFullSignalString(name);
+                            //TraceManager.addDev("Searching for " + name + " in block " + _b);
+                            AvatarSignal sig = _b.getAvatarSignalFromName(name);
+                            if (sig != null) {
+                                //TraceManager.addDev("Sig " + name + " is attached to a relation");
+                                sig.attachedToARelation = true;
+                            }
+                        }
+                    }
+                }
+            }
+        }
     }
 
     @Override
@@ -179,8 +181,8 @@ public class AvatarBDPanel extends TDiagramPanel {
             //updateAllSignalsOnConnectors();
             return actionOnDoubleClick(tgc);
         } else if (tgc instanceof AvatarBDLibraryFunction) {
-            TraceManager.addDev("Renaming tab from " + ((AvatarBDLibraryFunction)tgc).getOldValue() + " to " + tgc.getValue());
-            changeStateMachineTabName( ((AvatarBDLibraryFunction)tgc).getOldValue(), tgc.getValue());
+            TraceManager.addDev("Renaming tab from " + ((AvatarBDLibraryFunction) tgc).getOldValue() + " to " + tgc.getValue());
+            changeStateMachineTabName(((AvatarBDLibraryFunction) tgc).getOldValue(), tgc.getValue());
             return actionOnDoubleClick(tgc);
         }
         return false;
@@ -195,39 +197,39 @@ public class AvatarBDPanel extends TDiagramPanel {
         return false;
 	}*/
 
-    
+
     @Override
     public boolean areAttributesVisible() {
         return attributesVisible;
     }
-    
+
     @Override
     public String getXMLHead() {
-        return "<AVATARBlockDiagramPanel name=\"" + name + "\"" + sizeParam()  + zoomParam() + " >\n" + displayParam();
+        return "<AVATARBlockDiagramPanel name=\"" + name + "\"" + sizeParam() + zoomParam() + " >\n" + displayParam();
     }
-    
+
     @Override
     public String getXMLTail() {
         return "</AVATARBlockDiagramPanel>";
     }
-    
+
     @Override
     public String getXMLSelectedHead() {
         return "<AVATARBlockDiagramPanelCopy name=\"" + name + "\" xSel=\"" + xSel + "\" ySel=\"" + ySel + "\" widthSel=\"" + widthSel + "\" heightSel=\"" + heightSel +
-                "\"" + zoomParam()  + ">";
+                "\"" + zoomParam() + ">";
     }
-    
+
     @Override
     public String getXMLSelectedTail() {
         return "</AVATARBlockDiagramPanelCopy>";
     }
-    
+
     @Override
     public String getXMLCloneHead() {
         return "<AVATARBlockDiagramPanelCopy name=\"" + name + "\" xSel=\"" + 0 + "\" ySel=\"" + 0 + "\" widthSel=\"" + 0 + "\" heightSel=\"" + 0 +
-                "\"" + zoomParam()  + ">";
+                "\"" + zoomParam() + ">";
     }
-    
+
     @Override
     public String getXMLCloneTail() {
         return "</AVATARBlockDiagramPanelCopy>";
@@ -236,12 +238,12 @@ public class AvatarBDPanel extends TDiagramPanel {
     public void setConnectorsToFront() {
         ArrayList<TGComponent> list = new ArrayList<TGComponent>();
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (!(tgc instanceof TGConnector))
                 list.add(tgc);
 
         //
-        for(TGComponent tgc1: list) {
+        for (TGComponent tgc1 : list) {
             //
             componentList.remove(tgc1);
             componentList.add(tgc1);
@@ -268,11 +270,11 @@ public class AvatarBDPanel extends TDiagramPanel {
 
     public String displayParam() {
         String s = "";
-        String [] tmp =  Conversion.wrapText(mainCode);
+        String[] tmp = Conversion.wrapText(mainCode);
         //String tmps;
         int i;
 
-        for(i=0; i<tmp.length; i++) {
+        for (i = 0; i < tmp.length; i++) {
             s += "<MainCode value=\"" + GTURTLEModeling.transformString(tmp[i]) + "\"/>\n";
         }
 
@@ -291,8 +293,8 @@ public class AvatarBDPanel extends TDiagramPanel {
         if (validated == null) {
             s += "<Validated value=\"\" />\n";
         } else {
-            s+= "<Validated value=\"";
-            for (AvatarBDStateMachineOwner o: validated)
+            s += "<Validated value=\"";
+            for (AvatarBDStateMachineOwner o : validated)
                 s += o.getOwnerName() + ";";
             s += "\" />\n";
         }
@@ -300,8 +302,8 @@ public class AvatarBDPanel extends TDiagramPanel {
         if (ignored == null) {
             s += "<Ignored value=\"\" />\n";
         } else {
-            s+= "<Ignored value=\"";
-            for (AvatarBDStateMachineOwner o: ignored)
+            s += "<Ignored value=\"";
+            for (AvatarBDStateMachineOwner o : ignored)
                 s += o.getOwnerName() + ";";
             s += "\" />\n";
         }
@@ -319,13 +321,13 @@ public class AvatarBDPanel extends TDiagramPanel {
         Node n;
 
         try {
-            if (nl.getLength()>0) {
+            if (nl.getLength() > 0) {
                 mainCode = "";
             }
-            for(int i=0; i<nl.getLength(); i++) {
+            for (int i = 0; i < nl.getLength(); i++) {
                 n = nl.item(i);
                 if (n.getNodeType() == Node.ELEMENT_NODE) {
-                    s = ((Element)n).getAttribute("value");
+                    s = ((Element) n).getAttribute("value");
                     //TraceManager.addDev("Found value=" + s);
                     if (s != null) {
                         mainCode += s + "\n";
@@ -342,10 +344,10 @@ public class AvatarBDPanel extends TDiagramPanel {
         // Optimized
         nl = elt.getElementsByTagName("Optimized");
         try {
-            if (nl.getLength()>0) {
+            if (nl.getLength() > 0) {
                 n = nl.item(0);
                 if (n.getNodeType() == Node.ELEMENT_NODE) {
-                    s = ((Element)n).getAttribute("value");
+                    s = ((Element) n).getAttribute("value");
                     //TraceManager.addDev("Found value=" + s);
                     if (s != null) {
                         optimized = s.compareTo("true") == 0;
@@ -362,10 +364,10 @@ public class AvatarBDPanel extends TDiagramPanel {
         // consider timing ops
         nl = elt.getElementsByTagName("considerTimingOperators");
         try {
-            if (nl.getLength()>0) {
+            if (nl.getLength() > 0) {
                 n = nl.item(0);
                 if (n.getNodeType() == Node.ELEMENT_NODE) {
-                    s = ((Element)n).getAttribute("value");
+                    s = ((Element) n).getAttribute("value");
                     //TraceManager.addDev("Found value=" + s);
                     if (s != null) {
                         considerTimingOperators = s.compareTo("true") == 0;
@@ -381,10 +383,10 @@ public class AvatarBDPanel extends TDiagramPanel {
         // Validated
         nl = elt.getElementsByTagName("Validated");
         try {
-            if (nl.getLength()>0) {
+            if (nl.getLength() > 0) {
                 n = nl.item(0);
                 if (n.getNodeType() == Node.ELEMENT_NODE) {
-                    s = ((Element)n).getAttribute("value");
+                    s = ((Element) n).getAttribute("value");
                     //TraceManager.addDev("Found value=" + s);
                     if (s != null) {
                         val = s;
@@ -400,10 +402,10 @@ public class AvatarBDPanel extends TDiagramPanel {
         // Ignored
         nl = elt.getElementsByTagName("Ignored");
         try {
-            if (nl.getLength()>0) {
+            if (nl.getLength() > 0) {
                 n = nl.item(0);
                 if (n.getNodeType() == Node.ELEMENT_NODE) {
-                    s = ((Element)n).getAttribute("value");
+                    s = ((Element) n).getAttribute("value");
                     //TraceManager.addDev("Found value=" + s);
                     if (s != null) {
                         ign = s;
@@ -426,12 +428,12 @@ public class AvatarBDPanel extends TDiagramPanel {
     public void connectSignals() {
         for (TGComponent tgc : this.getAllComponentList()) {
             if (tgc instanceof AvatarBDPortConnector) {
-                AvatarBDBlock block1 = ((AvatarBDPortConnector)tgc).getAvatarBDBlock1();
-                AvatarBDBlock block2 = ((AvatarBDPortConnector)tgc).getAvatarBDBlock2();
+                AvatarBDBlock block1 = ((AvatarBDPortConnector) tgc).getAvatarBDBlock1();
+                AvatarBDBlock block2 = ((AvatarBDPortConnector) tgc).getAvatarBDBlock2();
                 List<AvatarSignal> available1 = block1.getListOfAvailableSignals();
-                connectIfPossible((AvatarBDPortConnector)tgc, block1, available1, block2, true);
-                updateSignalAttachement(block1, (AvatarBDPortConnector)tgc);
-                updateSignalAttachement(block2, (AvatarBDPortConnector)tgc);
+                connectIfPossible((AvatarBDPortConnector) tgc, block1, available1, block2, true);
+                updateSignalAttachement(block1, (AvatarBDPortConnector) tgc);
+                updateSignalAttachement(block2, (AvatarBDPortConnector) tgc);
             }
         }
     }
@@ -439,26 +441,26 @@ public class AvatarBDPanel extends TDiagramPanel {
     public void updateInfoOnSignalAttachment() {
         for (TGComponent tgc : this.getAllComponentList()) {
             if (tgc instanceof AvatarBDPortConnector) {
-                AvatarBDBlock block1 = ((AvatarBDPortConnector)tgc).getAvatarBDBlock1();
-                AvatarBDBlock block2 = ((AvatarBDPortConnector)tgc).getAvatarBDBlock2();
+                AvatarBDBlock block1 = ((AvatarBDPortConnector) tgc).getAvatarBDBlock1();
+                AvatarBDBlock block2 = ((AvatarBDPortConnector) tgc).getAvatarBDBlock2();
                 List<AvatarSignal> available1 = block1.getListOfAvailableSignals();
                 //connectIfPossible((AvatarBDPortConnector)tgc, block1, available1, block2, true);
-                updateSignalAttachement(block1, (AvatarBDPortConnector)tgc);
-                updateSignalAttachement(block2, (AvatarBDPortConnector)tgc);
+                updateSignalAttachement(block1, (AvatarBDPortConnector) tgc);
+                updateSignalAttachement(block2, (AvatarBDPortConnector) tgc);
             }
         }
     }
 
     private void connectIfPossible(AvatarBDPortConnector port, AvatarBDBlock block, List<AvatarSignal> sigs, AvatarBDBlock otherBlock,
                                    boolean origin) {
-        for(AvatarSignal sig: sigs) {
+        for (AvatarSignal sig : sigs) {
             // Try to associate it with one of the signals
-            for(AvatarSignal pot: otherBlock.getListOfAvailableSignals()) {
+            for (AvatarSignal pot : otherBlock.getListOfAvailableSignals()) {
                 TraceManager.addDev("Testing block" + block.getBlockName() + " sig=" + sig.getId() +
-                        " with block " + otherBlock.getBlockName()  + " sig = " + pot.getId());
-                if ( sig.isCompatibleWith(pot) && (sig.getId().compareTo(pot.getId()) == 0) ) {
+                        " with block " + otherBlock.getBlockName() + " sig = " + pot.getId());
+                if (sig.isCompatibleWith(pot) && (sig.getId().compareTo(pot.getId()) == 0)) {
                     TraceManager.addDev("Added connection: block " + block.getBlockName() + " sig=" + sig.getId() +
-                            " with block " + otherBlock.getBlockName()  + " sig = " + pot.getId());
+                            " with block " + otherBlock.getBlockName() + " sig = " + pot.getId());
                     port.addSignal(sig.toString(), sig.getInOut() == AvatarSignal.IN, origin);
                     port.addSignal(pot.toString(), pot.getInOut() == AvatarSignal.IN, !origin);
 
@@ -469,15 +471,15 @@ public class AvatarBDPanel extends TDiagramPanel {
     }
 
     public void updateAllSignalsOnConnectors() {
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDPortConnector) {
-                AvatarBDPortConnector port = (AvatarBDPortConnector)tgc;
+                AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
                 port.updateAllSignals();
             }
     }
 
     public List<AvatarSignal> getListOfAvailableSignals(AvatarBDBlock _block) {
-        List<AvatarSignal> v = new LinkedList<AvatarSignal> ();
+        List<AvatarSignal> v = new LinkedList<AvatarSignal>();
 
         List<AvatarSignal> listOfBlock = _block.getSignalList();
         if (listOfBlock.size() == 0)
@@ -485,7 +487,7 @@ public class AvatarBDPanel extends TDiagramPanel {
 
         v.addAll(listOfBlock);
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDPortConnector) {
                 AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
                 if (port.getAvatarBDBlock1() == _block) {
@@ -501,8 +503,8 @@ public class AvatarBDPanel extends TDiagramPanel {
         return v;
     }
 
-    public List<AvatarSignal> getListOfAvailableOutSignals (AvatarBDBlock _block) {
-        List<AvatarSignal> v = new LinkedList<AvatarSignal> ();
+    public List<AvatarSignal> getListOfAvailableOutSignals(AvatarBDBlock _block) {
+        List<AvatarSignal> v = new LinkedList<AvatarSignal>();
 
         List<AvatarSignal> listOfBlock = _block.getOutSignalList();
         if (listOfBlock.size() == 0)
@@ -510,9 +512,9 @@ public class AvatarBDPanel extends TDiagramPanel {
 
         v.addAll(listOfBlock);
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDPortConnector) {
-                AvatarBDPortConnector port = (AvatarBDPortConnector)tgc;
+                AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
                 if (port.getAvatarBDBlock1() == _block) {
                     List<String> ll = port.getListOfSignalsOrigin();
                     removeSignals(v, ll);
@@ -527,7 +529,7 @@ public class AvatarBDPanel extends TDiagramPanel {
     }
 
     public List<AvatarSignal> getListOfAvailableInSignals(AvatarBDBlock _block) {
-        List<AvatarSignal> v = new LinkedList<AvatarSignal> ();
+        List<AvatarSignal> v = new LinkedList<AvatarSignal>();
 
         List<AvatarSignal> listOfBlock = _block.getInSignalList();
         if (listOfBlock.size() == 0)
@@ -535,9 +537,9 @@ public class AvatarBDPanel extends TDiagramPanel {
 
         v.addAll(listOfBlock);
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDPortConnector) {
-                AvatarBDPortConnector port = (AvatarBDPortConnector)tgc;
+                AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
                 if (port.getAvatarBDBlock1() == _block) {
                     List<String> ll = port.getListOfSignalsOrigin();
                     removeSignals(v, ll);
@@ -552,8 +554,8 @@ public class AvatarBDPanel extends TDiagramPanel {
     }
     //ajoute DG 27.02.: Interface signals treated as block signals to avoid too many case distinctions - is this possible ?
 
-  public List<AvatarSignal> getListOfAvailableSignals(AvatarBDInterface _block) {
-        List<AvatarSignal> v = new LinkedList<AvatarSignal> ();
+    public List<AvatarSignal> getListOfAvailableSignals(AvatarBDInterface _block) {
+        List<AvatarSignal> v = new LinkedList<AvatarSignal>();
 
         List<AvatarSignal> listOfBlock = _block.getSignalList();
         if (listOfBlock.size() == 0)
@@ -561,7 +563,7 @@ public class AvatarBDPanel extends TDiagramPanel {
 
         v.addAll(listOfBlock);
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDPortConnector) {
                 AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
                 if (port.getAvatarBDInterface1() == _block) {
@@ -577,8 +579,8 @@ public class AvatarBDPanel extends TDiagramPanel {
         return v;
     }
 
-    public List<AvatarSignal> getListOfAvailableOutSignals (AvatarBDInterface _block) {
-        List<AvatarSignal> v = new LinkedList<AvatarSignal> ();
+    public List<AvatarSignal> getListOfAvailableOutSignals(AvatarBDInterface _block) {
+        List<AvatarSignal> v = new LinkedList<AvatarSignal>();
 
         List<AvatarSignal> listOfBlock = _block.getOutSignalList();
         if (listOfBlock.size() == 0)
@@ -586,10 +588,10 @@ public class AvatarBDPanel extends TDiagramPanel {
 
         v.addAll(listOfBlock);
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDPortConnector) {
-                AvatarBDPortConnector port = (AvatarBDPortConnector)tgc;
-                if (port.getAvatarBDInterface1() ==  _block) {
+                AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
+                if (port.getAvatarBDInterface1() == _block) {
                     List<String> ll = port.getListOfSignalsOrigin();
                     removeSignals(v, ll);
                 }
@@ -603,7 +605,7 @@ public class AvatarBDPanel extends TDiagramPanel {
     }
 
     public List<AvatarSignal> getListOfAvailableInSignals(AvatarBDInterface _block) {
-        List<AvatarSignal> v = new LinkedList<AvatarSignal> ();
+        List<AvatarSignal> v = new LinkedList<AvatarSignal>();
 
         List<AvatarSignal> listOfBlock = _block.getInSignalList();
         if (listOfBlock.size() == 0)
@@ -611,14 +613,14 @@ public class AvatarBDPanel extends TDiagramPanel {
 
         v.addAll(listOfBlock);
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDPortConnector) {
-                AvatarBDPortConnector port = (AvatarBDPortConnector)tgc;
-                if (port.getAvatarBDInterface1() ==  _block) {
+                AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
+                if (port.getAvatarBDInterface1() == _block) {
                     List<String> ll = port.getListOfSignalsOrigin();
                     removeSignals(v, ll);
                 }
-                if (port.getAvatarBDInterface2() ==  _block) {
+                if (port.getAvatarBDInterface2() == _block) {
                     List<String> ll = port.getListOfSignalsDestination();
                     removeSignals(v, ll);
                 }
@@ -628,81 +630,80 @@ public class AvatarBDPanel extends TDiagramPanel {
     }
 
 
-    
     //fin ajoute DG
-    
+
     // Remove AvatarSignals of v which name is provided in list
     private static void removeSignals(List<AvatarSignal> v, List<String> list) {
-        for(String s: list) {
-            Iterator<AvatarSignal> iterator = v.iterator ();
-            while (iterator.hasNext ()) {
-                AvatarSignal as = iterator.next ();
-                if (as.toString().equals (s)) {
-                    iterator.remove ();
+        for (String s : list) {
+            Iterator<AvatarSignal> iterator = v.iterator();
+            while (iterator.hasNext()) {
+                AvatarSignal as = iterator.next();
+                if (as.toString().equals(s)) {
+                    iterator.remove();
                     break;
                 }
             }
         }
     }
 
-	public Map<String, List<String>> getBlockStrings(boolean addAttributes, boolean addStates, boolean addSignals){
-		Map<String,List<String>> blockStringMap = new HashMap<String, List<String>>();
-		for (AvatarBDBlock block: getFullBlockList()){
-		    //TraceManager.addDev("Got block:" + block.getBlockName());
-			List<String> strs = new ArrayList<String>();
-			if (addAttributes){
-				for (TAttribute attr: getAllAttributesOfBlock(block.getBlockName())){
-					strs.add(attr.getId());
-				}
-			}
-			if (addStates){
-				AvatarSMDPanel smd = block.getAvatarSMDPanel();
-				strs.addAll(smd.getAllStates());
-			}
-			if (addSignals){
-				for (AvatarSignal sig: getAllSignalsOfBlock(block.getBlockName())){
-					strs.add(sig.getId());
-				}
-			}
-			blockStringMap.put(block.getBlockName(), strs);		
-		}
-		return blockStringMap;
-	}
+    public Map<String, List<String>> getBlockStrings(boolean addAttributes, boolean addStates, boolean addSignals) {
+        Map<String, List<String>> blockStringMap = new HashMap<String, List<String>>();
+        for (AvatarBDBlock block : getFullBlockList()) {
+            //TraceManager.addDev("Got block:" + block.getBlockName());
+            List<String> strs = new ArrayList<String>();
+            if (addAttributes) {
+                for (TAttribute attr : getAllAttributesOfBlock(block.getBlockName())) {
+                    strs.add(attr.getId());
+                }
+            }
+            if (addStates) {
+                AvatarSMDPanel smd = block.getAvatarSMDPanel();
+                strs.addAll(smd.getAllStates());
+            }
+            if (addSignals) {
+                for (AvatarSignal sig : getAllSignalsOfBlock(block.getBlockName())) {
+                    strs.add(sig.getId());
+                }
+            }
+            blockStringMap.put(block.getBlockName(), strs);
+        }
+        return blockStringMap;
+    }
 
     public List<AvatarBDStateMachineOwner> getFullStateMachineOwnerList() {
         List<AvatarBDStateMachineOwner> list = new LinkedList<AvatarBDStateMachineOwner>();
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDStateMachineOwner) {
-                list.add ((AvatarBDStateMachineOwner) tgc);
+                list.add((AvatarBDStateMachineOwner) tgc);
                 if (tgc instanceof AvatarBDBlock) {
-                    list.addAll (((AvatarBDBlock) tgc).getFullBlockList());
-                    list.addAll (((AvatarBDBlock) tgc).getFullLibraryFunctionList());
+                    list.addAll(((AvatarBDBlock) tgc).getFullBlockList());
+                    list.addAll(((AvatarBDBlock) tgc).getFullLibraryFunctionList());
                 }
             }
 
         return list;
     }
 
-    public List<AvatarBDBlock> getFullBlockList () {
-        List<AvatarBDBlock> list = new LinkedList<AvatarBDBlock> ();
+    public List<AvatarBDBlock> getFullBlockList() {
+        List<AvatarBDBlock> list = new LinkedList<AvatarBDBlock>();
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDBlock) {
-                list.add ((AvatarBDBlock) tgc);
-                list.addAll (((AvatarBDBlock) tgc).getFullBlockList());
+                list.add((AvatarBDBlock) tgc);
+                list.addAll(((AvatarBDBlock) tgc).getFullBlockList());
             }
 
         return list;
     }
 
-    public AvatarBDBlock getBlockFromFQN (String name) {
-        for (TGComponent tgc: this.componentList)
+    public AvatarBDBlock getBlockFromFQN(String name) {
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDBlock) {
                 if (((AvatarBDBlock) tgc).getFullyQualifiedName().equals(name))
                     return (AvatarBDBlock) tgc;
 
-                for (AvatarBDBlock subBlock: ((AvatarBDBlock) tgc).getFullBlockList()) {
+                for (AvatarBDBlock subBlock : ((AvatarBDBlock) tgc).getFullBlockList()) {
                     if (subBlock.getFullyQualifiedName().equals(name))
                         return subBlock;
                 }
@@ -711,13 +712,13 @@ public class AvatarBDPanel extends TDiagramPanel {
         return null;
     }
 
-    public AvatarBDBlock getBlockFromOwnerName (String name) {
-        for (TGComponent tgc: this.componentList)
+    public AvatarBDBlock getBlockFromOwnerName(String name) {
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDBlock) {
                 if (((AvatarBDBlock) tgc).getOwnerName().equals(name))
                     return (AvatarBDBlock) tgc;
 
-                for (AvatarBDBlock subBlock: ((AvatarBDBlock) tgc).getFullBlockList()) {
+                for (AvatarBDBlock subBlock : ((AvatarBDBlock) tgc).getFullBlockList()) {
                     if (subBlock.getOwnerName().equals(name))
                         return subBlock;
                 }
@@ -726,21 +727,20 @@ public class AvatarBDPanel extends TDiagramPanel {
         return null;
     }
 
-    public List<AvatarBDLibraryFunction> getFullLibraryFunctionList () {
-        List<AvatarBDLibraryFunction> list = new LinkedList<AvatarBDLibraryFunction> ();
+    public List<AvatarBDLibraryFunction> getFullLibraryFunctionList() {
+        List<AvatarBDLibraryFunction> list = new LinkedList<AvatarBDLibraryFunction>();
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDLibraryFunction)
-                list.add ((AvatarBDLibraryFunction) tgc);
+                list.add((AvatarBDLibraryFunction) tgc);
 
         return list;
     }
 
-    public List<AvatarBDLibraryFunction> getAllLibraryFunctionsForBlock (String _name) {
-        List<AvatarBDLibraryFunction> list = new LinkedList<AvatarBDLibraryFunction> ();
+    public List<AvatarBDLibraryFunction> getAllLibraryFunctionsForBlock(String _name) {
+        List<AvatarBDLibraryFunction> list = new LinkedList<AvatarBDLibraryFunction>();
 
-        for(AvatarBDStateMachineOwner block: getFullStateMachineOwnerList())
-        {
+        for (AvatarBDStateMachineOwner block : getFullStateMachineOwnerList()) {
             if (block instanceof AvatarBDLibraryFunction)
                 list.add((AvatarBDLibraryFunction) block);
         }
@@ -750,50 +750,50 @@ public class AvatarBDPanel extends TDiagramPanel {
 
     public TAttribute getAttributeByBlockName(String _blockName, String attributeName) {
         //TAttribute a;
-        for(AvatarBDStateMachineOwner block: getFullStateMachineOwnerList())
-            if (block.getOwnerName().equals (_blockName))
+        for (AvatarBDStateMachineOwner block : getFullStateMachineOwnerList())
+            if (block.getOwnerName().equals(_blockName))
                 return block.getAttributeByName(attributeName);
 
         return null;
     }
 
-    public List<TAttribute> getAllAttributesOfBlock (String _name) {
-        List<AvatarBDStateMachineOwner> list = getFullStateMachineOwnerList ();
-        for(AvatarBDStateMachineOwner block: list)
-            if (block.getOwnerName ().equals (_name))
-                return block.getAttributeList ();
+    public List<TAttribute> getAllAttributesOfBlock(String _name) {
+        List<AvatarBDStateMachineOwner> list = getFullStateMachineOwnerList();
+        for (AvatarBDStateMachineOwner block : list)
+            if (block.getOwnerName().equals(_name))
+                return block.getAttributeList();
 
-        return new LinkedList<TAttribute> ();
+        return new LinkedList<TAttribute>();
     }
 
     public List<AvatarMethod> getAllMethodsOfBlock(String _name) {
-        for(AvatarBDStateMachineOwner block: getFullStateMachineOwnerList())
-            if (block.getOwnerName().equals (_name))
+        for (AvatarBDStateMachineOwner block : getFullStateMachineOwnerList())
+            if (block.getOwnerName().equals(_name))
                 return block.getAllMethodList();
 
         return null;
     }
 
     public List<AvatarSignal> getAllSignalsOfBlock(String _name) {
-        for(AvatarBDStateMachineOwner block: getFullStateMachineOwnerList())
-            if (block.getOwnerName().equals (_name))
+        for (AvatarBDStateMachineOwner block : getFullStateMachineOwnerList())
+            if (block.getOwnerName().equals(_name))
                 return block.getAllSignalList();
 
         return null;
     }
 
     public List<String> getAllTimersOfBlock(String _name) {
-        for(AvatarBDStateMachineOwner block: getFullStateMachineOwnerList())
-            if (block.getOwnerName().equals (_name))
+        for (AvatarBDStateMachineOwner block : getFullStateMachineOwnerList())
+            if (block.getOwnerName().equals(_name))
                 return block.getAllTimerList();
 
         return null;
     }
 
     public List<TAttribute> getAttributesOfDataType(String _name) {
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDDataType) {
-                AvatarBDDataType adt = (AvatarBDDataType)tgc;
+                AvatarBDDataType adt = (AvatarBDDataType) tgc;
                 if (adt.getDataTypeName().compareTo(_name) == 0)
                     return adt.getAttributeList();
             }
@@ -838,11 +838,11 @@ public class AvatarBDPanel extends TDiagramPanel {
         return considerTimingOperators;
     }
 
-    public void setValidated( List<AvatarBDStateMachineOwner> _validated) {
+    public void setValidated(List<AvatarBDStateMachineOwner> _validated) {
         validated = _validated;
     }
 
-    public void setIgnored( List<AvatarBDStateMachineOwner> _ignored) {
+    public void setIgnored(List<AvatarBDStateMachineOwner> _ignored) {
         ignored = _ignored;
     }
 
@@ -856,16 +856,16 @@ public class AvatarBDPanel extends TDiagramPanel {
 
     public void makeValidated() {
         //TraceManager.addDev("Making validated with val=" + val);
-        validated = new LinkedList<AvatarBDStateMachineOwner> ();
+        validated = new LinkedList<AvatarBDStateMachineOwner>();
         List<AvatarBDStateMachineOwner> list = getFullStateMachineOwnerList();
         String tmp;
 
         String split[] = val.split(";");
-        for(int i=0; i<split.length; i++) {
+        for (int i = 0; i < split.length; i++) {
             tmp = split[i].trim();
             if (tmp.length() > 0) {
-                for (AvatarBDStateMachineOwner block: list) {
-                    if (block.getOwnerName().equals (tmp)) {
+                for (AvatarBDStateMachineOwner block : list) {
+                    if (block.getOwnerName().equals(tmp)) {
                         validated.add(block);
                         break;
                     }
@@ -877,16 +877,16 @@ public class AvatarBDPanel extends TDiagramPanel {
 
     public void makeIgnored() {
         //TraceManager.addDev("Making ignored with ign=" + val);
-        ignored = new LinkedList<AvatarBDStateMachineOwner> ();
+        ignored = new LinkedList<AvatarBDStateMachineOwner>();
         List<AvatarBDStateMachineOwner> list = getFullStateMachineOwnerList();
         String tmp;
 
         String split[] = ign.split(";");
-        for(int i=0; i<split.length; i++) {
+        for (int i = 0; i < split.length; i++) {
             tmp = split[i].trim();
             if (tmp.length() > 0) {
-                for (AvatarBDStateMachineOwner block: list) {
-                    if (block.getOwnerName().equals (tmp)) {
+                for (AvatarBDStateMachineOwner block : list) {
+                    if (block.getOwnerName().equals(tmp)) {
                         ignored.add(block);
                         break;
                     }
@@ -903,13 +903,13 @@ public class AvatarBDPanel extends TDiagramPanel {
         String name;
 
 
-        for(AvatarBDBlock block: getFullBlockList()) {
+        for (AvatarBDBlock block : getFullBlockList()) {
 
             name = block.getBlockName();
             if (ref && name.equals(_name)) {
                 list.add(_topName + "::" + name);
             } else {
-                if (!_tadp.isMapped(_topName,  name)) {
+                if (!_tadp.isMapped(_topName, name)) {
                     list.add(_topName + "::" + name);
                 }
             }
@@ -919,21 +919,28 @@ public class AvatarBDPanel extends TDiagramPanel {
         return list;
     }
 
-    
+
     public ArrayList<String> getAllNonMappedAvatarChannelNames(String _topName, ADDDiagramPanel _tadp) {
         ArrayList<String> list = new ArrayList<String>();
 
-        for (TGComponent tgc: this.componentList)
+        for (TGComponent tgc : this.componentList)
             if (tgc instanceof AvatarBDPortConnector) {
-                AvatarBDPortConnector port = (AvatarBDPortConnector)tgc;
-		if (port.getListOfSignalsOrigin().size() > 0) {
+                AvatarBDPortConnector port = (AvatarBDPortConnector) tgc;
+                if (port.getListOfSignalsOrigin().size() > 0) {
                     String name = port.getChannelName();
-		    TraceManager.addDev("[CHA] Channel name=" + name);
-		    if (!_tadp.isChannelMapped(_topName,  name))
-			list.add(_topName + "::" + name);
-		}
+                    TraceManager.addDev("[CHA] Channel name=" + name);
+                    if (!_tadp.isChannelMapped(_topName, name))
+                        list.add(_topName + "::" + name);
+                }
             }
 
         return list;
     }
+
+    public void loadAndUpdateFromText(String _spec) throws org.json.JSONException {
+        TraceManager.addDev("Loading blocks from JSON:" + _spec);
+
+        AvatarSpecification avatarSpec = AvatarSpecification.fromJSON(_spec, getName(), this);
+        mgui.drawAvatarSpecification(avatarSpec);
+    }
 }
diff --git a/src/main/java/ui/avatarrd/AvatarRDPanel.java b/src/main/java/ui/avatarrd/AvatarRDPanel.java
index 06e371fbb6..1a6396edef 100755
--- a/src/main/java/ui/avatarrd/AvatarRDPanel.java
+++ b/src/main/java/ui/avatarrd/AvatarRDPanel.java
@@ -742,6 +742,8 @@ public class AvatarRDPanel extends TDiagramPanel implements TDPWithAttributes, N
 
         _spec = _spec.substring(indexStart, indexStop+1);
 
+
+
         TraceManager.addDev("Cut spec: " + _spec);
 
         JSONArray reqArray = new JSONArray(_spec);
@@ -789,14 +791,67 @@ public class AvatarRDPanel extends TDiagramPanel implements TDPWithAttributes, N
             // We complete other values
             String doc = reqArray.getJSONObject(i).getString("doc");
             selectedReq.setText(doc);
-            /*JSONObject derive = reqArray.getJSONObject(i).getJSONObject("derive");
-            JSONObject refine = reqArray.getJSONObject(i).getJSONObject("refine");*/
+
+
+        }
+
+        // 2nd parsing: relations
+        for(int i=0; i<reqArray.length(); i++) {
+            String name = reqArray.getJSONObject(i).getString("name");
+            name = name.trim().replaceAll(" ", "_");
+            AvatarRDRequirement reqName = getReqWithName(name);
+
+            if (reqName != null) {
+                String [] relations = {"compose", "derive", "refine"};
+                for (int j=0; j<relations.length; j++) {
+                    JSONArray rel = reqArray.getJSONObject(i).getJSONArray(relations[j]);
+                    for(int k=0; k<rel.length(); k++) {
+                        String nameOther = rel.getString(k);
+                        AvatarRDRequirement reqOther = getReqWithName(nameOther.trim().replaceAll(" ", "_"));
+                        if (reqOther != null) {
+                            addRelation(relations[j], reqName, reqOther);
+                        }
+                    }
+                }
+
+            }
         }
 
 
 
     }
 
+    public void addRelation(String relation, AvatarRDRequirement reqOrigin, AvatarRDRequirement reqDest) {
+
+        TraceManager.addDev("Adding " + relation + " between " + reqOrigin.getValue() + " and " + reqDest.getValue());
+
+        TGConnectingPoint p1 = reqOrigin.closerFreeTGConnectingPoint(reqDest.getX(), reqDest.getY(), true, true);
+        TGConnectingPoint p2 = reqDest.closerFreeTGConnectingPoint(reqOrigin.getX(), reqOrigin.getY(), true, true);
+
+        if ( (p1 != null) && (p2 != null) ) {
+            TGConnector conn = null;
+            if (relation.compareTo("compose") == 0) {
+                conn = new AvatarRDCompositionConnector(reqOrigin.getX(), reqOrigin.getY(), reqOrigin.getCurrentMinX(), reqOrigin.getCurrentMinY(),
+                        reqOrigin.getCurrentMaxX(), reqOrigin.getCurrentMaxY(), true, null, reqOrigin.getTDiagramPanel(),
+                        p1, p2, null);
+            } else if (relation.compareTo("refine") == 0) {
+                conn = new AvatarRDRefineConnector(reqOrigin.getX(), reqOrigin.getY(), reqOrigin.getCurrentMinX(), reqOrigin.getCurrentMinY(),
+                        reqOrigin.getCurrentMaxX(), reqOrigin.getCurrentMaxY(), true, null, reqOrigin.getTDiagramPanel(),
+                        p1, p2, null);
+            } else if (relation.compareTo("derive") == 0) {
+                conn = new AvatarRDDeriveConnector(reqOrigin.getX(), reqOrigin.getY(), reqOrigin.getCurrentMinX(), reqOrigin.getCurrentMinY(),
+                        reqOrigin.getCurrentMaxX(), reqOrigin.getCurrentMaxY(), true, null, reqOrigin.getTDiagramPanel(),
+                        p1, p2, null);
+            }
+
+            if (conn != null) {
+                p1.setFree(false);
+                p2.setFree(false);
+                reqOrigin.getTDiagramPanel().addComponent(conn, p1.getX(), p1.getY(), false, true);
+            }
+        }
+
+    }
 
 
 }
diff --git a/src/main/java/ui/window/JFrameAI.java b/src/main/java/ui/window/JFrameAI.java
index 3789c86c09..f6523ccb96 100644
--- a/src/main/java/ui/window/JFrameAI.java
+++ b/src/main/java/ui/window/JFrameAI.java
@@ -49,6 +49,8 @@ import myutil.AIInterfaceException;
 import myutil.GraphicLib;
 import myutil.TraceManager;
 import ui.*;
+import ui.avatarbd.AvatarBDBlock;
+import ui.avatarbd.AvatarBDPanel;
 import ui.avatarrd.AvatarRDPanel;
 import ui.avatarrd.AvatarRDRequirement;
 import ui.util.IconManager;
@@ -74,9 +76,12 @@ public class JFrameAI extends JFrame implements ActionListener, Runnable {
     private static int IDENTIFY_REQUIREMENT = 1;
     private static int KIND_CLASSIFY_REQUIREMENT = 2;
     private static int IDENTIFY_PROPERTIES = 3;
+    private static int IDENTIFY_SYSTEM_BLOCKS = 4;
+    private static int IDENTIFY_SOFTWARE_BLOCKS = 5;
 
     private static String[] POSSIBLE_ACTIONS = {"Chat", "Identify requirements from a specification", "Classify requirements from a requirement " +
-            "diagram", "Identify properties from a design"};
+            "diagram", "Identify properties from a design", "Identify system blocks from a specification", "Identify software blocks from a " +
+            "specification"};
     protected JComboBox<String> listOfPossibleActions;
     private String QUESTION_CLASSIFY_REQ = "I would like to identify the \"type\" attribute, i.e. the classification, " +
             "of the following requirements. Could you give me a correct type among: safety, security, functional, " +
@@ -90,9 +95,24 @@ public class JFrameAI extends JFrame implements ActionListener, Runnable {
             "name: name of the requirement, id: id of the requirement (as a string), doc: text of the requirement  " +
             "compose: all req names, derive: all req names, refine: all req names. The name " +
             "should be an english " +
-            "name and not a number or an identifier";
+            "name and not a number or an identifier. Identify the relations (compose, derive, refine) even if they are not given in the " +
+            "specification. Use the name of requirements and not the id in the list of relations.";
     private String QUESTION_IDENTIFY_PROPERTIES = "List properties of the following SysML V2 specification.";
 
+    private String KNOWLEDGE_ON_JSON_FOR_BLOCKS = "JSON for block diagram is as follows: " +
+            "{blocks: [{ \"name\": \"Name of block\", \"attributes\": [\"name\": \"name of attribute\", \"type\": \"int or boolean\" ...}" + " same" +
+            "(with its parameters : int, boolean ; and its return type : nothing, int or boolean)" +
+            "and signals (with its list of parameters : int or boolean ; and each signal is either an output signal or an input signal), ..." +
+            " then the list of connections between block signals: \"connections\": [\n" +"{\n" + " \"sourceBlock\": \"name of block\",\n" +
+            " \"sourceSignal\": \"name of output signal\",\n" +
+            " \"destinationBlock\": \"name of destination block\",\n" +
+            " \"destinationSignal\": \"rechargeBattery\",\n" +
+            " \"communicationType\": \"synchronous (or asynchronous)\"\n" +
+            "},";
+
+    private String QUESTION_IDENTIFY_SYSTEM_BLOCKS = "From the following system specification, using the specified JSON format, identify the " +
+            "typical system blocks. All this in JSON.\n";
+
     private String KNOWLEDGE_ON_DESIGN_PROPERTIES = "Properties of Design are of the following types\n" +
             "- A<>expr means that all states of all paths must respect expr\n" +
             "- A[]expr means that all states of at least one path must respect expr\n" +
@@ -116,6 +136,7 @@ public class JFrameAI extends JFrame implements ActionListener, Runnable {
     private class ChatData {
         public AIInterface aiinterface;
         public boolean knowledgeOnProperties = false;
+        public boolean knowledgeOnBlockJSON = false;
         public JTextPane answer = new JTextPane();
         public String lastAnswer = "";
         public int previousKind;
@@ -318,6 +339,8 @@ public class JFrameAI extends JFrame implements ActionListener, Runnable {
             applyRequirementClassification();
         } else if (selectedChat().previousKind == IDENTIFY_REQUIREMENT) {
             applyRequirementIdentification();
+        } else if (selectedChat().previousKind == IDENTIFY_SYSTEM_BLOCKS) {
+            applyIdentifySystemBlocks();
         } else {
             return;
         }
@@ -350,7 +373,35 @@ public class JFrameAI extends JFrame implements ActionListener, Runnable {
         }
         rdpanel.repaint();
 
-        inform("Enhancing requirement diagram with ai answer: done<\n");
+        inform("Enhancing requirement diagram with ai answer: done\n");
+    }
+
+    private void applyIdentifySystemBlocks() {
+        if (selectedChat().tdp == null) {
+            error("No diagram has been selected\n");
+            return;
+        }
+
+        if (!(selectedChat().tdp instanceof AvatarBDPanel)) {
+            error("Wrong diagram has been selected\n");
+            return;
+        }
+
+        AvatarBDPanel bdpanel = (AvatarBDPanel) selectedChat().tdp;
+
+        TraceManager.addDev("Considered JSON array: " + selectedChat().lastAnswer);
+        try {
+            bdpanel.loadAndUpdateFromText( selectedChat().lastAnswer );
+        } catch (org.json.JSONException e) {
+            TraceManager.addDev("JSON Exception: " + e.getMessage());
+            inform("Answer provided by AI does not respect the JSON format necessary for TTool");
+            bdpanel.repaint();
+            return;
+        }
+        bdpanel.repaint();
+
+        inform("System blocks added to diagram from ai answer: done\n");
+
     }
 
     private void applyRequirementClassification() {
@@ -448,6 +499,10 @@ public class JFrameAI extends JFrame implements ActionListener, Runnable {
             if (listOfPossibleActions.getSelectedIndex() == IDENTIFY_PROPERTIES) {
                 identifyProperties();
             }
+
+            if (listOfPossibleActions.getSelectedIndex() == IDENTIFY_SYSTEM_BLOCKS) {
+                identifySystemBlocks();
+            }
         }
 
         go = false;
@@ -548,6 +603,32 @@ public class JFrameAI extends JFrame implements ActionListener, Runnable {
         makeQuestion(question, IDENTIFY_PROPERTIES, tdp);
     }
 
+    private void identifySystemBlocks() {
+        inform("Identifying system blocks\n");
+        TDiagramPanel tdp = mgui.getCurrentTDiagramPanel();
+        if (!(tdp instanceof AvatarBDPanel)) {
+            error("An Avatar block diagram must be selected first");
+            return;
+        }
+        if (!hasQuestion()) {
+            error("A system specification must be provided in the \"question\" area");
+            return;
+        }
+
+        TraceManager.addDev("Asking for system blocks");
+
+        String questionT = QUESTION_IDENTIFY_SYSTEM_BLOCKS + "\n" + question.getText().trim() + "\n";
+        if (!(chats.get(answerPane.getSelectedIndex()).knowledgeOnBlockJSON)) {
+            chats.get(answerPane.getSelectedIndex()).knowledgeOnBlockJSON = true;
+            questionT = KNOWLEDGE_ON_JSON_FOR_BLOCKS + "\n" + questionT;
+        }
+
+
+        String answer = makeQuestion(questionT, IDENTIFY_SYSTEM_BLOCKS, tdp);
+
+
+    }
+
     private String makeQuestion(String _question, int _kind, TDiagramPanel _tdp) {
         GraphicLib.appendToPane(chatOfStart().answer, _question, Color.blue);
 
-- 
GitLab