Skip to content
Snippets Groups Projects
AvatarPanelDrawer.java 40.7 KiB
Newer Older
/* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille
 * 
 * ludovic.apvrille AT enst.fr
 * 
 * This software is a computer program whose purpose is to allow the
 * edition of TURTLE analysis, design and deployment diagrams, to
 * allow the generation of RT-LOTOS or Java code from this diagram,
 * and at last to allow the analysis of formal validation traces
 * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP
 * from INRIA Rhone-Alpes.
 * 
 * This software is governed by the CeCILL  license under French law and
 * abiding by the rules of distribution of free software.  You can  use,
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info".
 * 
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability.
 * 
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or
 * data to be ensured and,  more generally, to use and operate it in the
 * same conditions as regards security.
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
 */



 
package ui;


import avatartranslator.*;
import myutil.TraceManager;
import ui.avatarbd.*;
import ui.avatarsmd.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * Class AvatarPanelDrawer
 * Possible states of TGComponent
 * Creation: 12/06/2024
 * @author Ludovic APVRILLE
 * @see avatartranslator.AvatarSpecification
 */
public class AvatarPanelDrawer {

	private boolean hasCrypto = false;

	public AvatarPanelDrawer() {

	}

	public void drawPanel(AvatarSpecification avspec, AvatarDesignPanel adp, boolean useOriginalValuesFirst) {
		//
		// Check Errors in AVSPEC
		avspec.removeAnomalies();
		TraceManager.addDev("Checking syntax of avatar spec.");
		ArrayList<AvatarError> list = AvatarSyntaxChecker.checkSyntaxErrors(avspec);
		for (AvatarError error : list) {
			TraceManager.addDev("\n*********** Error: " + error.toString() + "\n\n");
		}
		//TraceManager.addDev("Check done. " + checkingErrors.size() + " errors found\nAvatar Spec:\n" + avspec.toString());

		TraceManager.addDev("Avspec to be drawn:" + avspec.toStringRecursive(false));


		// Go for drawing!
		hasCrypto = false;
		//Map<String, Set<String>> originDestMap = new HashMap<String, Set<String>>();
		Map<String, AvatarBDBlock> blockMap = new HashMap<String, AvatarBDBlock>();
		if (adp == null) {
			return;
		}
		if (avspec == null) {
			return;
		}
		AvatarBDPanel abd = adp.abdp;

		//Find all blocks, create nested blocks starting from top left
		int xpos = 10;
		int ypos = 40;

		// Create blocks recursively, starting from top level ones with no father
		// Lowest level blocks should be 100x100, next should be 100x(number of children*100+50)...etc,
		// Find level #, 0 refers to no father, etc
		Map<AvatarBlock, Integer> blockLevelMap = new HashMap<AvatarBlock, Integer>();
		Map<AvatarBlock, Integer> blockSizeMap = new HashMap<AvatarBlock, Integer>();
		Map<AvatarBlock, Integer> blockIncMap = new HashMap<AvatarBlock, Integer>();
		int maxLevel = 0;
		for (AvatarBlock ab : avspec.getListOfBlocks()) {
			int level = 0;
			AvatarBlock block = ab;
			while (block.getFather() != null) {
				if (blockSizeMap.containsKey(block.getFather())) {
					blockSizeMap.put(block.getFather(), blockSizeMap.get(block.getFather()) + 1);
				} else {
					blockSizeMap.put(block.getFather(), 1);
					blockIncMap.put(block.getFather(), 10);
				}
				level++;
				block = block.getFather();
			}
			if (level > maxLevel) {
				maxLevel = level;
			}
			if (!blockSizeMap.containsKey(block)) {
				blockSizeMap.put(block, 0);
				blockIncMap.put(block, 10);
			}
			blockLevelMap.put(ab, level);
		}


		for (int level = 0; level < maxLevel + 1; level++) {
			for (AvatarBlock ab : avspec.getListOfBlocks()) {
				if (blockLevelMap.get(ab) == level) {
					if (level == 0) {
						TraceManager.addDev("New block at level 0");
						AvatarBDBlock bl = new AvatarBDBlock(xpos, ypos, abd.getMinX(), abd.getMaxX(), abd.getMinY(), abd.getMaxY(), false,
								null, abd);
						if ((ab.getReferenceObject() != null) && (ab.getReferenceObject() instanceof CDElement)) {
							CDElement cd = (CDElement) ab.getReferenceObject();
							bl.setUserResize(cd.getX(), cd.getY(), cd.getWidth(), cd.getHeight());
							abd.addComponent(bl, cd.getX(), cd.getY(), false, true);
						} else {
							abd.addComponent(bl, xpos, ypos, false, true);
							bl.resize(100 * blockSizeMap.get(ab) + 100, 100 + (maxLevel - level) * 50);
						}
						drawBlockProperties(avspec, ab, bl, useOriginalValuesFirst);
						AvatarSMDPanel smp = adp.getAvatarSMDPanel(bl.getValue());
						//TraceManager.addDev("\nBuilding state machine of block " + ab.getName() + " smd:" + ab.getStateMachine().toString() + "\n" +
						//       "\n");
						buildStateMachine(ab, bl, smp, useOriginalValuesFirst);
						//TraceManager.addDev("Putting in block")
						blockMap.put(bl.getValue().split("__")[bl.getValue().split("__").length - 1], bl);
						xpos += 100 * blockSizeMap.get(ab) + 200;
					} else {
						TraceManager.addDev("New block at level " + level);
						AvatarBDBlock father =
								blockMap.get(ab.getFather().getName().split("__")[ab.getFather().getName().split("__").length - 1]);
						TraceManager.addDev("Father name: " + father.getBlockName());
						if (father == null) {
							//
							continue;
						}
						AvatarBDBlock bl = new AvatarBDBlock(father.getX() + blockIncMap.get(ab.getFather()), father.getY() + 10,
								abd.getMinX(), abd.getMaxX(), abd.getMinY(), abd.getMaxY(), false, father, abd);
                        /*if ((ab.getReferenceObject() != null) && (ab.getReferenceObject() instanceof CDElement)) {
                            CDElement cd = (CDElement) ab.getReferenceObject();
                            bl.setUserResize(cd.getX(), cd.getY(), cd.getWidth(), cd.getHeight());
                            abd.addComponent(bl, cd.getX(), cd.getY(), false, true);
                        } else {*/
						//abd.addComponent(bl, xpos, ypos, false, true);
						//bl.resize(100 * blockSizeMap.get(ab) + 100, 100 + (maxLevel - level) * 50);
						//}
						abd.addComponent(bl, father.getX() + blockIncMap.get(ab.getFather()), father.getY() + 10, false, true);
						int size = 100;
						if ((ab.getReferenceObject() != null) && (ab.getReferenceObject() instanceof CDElement)) {
							CDElement cd = (CDElement) ab.getReferenceObject();
							bl.setUserResize(cd.getX(), cd.getY(), cd.getWidth(), cd.getHeight());
						} else {

							if (blockSizeMap.containsKey(ab)) {
								size = 100 * blockSizeMap.get(ab) + 50;
							}
							bl.resize(size, 100 + (maxLevel - level) * 50);
						}
						drawBlockProperties(avspec, ab, bl, useOriginalValuesFirst);
						abd.attach(bl);
						AvatarSMDPanel smp = adp.getAvatarSMDPanel(bl.getValue());
						buildStateMachine(ab, bl, smp, useOriginalValuesFirst);
						blockMap.put(bl.getValue().split("__")[bl.getValue().split("__").length - 1], bl);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
						blockIncMap.put(ab.getFather(), blockIncMap.get(ab.getFather()) + size + 10);
					}
				}
			}
		}

		// Data types
		// Put them on the lower part


		for (AvatarRelation ar : avspec.getRelations()) {
			String bl1 = ar.block1.getName();
			String bl2 = ar.block2.getName();

			bl1 = getLastKeyword(bl1);
			bl2 = getLastKeyword(bl2);

			Vector<Point> points = new Vector<Point>();

			//TraceManager.addDev("bl1:" + bl1 + " bl2:" + bl2);

			// Printing blockMap
            /*for(String s: blockMap.keySet()) {
                AvatarBDBlock block = blockMap.get(s);
                TraceManager.addDev("String s:" + s + " / Block : " + block.getBlockName());
            }*/

			if ((blockMap.get(bl1) != null) && (blockMap.get(bl2) != null)) {
				//TraceManager.addDev("Handling blocks");
				AvatarBDBlock b1 = blockMap.get(bl1);
				AvatarBDBlock b2 = blockMap.get(bl2);
				//TGConnectingPoint p1 = blockMap.get(bl1).findFirstFreeTGConnectingPoint(true, true);
				//TGConnectingPoint p2 = blockMap.get(bl2).findFirstFreeTGConnectingPoint(true, true);


				TGConnectingPoint p1 = null, p2 = null;

				if ((ar.getReferenceObject() instanceof AvatarBDPortConnector) && (ar.getOtherReferenceObjects().size() > 1)) {
					//TraceManager.addDev("*----* Found reference objects before / after port connector");
					AvatarBDPortConnector connOld = (AvatarBDPortConnector) (ar.getReferenceObject());
					if ((ar.getOtherReferenceObjects().get(0) instanceof TGComponent) && (ar.getOtherReferenceObjects().get(1) instanceof TGComponent)) {
						TGComponent tgc1 = (TGComponent) (ar.getOtherReferenceObjects().get(0));
						TGComponent tgc2 = (TGComponent) (ar.getOtherReferenceObjects().get(1));

						int index1 = tgc1.getIndexOfTGConnectingPoint(connOld.getTGConnectingPointP1());
						int index2 = tgc2.getIndexOfTGConnectingPoint(connOld.getTGConnectingPointP2());

						if ((index1 != -1) && (index2 != -1)) {
							p1 = b1.getFreeTGConnectingPoint(index1);
							p2 = b2.getFreeTGConnectingPoint(index2);
						}
					}
				}

				if (p1 == null)
					p1 = b1.closerFreeTGConnectingPoint(b2.getX(), b2.getY(), false, true);
				if (p2 == null)
					p2 = b2.closerFreeTGConnectingPoint(b1.getX(), b1.getY(), true, false);
				if (p1 != null && p2 != null) {
					p1.setFree(false);
					p2.setFree(false);

                    /*if (bl2.equals(bl1)) {
                        // Add 2 point so the connection looks square
                        Point p = new Point(p1.getX(), p1.getY() - 10);
                        points.add(p);
                        p = new Point(p2.getX(), p2.getY() - 10);
                        points.add(p);
                    }*/

					for (int i = 0; i < ar.getOtherReferenceObjects().size(); i++) {
						Object o = ar.getOtherReferenceObjects().get(i);
						if (o instanceof TGCPointOfConnector) {
							TGCPointOfConnector op = (TGCPointOfConnector) o;
							points.add(new Point(op.getX(), op.getY()));
						}
					}

					AvatarBDPortConnector conn = new AvatarBDPortConnector(0, 0, 0, 0, 0, 0, true, null,
							abd, p1, p2, points);
					abd.addComponent(conn, 0, 0, false, true);
					conn.setAsynchronous(ar.isAsynchronous());
					conn.setSynchronous(!ar.isAsynchronous());
					conn.setAMS(false);
					conn.setBlocking(ar.isBlocking());
					conn.setPrivate(ar.isPrivate());
					conn.setSizeOfFIFO(ar.getSizeOfFIFO());

					for (int indexSig = 0; indexSig < ar.getSignals1().size(); indexSig++) {

						//TraceManager.addDev("Adding signal 1: " + ar.getSignal1(i).toString() + " of block " + ar.block1.getName());
						conn.addSignal(ar.getSignal1(indexSig).toString(), ar.getSignal1(indexSig).getInOut() == 0, ar.block1.getName().contains(bl1));
						//TraceManager.addDev("Adding signal 2:" + ar.getSignal2(i).toString() + " of block " + ar.block2.getName());
						conn.addSignal(ar.getSignal2(indexSig).toString(), ar.getSignal2(indexSig).getInOut() == 0, !ar.block2.getName().contains(bl2));

						conn.updateAllSignals();
					}


					conn.updateAllSignals();
				}
			}
		}

		xpos = 50;
		ypos = 500;

		for (AvatarDataType adt : avspec.getDataTypes()) {
			AvatarBDDataType abdType = new AvatarBDDataType(xpos, ypos, abd.getMinX(), abd.getMaxX(), abd.getMinY(), abd.getMaxY(), false,
					null, abd);
			abdType.setDataTypeName(adt.getName());
			// Adding attributes
			for (AvatarAttribute attr : adt.getAttributes()) {
				int type = 4;
				if (attr.getType() == AvatarType.INTEGER) {
					type = 0;
				}
				if (attr.getType() == AvatarType.TIMER) {
					type = 9;
				}
				abdType.addAttribute(new TAttribute(0, attr.getName(), attr.getType().getDefaultInitialValue(), type));
			}


			// Drawing
			if ((adt.getReferenceObject() != null) && (adt.getReferenceObject() instanceof CDElement)) {
				CDElement cd = ((CDElement) (adt.getReferenceObject()));
				abdType.setUserResize(cd.getX(), cd.getY(), cd.getWidth(), cd.getHeight());
				abd.addComponent(abdType, cd.getX(), cd.getY(), false, true);
			} else {
				abd.addComponent(abdType, xpos, ypos, false, true);
			}

		}


		//Add Pragmas


		if (avspec.getPragmas().size() > 0) {
			AvatarBDPragma pragma = new AvatarBDPragma(xpos, ypos, xpos, xpos * 2, ypos, ypos * 2, false, null, abd);
			//  String[] arr = new String[avspec.getPragmas().size()];
			String s = "";
			// int i=0;
			for (AvatarPragma p : avspec.getPragmas()) {

				//    arr[i] = p.getName();
				TraceManager.addDev("Handling pragma: " + p.getName());
				String t = "";
				String[] split = p.getName().split(" ");
				if (p.getName().startsWith("Confidentiality")) {
					for (String str : split) {
						if (str.contains(".")) {
							String tmp = str.split("\\.")[0];
							String tmp2 = str.split("\\.")[1];
							t = t.concat(tmp.split("__")[tmp.split("__").length - 1] +
									"." + tmp2.split("__")[tmp2.split("__").length - 1] + " ");
						} else {
							t = t.concat(str + " ");
						}
					}
				} else if (p.getName().startsWith("Authenticity")) {
					t = p.getName();
				} else if (p.getName().startsWith("Initial")) {
					t = p.getName();
				} else {
					t = p.getName();
				}
				//TraceManager.addDev("1. pragma=" + t);
				t = t.trim();
				if (t.startsWith("Confidentiality") || t.startsWith("Authenticity")) {
					t = "#" + t;
				}
				//TraceManager.addDev("2. pragma=" + t);
				s = s + "\n";
				s = s.concat(t);

				//  i++;
			}
			pragma.setValue(s);
			pragma.makeValue();
			abd.addComponent(pragma, xpos, ypos, false, true);
		}


		//Add message and key datatype if there is a cryptoblock

		xpos = 50;
		ypos += 200;
		if (hasCrypto) {
			AvatarBDDataType message = new AvatarBDDataType(xpos, ypos, xpos, xpos * 2, ypos, ypos * 2, false, null, abd);
			message.setValue("Message");

			abd.addComponent(message, xpos, ypos, false, true);
			message.resize(200, 100);
			xpos += 400;

			AvatarBDDataType key = new AvatarBDDataType(xpos, ypos, xpos, xpos * 2, ypos, ypos * 2, false, null, abd);
			key.setValue("Key");
			TAttribute attr = new TAttribute(2, "data", "0", 8);
			message.addAttribute(attr);
			key.addAttribute(attr);
			key.resize(200, 100);
			abd.addComponent(key, xpos, ypos, false, true);
		}

		adp.getAvatarBDPanel().connectSignals();
		adp.getAvatarBDPanel().repaint();

	}

