/** * Create a resource from the given URI and append a new Network instance to its contents. The * resourceSet used must be authorized to write on the disk. This means that the default * EditingDomain's resourceSet must be used in a write transaction (for example). If it is not * possible, do not provide a resourceSet, the default one will be used. * * @param resourceSet * @param uri * @return The created network * @throws IOException */ public static Network createNetworkResource(final ResourceSet resourceSet, final URI uri) throws IOException { final String fileName; if (uri.isPlatform()) { fileName = uri.toPlatformString(true); } else { fileName = uri.toString(); } // Create the network final Network network = DfFactory.eINSTANCE.createNetwork(fileName); // Compute the new network name final Path networkPath = new Path(uri.trimFileExtension().path()); // 3 first segments are resource/<PROJECT>/src network.setName(networkPath.removeFirstSegments(3).toString().replace('/', '.')); // Create the resource Resource res = resourceSet.createResource(uri); res.getContents().add(network); res.save(Collections.EMPTY_MAP); return network; }
@Override public Void casePort(Port port) { List<Edge> connections = new ArrayList<Edge>(port.getOutgoing()); if (connections.size() > 1) { createBroadcast(network.getSimpleName(), port, connections); } return null; }
/** * Check if the given network contains a vertex (instance or port) with the given base name. If * yes, return a new unique name formed from the given base and a numeric suffix. If not, returns * the given base. * * @param network The network to check for existing vertex with the given name * @param base The base name to assign to a new vertex * @return A unique name to assign to a new Instance / Port in the given network */ public static String uniqueVertexName(final Network network, final String base) { final Set<String> existingNames = new HashSet<String>(); for (Vertex vertex : network.getVertices()) { existingNames.add(vertex.getLabel()); } if (!existingNames.contains(base)) { return base; } else { int index = 0; while (existingNames.contains(base + '_' + index)) { ++index; } return base + '_' + index; } }
@Override public Void caseNetwork(Network network) { this.network = network; // make a copy of the existing vertex set because the set returned is // modified when broadcasts are added List<Vertex> vertexSet = new ArrayList<Vertex>(network.getVertices()); for (Vertex vertex : vertexSet) { if (vertex instanceof Network) { new BroadcastAdder().doSwitch(vertex); } else { doSwitch(vertex); } } handle(network); return null; }
protected void createBroadcast(String id, Port port, List<? extends Edge> outList) { // Add broadcast vertex Actor bcast = dfFactory.createActor(); bcast.setName(id + "_" + port.getName()); Instance instance = dfFactory.createInstance(id + "_" + port.getName() + "_bcast", bcast); network.add(instance); Type portVarType = irFactory.createTypeList(1, port.getType()); Pattern inputPattern = dfFactory.createPattern(); Pattern outputPattern = dfFactory.createPattern(); Port input = dfFactory.createPort(EcoreUtil.copy(port.getType()), "input"); bcast.getInputs().add(input); // Creates a connection between the vertex and the broadcast Connection conn = (Connection) outList.get(0); Connection incoming = dfFactory.createConnection( conn.getSource(), conn.getSourcePort(), instance, input, EcoreUtil.copyAll(conn.getAttributes())); incoming.getAttributes().addAll(EcoreUtil.copyAll(conn.getAttributes())); network.getConnections().add(incoming); inputPattern.setNumTokens(input, 1); inputPattern.setVariable(input, irFactory.createVar(portVarType, "input", true, 0)); // Change the source of the other connections int i = 0; for (Edge edge : outList) { Port output = dfFactory.createPort(EcoreUtil.copy(port.getType()), "output_" + i); bcast.getOutputs().add(output); Connection connection = (Connection) edge; connection.setSourcePort(output); connection.setSource(instance); outputPattern.setNumTokens(output, 1); outputPattern.setVariable(output, irFactory.createVar(portVarType, "output_" + i, true, 0)); i++; } // Create body of the broadcast Procedure body = irFactory.createProcedure("copy", 0, irFactory.createTypeVoid()); Var tmpVar = body.newTempLocalVariable(IrUtil.copy(port.getType()), "tmp"); BlockBasic block = IrUtil.getLast(body.getBlocks()); block.add(irFactory.createInstLoad(tmpVar, inputPattern.getVariable(input), 0)); for (Port output : bcast.getOutputs()) { block.add(irFactory.createInstStore(outputPattern.getVariable(output), 0, tmpVar)); } // Create the scheduler Procedure scheduler = irFactory.createProcedure("isSchedulable_copy", 0, irFactory.createTypeBool()); BlockBasic block2 = IrUtil.getLast(scheduler.getBlocks()); block2.add(irFactory.createInstReturn(irFactory.createExprBool(true))); Action copy = dfFactory.createAction( "copy", inputPattern, outputPattern, dfFactory.createPattern(), scheduler, body); bcast.getActions().add(copy); bcast.getActionsOutsideFsm().add(copy); }