Пример #1
0
  private Network createNetwork(Element element) {
    Network network = IrFactory.eINSTANCE.createNetwork();
    String id = element.getAttribute("id");
    network.setId(id);
    doAnnotations(network, element);

    addIrObject(id, network);

    network.setType((TypeActor) createType(getChild(element, "Type")));

    List<Element> declarations = getChildren(element, "Decl");
    for (Element e : declarations) {
      Declaration decl = (Declaration) createDeclaration(e);
      network.getDeclarations().add(decl);
      if (decl instanceof Variable && ((Variable) decl).isParameter()) {
        network.getParameters().add((Variable) decl);
      }
    }

    List<Element> ports = getChildren(element, "Port");
    for (Element e : ports) {
      Port port = createPort(e);
      if (e.getAttribute("direction").equals("in")) {
        network.getInputPorts().add(port);
      } else {
        network.getOutputPorts().add(port);
      }
    }

    List<Element> instances = getChildren(element, "Instance");
    for (Element e : instances) {
      ActorInstance instance = createActorInstance(e);
      network.getActors().add(instance);
    }

    List<Element> connections = getChildren(element, "Connection");
    for (Element e : connections) {
      Connection connection = createConnection(e, network);
      network.getConnections().add(connection);
    }

    return network;
  }
Пример #2
0
  @Override
  public Stream caseNetwork(Network network) {
    s =
        new Stream(
            folder
                + File.separator
                + UtilIR.namespace2Path(network.getType().getNamespace())
                + File.separator
                + network.getType().getName()
                + ".dot");

    java.util.Date date = new java.util.Date();
    s.println();
    s.println("/* --- CAL Dotty Printer " + new Timestamp(date.getTime()) + " --- */");

    s.println("digraph " + network.getType().getName() + "{");
    s.inc();
    s.println("node [shape=none];");
    s.println("rankdir=LR;");

    for (ActorInstance instance : network.getActors()) {
      caseActorInstance(instance);
    }

    for (Connection c : network.getConnections()) {
      doSwitch(c);
    }

    /* Network input ports */
    List<Port> inputPorts = network.getInputPorts();
    if (inputPorts.size() > 0) {
      s.println("__INs" + " [label=<");
      s.println("<TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\">");
      s.println("<TR><TD COLSPAN=\"2\">INs</TD></TR>");
      s.inc();
      for (int i = 0; i < inputPorts.size(); i++) {
        String in = inputPorts.get(i).getName();
        s.println("<TR><TD></TD><TD ALIGN=\"RIGHT\" PORT=\"" + in + "\">" + in + "</TD></TR>");
      }
      s.printlnDec("</TABLE>>];");
    }

    /* Network output ports */
    List<Port> outputPorts = network.getOutputPorts();
    if (outputPorts.size() > 0) {
      s.println("__OUTs" + " [label=<");
      s.println("<TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\">");
      s.printlnInc("<TR><TD COLSPAN=\"2\">OUTs</TD></TR>");
      for (int i = 0; i < outputPorts.size(); i++) {
        String out = outputPorts.get(i).getName();
        s.println("<TR><TD ALIGN=\"LEFT\" PORT=\"" + out + "\">" + out + "</TD><TD></TD></TR>");
      }
      s.printlnDec("</TABLE>>];");
    }

    s.println();
    s.println();

    s.println("}");

    return s;
  }
  public static void addAndResolveDeclarations(Network network, Set<Namespace> namespaces) {

    Set<IrDeclVertex> added = new HashSet<IrDeclVertex>();

    final Map<Declaration, Declaration> imported = new HashMap<Declaration, Declaration>();

    for (Declaration decl : network.getDeclarations()) {

      try {
        if (decl instanceof TypeDeclarationImport) {
          Declaration newDecl = ActorDirectory.findTypeDeclaration((TypeDeclarationImport) decl);
          imported.put(decl, newDecl);
          decl = newDecl;
        } else if (decl instanceof VariableImport) {
          Declaration newDecl = ActorDirectory.findVariable((VariableImport) decl);
          imported.put(decl, newDecl);
          decl = newDecl;
        }
        added.add(new IrDeclVertex(decl));
      } catch (DirectoryException x) {
        System.err.println("[ExpandIrSymbols] Internal error #1");
        return;
      }
    }

    for (Namespace ns : namespaces) {
      for (Declaration decl : ns.getDeclarations()) {

        try {
          if (decl instanceof TypeDeclarationImport) {
            Declaration newDecl = ActorDirectory.findTypeDeclaration((TypeDeclarationImport) decl);
            imported.put(decl, newDecl);
            decl = newDecl;
          } else if (decl instanceof VariableImport) {
            Declaration newDecl = ActorDirectory.findVariable((VariableImport) decl);
            imported.put(decl, newDecl);
            decl = newDecl;
          }
          if (!added.contains(decl)) {
            network.getDeclarations().add(decl);
            added.add(new IrDeclVertex(decl));
          }
        } catch (DirectoryException x) {
          System.err.println("[ExpandIrSymbols] Internal error #2");
          return;
        }
      }
    }

    // Redirect all declarations that reference to TypedefImport/VariableImport
    // to point at the real thing.
    for (VertexData data : added) {
      new IrReplaceSwitch() {
        @Override
        public Expression caseVariableExpression(VariableExpression e) {
          if (imported.containsKey(e.getVariable())) {
            e.setVariable(imported.get(e.getVariable()));
          }

          return e;
        }

        @Override
        public TypeUser caseTypeUser(TypeUser t) {
          if (imported.containsKey(t.getDeclaration())) {
            t.setDeclaration(imported.get(t.getDeclaration()));
          }

          return t;
        }

        @Override
        public Expression caseTypeConstructorCall(TypeConstructorCall expr) {
          if (imported.containsKey(expr.getTypedef())) {
            expr.setTypedef(imported.get(expr.getTypedef()));
          }

          return expr;
        }
      }.doSwitch((EObject) data.getData());
    }

    // Do all the definitions, sort first
    List<VertexData> graphData = new ArrayList<VertexData>(added);
    graphData = new Graph(graphData).sortByDependency();

    List<Declaration> sortedDeclarations = new ArrayList<Declaration>();
    for (VertexData data : graphData) {
      sortedDeclarations.add((Declaration) data.getData());
    }

    int nrOfDeclarations = network.getDeclarations().size();
    for (int i = 0; i < nrOfDeclarations; i++) {
      network.getDeclarations().remove(0);
    }

    for (Declaration decl : sortedDeclarations) {
      network.getDeclarations().add(decl);
    }

    // Finally, redirect all expressions that references TypedefImport/VariableImport
    // to point at the real thing.
    new IrReplaceSwitch() {
      @Override
      public Expression caseVariableExpression(VariableExpression e) {
        if (imported.containsKey(e.getVariable())) {
          e.setVariable(imported.get(e.getVariable()));
        }
        return e;
      }
    }.doSwitch(network);
  }