Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comparing two boolean values and the unary 'not' #217

Open
Pippin555 opened this issue Mar 21, 2021 · 7 comments
Open

comparing two boolean values and the unary 'not' #217

Pippin555 opened this issue Mar 21, 2021 · 7 comments

Comments

@Pippin555
Copy link

This is not an issue, but a request for a smarter solution,

I would like to compare two boolean values with an 'is' operator. But that operator is not available for groovysh_evaluate.

    t = (g.V()
         .limit(1)
         .sack(assign)
         .by(constant(true))
         .sack(is)
         .by(constant(false))
         .sack()
         )

When I try to negate a boolean value, there is no operator for that either.

          .sack(assign)
           .by(constant(true))
           .sack(not)
          .sack()

In the end I wrote a solution with a .choose, comparing with constant(true), see below.

Another solution is to convert boolean to int 0 and 1 and then use .math('x - y'), when that gives 0, the booleans are the same.

I can't believe that there is no shorter way to achieve this.

This determines the exclusive-or for the values in as('x') and as('y'):

        .choose(
                or_(
                    and_(
                        select('x').is(true),
                        select('y').is(true)),
                    and_(
                        select('x').is(false),
                        select('y').is(false))),
                constant(false),
                constant(true))
@Pippin555
Copy link
Author

Pippin555 commented Mar 22, 2021

Just a couple of chapters further on:

             .choose(where('x', neq('y')),
                     constant(true),
                     constant(false))

this solves the first issue:

x y xor
F F F
F T T
T F T
T T F

@krlawrence
Copy link
Owner

You could implement boolean not logic using a Coalesce step

gremlin> g.inject(false).coalesce(is(true).constant(false),is(false).constant(true))
==>true  

@krlawrence
Copy link
Owner

Also I updated the section that talks about choose and option over the weekend. The option step now accepts predicates which may also help in your case.

@Pippin555
Copy link
Author

Pippin555 commented Mar 23, 2021

a small change to your example: 'GraphTraversalSource" object has no attribute 'coalesce', you'll need g.V()

My Python implementation:

    print('option')
    for x in [False, True]:
        t = (g.V()
             .choose(constant(x))
             .option(True, constant(False))
             .option(False, constant(True)))
        r = next(t, None)
        print(f'{x}, {r}')

@krlawrence
Copy link
Owner

krlawrence commented Mar 24, 2021

In my example above the V() is not needed as the inject is there. That turns the source into a Traversal. Keep in mind that g.V() looks at ALL vertices in the graph unless constrained by a filter like has or where.

@Pippin555
Copy link
Author

I used
g.coalesce(constant(true).is(true).constant(false),constant(true))
and that does not work...

About V(): yes, I already have inadvertently doubled all vertices in the database when I used g.V().addV(...) , wondering where all those vertices came from...

on the side:
Could you add in chapter 4.12 a word about where lambda functions are executed? I sent a question about those functions to the makers of ArangoDB Tinkerpop Provider and they asked me whether it would be executed "in the database" (I would say "on the server side") and they do not allow that.

@krlawrence
Copy link
Owner

Now that work has started on a second edition we will take the remaining discussions in this issue as part of that work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants