[37/50] [abbrv] metron git commit: METRON-1619: Stellar empty collections should be considered false in boolean expressions closes apache/incubator-metron#1064
METRON-1619: Stellar empty collections should be considered false in boolean expressions closes apache/incubator-metron#1064 Project: http://git-wip-us.apache.org/repos/asf/metron/repo Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/c4c790db Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/c4c790db Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/c4c790db Branch: refs/heads/feature/METRON-1554-pcap-query-panel Commit: c4c790dbe4c481c8f19293ef9d6134539674d253 Parents: 81282de Author: cstella Authored: Thu Jun 28 15:57:08 2018 -0400 Committer: cstella Committed: Thu Jun 28 15:57:08 2018 -0400 -- metron-stellar/stellar-common/README.md | 14 .../metron/stellar/common/StellarCompiler.java | 82 +--- .../org/apache/metron/stellar/dsl/Token.java| 1 + .../stellar/dsl/functions/BasicStellarTest.java | 25 ++ .../metron/stellar/dsl/functions/MatchTest.java | 35 + use-cases/forensic_clustering/README.md | 2 +- use-cases/geographic_login_outliers/README.md | 5 +- use-cases/typosquat_detection/README.md | 2 +- 8 files changed, 149 insertions(+), 17 deletions(-) -- http://git-wip-us.apache.org/repos/asf/metron/blob/c4c790db/metron-stellar/stellar-common/README.md -- diff --git a/metron-stellar/stellar-common/README.md b/metron-stellar/stellar-common/README.md index d8d09b4..0dd8e16 100644 --- a/metron-stellar/stellar-common/README.md +++ b/metron-stellar/stellar-common/README.md @@ -54,6 +54,20 @@ The Stellar language supports the following: * The ability to have parenthesis to make order of operations explicit * User defined functions, including Lambda expressions +### Boolean Expressions + +Variables may be used in boolean expressions and variables which are not +explicitly boolean may be interpreted as booleans subject to the +following rules: +* Similar to python and javascript, empty collections (e.g. `[]`) will be + interpreted as `false` +* Similar to python and javascript, missing variables will be + interpreted as `false` +* Variables set to `null` will be interpreted as `false` + +Otherwise, boolean variables will be interpreted as their values +reflect. + ### Stellar Language Keywords The following keywords need to be single quote escaped in order to be used in Stellar expressions: http://git-wip-us.apache.org/repos/asf/metron/blob/c4c790db/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java -- diff --git a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java index 72f0d0a..8a328a2 100644 --- a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java +++ b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java @@ -29,7 +29,10 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; + +import com.google.common.collect.Iterables; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.metron.stellar.common.evaluators.ArithmeticEvaluator; @@ -104,6 +107,52 @@ public class StellarCompiler extends StellarBaseListener { return tokenDeque; } +/** + * When treating empty or missing values as false, we need to ensure we ONLY do so in a conditional context. + * @param tokenValueType + * @return + */ +private boolean isConditionalContext(Class tokenValueType) { + return tokenValueType != null && ( + tokenValueType == BooleanArg.class +|| tokenValueType == IfExpr.class +|| tokenValueType == MatchClauseCheckExpr.class + ); +} + +/** + * Determine if a token and value is an empty list in the appropriate conditional context + * @param token + * @param value + * @return + */ +private boolean isEmptyList(Token token, Object value) { + if(value != null && isConditionalContext(token.getUnderlyingType())) { +if (value instanceof Iterable) { + return Iterables.isEmpty((Iterable) value); +} else if (value instanceof Map) { + return ((Map) value).isEmpty(); +} +else { + return false; +} + }else { +return false; + } +} + +/** + * Determine if a token is missing in a conditional context. + * @param token + * @return + */ +private boolean isBoolean(Token token, Object value)
metron git commit: METRON-1619: Stellar empty collections should be considered false in boolean expressions closes apache/incubator-metron#1064
Repository: metron Updated Branches: refs/heads/master 81282de28 -> c4c790dbe METRON-1619: Stellar empty collections should be considered false in boolean expressions closes apache/incubator-metron#1064 Project: http://git-wip-us.apache.org/repos/asf/metron/repo Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/c4c790db Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/c4c790db Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/c4c790db Branch: refs/heads/master Commit: c4c790dbe4c481c8f19293ef9d6134539674d253 Parents: 81282de Author: cstella Authored: Thu Jun 28 15:57:08 2018 -0400 Committer: cstella Committed: Thu Jun 28 15:57:08 2018 -0400 -- metron-stellar/stellar-common/README.md | 14 .../metron/stellar/common/StellarCompiler.java | 82 +--- .../org/apache/metron/stellar/dsl/Token.java| 1 + .../stellar/dsl/functions/BasicStellarTest.java | 25 ++ .../metron/stellar/dsl/functions/MatchTest.java | 35 + use-cases/forensic_clustering/README.md | 2 +- use-cases/geographic_login_outliers/README.md | 5 +- use-cases/typosquat_detection/README.md | 2 +- 8 files changed, 149 insertions(+), 17 deletions(-) -- http://git-wip-us.apache.org/repos/asf/metron/blob/c4c790db/metron-stellar/stellar-common/README.md -- diff --git a/metron-stellar/stellar-common/README.md b/metron-stellar/stellar-common/README.md index d8d09b4..0dd8e16 100644 --- a/metron-stellar/stellar-common/README.md +++ b/metron-stellar/stellar-common/README.md @@ -54,6 +54,20 @@ The Stellar language supports the following: * The ability to have parenthesis to make order of operations explicit * User defined functions, including Lambda expressions +### Boolean Expressions + +Variables may be used in boolean expressions and variables which are not +explicitly boolean may be interpreted as booleans subject to the +following rules: +* Similar to python and javascript, empty collections (e.g. `[]`) will be + interpreted as `false` +* Similar to python and javascript, missing variables will be + interpreted as `false` +* Variables set to `null` will be interpreted as `false` + +Otherwise, boolean variables will be interpreted as their values +reflect. + ### Stellar Language Keywords The following keywords need to be single quote escaped in order to be used in Stellar expressions: http://git-wip-us.apache.org/repos/asf/metron/blob/c4c790db/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java -- diff --git a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java index 72f0d0a..8a328a2 100644 --- a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java +++ b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/StellarCompiler.java @@ -29,7 +29,10 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; + +import com.google.common.collect.Iterables; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.metron.stellar.common.evaluators.ArithmeticEvaluator; @@ -104,6 +107,52 @@ public class StellarCompiler extends StellarBaseListener { return tokenDeque; } +/** + * When treating empty or missing values as false, we need to ensure we ONLY do so in a conditional context. + * @param tokenValueType + * @return + */ +private boolean isConditionalContext(Class tokenValueType) { + return tokenValueType != null && ( + tokenValueType == BooleanArg.class +|| tokenValueType == IfExpr.class +|| tokenValueType == MatchClauseCheckExpr.class + ); +} + +/** + * Determine if a token and value is an empty list in the appropriate conditional context + * @param token + * @param value + * @return + */ +private boolean isEmptyList(Token token, Object value) { + if(value != null && isConditionalContext(token.getUnderlyingType())) { +if (value instanceof Iterable) { + return Iterables.isEmpty((Iterable) value); +} else if (value instanceof Map) { + return ((Map) value).isEmpty(); +} +else { + return false; +} + }else { +return false; + } +} + +/** + * Determine if a token is missing in a conditional context. + * @param token + * @return + */ +