	public void buildStateMachine(AvatarBlock ab, AvatarBDBlock bl, AvatarSMDPanel smp, boolean useOriginalValuesFirst) {

		//TraceManager.addDev("Building state machine of " + ab.getName());

		Map<AvatarTransition, TGComponent> tranSourceMap = new HashMap<AvatarTransition, TGComponent>();
		Map<AvatarTransition, AvatarStateMachineElement> tranDestMap = new HashMap<AvatarTransition, AvatarStateMachineElement>();
		Map<AvatarStateMachineElement, TGComponent> locMap = new HashMap<AvatarStateMachineElement, TGComponent>();
		Map<AvatarStateMachineElement, TGComponent> SMDMap = new HashMap<AvatarStateMachineElement, TGComponent>();

		Map<Object, TGComponent> refMap = new HashMap<>();

		//Build the state machine
		int smx = 400;
		int smy = 40;

		if (smp == null) {
			return;
		}

		smp.removeAll();
		AvatarStateMachine asm = ab.getStateMachine();

		//TraceManager.addDev("\nState machine: " + asm.toString() + "\n\n");
		//TraceManager.addDev("\nRecursive state machine: " + asm.toStringRecursive() + "\n\n");

		//Remove the empty check states

		AvatarStartState start = asm.getStartState();

		addStates(start, smx, smy, smp, bl, SMDMap, locMap, tranDestMap, tranSourceMap, refMap, useOriginalValuesFirst);

		//TraceManager.addDev("\nState machine: " + asm.toString() + "\n\n");

		//Add transitions
		for (AvatarTransition t : tranSourceMap.keySet()) {
			if (tranSourceMap.get(t) == null || tranDestMap.get(t) == null || locMap.get(tranDestMap.get(t)) == null) {
				continue;
			}

			int x = tranSourceMap.get(t).getX() + tranSourceMap.get(t).getWidth() / 2;
			int y = tranSourceMap.get(t).getY() + tranSourceMap.get(t).getHeight();

			//    TGConnectingPoint p1 = tranSourceMap.get(t).findFirstFreeTGConnectingPoint(true,false);


            /*for(Object o: t.getReferenceObject()) {

            }*/

			TGConnectingPoint p1 = null, p2 = null;

			if ((t.getOtherReferenceObjects() != null) && (t.getOtherReferenceObjects().size() > 4)) {
				try {
					Point p = (Point) (t.getOtherReferenceObjects().get(4));
					TGComponent newTGC1 = refMap.get(t.getOtherReferenceObjects().get(2));
					if (newTGC1 != null) {
						p1 = newTGC1.getFreeTGConnectingPoint(p.x);
					}
					TGComponent newTGC2 = refMap.get(t.getOtherReferenceObjects().get(3));
					if (newTGC2 != null) {
						p2 = newTGC2.getFreeTGConnectingPoint(p.y);
					}

				} catch (Exception e) {
					TraceManager.addDev("**** EXCEPTION: " + e.getMessage());
				}
			}


            /*if ((t.getReferenceObject() instanceof TGConnector) && (t.getOtherReferenceObjects().size() > 1)) {
                TraceManager.addDev("Found reference objects before / after avatar transition");
                TGConnector tgcon = (TGConnector) (t.getReferenceObject());
                if ((t.getOtherReferenceObjects().get(0) instanceof TGComponent) && (t.getOtherReferenceObjects().get(1) instanceof TGComponent)) {
                    // Find the first two components which are not TGCPointOfConnector
                    TGComponent tgc1 = findTGComponentNoPoint(t.getOtherReferenceObjects(), 0);
                    TGComponent tgc2 = findTGComponentNoPoint(t.getOtherReferenceObjects(), 1);

                    // Find if there are other TGComponents which are not points
                    TraceManager.addDev("\t source: " + tgc1 + "x=" + tgc1.getX() + " y=" + tgc1.getY());
                    TraceManager.addDev("\t dest: " + tgc2 + "x=" + tgc2.getX() + " y=" + tgc2.getY());

                    int index1 = tgc1.getIndexOfTGConnectingPoint(tgcon.getTGConnectingPointP1());
                    int index2 = tgc2.getIndexOfTGConnectingPoint(tgcon.getTGConnectingPointP2());


                    if ((index1 != -1) && (index2 != -1)) {
                        TraceManager.addDev("Computing p1 and p2");
                        p1 = tranSourceMap.get(t).getFreeTGConnectingPoint(index1);
                        p2 = locMap.get(tranDestMap.get(t)).getFreeTGConnectingPoint(index2);
                    } else if (tgcon.getTDiagramPanel() != null) {
                        TraceManager.addDev("Index of points is not valid");
                        TGComponent tgc3 = null;
                        TGComponent tgc4 = null;

                        // look for the connecting point in the panel
                        tgc3 = tgcon.getTDiagramPanel().getComponentToWhichBelongs(tgcon.getTGConnectingPointP1());
                        if ((tgc3 != null) && (tgc3.getClass() == tgc1.getClass())) {
                            // Get equivalent tgc in new tdp
                            tgc3 = smp.getEquivalentComponent(tgc3);
                        }

                        // look for the connecting point in the panel
                        tgc4 = tgcon.getTDiagramPanel().getComponentToWhichBelongs(tgcon.getTGConnectingPointP2());
                        if ((tgc4 != null) && (tgc4.getClass() == tgc2.getClass())) {
                            // Get equivalent tgc in new tdp
                            tgc4 = smp.getEquivalentComponent(tgc4);
                        }

                        if ((tgc3 != null) && (tgc4 != null)) {
                            index1 = tgc3.getIndexOfTGConnectingPoint(tgcon.getTGConnectingPointP1());
                            index2 = tgc4.getIndexOfTGConnectingPoint(tgcon.getTGConnectingPointP2());
                            if ((index1 != -1) && (index2 != -1)) {
                                p1 = tgc3.getFreeTGConnectingPoint(index1);
                                p2 = tgc4.getFreeTGConnectingPoint(index2);
                            }
                        }
                    }
                }
            }*/
			if ((p1 == null) || (p2 == null)) {
				if (p1 == null) {
					p1 = tranSourceMap.get(t).closerFreeTGConnectingPoint(x, y, false, true);
					if (p1 == null) {
						TraceManager.addDev("NULL P1 in " + tranSourceMap.get(t).getName() + "/" + tranSourceMap.get(t).getValue());
						p1 = tranSourceMap.get(t).findFirstFreeTGConnectingPoint(true, false);
						//p1=tranSourceMap.get(t).closerFreeTGConnectingPoint(x,y,true, true);
					}
					x = locMap.get(tranDestMap.get(t)).getX() + locMap.get(tranDestMap.get(t)).getWidth() / 2;
					y = locMap.get(tranDestMap.get(t)).getY();
					if (tranSourceMap.get(t).getY() > locMap.get(tranDestMap.get(t)).getY()) {
						y = locMap.get(tranDestMap.get(t)).getY() + locMap.get(tranDestMap.get(t)).getHeight() / 2;
						if (tranSourceMap.get(t).getX() < locMap.get(tranDestMap.get(t)).getX()) {
							x = locMap.get(tranDestMap.get(t)).getX();
						} else {
							x = locMap.get(tranDestMap.get(t)).getX() + locMap.get(tranDestMap.get(t)).getWidth();
						}
					}
				}
				if (p2 == null) {
					p2 = locMap.get(tranDestMap.get(t)).closerFreeTGConnectingPoint(x, y, true, false);
					if (p2 == null) {
						TraceManager.addDev("NULL P2 in " + locMap.get(tranDestMap.get(t)).getName() + "/" + locMap.get(tranDestMap.get(t)).getValue());
						p2 = locMap.get(tranDestMap.get(t)).findFirstFreeTGConnectingPoint(false, true);
					}
				}
			} else {
				//TraceManager.addDev("Using original connecting points");
			}
			Vector<Point> points = new Vector<Point>();
			if (p1 == null || p2 == null) {
				TraceManager.addDev("Null P1 and P2");

				return;
			}
			if (t.getOtherReferenceObjects() != null) {
				for (int i = 0; i < t.getOtherReferenceObjects().size(); i++) {
					Object o = t.getOtherReferenceObjects().get(i);
					if (o instanceof TGCPointOfConnector) {
						TGCPointOfConnector op = (TGCPointOfConnector) o;
						points.add(new Point(op.getX(), op.getY()));
					}
				}
			}
			AvatarSMDConnector SMDCon = new AvatarSMDConnector(p1.getX(), p1.getY(), p1.getX(), p1.getY(), p1.getX(), p1.getY(), true, null, smp,
					p1, p2, points);
			//
			///
			p1.setFree(false);
			p2.setFree(false);
			String action = "";
			if (t.getActions().size() == 0) {
				action = "";
			} else {
				action = t.getAction(0, useOriginalValuesFirst).toString().replaceAll(" ", "");
			}
			// Delays
			SMDCon.setTransitionTime(t.getMinDelay(useOriginalValuesFirst), t.getMaxDelay(useOriginalValuesFirst), t.getMinCompute(), t.getMaxCompute());

			// Guard
			SMDCon.setTransitionInfo(t.getGuard(useOriginalValuesFirst), action);

			// Action
			for (int i = 1; i < t.getActions().size(); i++) {
				SMDCon.setTransitionInfo("", t.getAction(i, useOriginalValuesFirst).replaceAll(" ", ""));
			}

			smp.addComponent(SMDCon, p1.getX(), p1.getY(), false, true);

			if (t.getOtherReferenceObjects() != null) {
				for (int i = 0; i < t.getOtherReferenceObjects().size(); i++) {
					Object o = t.getOtherReferenceObjects().get(i);
					if (o instanceof AvatarSMDTransitionInfo) {
						//TraceManager.addDev(" ----- Position transition x y width height");
						AvatarSMDTransitionInfo tInfo = (AvatarSMDTransitionInfo) o;
						SMDCon.getAvatarSMDTransitionInfo().setMoveCd(tInfo.getX(), tInfo.getY(), true);
						SMDCon.getAvatarSMDTransitionInfo().resize(tInfo.getWidth(), tInfo.getHeight());
						break;
					}
				}
			}


		}
	}

