-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for the Metapath fn:distinct-values function. (#278)
- Loading branch information
1 parent
bcfee31
commit 5410178
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
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
95 changes: 95 additions & 0 deletions
95
...ain/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDistinctValues.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,95 @@ | ||
/* | ||
* SPDX-FileCopyrightText: none | ||
* SPDX-License-Identifier: CC0-1.0 | ||
*/ | ||
|
||
package gov.nist.secauto.metaschema.core.metapath.function.library; | ||
|
||
import gov.nist.secauto.metaschema.core.metapath.DynamicContext; | ||
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; | ||
import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; | ||
import gov.nist.secauto.metaschema.core.metapath.function.IArgument; | ||
import gov.nist.secauto.metaschema.core.metapath.function.IFunction; | ||
import gov.nist.secauto.metaschema.core.metapath.function.InvalidTypeFunctionException; | ||
import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; | ||
import gov.nist.secauto.metaschema.core.metapath.item.IItem; | ||
import gov.nist.secauto.metaschema.core.metapath.item.ISequence; | ||
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; | ||
import gov.nist.secauto.metaschema.core.util.ObjectUtils; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.stream.Stream; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
/** | ||
* /** Implements <a href= | ||
* "https://www.w3.org/TR/xpath-functions-31/#func-distinct-values">fn:distinct-values</a> | ||
* functions. This implementation does not implement the two-arg variant with | ||
* collation at this time. | ||
*/ | ||
public final class FnDistinctValues { | ||
@NonNull | ||
private static final String NAME = "distinct-values"; | ||
@NonNull | ||
static final IFunction SIGNATURE_ONE_ARG = IFunction.builder() | ||
.name(NAME) | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) | ||
.deterministic() | ||
.contextDependent() | ||
.focusIndependent() | ||
.argument(IArgument.builder() | ||
.name("arg") | ||
.type(IAnyAtomicItem.type()) | ||
.zeroOrMore() | ||
.build()) | ||
.returnType(IAnyAtomicItem.type()) | ||
.returnZeroOrMore() | ||
.functionHandler(FnDistinctValues::executeOneArg) | ||
.build(); | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<IAnyAtomicItem> executeOneArg(@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
IItem focus) { | ||
ISequence<IAnyAtomicItem> seq = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); | ||
|
||
return ISequence.of(fnDistinctValues(seq)); | ||
} | ||
|
||
/** | ||
* Get the first occurrence of each distinct value in the provided list. | ||
* <p> | ||
* Based on the XPath 3.1 <a href= | ||
* "https://www.w3.org/TR/xpath-functions-31/#func-distinct-values">fn:distinct-values</a> | ||
* function. | ||
* | ||
* @param values | ||
* the items to get destinct values for | ||
* @return a the list of distinct values | ||
*/ | ||
@NonNull | ||
public static Stream<IAnyAtomicItem> fnDistinctValues(@NonNull List<IAnyAtomicItem> values) { | ||
Set<IAnyAtomicItem> distinctValues = new TreeSet<>(FnDistinctValues::compare); | ||
distinctValues.addAll(values); | ||
return ObjectUtils.notNull(distinctValues.stream()); | ||
} | ||
|
||
private static int compare(@NonNull IAnyAtomicItem item1, @NonNull IAnyAtomicItem item2) { | ||
int retval; | ||
try { | ||
retval = item1.compareTo(item2); | ||
} catch (InvalidTypeFunctionException | InvalidValueForCastFunctionException ex) { | ||
retval = 1; | ||
} | ||
return retval; | ||
} | ||
|
||
private FnDistinctValues() { | ||
// disable construction | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDistinctValuesTest.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,47 @@ | ||
/* | ||
* SPDX-FileCopyrightText: none | ||
* SPDX-License-Identifier: CC0-1.0 | ||
*/ | ||
|
||
package gov.nist.secauto.metaschema.core.metapath.function.library; | ||
|
||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.decimal; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.integer; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.sequence; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.string; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import gov.nist.secauto.metaschema.core.metapath.ExpressionTestBase; | ||
import gov.nist.secauto.metaschema.core.metapath.IMetapathExpression; | ||
import gov.nist.secauto.metaschema.core.metapath.item.ISequence; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
class FnDistinctValuesTest | ||
extends ExpressionTestBase { | ||
private static Stream<Arguments> provideValues() { | ||
return Stream.of( | ||
Arguments.of( | ||
sequence(integer(1), decimal(2.0), integer(3)), | ||
"fn:distinct-values((1, 2.0, 3, 2))"), | ||
Arguments.of( | ||
sequence(string("cherry"), string("plum")), | ||
"fn:distinct-values((meta:string('cherry'),meta:string('plum'),meta:string('plum')))"), | ||
Arguments.of( | ||
sequence(string("a"), integer(2)), | ||
"fn:distinct-values(('a', 2, 'a', 2.0))")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValues") | ||
void test(@NonNull ISequence<?> expected, @NonNull String metapath) { | ||
assertEquals(expected, IMetapathExpression.compile(metapath) | ||
.evaluate(null, newDynamicContext())); | ||
} | ||
} |