@Override public <N extends Oplet<C, P>, C, P> Connector<P> pipe(Connector<C> output, N oplet) { Vertex<N, C, P> pipeVertex = insert(oplet, 1, 1); output.connect(pipeVertex, 0); return pipeVertex.getConnectors().get(0); }
@Override public <N extends Oplet<C, P>, C, P> Connector<P> pipe(Connector<C> output, N oplet) { Vertex<N, C, P> pipeVertex = insert(oplet, 1, 1); output.connect(pipeVertex, 0); return pipeVertex.getConnectors().get(0); }
@Override public <U> TStream<U> fanin(FanIn<T,U> fanin, List<TStream<T>> others) { if (others.isEmpty() || others.size() == 1 && others.contains(this)) throw new IllegalArgumentException("others"); // use pipe() if (new HashSet<>(others).size() != others.size()) throw new IllegalArgumentException("others has dups"); for (TStream<T> other : others) verify(other); others = new ArrayList<>(others); others.add(0, this); Vertex<Oplet<T,U>, T, U> fanInVertex = graph().insert(fanin, others.size(), 1); int inputPort = 0; for (TStream<T> other : others) { @SuppressWarnings("unchecked") ConnectorStream<G,T> cs = (ConnectorStream<G, T>) other; cs.connector.connect(fanInVertex, inputPort++); } return derived(fanInVertex.getConnectors().get(0)); }
@Override public <U> TStream<U> fanin(FanIn<T,U> fanin, List<TStream<T>> others) { if (others.isEmpty() || others.size() == 1 && others.contains(this)) throw new IllegalArgumentException("others"); // use pipe() if (new HashSet<>(others).size() != others.size()) throw new IllegalArgumentException("others has dups"); for (TStream<T> other : others) verify(other); others = new ArrayList<>(others); others.add(0, this); Vertex<Oplet<T,U>, T, U> fanInVertex = graph().insert(fanin, others.size(), 1); int inputPort = 0; for (TStream<T> other : others) { @SuppressWarnings("unchecked") ConnectorStream<G,T> cs = (ConnectorStream<G, T>) other; cs.connector.connect(fanInVertex, inputPort++); } return derived(fanInVertex.getConnectors().get(0)); }
@Override public List<TStream<T>> split(int n, ToIntFunction<T> splitter) { if (n <= 0) throw new IllegalArgumentException("n <= 0"); Split<T> splitOp = new Split<T>(splitter); Vertex<Split<T>, T, T> splitVertex = graph().insert(splitOp, 1, n); connector.connect(splitVertex, 0); List<TStream<T>> outputs = new ArrayList<>(n); for (int i = 0; i < n; i++) { outputs.add(derived(splitVertex.getConnectors().get(i))); } return outputs; }
@Override public List<TStream<T>> split(int n, ToIntFunction<T> splitter) { if (n <= 0) throw new IllegalArgumentException("n <= 0"); Split<T> splitOp = new Split<T>(splitter); Vertex<Split<T>, T, T> splitVertex = graph().insert(splitOp, 1, n); connector.connect(splitVertex, 0); List<TStream<T>> outputs = new ArrayList<>(n); for (int i = 0; i < n; i++) { outputs.add(derived(splitVertex.getConnectors().get(i))); } return outputs; }
@Override public TStream<T> union(Set<TStream<T>> others) { if (others.isEmpty()) return this; if (others.size() == 1 && others.contains(this)) return this; for (TStream<T> other : others) verify(other); // Create a set we can modify and add this stream others = new HashSet<>(others); others.add(this); Union<T> fanInOp = new Union<T>(); Vertex<Union<T>, T, T> fanInVertex = graph().insert(fanInOp, others.size(), 1); int inputPort = 0; for (TStream<T> other : others) { @SuppressWarnings("unchecked") ConnectorStream<G,T> cs = (ConnectorStream<G, T>) other; cs.connector.connect(fanInVertex, inputPort++); } return derived(fanInVertex.getConnectors().get(0)); }
@Override public TSink<T> sink(Sink<T> oplet) { Vertex<Sink<T>, T, Void> sinkVertex = graph().insert(oplet, 1, 0); connector.connect(sinkVertex, 0); return new ConnectorSink<>(this); }
@Override public TStream<T> union(Set<TStream<T>> others) { if (others.isEmpty()) return this; if (others.size() == 1 && others.contains(this)) return this; for (TStream<T> other : others) verify(other); // Create a set we can modify and add this stream others = new HashSet<>(others); others.add(this); Union<T> fanInOp = new Union<T>(); Vertex<Union<T>, T, T> fanInVertex = graph().insert(fanInOp, others.size(), 1); int inputPort = 0; for (TStream<T> other : others) { @SuppressWarnings("unchecked") ConnectorStream<G,T> cs = (ConnectorStream<G, T>) other; cs.connector.connect(fanInVertex, inputPort++); } return derived(fanInVertex.getConnectors().get(0)); }
@Override public TSink<T> sink(Sink<T> oplet) { Vertex<Sink<T>, T, Void> sinkVertex = graph().insert(oplet, 1, 0); connector.connect(sinkVertex, 0); return new ConnectorSink<>(this); }
@Test public void testGraphToJson4() { Graph g = getGraph(); /* /-- V2 * V0(Integer)-- V1(Double)-- FanOut * \-- V3 */ Vertex<TestOp<String, Integer>, String, Integer> v0 = g.insert(new TestOp<>(), 1, 1); Connector<Integer> out0 = v0.getConnectors().get(0); Connector<Double> out1 = g.pipe(out0, new TestOp<Integer, Double>()); Vertex<TestOp<Double, String>, Double, String> v2 = g.insert(new TestOp<Double, String>(), 1, 1); Vertex<TestOp<Double, String>, Double, String> v3 = g.insert(new TestOp<Double, String>(), 1, 1); out1.connect(v2, 0); out1.connect(v3, 0); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(new GraphType(g)); GraphType gt2 = new Gson().fromJson(json, GraphType.class); assertEquals(5, gt2.getVertices().size()); assertEquals(4, gt2.getEdges().size()); }
v.getConnectors().get(0).connect(v2, 0);