	public void addStates(AvatarStateMachineElement asme, int x, int y, AvatarSMDPanel smp, AvatarBDBlock bl,
						  Map<AvatarStateMachineElement, TGComponent> SMDMap, Map<AvatarStateMachineElement, TGComponent> locMap,
						  Map<AvatarTransition, AvatarStateMachineElement> tranDestMap, Map<AvatarTransition, TGComponent> tranSourceMap,
						  Map<Object, TGComponent> refMap,
						  boolean useOriginalValuesFirst) {
		// TGConnectingPoint tp = new TGConnectingPoint(null, x, y, false, false);
		//Create dummy tgcomponent

		if (asme == null) {
			return;
		}

		//TGComponent tgcomp = new AvatarSMDStartState(x, y, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false, null, smp);
		TGComponent tgcomp = null;
		//TraceManager.addDev("Block " + bl.getName() + " / " + bl.getValue() + " Found: " + asme.getExtendedName());
		int newX = x;
		int newY = y;
		int width = 0;
		int height = 0;

		TGComponent tgc = null;

		if (asme.getReferenceObject() instanceof TGComponent) {
			tgc = (TGComponent) (asme.getReferenceObject());
			newX = tgc.getX();
			newY = tgc.getY();
			width = tgc.getWidth();
			height = tgc.getHeight();
		} else {
			newX = x;
			newY = y;
		}

		if (asme instanceof AvatarStartState) {
			AvatarSMDStartState smdss = new AvatarSMDStartState(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false, null,
					smp);
			tgcomp = smdss;
			smp.addComponent(smdss, newX, newY, false, true);
			if (tgc != null) smdss.resize(width, height);
			SMDMap.put(asme, smdss);
			//   tp = smdss.tgconnectingPointAtIndex(0);
			locMap.put(asme, smdss);
			refMap.put(asme.getReferenceObject(), smdss);
		}
		if (asme instanceof AvatarTransition) {
			//
		}
		if (asme instanceof AvatarRandom) {
			AvatarSMDRandom smdr = new AvatarSMDRandom(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false, null, smp);
			AvatarRandom ar = (AvatarRandom) asme;
			smdr.setVariable(ar.getVariable());
			if (useOriginalValuesFirst && (ar.getOriginalMinValue() != null) && (ar.getOriginalMinValue().length() > 0)) {
				smdr.setMinValue(ar.getOriginalMinValue());
			} else {
				smdr.setMinValue(ar.getMinValue());
			}
			if (useOriginalValuesFirst && (ar.getOriginalMaxValue() != null) && (ar.getOriginalMaxValue().length() > 0)) {
				smdr.setMaxValue(ar.getOriginalMaxValue());
			} else {
				smdr.setMaxValue(ar.getMaxValue());
			}
			smp.addComponent(smdr, newX, newY, false, true);
			if (tgc != null) smdr.resize(width, height);
			tgcomp = smdr;
			SMDMap.put(asme, smdr);
			locMap.put(asme, smdr);
			refMap.put(asme.getReferenceObject(), smdr);
		}

		if (asme instanceof AvatarQueryOnSignal) {
			AvatarSMDQueryReceiveSignal smdqrs = new AvatarSMDQueryReceiveSignal(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(),
					smp.getMaxY(),
					false, null,
					smp);
			smdqrs.setNames(((AvatarQueryOnSignal) asme).getAttribute().getName(), ((AvatarQueryOnSignal) asme).getSignal().getName());
			smp.addComponent(smdqrs, newX, newY, false, true);
			if (tgc != null) smdqrs.resize(width, height);
			tgcomp = smdqrs;
			SMDMap.put(asme, smdqrs);
			locMap.put(asme, smdqrs);
			refMap.put(asme.getReferenceObject(), smdqrs);
		}

		if (asme instanceof AvatarActionOnSignal) {
			//TraceManager.addDev("Block " + bl.getName() + " / " + bl.getValue() + " Found AvatarActionOnSignal: " + asme.getExtendedName());
			avatartranslator.AvatarSignal sig = ((AvatarActionOnSignal) asme).getSignal();
			if (sig.isIn()) {
				AvatarSMDReceiveSignal smdrs = new AvatarSMDReceiveSignal(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(),
						false,
						null, smp);
				tgcomp = smdrs;
				smp.addComponent(smdrs, newX, newY, false, true);
				//                              String name=sig.minString();
				//
				if (tgc != null) smdrs.resize(width, height);
				AvatarActionOnSignal aaos = (AvatarActionOnSignal) asme;

				String parameters = aaos.getAllOriginalVals();
				//TraceManager.addDev("1. Parameters=" + parameters);
				String parametersA = aaos.getAllVals();


				if (parameters.length() == 0 && parametersA.length() > parameters.length()) {
					parameters = parametersA;
				} else if (!useOriginalValuesFirst) {
					parameters = parametersA;
				}

				//TraceManager.addDev("2. Parameters=" + parameters);

                /*if (((AvatarActionOnSignal) asme).getValues().size() > 0) {
                    parameters += ((AvatarActionOnSignal) asme).getValues().get(0);
                    for (int i = 1; i < ((AvatarActionOnSignal) asme).getValues().size(); i++) {
                        parameters = parameters + "," + ((AvatarActionOnSignal) asme).getValues().get(i);



                }*/
				String name = sig.getName() + "(" + parameters + ")";
				smdrs.setValue(name);
				// sig.setName(name);
				smdrs.recalculateSize();
				SMDMap.put(asme, smdrs);
				//   tp = smdrs.getFreeTGConnectingPoint(x+smdrs.getWidth()/2,y+smdrs.getHeight());
				//  TGConnectingPoint tp2 = smdrs.getFreeTGConnectingPoint(x+smdrs.getWidth()/2,y);
				locMap.put(asme, smdrs);
				refMap.put(asme.getReferenceObject(), smdrs);

				if (bl.getAvatarSignalFromName(name) == null) {
					//bl.addSignal(new ui.AvatarSignal(0, name, new String[0], new String[0]));
				}

			} else {
				AvatarSMDSendSignal smdss = new AvatarSMDSendSignal(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false,
						null,
						smp);
				tgcomp = smdss;
				smp.addComponent(smdss, newX, newY, false, true);
				if (tgc != null) smdss.resize(width, height);
				AvatarActionOnSignal aaos = (AvatarActionOnSignal) asme;
				String parameters = aaos.getAllOriginalVals();
				//TraceManager.addDev("1. Parameters=" + parameters);
				String parametersA = aaos.getAllVals();
				if (parameters.length() == 0 && parametersA.length() > parameters.length()) {
					parameters = parametersA;
				} else if (!useOriginalValuesFirst) {
					parameters = parametersA;
				}

				//TraceManager.addDev("2. Parameters=" + parameters);
                /*String parameters = "";
                if (((AvatarActionOnSignal) asme).getValues().size() > 0) {
                    parameters += ((AvatarActionOnSignal) asme).getValues().get(0);
                    for (int i = 1; i < ((AvatarActionOnSignal) asme).getValues().size(); i++) {
                        parameters = parameters + "," + ((AvatarActionOnSignal) asme).getValues().get(i);
                    }
                }*/
				String name = sig.getName() + "(" + parameters + ")";
				//String name=sig.minString();
				smdss.setValue(name);
				smdss.recalculateSize();
				SMDMap.put(asme, smdss);
				//  tp = smdss.getFreeTGConnectingPoint(x+smdss.getWidth()/2,y+smdss.getHeight());
				//      TGConnectingPoint tp2 = smdss.getFreeTGConnectingPoint(x+smdss.getWidth()/2,y);
				locMap.put(asme, smdss);
				refMap.put(asme.getReferenceObject(), smdss);
				if (bl.getAvatarSignalFromName(name) == null) {
					// bl.addSignal(new ui.AvatarSignal(1, name, new String[0], new String[0]));
				}
			}

		}
		if (asme instanceof AvatarStopState) {
			AvatarSMDStopState smdstop = new AvatarSMDStopState(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false, null,
					smp);
			tgcomp = smdstop;
			SMDMap.put(asme, smdstop);
			smp.addComponent(smdstop, newX, newY, false, true);
			if (tgc != null) smdstop.resize(width, height);
			//  tp = smdstop.tgconnectingPointAtIndex(0);
			locMap.put(asme, smdstop);
			refMap.put(asme.getReferenceObject(), smdstop);
		}

		if (asme instanceof AvatarSetTimer) {
			AvatarSMDSetTimer timerSet = new AvatarSMDSetTimer(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false, null,
					smp);
			AvatarSetTimer ast = (AvatarSetTimer) asme;
			String timerValue = ast.getTimerValue();
			if (useOriginalValuesFirst) {
				if ((ast.getTimerOriginalValue() != null) && (ast.getTimerOriginalValue().length() > 0)) {
					timerValue = ast.getTimerOriginalValue();
				}
			}
			timerSet.setComplexValue(ast.getTimer().getName(), timerValue);
			tgcomp = timerSet;
			SMDMap.put(asme, timerSet);
			smp.addComponent(timerSet, newX, newY, false, true);
			if (tgc != null) timerSet.resize(width, height);
			//  tp = smdstop.tgconnectingPointAtIndex(0);
			locMap.put(asme, timerSet);
			refMap.put(asme.getReferenceObject(), timerSet);
		}

		if (asme instanceof AvatarExpireTimer) {
			AvatarSMDExpireTimer timerExpire = new AvatarSMDExpireTimer(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false,
					null,
					smp);
			timerExpire.setComplexValue(((AvatarExpireTimer) asme).getTimer().getName());
			tgcomp = timerExpire;

			SMDMap.put(asme, timerExpire);
			smp.addComponent(timerExpire, newX, newY, false, true);
			if (tgc != null) timerExpire.resize(width, height);
			//  tp = smdstop.tgconnectingPointAtIndex(0);
			locMap.put(asme, timerExpire);
			refMap.put(asme.getReferenceObject(), timerExpire);
		}

		if (asme instanceof AvatarResetTimer) {
			AvatarSMDResetTimer timerReset = new AvatarSMDResetTimer(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false,
					null,
					smp);
			timerReset.setComplexValue(((AvatarResetTimer) asme).getTimer().getName());
			tgcomp = timerReset;
			SMDMap.put(asme, timerReset);
			smp.addComponent(timerReset, newX, newY, false, true);
			if (tgc != null) timerReset.resize(width, height);
			//  tp = smdstop.tgconnectingPointAtIndex(0);
			locMap.put(asme, timerReset);
			refMap.put(asme.getReferenceObject(), timerReset);
		}

		if (asme instanceof AvatarState) {
			//check if empty checker state
			/* if (asme.getName().contains("signalstate_")) {
			//don't add the state, ignore next transition,
			if (asme.getNexts().size()==1) {
			AvatarStateMachineElement next = asme.getNext(0).getNext(0);
			//Reroute transition
			for (AvatarTransition at: tranDestMap.keySet()) {
			if (tranDestMap.get(at) == asme) {
			tranDestMap.put(at, next);
			}
			}
			addStates(next, x, y, smp,bl, SMDMap, locMap, tranDestMap, tranSourceMap);
			return;
			}
			}*/
			AvatarSMDState smdstate = new AvatarSMDState(newX, newY, smp.getMinX(), smp.getMaxX(), smp.getMinY(), smp.getMaxY(), false, null, smp);
			tgcomp = smdstate;
			smp.addComponent(smdstate, newX, newY, false, true);
			if (tgc != null) smdstate.resize(width, height);
			smdstate.setValue(asme.getName());
			//smdstate.recalculateSize();
			SMDMap.put(asme, smdstate);
			//   tp = smdstate.getFreeTGConnectingPoint(x+smdstate.getWidth()/2,y+smdstate.getHeight());
			//  TGConnectingPoint tp2 = smdstate.getFreeTGConnectingPoint(x+smdstate.getWidth()/2,y);
			locMap.put(asme, smdstate);
			refMap.put(asme.getReferenceObject(), smdstate);

			if (asme.getReferenceObjects() != null) {
				for (Object o : asme.getReferenceObjects()) {
					if ((o != asme.getReferenceObject()) && (o instanceof TGComponent)) {
						TGComponent tmpT = (TGComponent) o;
						//TraceManager.addDev("Drawing second state called " + asme.getName());
						AvatarSMDState smdstateBis = new AvatarSMDState(tmpT.getX(), tmpT.getY(), smp.getMinX(), smp.getMaxX(), smp.getMinY(),
								smp.getMaxY(), false, null, smp);
						//tgcomp = smdstate;
						smp.addComponent(smdstateBis, tmpT.getX(), tmpT.getY(), false, true);
						smdstateBis.resize(tmpT.getWidth(), tmpT.getHeight());
						smdstateBis.setValue(asme.getName());
						refMap.put(o, smdstateBis);
					}
				}
			}
		}

		int i = 0;
		int diff = 300;
		int ydiff = 50;
		//int num = asme.nbOfNexts();
        /*if (!(asme instanceof AvatarTransition)) {
            //TraceManager.addDev("element asme: " + asme.toString());
            if (asme.getNexts() != null) {
                for (AvatarStateMachineElement el : asme.getNexts()) {
                    if (!(el instanceof AvatarTransition)) {
                        if ((asme.getReferenceObject() != null) && (asme.getReferenceObject() instanceof CDElement)) {
                            CDElement cd = (CDElement) asme.getReferenceObject();
                            tgcomp.setCd(cd.getX(), cd.getY());
                        }
                    }
                }
            }
        }*/
		if (asme.getNexts() != null) {
			for (AvatarStateMachineElement el : asme.getNexts()) {
                /*TraceManager.addDev("Block " + bl.getName() + " / " + bl.getValue() +
                        " Handling next of " + asme.getExtendedName() + ": " + el.getExtendedName());*/
				if (el instanceof AvatarTransition) {
					tranSourceMap.put((AvatarTransition) el, tgcomp);
				} else {
					if (asme instanceof AvatarTransition) {
						AvatarTransition t = (AvatarTransition) asme;
						tranDestMap.put(t, el);
					}
				}
				if (!SMDMap.containsKey(el)) {
                    /*TraceManager.addDev("not in map: Block " + bl.getName() + " / " + bl.getValue() +
                            " Handling next of " + asme.getExtendedName() + ": " + el.getExtendedName());*/
					addStates(el, x + diff * i, y + ydiff, smp, bl, SMDMap, locMap, tranDestMap, tranSourceMap, refMap, useOriginalValuesFirst);
				}
				i++;
			}
			return;
		}
	}

