Skip to content

Commit

Permalink
Added support for the Metapath function map:for-each. Resolves metasc…
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Jan 4, 2025
1 parent b2e2409 commit 37cd30b
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ protected ISequence<?> executeInternal(
subContext.bindVariableValue(param.getName(), ObjectUtils.notNull(sequence));
}

return body.accept(subContext, ISequence.of(focus));
// the focus is not present according to
// https://www.w3.org/TR/xpath-31/#id-eval-function-call
// paragraph 5.b.ii
return body.accept(subContext, ISequence.empty());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ enum FunctionProperty {
UNBOUNDED_ARITY;
}

/**
* Get the type information for this item.
*
* @return the type information
*/
@NonNull
static IItemType type() {
return IItemType.function();
}

@Override
default IItemType getType() {
// TODO: implement this based on the signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ public DefaultFunctionLibrary() { // NOPMD - intentional
registerFunction(MapEntry.SIGNATURE);
// https://www.w3.org/TR/xpath-functions-31/#func-map-remove
registerFunction(MapRemove.SIGNATURE);
// P3: https://www.w3.org/TR/xpath-functions-31/#func-map-for-each
// https://www.w3.org/TR/xpath-functions-31/#func-map-for-each
registerFunction(MapForEach.SIGNATURE);

// metapath casting functions
DataTypeService.instance().getDataTypes().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.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.metapath.item.function.IMapItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.List;
import java.util.function.BiFunction;

import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Implements the XPath 3.1 <a href=
* "https://www.w3.org/TR/xpath-functions-31/#func-map-contains">map:contains</a>
* function.
*/
public final class MapForEach {
private static final String NAME = "for-each";
@NonNull
static final IFunction SIGNATURE = IFunction.builder()
.name(NAME)
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_MAP)
.deterministic()
.contextIndependent()
.focusIndependent()
.argument(IArgument.builder()
.name("map")
.type(IMapItem.type())
.one()
.build())
.argument(IArgument.builder()
.name("action")
.type(IFunction.type())
.one()
.build())
.returnType(IItem.type())
.returnOne()
.functionHandler(MapForEach::execute)
.build();

private MapForEach() {
// disable construction
}

@SuppressWarnings("unused")
@NonNull
private static ISequence<?> execute(@NonNull IFunction function,
@NonNull List<ISequence<?>> arguments,
@NonNull DynamicContext dynamicContext,
IItem focus) {
IMapItem<?> map = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true)));
IFunction action = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true)));

BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> lambda = adaptFunction(action, dynamicContext);
return forEach(map, lambda);
}

/**
* Adapts the provided function call to a lambda expression for use with
* {@link #forEach(IMapItem, BiFunction)}.
*
* @param function
* the function to adapt
* @param dynamicContext
* the dynamic context for use in evaluating the function
* @return
*/
@NonNull
private static BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> adaptFunction(
@NonNull IFunction function,
@NonNull DynamicContext dynamicContext) {
return (key, value) -> {
List<ISequence<?>> arguments = ObjectUtils.notNull(List.of(
ISequence.of(key),
value.toSequence()));

return function.execute(arguments, dynamicContext, ISequence.empty());
};
}

/**
* An implementation of XPath 3.1 <a href=
* "https://www.w3.org/TR/xpath-functions-31/#func-map-for-each">map:for-each</a>.
*
* @param map
* the map of Metapath items that is the target of retrieval
* @param action
* the action to perform on each entry in the map
* @return a stream resulting from concatenating the resulting streams provided
* by the action result
*/
@NonNull
public static ISequence<?> forEach(
@NonNull IMapItem<?> map,
@NonNull BiFunction<IAnyAtomicItem, ISequence<?>, ISequence<?>> action) {
return ISequence.of(ObjectUtils.notNull(map.entrySet().stream()
.flatMap(entry -> {
IAnyAtomicItem key = entry.getKey().getKey();
ISequence<?> values = entry.getValue().contentsAsSequence();
return action.apply(key, values).stream();
})));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.entry;
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.integer;
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.map;
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.sequence;
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.string;
import static org.assertj.core.api.Assertions.assertThat;

import gov.nist.secauto.metaschema.core.metapath.ExpressionTestBase;
import gov.nist.secauto.metaschema.core.metapath.IMetapathExpression;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
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 MapForEachTest
extends ExpressionTestBase {

private static Stream<Arguments> provideValues() { // NOPMD - false positive
return Stream.of(
Arguments.of(
sequence(integer(1), integer(2)),
"map:for-each(map{1:'yes', 2:'no'}, function($k, $v){$k})"),
Arguments.of(
sequence(string("yes"), string("no")),
"distinct-values(map:for-each(map{1:'yes', 2:'no'}, function($k, $v){$v}))"),
Arguments.of(
sequence(map(entry(string("a"), integer(2)), entry(string("b"), integer(3)))),
"map:merge(map:for-each(map{'a':1, 'b':2}, function($k, $v){map:entry($k, $v+1)}))"));
}

@ParameterizedTest
@MethodSource("provideValues")
void testExpression(@NonNull ISequence<IItem> expected, @NonNull String metapath) {

ISequence<IItem> result = IMetapathExpression.compile(metapath).evaluate(null, newDynamicContext());
assertThat(result).containsAll(expected);
}
}

0 comments on commit 37cd30b

Please sign in to comment.