Skip to content

Commit 37cd30b

Browse files
Added support for the Metapath function map:for-each. Resolves metaschema-framework#301
1 parent b2e2409 commit 37cd30b

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AnonymousFunctionCall.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ protected ISequence<?> executeInternal(
146146
subContext.bindVariableValue(param.getName(), ObjectUtils.notNull(sequence));
147147
}
148148

149-
return body.accept(subContext, ISequence.of(focus));
149+
// the focus is not present according to
150+
// https://www.w3.org/TR/xpath-31/#id-eval-function-call
151+
// paragraph 5.b.ii
152+
return body.accept(subContext, ISequence.empty());
150153
}
151154
}
152155
}

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/IFunction.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ enum FunctionProperty {
7070
UNBOUNDED_ARITY;
7171
}
7272

73+
/**
74+
* Get the type information for this item.
75+
*
76+
* @return the type information
77+
*/
78+
@NonNull
79+
static IItemType type() {
80+
return IItemType.function();
81+
}
82+
7383
@Override
7484
default IItemType getType() {
7585
// TODO: implement this based on the signature

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ public DefaultFunctionLibrary() { // NOPMD - intentional
268268
registerFunction(MapEntry.SIGNATURE);
269269
// https://www.w3.org/TR/xpath-functions-31/#func-map-remove
270270
registerFunction(MapRemove.SIGNATURE);
271-
// P3: https://www.w3.org/TR/xpath-functions-31/#func-map-for-each
271+
// https://www.w3.org/TR/xpath-functions-31/#func-map-for-each
272+
registerFunction(MapForEach.SIGNATURE);
272273

273274
// metapath casting functions
274275
DataTypeService.instance().getDataTypes().stream()
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* SPDX-FileCopyrightText: none
3+
* SPDX-License-Identifier: CC0-1.0
4+
*/
5+
6+
package gov.nist.secauto.metaschema.core.metapath.function.library;
7+
8+
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
9+
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
10+
import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
11+
import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
12+
import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
13+
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
14+
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
15+
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
16+
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem;
17+
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
18+
19+
import java.util.List;
20+
import java.util.function.BiFunction;
21+
22+
import edu.umd.cs.findbugs.annotations.NonNull;
23+
24+
/**
25+
* Implements the XPath 3.1 <a href=
26+
* "https://www.w3.org/TR/xpath-functions-31/#func-map-contains">map:contains</a>
27+
* function.
28+
*/
29+
public final class MapForEach {
30+
private static final String NAME = "for-each";
31+
@NonNull
32+
static final IFunction SIGNATURE = IFunction.builder()
33+
.name(NAME)
34+
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_MAP)
35+
.deterministic()
36+
.contextIndependent()
37+
.focusIndependent()
38+
.argument(IArgument.builder()
39+
.name("map")
40+
.type(IMapItem.type())
41+
.one()
42+
.build())
43+
.argument(IArgument.builder()
44+
.name("action")
45+
.type(IFunction.type())
46+
.one()
47+
.build())
48+
.returnType(IItem.type())
49+
.returnOne()
50+
.functionHandler(MapForEach::execute)
51+
.build();
52+
53+
private MapForEach() {
54+
// disable construction
55+
}
56+
57+
@SuppressWarnings("unused")
58+
@NonNull
59+
private static ISequence<?> execute(@NonNull IFunction function,
60+
@NonNull List<ISequence<?>> arguments,
61+
@NonNull DynamicContext dynamicContext,
62+
IItem focus) {
63+
IMapItem<?> map = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true)));
64+
IFunction action = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true)));
65+
66+
BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> lambda = adaptFunction(action, dynamicContext);
67+
return forEach(map, lambda);
68+
}
69+
70+
/**
71+
* Adapts the provided function call to a lambda expression for use with
72+
* {@link #forEach(IMapItem, BiFunction)}.
73+
*
74+
* @param function
75+
* the function to adapt
76+
* @param dynamicContext
77+
* the dynamic context for use in evaluating the function
78+
* @return
79+
*/
80+
@NonNull
81+
private static BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> adaptFunction(
82+
@NonNull IFunction function,
83+
@NonNull DynamicContext dynamicContext) {
84+
return (key, value) -> {
85+
List<ISequence<?>> arguments = ObjectUtils.notNull(List.of(
86+
ISequence.of(key),
87+
value.toSequence()));
88+
89+
return function.execute(arguments, dynamicContext, ISequence.empty());
90+
};
91+
}
92+
93+
/**
94+
* An implementation of XPath 3.1 <a href=
95+
* "https://www.w3.org/TR/xpath-functions-31/#func-map-for-each">map:for-each</a>.
96+
*
97+
* @param map
98+
* the map of Metapath items that is the target of retrieval
99+
* @param action
100+
* the action to perform on each entry in the map
101+
* @return a stream resulting from concatenating the resulting streams provided
102+
* by the action result
103+
*/
104+
@NonNull
105+
public static ISequence<?> forEach(
106+
@NonNull IMapItem<?> map,
107+
@NonNull BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> action) {
108+
return ISequence.of(ObjectUtils.notNull(map.entrySet().stream()
109+
.flatMap(entry -> {
110+
IAnyAtomicItem key = entry.getKey().getKey();
111+
ISequence<?> values = entry.getValue().contentsAsSequence();
112+
return action.apply(key, values).stream();
113+
})));
114+
}
115+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* SPDX-FileCopyrightText: none
3+
* SPDX-License-Identifier: CC0-1.0
4+
*/
5+
6+
package gov.nist.secauto.metaschema.core.metapath.function.library;
7+
8+
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.entry;
9+
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.integer;
10+
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.map;
11+
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.sequence;
12+
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.string;
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
import gov.nist.secauto.metaschema.core.metapath.ExpressionTestBase;
16+
import gov.nist.secauto.metaschema.core.metapath.IMetapathExpression;
17+
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
18+
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
19+
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.Arguments;
22+
import org.junit.jupiter.params.provider.MethodSource;
23+
24+
import java.util.stream.Stream;
25+
26+
import edu.umd.cs.findbugs.annotations.NonNull;
27+
28+
class MapForEachTest
29+
extends ExpressionTestBase {
30+
31+
private static Stream<Arguments> provideValues() { // NOPMD - false positive
32+
return Stream.of(
33+
Arguments.of(
34+
sequence(integer(1), integer(2)),
35+
"map:for-each(map{1:'yes', 2:'no'}, function($k, $v){$k})"),
36+
Arguments.of(
37+
sequence(string("yes"), string("no")),
38+
"distinct-values(map:for-each(map{1:'yes', 2:'no'}, function($k, $v){$v}))"),
39+
Arguments.of(
40+
sequence(map(entry(string("a"), integer(2)), entry(string("b"), integer(3)))),
41+
"map:merge(map:for-each(map{'a':1, 'b':2}, function($k, $v){map:entry($k, $v+1)}))"));
42+
}
43+
44+
@ParameterizedTest
45+
@MethodSource("provideValues")
46+
void testExpression(@NonNull ISequence<IItem> expected, @NonNull String metapath) {
47+
48+
ISequence<IItem> result = IMetapathExpression.compile(metapath).evaluate(null, newDynamicContext());
49+
assertThat(result).containsAll(expected);
50+
}
51+
}

0 commit comments

Comments
 (0)