	public void drawBlockProperties(AvatarSpecification as, AvatarBlock ab, AvatarBDBlock bl, boolean useOriginalValues) {

		// Signals
		for (avatartranslator.AvatarSignal sig : ab.getSignals()) {
			String name = sig.getName().split("__")[sig.getName().split("__").length - 1];

			//           sig.setName(name);
			String[] types;
			String[] typeIds;
			List<AvatarAttribute> listOfAttributes;
			if (useOriginalValues && (sig.getListOfOriginalAttributes().size() > 0 || sig.getListOfOriginalReturnAttributes().size() > 0)) {
				listOfAttributes = sig.getListOfOriginalAttributes();
			} else {
				listOfAttributes = sig.getListOfAttributes();
			}
			int i = 0;
			types = new String[listOfAttributes.size()];
			typeIds = new String[listOfAttributes.size()];
			for (AvatarAttribute attr : listOfAttributes) {
				if (attr.isDataType()) {
					types[i] = attr.getDataType().getName();
				} else {
					types[i] = attr.getType().getStringType();
				}
				typeIds[i] = attr.getName();
				i++;
			}

			//TraceManager.addDev("Adding signal " + sig + " with name=" + name);
			bl.addSignal(new ui.AvatarSignal(sig.getInOut(), name, types, typeIds));
		}

		bl.setValueWithChange(ab.getName().split("__")[ab.getName().split("__").length - 1]);

		TraceManager.addDev("Setting name of block: " + bl.getBlockName());

		if (useOriginalValues) {
			for (AvatarAttribute attr : ab.getOriginalAttributes()) {
				if (attr.isDataType()) {
					bl.addAttribute(new TAttribute(0, attr.getName(), attr.getInitialValue(), attr.getDataType().getName()));
				} else {
					int type = TAttribute.OTHER;
					if (attr.getType() == AvatarType.BOOLEAN) {
						type = TAttribute.BOOLEAN;
					}
					if (attr.getType() == AvatarType.INTEGER) {
						type = TAttribute.NATURAL;
					}
					if (attr.getType() == AvatarType.TIMER) {
						type = TAttribute.TIMER;
					}
					if (attr.hasInitialValue()) {
						bl.addAttribute(new TAttribute(0, attr.getName(), attr.getInitialValue(), type));
					} else {
						bl.addAttribute(new TAttribute(0, attr.getName(), attr.getType().getDefaultInitialValue(), type));
					}
				}

				if (attr.getName().contains("key_") || attr.getName().contains("privKey_")) {
					hasCrypto = true;
					bl.addCryptoElements();
				}
			}
		} else {
			for (AvatarAttribute attr : ab.getAttributes()) {
				int type = TAttribute.OTHER;
				if (attr.getType() == AvatarType.BOOLEAN) {
					type = TAttribute.BOOLEAN;
				}
				if (attr.getType() == AvatarType.INTEGER) {
					type = TAttribute.NATURAL;
				}
				if (attr.getType() == AvatarType.TIMER) {
					type = TAttribute.TIMER;