= rename FlowGraph* to GraphDSL

This commit is contained in:
hepin1989(虎鸣) 2016-04-12 20:24:08 +08:00 committed by kerr
parent 66a0aa280d
commit e9670ee91d
19 changed files with 130 additions and 135 deletions

View file

@ -27,14 +27,14 @@ import akka.stream.*;
import akka.stream.javadsl.*;
import akka.testkit.JavaTestKit;
public class FlowGraphDocTest extends AbstractJavaTest {
public class GraphDSLDocTest extends AbstractJavaTest {
static ActorSystem system;
static Materializer mat;
@BeforeClass
public static void setup() {
system = ActorSystem.create("FlowGraphDocTest");
system = ActorSystem.create("GraphDSLDocTest");
mat = ActorMaterializer.create(system);
}
@ -47,7 +47,7 @@ public class FlowGraphDocTest extends AbstractJavaTest {
@Test
public void demonstrateBuildSimpleGraph() throws Exception {
//#simple-flow-graph
//#simple-graph-dsl
final Source<Integer, NotUsed> in = Source.from(Arrays.asList(1, 2, 3, 4, 5));
final Sink<List<String>, CompletionStage<List<String>>> sink = Sink.head();
final Sink<List<Integer>, CompletionStage<List<Integer>>> sink2 = Sink.head();
@ -72,7 +72,7 @@ public class FlowGraphDocTest extends AbstractJavaTest {
builder.from(bcast).via(builder.add(f4)).toFanIn(merge);
return ClosedShape.getInstance();
}));
//#simple-flow-graph
//#simple-graph-dsl
final List<String> list = result.run(mat).toCompletableFuture().get(3, TimeUnit.SECONDS);
final String[] res = list.toArray(new String[] {});
Arrays.sort(res, null);
@ -107,7 +107,7 @@ public class FlowGraphDocTest extends AbstractJavaTest {
@Test
public void demonstrateReusingFlowInGraph() throws Exception {
//#flow-graph-reusing-a-flow
//#graph-dsl-reusing-a-flow
final Sink<Integer, CompletionStage<Integer>> topHeadSink = Sink.head();
final Sink<Integer, CompletionStage<Integer>> bottomHeadSink = Sink.head();
final Flow<Integer, Integer, NotUsed> sharedDoubler = Flow.of(Integer.class).map(elem -> elem * 2);
@ -129,7 +129,7 @@ public class FlowGraphDocTest extends AbstractJavaTest {
}
)
);
//#flow-graph-reusing-a-flow
//#graph-dsl-reusing-a-flow
final Pair<CompletionStage<Integer>, CompletionStage<Integer>> pair = g.run(mat);
assertEquals(Integer.valueOf(2), pair.first().toCompletableFuture().get(3, TimeUnit.SECONDS));
assertEquals(Integer.valueOf(2), pair.second().toCompletableFuture().get(3, TimeUnit.SECONDS));
@ -137,7 +137,7 @@ public class FlowGraphDocTest extends AbstractJavaTest {
@Test
public void demonstrateMatValue() throws Exception {
//#flow-graph-matvalue
//#graph-dsl-matvalue
final Sink<Integer, CompletionStage<Integer>> foldSink = Sink.<Integer, Integer> fold(0, (a, b) -> {
return a + b;
});
@ -152,9 +152,9 @@ public class FlowGraphDocTest extends AbstractJavaTest {
fold.in(),
b.from(b.materializedValue()).via(b.add(flatten)).out());
}));
//#flow-graph-matvalue
//#graph-dsl-matvalue
//#flow-graph-matvalue-cycle
//#graph-dsl-matvalue-cycle
// This cannot produce any value:
final Source<Integer, CompletionStage<Integer>> cyclicSource = Source.fromGraph(
GraphDSL.create(foldSink,
@ -168,6 +168,6 @@ public class FlowGraphDocTest extends AbstractJavaTest {
return SourceShape.of(b.from(b.materializedValue()).via(b.add(flatten)).out());
}));
//#flow-graph-matvalue-cycle
//#graph-dsl-matvalue-cycle
}
}

