Beispiel #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;
  }
  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);
  }