-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0defaff
commit 1d44bcd
Showing
7 changed files
with
310 additions
and
4 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
stroom-query/stroom-query-language/src/main/java/stroom/query/language/functions/And.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2017 Crown Copyright | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package stroom.query.language.functions; | ||
|
||
import stroom.query.language.functions.ref.StoredValues; | ||
import stroom.query.language.token.Param; | ||
|
||
import java.text.ParseException; | ||
import java.util.function.Supplier; | ||
|
||
@SuppressWarnings("unused") //Used by FunctionFactory | ||
@FunctionDef( | ||
name = And.NAME, | ||
commonCategory = FunctionCategory.LOGIC, | ||
commonReturnType = ValBoolean.class, | ||
commonReturnDescription = "If all supplied arguments evaluate to true then return true else false.", | ||
signatures = @FunctionSignature( | ||
description = "Logical 'and' operator.", | ||
args = { | ||
@FunctionArg( | ||
name = "boolean1", | ||
description = "First boolean argument.", | ||
argType = ValBoolean.class), | ||
@FunctionArg( | ||
name = "booleanN", | ||
description = "Additional boolean arguments.", | ||
argType = ValBoolean.class)})) | ||
class And extends AbstractManyChildFunction { | ||
|
||
static final String NAME = "and"; | ||
|
||
public And(final String name) { | ||
super(name, 1, Integer.MAX_VALUE); | ||
} | ||
|
||
@Override | ||
public void setParams(final Param[] params) throws ParseException { | ||
super.setParams(params); | ||
} | ||
|
||
@Override | ||
protected Generator createGenerator(final Generator[] childGenerators) { | ||
return new Gen(childGenerators); | ||
} | ||
|
||
@Override | ||
public Type getCommonReturnType() { | ||
return Type.BOOLEAN; | ||
} | ||
|
||
private static final class Gen extends AbstractManyChildGenerator { | ||
|
||
Gen(final Generator[] childGenerators) { | ||
super(childGenerators); | ||
} | ||
|
||
@Override | ||
public Val eval(final StoredValues storedValues, final Supplier<ChildData> childDataSupplier) { | ||
try { | ||
for (final Generator generator : childGenerators) { | ||
if (generator == null) { | ||
return ValBoolean.FALSE; | ||
} | ||
final Val val = generator.eval(storedValues, childDataSupplier); | ||
if (val == null || !val.type().isValue() || val.toBoolean() == null || !val.toBoolean()) { | ||
return ValBoolean.FALSE; | ||
} | ||
} | ||
return ValBoolean.TRUE; | ||
} catch (final RuntimeException e) { | ||
return ValErr.create(e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
stroom-query/stroom-query-language/src/main/java/stroom/query/language/functions/Or.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2017 Crown Copyright | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package stroom.query.language.functions; | ||
|
||
import stroom.query.language.functions.ref.StoredValues; | ||
import stroom.query.language.token.Param; | ||
|
||
import java.text.ParseException; | ||
import java.util.function.Supplier; | ||
|
||
@SuppressWarnings("unused") //Used by FunctionFactory | ||
@FunctionDef( | ||
name = Or.NAME, | ||
commonCategory = FunctionCategory.LOGIC, | ||
commonReturnType = ValBoolean.class, | ||
commonReturnDescription = "If one or more of the supplied arguments evaluate to true then return true else false.", | ||
signatures = @FunctionSignature( | ||
description = "Logical 'or' operator.", | ||
args = { | ||
@FunctionArg( | ||
name = "boolean1", | ||
description = "First boolean argument.", | ||
argType = ValBoolean.class), | ||
@FunctionArg( | ||
name = "booleanN", | ||
description = "Additional boolean arguments.", | ||
argType = ValBoolean.class)})) | ||
class Or extends AbstractManyChildFunction { | ||
|
||
static final String NAME = "or"; | ||
|
||
public Or(final String name) { | ||
super(name, 1, Integer.MAX_VALUE); | ||
} | ||
|
||
@Override | ||
public void setParams(final Param[] params) throws ParseException { | ||
super.setParams(params); | ||
} | ||
|
||
@Override | ||
protected Generator createGenerator(final Generator[] childGenerators) { | ||
return new Gen(childGenerators); | ||
} | ||
|
||
@Override | ||
public Type getCommonReturnType() { | ||
return Type.BOOLEAN; | ||
} | ||
|
||
private static final class Gen extends AbstractManyChildGenerator { | ||
|
||
Gen(final Generator[] childGenerators) { | ||
super(childGenerators); | ||
} | ||
|
||
@Override | ||
public Val eval(final StoredValues storedValues, final Supplier<ChildData> childDataSupplier) { | ||
try { | ||
for (final Generator generator : childGenerators) { | ||
if (generator != null) { | ||
final Val val = generator.eval(storedValues, childDataSupplier); | ||
if (val != null && val.type().isValue() && val.toBoolean() != null && val.toBoolean()) { | ||
return ValBoolean.TRUE; | ||
} | ||
} | ||
} | ||
return ValBoolean.FALSE; | ||
} catch (final RuntimeException e) { | ||
return ValErr.create(e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...om-query/stroom-query-language/src/test/java/stroom/query/language/functions/TestAnd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package stroom.query.language.functions; | ||
|
||
import java.util.stream.Stream; | ||
|
||
class TestAnd extends AbstractFunctionTest<And> { | ||
|
||
@Override | ||
Class<And> getFunctionType() { | ||
return And.class; | ||
} | ||
|
||
@Override | ||
Stream<TestCase> getTestCases() { | ||
return Stream.of( | ||
TestCase.of( | ||
"both false", | ||
ValBoolean.FALSE, | ||
ValBoolean.FALSE, | ||
ValBoolean.FALSE), | ||
TestCase.of( | ||
"one false", | ||
ValBoolean.FALSE, | ||
ValBoolean.FALSE, | ||
ValBoolean.TRUE), | ||
TestCase.of( | ||
"both true", | ||
ValBoolean.TRUE, | ||
ValBoolean.TRUE, | ||
ValBoolean.TRUE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
stroom-query/stroom-query-language/src/test/java/stroom/query/language/functions/TestOr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package stroom.query.language.functions; | ||
|
||
import java.util.stream.Stream; | ||
|
||
class TestOr extends AbstractFunctionTest<Or> { | ||
|
||
@Override | ||
Class<Or> getFunctionType() { | ||
return Or.class; | ||
} | ||
|
||
@Override | ||
Stream<TestCase> getTestCases() { | ||
return Stream.of( | ||
TestCase.of( | ||
"both false", | ||
ValBoolean.FALSE, | ||
ValBoolean.FALSE, | ||
ValBoolean.FALSE), | ||
TestCase.of( | ||
"one true", | ||
ValBoolean.TRUE, | ||
ValBoolean.FALSE, | ||
ValBoolean.TRUE), | ||
TestCase.of( | ||
"both true", | ||
ValBoolean.TRUE, | ||
ValBoolean.TRUE, | ||
ValBoolean.TRUE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
* Issue **#3948** : Add `and` and `or` functions. | ||
|
||
|
||
```sh | ||
# ******************************************************************************** | ||
# Issue title: Expressions, how do I implement an `and` or `or` condition | ||
# Issue link: https://github.com/gchq/stroom/issues/3948 | ||
# ******************************************************************************** | ||
|
||
# ONLY the top line will be included as a change entry in the CHANGELOG. | ||
# The entry should be in GitHub flavour markdown and should be written on a SINGLE | ||
# line with no hard breaks. You can have multiple change files for a single GitHub issue. | ||
# The entry should be written in the imperative mood, i.e. 'Fix nasty bug' rather than | ||
# 'Fixed nasty bug'. | ||
# | ||
# Examples of acceptable entries are: | ||
# | ||
# | ||
# * Issue **123** : Fix bug with an associated GitHub issue in this repository | ||
# | ||
# * Issue **namespace/other-repo#456** : Fix bug with an associated GitHub issue in another repository | ||
# | ||
# * Fix bug with no associated GitHub issue. | ||
``` |