View file

@ -3,36 +3,35 @@
*/
package docs.stream;
import static org.junit.Assert.assertEquals;
import java.util.*;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import akka.Done;
import akka.NotUsed;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.japi.Pair;
import akka.stream.*;
import akka.stream.javadsl.*;
import akka.testkit.JavaTestKit;
import docs.AbstractJavaTest;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import akka.actor.*;
import akka.japi.Pair;
import akka.stream.*;
import akka.stream.javadsl.*;
import akka.testkit.JavaTestKit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
public class StreamPartialFlowGraphDocTest extends AbstractJavaTest {
import static org.junit.Assert.assertEquals;
public class StreamPartialGraphDSLDocTest extends AbstractJavaTest {
static ActorSystem system;
static Materializer mat;
@BeforeClass
public static void setup() {
system = ActorSystem.create("StreamPartialFlowGraphDocTest");
system = ActorSystem.create("StreamPartialGraphDSLDocTest");
mat = ActorMaterializer.create(system);
}
@ -45,7 +44,7 @@ public class StreamPartialFlowGraphDocTest extends AbstractJavaTest {
@Test
public void demonstrateBuildWithOpenPorts() throws Exception {
//#simple-partial-flow-graph
//#simple-partial-graph-dsl
final Graph<FanInShape2<Integer, Integer, Integer>, NotUsed> zip =
ZipWith.create((Integer left, Integer right) -> Math.max(left, right));
@ -65,7 +64,7 @@ public class StreamPartialFlowGraphDocTest extends AbstractJavaTest {
final RunnableGraph<CompletionStage<Integer>> g =
RunnableGraph.<CompletionStage<Integer>>fromGraph(
GraphDSL.create(resultSink, (builder, sink) -> {
// import the partial flow graph explicitly
// import the partial graph explicitly
final UniformFanInShape<Integer, Integer> pm = builder.add(pickMaxOfThree);
builder.from(builder.add(Source.single(1))).toInlet(pm.in(0));
@ -76,11 +75,11 @@ public class StreamPartialFlowGraphDocTest extends AbstractJavaTest {
}));
final CompletionStage<Integer> max = g.run(mat);
//#simple-partial-flow-graph
//#simple-partial-graph-dsl
assertEquals(Integer.valueOf(3), max.toCompletableFuture().get(3, TimeUnit.SECONDS));
}
//#source-from-partial-flow-graph
//#source-from-partial-graph-dsl
// first create an indefinite source of integer numbers
class Ints implements Iterator<Integer> {
private int next = 0;
@ -93,11 +92,11 @@ public class StreamPartialFlowGraphDocTest extends AbstractJavaTest {
return next++;
}
}
//#source-from-partial-flow-graph
//#source-from-partial-graph-dsl
@Test
public void demonstrateBuildSourceFromPartialFlowGraphCreate() throws Exception {
//#source-from-partial-flow-graph
public void demonstrateBuildSourceFromPartialGraphDSLCreate() throws Exception {
//#source-from-partial-graph-dsl
final Source<Integer, NotUsed> ints = Source.fromIterator(() -> new Ints());
final Source<Pair<Integer, Integer>, NotUsed> pairs = Source.fromGraph(
@ -114,13 +113,13 @@ public class StreamPartialFlowGraphDocTest extends AbstractJavaTest {
final CompletionStage<Pair<Integer, Integer>> firstPair =
pairs.runWith(Sink.<Pair<Integer, Integer>>head(), mat);
//#source-from-partial-flow-graph
//#source-from-partial-graph-dsl
assertEquals(new Pair<>(0, 1), firstPair.toCompletableFuture().get(3, TimeUnit.SECONDS));
}
@Test
public void demonstrateBuildFlowFromPartialFlowGraphCreate() throws Exception {
//#flow-from-partial-flow-graph
public void demonstrateBuildFlowFromPartialGraphDSLCreate() throws Exception {
//#flow-from-partial-graph-dsl
final Flow<Integer, Pair<Integer, String>, NotUsed> pairs = Flow.fromGraph(GraphDSL.create(
b -> {
final UniformFanOutShape<Integer, Integer> bcast = b.add(Broadcast.create(2));
@ -133,11 +132,11 @@ public class StreamPartialFlowGraphDocTest extends AbstractJavaTest {
return FlowShape.of(bcast.in(), zip.out());
}));
//#flow-from-partial-flow-graph
//#flow-from-partial-graph-dsl
final CompletionStage<Pair<Integer, String>> matSink =
//#flow-from-partial-flow-graph
//#flow-from-partial-graph-dsl
Source.single(1).via(pairs).runWith(Sink.<Pair<Integer, String>>head(), mat);
//#flow-from-partial-flow-graph
//#flow-from-partial-graph-dsl
assertEquals(new Pair<>(1, "1"), matSink.toCompletableFuture().get(3, TimeUnit.SECONDS));
}

View file

@ -275,10 +275,10 @@ public class TwitterStreamQuickstartDocTest extends AbstractJavaTest {
}
static abstract class HiddenDefinitions {
//#flow-graph-broadcast
//#graph-dsl-broadcast
Sink<Author, NotUsed> writeAuthors;
Sink<Hashtag, NotUsed> writeHashtags;
//#flow-graph-broadcast
//#graph-dsl-broadcast
}
@Test
@ -286,7 +286,7 @@ public class TwitterStreamQuickstartDocTest extends AbstractJavaTest {
final Sink<Author, CompletionStage<Done>> writeAuthors = Sink.ignore();
final Sink<Hashtag, CompletionStage<Done>> writeHashtags = Sink.ignore();
//#flow-graph-broadcast
//#graph-dsl-broadcast
RunnableGraph.fromGraph(GraphDSL.create(b -> {
final UniformFanOutShape<Tweet, Tweet> bcast = b.add(Broadcast.create(2));
final FlowShape<Tweet, Author> toAuthor =
@ -300,7 +300,7 @@ public class TwitterStreamQuickstartDocTest extends AbstractJavaTest {
b.from(bcast).via(toTags).to(hashtags);
return ClosedShape.getInstance();
})).run(mat);
//#flow-graph-broadcast
//#graph-dsl-broadcast
}
long slowComputation(Tweet t) {

View file

@ -15,7 +15,7 @@ Some graph operations which are common enough and fit the linear style of Flows,
streams, such that the second one is consumed after the first one has completed), may have shorthand methods defined on
:class:`Flow` or :class:`Source` themselves, however you should keep in mind that those are also implemented as graph junctions.
.. _flow-graph-java:
.. _graph-dsl-java:
Constructing Graphs
-------------------
@ -51,7 +51,7 @@ Such graph is simple to translate to the Graph DSL since each linear element cor
and each circle corresponds to either a :class:`Junction` or a :class:`Source` or :class:`Sink` if it is beginning
or ending a :class:`Flow`.
.. includecode:: ../code/docs/stream/FlowGraphDocTest.java#simple-flow-graph
.. includecode:: ../code/docs/stream/GraphDSLDocTest.java#simple-graph-dsl
.. note::
Junction *reference equality* defines *graph node equality* (i.e. the same merge *instance* used in a GraphDSL
@ -75,9 +75,9 @@ In the example below we prepare a graph that consists of two parallel streams,
in which we re-use the same instance of :class:`Flow`, yet it will properly be
materialized as two connections between the corresponding Sources and Sinks:
.. includecode:: ../code/docs/stream/FlowGraphDocTest.java#flow-graph-reusing-a-flow
.. includecode:: ../code/docs/stream/GraphDSLDocTest.java#graph-dsl-reusing-a-flow
.. _partial-flow-graph-java:
.. _partial-graph-dsl-java:
Constructing and combining Partial Graphs
-----------------------------------------
@ -97,7 +97,7 @@ Let's imagine we want to provide users with a specialized element that given 3 i
the greatest int value of each zipped triple. We'll want to expose 3 input ports (unconnected sources) and one output port
(unconnected sink).
.. includecode:: ../code/docs/stream/StreamPartialFlowGraphDocTest.java#simple-partial-flow-graph
.. includecode:: ../code/docs/stream/StreamPartialGraphDSLDocTest.java#simple-partial-graph-dsl
As you can see, first we construct the partial graph that describes how to compute the maximum of two input streams, then
we reuse that twice while constructing the partial graph that extends this to three input streams,
@ -136,12 +136,12 @@ be attached before this Source can run”.
Refer to the example below, in which we create a Source that zips together two numbers, to see this graph
construction in action:
.. includecode:: ../code/docs/stream/StreamPartialFlowGraphDocTest.java#source-from-partial-flow-graph
.. includecode:: ../code/docs/stream/StreamPartialGraphDSLDocTest.java#source-from-partial-graph-dsl
Similarly the same can be done for a ``Sink<T>`` using ``SinkShape.of`` in which case the provided value must be an
``Inlet<T>``. For defining a ``Flow<T>`` we need to expose both an undefined source and sink:
.. includecode:: ../code/docs/stream/StreamPartialFlowGraphDocTest.java#flow-from-partial-flow-graph
.. includecode:: ../code/docs/stream/StreamPartialGraphDSLDocTest.java#flow-from-partial-graph-dsl
Combining Sources and Sinks with simplified API
-----------------------------------------------
@ -150,11 +150,11 @@ There is simplified API you can use to combine sources and sinks with junctions
``Merge<In>`` and ``Concat<A>`` without the need for using the Graph DSL. The combine method takes care of constructing
the necessary graph underneath. In following example we combine two sources into one (fan-in):
.. includecode:: ../code/docs/stream/StreamPartialFlowGraphDocTest.java#source-combine
.. includecode:: ../code/docs/stream/StreamPartialGraphDSLDocTest.java#source-combine
The same can be done for a ``Sink`` but in this case it will be fan-out:
.. includecode:: ../code/docs/stream/StreamPartialFlowGraphDocTest.java#sink-combine
.. includecode:: ../code/docs/stream/StreamPartialGraphDSLDocTest.java#sink-combine
.. _bidi-flow-java:
@ -219,12 +219,12 @@ can be used in the graph as an ordinary source or outlet, and which will eventua
If the materialized value is needed at more than one place, it is possible to call ``materializedValue`` any number of
times to acquire the necessary number of outlets.
.. includecode:: ../code/docs/stream/FlowGraphDocTest.java#flow-graph-matvalue
.. includecode:: ../code/docs/stream/GraphDSLDocTest.java#graph-dsl-matvalue
Be careful not to introduce a cycle where the materialized value actually contributes to the materialized value.
The following example demonstrates a case where the materialized ``CompletionStage`` of a fold is fed back to the fold itself.
.. includecode:: ../code/docs/stream/FlowGraphDocTest.java#flow-graph-matvalue-cycle
.. includecode:: ../code/docs/stream/GraphDSLDocTest.java#graph-dsl-matvalue-cycle
.. _graph-cycles-java:

View file

@ -243,7 +243,7 @@ at the expense of not reading as familiarly as collection transformations.
Graphs are constructed using :class:`GraphDSL` like this:
.. includecode:: ../code/docs/stream/TwitterStreamQuickstartDocTest.java#flow-graph-broadcast
.. includecode:: ../code/docs/stream/TwitterStreamQuickstartDocTest.java#graph-dsl-broadcast
As you can see, we use graph builder ``b`` to construct the graph using ``UniformFanOutShape`` and ``Flow`` s.
@ -257,7 +257,7 @@ Both :class:`Graph` and :class:`RunnableGraph` are *immutable, thread-safe, and
A graph can also have one of several other shapes, with one or more unconnected ports. Having unconnected ports
expresses a graph that is a *partial graph*. Concepts around composing and nesting graphs in large structures are
explained in detail in :ref:`composition-java`. It is also possible to wrap complex computation graphs
as Flows, Sinks or Sources, which will be explained in detail in :ref:`partial-flow-graph-java`.
as Flows, Sinks or Sources, which will be explained in detail in :ref:`partial-graph-dsl-java`.
Back-pressure in action