diff --git a/src/main/java/avatartranslator/AvatarSpecification.java b/src/main/java/avatartranslator/AvatarSpecification.java index 22a070ba0833bd3428de2fedc664a0804bd0164a..35e9c6b5eecb6076ac6cfcd1ec293e82bded2a98 100644 --- a/src/main/java/avatartranslator/AvatarSpecification.java +++ b/src/main/java/avatartranslator/AvatarSpecification.java @@ -114,411 +114,34 @@ public class AvatarSpecification extends AvatarElement implements IBSParamSpec { return null; } - /* - * 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 void removeAnomalies(){ + // Rename signals with the same name going from one block to the same block + // Rename it by adding "in_" at the beginning + + for(AvatarRelation ar: relations) { + if (ar.block1 == ar.block2) { + for(int i=0; i<ar.nbOfSignals(); i++) { + AvatarSignal sig1 = ar.getSignal1(i); + AvatarSignal sig2 = ar.getSignal2(i); + + if (sig1.getName().compareTo(sig2.getName()) == 0) { + // One of the two must be renamed. We rename the out signal + String name; + if (sig1.isOut()) { + name = ar.block1.findUniqueSignalName("out_" + sig1.getName()); + sig1.setName(name); + } else { + name = ar.block2.findUniqueSignalName("out_" + sig2.getName()); + sig2.setName(name); + } + } + } + } + } + + } + + public static AvatarSpecification fromJSON(String _spec, String _name, Object _referenceObject, boolean tryToCorrectErrors) { if (_spec == null) { return null; @@ -714,7 +337,7 @@ public class AvatarSpecification extends AvatarElement implements IBSParamSpec { spec.addRelation(ar); } - // If the signals have the same name and are in the same block ,they are renamed + // If the signals have the same name and are in the same block, they are renamed if ((sig.getName().compareTo(inSig.getName()) == 0) && (blockO == blockD)) { sig.setName(sig.getName() + "_out"); inSig.setName(sig.getName() + "_in"); diff --git a/src/main/java/tmltranslator/TMLModeling.java b/src/main/java/tmltranslator/TMLModeling.java index 069a51cad535e645282dcea009bef93b2f0e184d..85d1ac1e6aa7e4219dde58a7a18ac4d306663e46 100755 --- a/src/main/java/tmltranslator/TMLModeling.java +++ b/src/main/java/tmltranslator/TMLModeling.java @@ -886,6 +886,9 @@ public class TMLModeling<E> { public Object getReference() { if (getTasks() != null) { + if (getTasks().size() != 0) { + return null; + } if (getTasks().get(0) != null) { return getTasks().get(0).getReferenceObject(); } diff --git a/src/main/java/ui/GTURTLEModeling.java b/src/main/java/ui/GTURTLEModeling.java index df958d7277182a88b78f827a24cde85456a45a90..d245a5fded336042acc565bb4ce58284e7903aa5 100644 --- a/src/main/java/ui/GTURTLEModeling.java +++ b/src/main/java/ui/GTURTLEModeling.java @@ -9644,6 +9644,7 @@ public class GTURTLEModeling { public void drawPanel(AvatarSpecification avspec, AvatarDesignPanel adp) { // // Check Errors in AVSPEC + avspec.removeAnomalies(); TraceManager.addDev("Checking syntax of avatar spec."); ArrayList<AvatarError> list = AvatarSyntaxChecker.checkSyntaxErrors(avspec); for (AvatarError error : list) {