Help with trying/understanding Sideeffect in Java

445 views
Skip to first unread message

pShah

unread,
May 10, 2012, 5:40:19 PM5/10/12
to gremli...@googlegroups.com
A blog post by Marko: On the Nature of Pipes

There are side-effect pipes which seem pretty intersting.

However, I get confused with "it" when trying this example in pure Java:
g.v(1).sideEffect{x=it}.out('created').in('created').filter{it != x}

How can we specify in java to say "x=it" and then use it later in the filter "it != x" ?

I have been able to do some of the basic filter pipes and I get that it can be vertex or string, depending on what the previous pipe was, but how do you store something and use it again?

Any help will be great.

pShah

unread,
May 10, 2012, 6:35:57 PM5/10/12
to gremli...@googlegroups.com
I managed to solve it, but appreciate any guidance anyone has.

What I was missing was declaring the variable on the outside.

If anyone needs it:

        List<String> output = new ArrayList();
        final List<Vertex> temp = new ArrayList();
       
        IndexableGraph g = TinkerGraphFactory.createTinkerGraph();
           
        output = new GremlinPipeline(g.getVertex(1))
        .sideEffect(
                new PipeFunction<Vertex, Vertex>() {
                    public Vertex compute(Vertex it) {
                        temp.add(it);
                        return it;
                    }
                }
                )
        .out("created")
        .in("created")
        .filter(
                new PipeFunction<Vertex, Boolean>() {
                    public Boolean compute(Vertex it) {
                        if (temp.contains(it))
                            return false;
                        else
                            return true;
                    }
                }
                )
        .toList();
       
       
        System.out.println(output);

Pierre De Wilde

unread,
May 11, 2012, 1:05:10 AM5/11/12
to gremli...@googlegroups.com
Hey,

In Gremlin Groovy:

g.v(1).sideEffect{x=it}.out('created').in('created').filter{it != x}

may be expressed as

g.v(1).store(x).out('created').in('created').except(x)

assuming x = []

HTH,
Pierre

pShah

unread,
May 11, 2012, 10:25:01 AM5/11/12
to gremli...@googlegroups.com
Thanks Pierre
Reply all
Reply to author
Forward
0 new messages