You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@metron.apache.org by cestella <gi...@git.apache.org> on 2017/06/02 04:39:59 UTC

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

GitHub user cestella opened a pull request:

    https://github.com/apache/metron/pull/606

    METRON-980: Short circuit operations for Stellar

    ## Contributor Comments
    Stellar does not currently contain short circuit operations. In most languages, this is an important optimization, but for Stellar on Metron, this is a requirement due to the fact that some variables may be null legitimately and we cannot create multi-line conditionals or temporary variables at the moment.
    The short circuit operations supported:
    * short circuited `or` (e.g. `true or FUNC(...)` would never execute `FUNC`)
    * short circuited `and` (e.g. `false and FUNC(...)` would never execute `FUNC`)
    * short circuited if/then/else (e.g. `if true then FUNC(...) else FUNC2(...)` will never call `FUNC2`)
    
    This should be tested entirely via the unit tests for Stellar.  Please review the tests and ensure I've not missed a test case, reviewers.
    
    ## Pull Request Checklist
    
    Thank you for submitting a contribution to Apache Metron.  
    Please refer to our [Development Guidelines](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=61332235) for the complete guide to follow for contributions.  
    Please refer also to our [Build Verification Guidelines](https://cwiki.apache.org/confluence/display/METRON/Verifying+Builds?show-miniview) for complete smoke testing guides.  
    
    
    In order to streamline the review of the contribution we ask you follow these guidelines and ask you to double check the following:
    
    ### For all changes:
    - [x] Is there a JIRA ticket associated with this PR? If not one needs to be created at [Metron Jira](https://issues.apache.org/jira/browse/METRON/?selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel). 
    - [x] Does your PR title start with METRON-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
    - [x] Has your PR been rebased against the latest commit within the target branch (typically master)?
    
    
    ### For code changes:
    - [x] Have you included steps to reproduce the behavior or problem that is being changed or addressed?
    - [x] Have you included steps or a guide to how the change may be verified and tested manually?
    - [x] Have you ensured that the full suite of tests and checks have been executed in the root incubating-metron folder via:
      ```
      mvn -q clean integration-test install && build_utils/verify_licenses.sh 
      ```
    
    - [x] Have you written or updated unit tests and or integration tests to verify your changes?
    - [x] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
    - [x] Have you verified the basic functionality of the build by building and running locally with Vagrant full-dev environment or the equivalent?
    
    ### For documentation related changes:
    - [x] Have you ensured that format looks appropriate for the output in which it is rendered by building and verifying the site-book? If not then run the following commands and the verify changes via `site-book/target/site/index.html`:
    
      ```
      cd site-book
      mvn site
      ```
    
    #### Note:
    Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
    It is also recommended that [travis-ci](https://travis-ci.org) is set up for your personal repository such that your branches are built there before submitting a pull request.
    


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/cestella/incubator-metron short_circuit_stellar

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/metron/pull/606.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #606
    
----
commit c2a65b200c49a0ab748edf116f23a51b4811e66f
Author: cstella <ce...@gmail.com>
Date:   2017-05-15T16:18:07Z

    Added test to verify shortcutting works.

commit 0c86c2a93216b9f0418e22b78de56c0facb6f55a
Author: cstella <ce...@gmail.com>
Date:   2017-05-23T17:59:41Z

    Merge branch 'master' into short_circuit_stellar

commit d038f5c8738b11bba61d3f6cf07abf78b434f883
Author: cstella <ce...@gmail.com>
Date:   2017-06-02T04:21:30Z

    Short circuit operations.

commit ac9ee328ba56296621a982d279a7c356d8a6d82c
Author: cstella <ce...@gmail.com>
Date:   2017-06-02T04:21:54Z

    Merge branch 'master' into short_circuit_stellar

commit 7fe0e9b636e3fc6e7642af36de5d57261f6f3b78
Author: cstella <ce...@gmail.com>
Date:   2017-06-02T04:31:46Z

    Updating some docs.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r120622230
  
    --- Diff: metron-analytics/metron-profiler-client/src/main/java/org/apache/metron/profiler/client/window/WindowProcessor.java ---
    @@ -89,7 +89,7 @@ public void exitIdentifier(WindowParser.IdentifierContext ctx) {
         if(checkForException(ctx)) {
           return;
         }
    -    stack.push(new Token<>(ctx.getText().substring(1), String.class));
    +    stack.push(new Token<>(ctx.getText().substring(1), String.class, null));
    --- End diff --
    
    Yeah, the polymorphic constructor was added after the fact.  I can remove these now.  Good catch.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119838615
  
    --- Diff: metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarTest.java ---
    @@ -33,19 +31,66 @@
     import org.reflections.Reflections;
     import org.reflections.util.ConfigurationBuilder;
     
    -import java.util.ArrayList;
    -import java.util.Arrays;
    -import java.util.Collection;
    -import java.util.HashMap;
    -import java.util.HashSet;
    -import java.util.Map;
    +import java.util.*;
     
     import static org.apache.metron.common.utils.StellarProcessorUtils.run;
     import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate;
     
     @SuppressWarnings("ALL")
     public class StellarTest {
     
    +  @Stellar(
    +          description="throw exception",
    +          name="THROW",
    +          params = {
    +           "message - exception message"
    +          },
    +          returns="nothing"
    +  )
    +  public static class Throw implements StellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      throw new IllegalStateException(Joiner.on(" ").join(args));
    +    }
    +
    +    @Override
    +    public void initialize(Context context) {
    +
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return true;
    +    }
    +  }
    +
    +  @Stellar(
    --- End diff --
    
    Ah yes, will do


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron issue #606: METRON-980: Short circuit operations for Stellar

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on the issue:

    https://github.com/apache/metron/pull/606
  
    Thanks for submitting this, and thanks for the responses to the notes.  I'm +1.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119838557
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/BaseStellarProcessor.java ---
    @@ -163,7 +164,14 @@ public T parse(final String rule, final VariableResolver variableResolver, final
         lexer.removeErrorListeners();
         lexer.addErrorListener(new ErrorListener());
         TokenStream tokens = new CommonTokenStream(lexer);
    -    StellarParser parser = new StellarParser(tokens);
    +    StellarParser parser = new StellarParser(tokens) {
    --- End diff --
    
    Hah nothing to see here.  Forgot to undo a change; will remove :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by ottobackwards <gi...@git.apache.org>.
Github user ottobackwards commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119838139
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/BaseStellarProcessor.java ---
    @@ -163,7 +164,14 @@ public T parse(final String rule, final VariableResolver variableResolver, final
         lexer.removeErrorListeners();
         lexer.addErrorListener(new ErrorListener());
         TokenStream tokens = new CommonTokenStream(lexer);
    -    StellarParser parser = new StellarParser(tokens);
    +    StellarParser parser = new StellarParser(tokens) {
    --- End diff --
    
    What is going on here?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119840555
  
    --- Diff: metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarTest.java ---
    @@ -634,6 +679,40 @@ public void testLogicalFunctions() throws Exception {
         Assert.assertTrue(runPredicate("not(IN_SUBNET(ip_dst_addr, '192.168.0.0/24'))", v-> variableMap.get(v)));
       }
     
    +  @Test
    +  public void testShortCircuit_conditional() throws Exception {
    +    Assert.assertEquals("foo", run("if true then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("true ? 'foo' : THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if false then THROW('exception') else 'foo'", new HashMap<>()));
    +    Assert.assertEquals("foo", run("false ? THROW('exception') : 'foo'", new HashMap<>()));
    +    Assert.assertEquals(true, run("RET_TRUE(if true then 'foo' else THROW('expression'))", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if true or (true or THROW('if exception')) then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if true or (false or THROW('if exception')) then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if NOT(true or (false or THROW('if exception'))) then THROW('expression') else 'foo'", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if NOT('metron' in [ 'metron', 'metronicus'] ) then THROW('expression') else 'foo'", new HashMap<>()));
    +  }
    +
    +  @Test
    +  public void testShortCircuit_boolean() throws Exception {
    +    Assert.assertTrue(runPredicate("'metron' in ['metron', 'metronicus', 'mortron'] or (true or THROW('exception'))", x -> null));
    --- End diff --
    
    You're right, and this is why I shouldn't review things thirty seconds after waking up.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119832707
  
    --- Diff: metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarTest.java ---
    @@ -634,6 +679,40 @@ public void testLogicalFunctions() throws Exception {
         Assert.assertTrue(runPredicate("not(IN_SUBNET(ip_dst_addr, '192.168.0.0/24'))", v-> variableMap.get(v)));
       }
     
    +  @Test
    +  public void testShortCircuit_conditional() throws Exception {
    +    Assert.assertEquals("foo", run("if true then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("true ? 'foo' : THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if false then THROW('exception') else 'foo'", new HashMap<>()));
    +    Assert.assertEquals("foo", run("false ? THROW('exception') : 'foo'", new HashMap<>()));
    +    Assert.assertEquals(true, run("RET_TRUE(if true then 'foo' else THROW('expression'))", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if true or (true or THROW('if exception')) then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if true or (false or THROW('if exception')) then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if NOT(true or (false or THROW('if exception'))) then THROW('expression') else 'foo'", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if NOT('metron' in [ 'metron', 'metronicus'] ) then THROW('expression') else 'foo'", new HashMap<>()));
    +  }
    +
    +  @Test
    +  public void testShortCircuit_boolean() throws Exception {
    +    Assert.assertTrue(runPredicate("'metron' in ['metron', 'metronicus', 'mortron'] or (true or THROW('exception'))", x -> null));
    --- End diff --
    
    Adding a case like:
    `Assert.assertTrue(runPredicate("NOT(true) or THROW('exception'))", x -> null));` doesn't parse and I would expect it to.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/metron/pull/606


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r120926071
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---
    @@ -76,14 +93,80 @@ public Expression(Deque<Token<?>> tokenDeque) {
     
         public Object apply(ExpressionState state) {
           Deque<Token<?>> instanceDeque = new ArrayDeque<>();
    -      for(Iterator<Token<?>> it = getTokenDeque().descendingIterator();it.hasNext();) {
    -        Token<?> token = it.next();
    -        if(token.getUnderlyingType() == DeferredFunction.class) {
    -          DeferredFunction func = (DeferredFunction) token.getValue();
    -          func.apply(instanceDeque, state);
    -        }
    -        else {
    -          instanceDeque.push(token);
    +      {
    +        boolean skipElse = false;
    +        Token<?> token = null;
    +        for (Iterator<Token<?>> it = getTokenDeque().descendingIterator(); it.hasNext(); ) {
    +          token = it.next();
    +          //if we've skipped an else previously, then we need to skip the deferred tokens associated with the else.
    +          if(skipElse && token.getUnderlyingType() == ElseExpr.class) {
    +            while(it.hasNext()) {
    +              token = it.next();
    +              if(token.getUnderlyingType() == EndConditional.class) {
    +                break;
    +              }
    +            }
    +            skipElse = false;
    +          }
    +          /*
    +          curr is the current value on the stack.  This is the non-deferred actual evaluation for this expression
    +          and with the current context.
    +           */
    +          Token<?> curr = instanceDeque.peek();
    +          if( curr != null
    +           && curr.getValue() != null && curr.getValue() instanceof Boolean
    +           && ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType())
    +                  ) {
    +            //if we have a boolean as the current value and the next non-contextual token is a short circuit op
    +            //then we need to short circuit possibly
    +            if(token.getUnderlyingType() == BooleanArg.class) {
    +              if (curr.getMultiArgContext() != null
    +                      && curr.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_OR
    +                      && (Boolean) (curr.getValue())
    +                      ) {
    +                //short circuit the or
    +                FrameContext.Context context = curr.getMultiArgContext();
    +                shortCircuit(it, context);
    +              } else if (curr.getMultiArgContext() != null
    +                      && curr.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_AND
    +                      && !(Boolean) (curr.getValue())
    +                      ) {
    +                //short circuit the and
    +                FrameContext.Context context = curr.getMultiArgContext();
    +                shortCircuit(it, context);
    +              }
    +            }
    +            else if(token.getUnderlyingType() == IfExpr.class) {
    +              //short circuit the if/then/else
    +              instanceDeque.pop();
    +              if((Boolean)curr.getValue()) {
    +                //choose then
    +                skipElse = true;
    +              }
    +              else {
    +                //choose else
    +                while(it.hasNext()) {
    +                  Token<?> t = it.next();
    +                  if(t.getUnderlyingType() == ElseExpr.class) {
    +                    break;
    +                  }
    +                }
    +              }
    +            }
    +          }
    +          if (token.getUnderlyingType() == DeferredFunction.class) {
    +            DeferredFunction func = (DeferredFunction) token.getValue();
    +            func.apply(instanceDeque, state);
    +          }
    +          else if(token.getUnderlyingType() == ShortCircuitFrame.class
    +               || ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType())
    +                  ) {
    +            continue;
    --- End diff --
    
    `continue;` can be dropped here. There's nothing afterwards to skip


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119838484
  
    --- Diff: metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarTest.java ---
    @@ -634,6 +679,40 @@ public void testLogicalFunctions() throws Exception {
         Assert.assertTrue(runPredicate("not(IN_SUBNET(ip_dst_addr, '192.168.0.0/24'))", v-> variableMap.get(v)));
       }
     
    +  @Test
    +  public void testShortCircuit_conditional() throws Exception {
    +    Assert.assertEquals("foo", run("if true then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("true ? 'foo' : THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if false then THROW('exception') else 'foo'", new HashMap<>()));
    +    Assert.assertEquals("foo", run("false ? THROW('exception') : 'foo'", new HashMap<>()));
    +    Assert.assertEquals(true, run("RET_TRUE(if true then 'foo' else THROW('expression'))", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if true or (true or THROW('if exception')) then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if true or (false or THROW('if exception')) then 'foo' else THROW('expression')", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if NOT(true or (false or THROW('if exception'))) then THROW('expression') else 'foo'", new HashMap<>()));
    +    Assert.assertEquals("foo", run("if NOT('metron' in [ 'metron', 'metronicus'] ) then THROW('expression') else 'foo'", new HashMap<>()));
    +  }
    +
    +  @Test
    +  public void testShortCircuit_boolean() throws Exception {
    +    Assert.assertTrue(runPredicate("'metron' in ['metron', 'metronicus', 'mortron'] or (true or THROW('exception'))", x -> null));
    --- End diff --
    
    Isn't that too many closing parens after the throw?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r120623833
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---
    @@ -76,14 +92,80 @@ public Expression(Deque<Token<?>> tokenDeque) {
     
         public Object apply(ExpressionState state) {
           Deque<Token<?>> instanceDeque = new ArrayDeque<>();
    -      for(Iterator<Token<?>> it = getTokenDeque().descendingIterator();it.hasNext();) {
    -        Token<?> token = it.next();
    -        if(token.getUnderlyingType() == DeferredFunction.class) {
    -          DeferredFunction func = (DeferredFunction) token.getValue();
    -          func.apply(instanceDeque, state);
    -        }
    -        else {
    -          instanceDeque.push(token);
    +      {
    +        boolean skipElse = false;
    +        Token<?> token = null;
    +        for (Iterator<Token<?>> it = getTokenDeque().descendingIterator(); it.hasNext(); ) {
    +          token = it.next();
    +          //if we've skipped an else previously, then we need to skip the deferred tokens associated with the else.
    +          if(skipElse && token.getUnderlyingType() == ElseExpr.class) {
    +            while(it.hasNext()) {
    +              token = it.next();
    +              if(token.getUnderlyingType() == EndConditional.class) {
    +                break;
    +              }
    --- End diff --
    
    Yep, I just added this case to the `StellarTest`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r120623722
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---
    @@ -40,14 +40,22 @@
     import static java.lang.String.format;
     
     public class StellarCompiler extends StellarBaseListener {
    -  private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class);
    -  private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class);
    +  private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class, null);
    +  private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class, null);
     
       private Expression expression;
       private final ArithmeticEvaluator arithmeticEvaluator;
       private final NumberLiteralEvaluator numberLiteralEvaluator;
       private final ComparisonExpressionWithOperatorEvaluator comparisonExpressionWithOperatorEvaluator;
     
    +  public static class ShortCircuitFrame {}
    +  public static class ShortCircuitOp {}
    --- End diff --
    
    We are actually instantiating them in this class.  These are used to indicate an instance of a short circuit frame on the stack.  Unlike some of the other marker classes, we actually want separate instances of these because you could have nested frames.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by mattf-horton <gi...@git.apache.org>.
Github user mattf-horton commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119960003
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---
    @@ -76,14 +92,80 @@ public Expression(Deque<Token<?>> tokenDeque) {
     
         public Object apply(ExpressionState state) {
           Deque<Token<?>> instanceDeque = new ArrayDeque<>();
    -      for(Iterator<Token<?>> it = getTokenDeque().descendingIterator();it.hasNext();) {
    -        Token<?> token = it.next();
    -        if(token.getUnderlyingType() == DeferredFunction.class) {
    -          DeferredFunction func = (DeferredFunction) token.getValue();
    -          func.apply(instanceDeque, state);
    -        }
    -        else {
    -          instanceDeque.push(token);
    +      {
    +        boolean skipElse = false;
    +        Token<?> token = null;
    +        for (Iterator<Token<?>> it = getTokenDeque().descendingIterator(); it.hasNext(); ) {
    +          token = it.next();
    +          //if we've skipped an else previously, then we need to skip the deferred tokens associated with the else.
    +          if(skipElse && token.getUnderlyingType() == ElseExpr.class) {
    +            while(it.hasNext()) {
    +              token = it.next();
    +              if(token.getUnderlyingType() == EndConditional.class) {
    +                break;
    +              }
    --- End diff --
    
    So I admit I don't fully understand the token deque, but will this work with nested if/else statements? eg of form
    "if A1 then B1 else (if A2 then B2 else C2)"?  If A1 is true we'll want to skip all of the level-1 else clause, not just C2.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119831834
  
    --- Diff: metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarTest.java ---
    @@ -33,19 +31,66 @@
     import org.reflections.Reflections;
     import org.reflections.util.ConfigurationBuilder;
     
    -import java.util.ArrayList;
    -import java.util.Arrays;
    -import java.util.Collection;
    -import java.util.HashMap;
    -import java.util.HashSet;
    -import java.util.Map;
    +import java.util.*;
     
     import static org.apache.metron.common.utils.StellarProcessorUtils.run;
     import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate;
     
     @SuppressWarnings("ALL")
     public class StellarTest {
     
    +  @Stellar(
    +          description="throw exception",
    +          name="THROW",
    +          params = {
    +           "message - exception message"
    +          },
    +          returns="nothing"
    +  )
    +  public static class Throw implements StellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      throw new IllegalStateException(Joiner.on(" ").join(args));
    +    }
    +
    +    @Override
    +    public void initialize(Context context) {
    +
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return true;
    +    }
    +  }
    +
    +  @Stellar(
    --- End diff --
    
    Could you change the description, params, and returns to not be what's in THROW?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by mattf-horton <gi...@git.apache.org>.
Github user mattf-horton commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119931000
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---
    @@ -40,14 +40,22 @@
     import static java.lang.String.format;
     
     public class StellarCompiler extends StellarBaseListener {
    -  private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class);
    -  private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class);
    +  private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class, null);
    +  private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class, null);
    --- End diff --
    
    Again, don't need to add the explicit null.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by mattf-horton <gi...@git.apache.org>.
Github user mattf-horton commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119928654
  
    --- Diff: metron-platform/metron-common/src/main/antlr4/org/apache/metron/common/stellar/generated/Stellar.g4 ---
    @@ -139,9 +139,21 @@ transformation_expr:
       | in_expr #InExpression
       ;
     
    +if_expr:
    +  logical_expr
    +  ;
    +
    +then_expr:
    +  transformation_expr
    +  ;
    +
    +else_expr:
    +  transformation_expr
    +  ;
    +
     conditional_expr :
    -  logical_expr QUESTION transformation_expr COLON transformation_expr #TernaryFuncWithoutIf
    -  | IF logical_expr THEN transformation_expr ELSE transformation_expr #TernaryFuncWithIf
    +  if_expr QUESTION then_expr COLON else_expr #TernaryFuncWithoutIf
    +  | IF if_expr THEN then_expr ELSE else_expr #TernaryFuncWithIf
    --- End diff --
    
    Good clarification.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by mattf-horton <gi...@git.apache.org>.
Github user mattf-horton commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119948393
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---
    @@ -40,14 +40,22 @@
     import static java.lang.String.format;
     
     public class StellarCompiler extends StellarBaseListener {
    -  private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class);
    -  private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class);
    +  private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class, null);
    +  private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class, null);
     
       private Expression expression;
       private final ArithmeticEvaluator arithmeticEvaluator;
       private final NumberLiteralEvaluator numberLiteralEvaluator;
       private final ComparisonExpressionWithOperatorEvaluator comparisonExpressionWithOperatorEvaluator;
     
    +  public static class ShortCircuitFrame {}
    +  public static class ShortCircuitOp {}
    --- End diff --
    
    Should ShortCircuitOp be an interface instead?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by mattf-horton <gi...@git.apache.org>.
Github user mattf-horton commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r119928459
  
    --- Diff: metron-analytics/metron-profiler-client/src/main/java/org/apache/metron/profiler/client/window/WindowProcessor.java ---
    @@ -89,7 +89,7 @@ public void exitIdentifier(WindowParser.IdentifierContext ctx) {
         if(checkForException(ctx)) {
           return;
         }
    -    stack.push(new Token<>(ctx.getText().substring(1), String.class));
    +    stack.push(new Token<>(ctx.getText().substring(1), String.class, null));
    --- End diff --
    
    Seems unnecessary, it is implicit in the polymorphic constructor for Token<T>.  Or is this intended to be a clarification (although I don't see it)?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r121049130
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---
    @@ -76,14 +93,80 @@ public Expression(Deque<Token<?>> tokenDeque) {
     
         public Object apply(ExpressionState state) {
           Deque<Token<?>> instanceDeque = new ArrayDeque<>();
    -      for(Iterator<Token<?>> it = getTokenDeque().descendingIterator();it.hasNext();) {
    -        Token<?> token = it.next();
    -        if(token.getUnderlyingType() == DeferredFunction.class) {
    -          DeferredFunction func = (DeferredFunction) token.getValue();
    -          func.apply(instanceDeque, state);
    -        }
    -        else {
    -          instanceDeque.push(token);
    +      {
    +        boolean skipElse = false;
    +        Token<?> token = null;
    +        for (Iterator<Token<?>> it = getTokenDeque().descendingIterator(); it.hasNext(); ) {
    +          token = it.next();
    +          //if we've skipped an else previously, then we need to skip the deferred tokens associated with the else.
    +          if(skipElse && token.getUnderlyingType() == ElseExpr.class) {
    +            while(it.hasNext()) {
    +              token = it.next();
    +              if(token.getUnderlyingType() == EndConditional.class) {
    +                break;
    +              }
    +            }
    +            skipElse = false;
    +          }
    +          /*
    +          curr is the current value on the stack.  This is the non-deferred actual evaluation for this expression
    +          and with the current context.
    +           */
    +          Token<?> curr = instanceDeque.peek();
    +          if( curr != null
    +           && curr.getValue() != null && curr.getValue() instanceof Boolean
    +           && ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType())
    +                  ) {
    +            //if we have a boolean as the current value and the next non-contextual token is a short circuit op
    +            //then we need to short circuit possibly
    +            if(token.getUnderlyingType() == BooleanArg.class) {
    +              if (curr.getMultiArgContext() != null
    +                      && curr.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_OR
    +                      && (Boolean) (curr.getValue())
    +                      ) {
    +                //short circuit the or
    +                FrameContext.Context context = curr.getMultiArgContext();
    +                shortCircuit(it, context);
    +              } else if (curr.getMultiArgContext() != null
    +                      && curr.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_AND
    +                      && !(Boolean) (curr.getValue())
    +                      ) {
    +                //short circuit the and
    +                FrameContext.Context context = curr.getMultiArgContext();
    +                shortCircuit(it, context);
    +              }
    +            }
    +            else if(token.getUnderlyingType() == IfExpr.class) {
    +              //short circuit the if/then/else
    +              instanceDeque.pop();
    +              if((Boolean)curr.getValue()) {
    +                //choose then
    +                skipElse = true;
    +              }
    +              else {
    +                //choose else
    +                while(it.hasNext()) {
    +                  Token<?> t = it.next();
    +                  if(t.getUnderlyingType() == ElseExpr.class) {
    +                    break;
    +                  }
    +                }
    +              }
    +            }
    +          }
    +          if (token.getUnderlyingType() == DeferredFunction.class) {
    +            DeferredFunction func = (DeferredFunction) token.getValue();
    +            func.apply(instanceDeque, state);
    +          }
    +          else if(token.getUnderlyingType() == ShortCircuitFrame.class
    +               || ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType())
    +                  ) {
    +            continue;
    --- End diff --
    
    agreed.  I also demorganified it to make it clearer.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r120628075
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---
    @@ -40,14 +40,22 @@
     import static java.lang.String.format;
     
     public class StellarCompiler extends StellarBaseListener {
    -  private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class);
    -  private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class);
    +  private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class, null);
    +  private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class, null);
     
       private Expression expression;
       private final ArithmeticEvaluator arithmeticEvaluator;
       private final NumberLiteralEvaluator numberLiteralEvaluator;
       private final ComparisonExpressionWithOperatorEvaluator comparisonExpressionWithOperatorEvaluator;
     
    +  public static class ShortCircuitFrame {}
    +  public static class ShortCircuitOp {}
    --- End diff --
    
    Sorry, one of them can definitely be an interface.  I made the change.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---