Operation Chains

The code for this example is OperationChains.

In the previous examples we have had several steps to get data into Gaffer. As promised, we can now simplify this and use an operation chain.

We will show you a way of using an operation chain to both generate the elements from the data file and add them directly to the graph. Operation chains are simply a list of operations in which the operations are executed sequentially, the output of one operation is passed in as the input to the next operation.

So adding elements from a CSV file can now be done as follows:

final OperationChain<Void> addOpChain = new OperationChain.Builder()
        .first(new GenerateElements.Builder<String>()
                .generator(new RoadAndRoadUseElementGenerator())
                .input(IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath)))
        .then(new AddElements())

graph.execute(addOpChain, user);

This chain consists of 2 operations. The first, GenerateElements, which takes the data and an element generator and generates the Gaffer Edges. The second, AddElements, simply takes the generated edges and adds them to the graph. This operation chain can then be executed on the graph as before.

Another example of using an operation chain is when we are traversing the graph.

final OperationChain<? extends Iterable<? extends Element>> opChain =
        new OperationChain.Builder()
                .first(new GetAdjacentIds.Builder()
                        .input(new EntitySeed("M5"))
                .then(new GetElements.Builder()

final Iterable<? extends Element> results = graph.execute(opChain, user);

This operation chain starts with a seed vertex and traverses down all outgoing edges using the GetAdjacentIds operation. It then returns all the following connected edges using the GetElements operation:


We can continue to add operations to create longer operation chains.

Suppose we want to convert the edges returned back into csv format similar to the input file (junctionA, junctionB, count). Gaffer allows us to convert graph elements back into objects using the GenerateObjects operation. We tell Gaffer how to convert the elements using an ObjectGenerator.

public class RoadUseCsvGenerator implements OneToOneObjectGenerator<String> {
    public String _apply(final Element element) {
        if (!(element instanceof Edge && "RoadUse".equals(element.getGroup()))) {
            throw new UnsupportedOperationException("Only RoadUse edges should be used");

        final Edge edge = ((Edge) element);
        return edge.getSource() + "," + edge.getDestination() + "," + edge.getProperty("count");

Now we'll add the operation to the operation chain we executed before (note that this time the return type has changed from Element to String):

final OperationChain> opChainWithCSV = new OperationChain.Builder() .first(new GetAdjacentIds.Builder() .input(new EntitySeed("M5")) .inOutType(IncludeIncomingOutgoingType.OUTGOING) .build()) .then(new GetElements.Builder() .inOutType(IncludeIncomingOutgoingType.OUTGOING) .build()) .then(new GenerateObjects.Builder() .generator(new RoadUseCsvGenerator()) .build()) .build();

final Iterable<? extends String> csvResults = graph.execute(opChainWithCSV, user);

When we execute this query we get:


You can see the data has been converted back into csv.

Operation chains work with any combination of operations where sequential operations have compatible output/input formats.

For more examples of different types of operations see ../Operation Examples.

results matching ""

    No results matching ""