Skip to content

Commit

Permalink
Added support for the Metapath function map:for-each.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Jan 4, 2025
1 parent b2e2409 commit 9728d2d
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import edu.umd.cs.findbugs.annotations.Nullable;

/**
* Executes an unnamed function call based on a client provided Metapath
* expression that is declared inline within a Metapath expression.
* Executes an unnamed function call based on a client provided Metapath expression that is declared
* inline within a Metapath expression.
*/
public class AnonymousFunctionCall
extends AbstractExpression {
Expand Down Expand Up @@ -146,7 +146,9 @@ 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 @@ -38,30 +38,25 @@ public interface IFunction extends IItem {
*/
enum FunctionProperty {
/**
* Indicates that the function will produce identical results for the same
* arguments (see XPath 3.1 <a href=
* "https://www.w3.org/TR/xpath-functions-31/#dt-deterministic">deterministic</a>).
* If not assigned to a function definition, a function call with the same
* arguments is not guaranteed to produce the same results in the same order for
* subsequent calls within the same execution context.
* Indicates that the function will produce identical results for the same arguments (see XPath 3.1
* <a href= "https://www.w3.org/TR/xpath-functions-31/#dt-deterministic">deterministic</a>). If not
* assigned to a function definition, a function call with the same arguments is not guaranteed to
* produce the same results in the same order for subsequent calls within the same execution
* context.
*/
DETERMINISTIC,
/**
* Indicates that the result of the function depends on property values within
* the static or dynamic context and the provided arguments (see XPath 3.1
* <a href=
* "https://www.w3.org/TR/xpath-functions-31/#dt-context-dependent">context-dependent</a>).
* If not assigned to a function definition, a call will not be affected by the
* property values within the static or dynamic context and will not have any
* arguments.
* Indicates that the result of the function depends on property values within the static or dynamic
* context and the provided arguments (see XPath 3.1
* <a href= "https://www.w3.org/TR/xpath-functions-31/#dt-context-dependent">context-dependent</a>).
* If not assigned to a function definition, a call will not be affected by the property values
* within the static or dynamic context and will not have any arguments.
*/
CONTEXT_DEPENDENT,
/**
* Indicates that the result of the function depends on the current focus (see
* XPath 3.1 <a href=
* "https://www.w3.org/TR/xpath-functions-31/#dt-focus-independent">focus-dependent</a>).
* If not assigned to a function definition, a call will not be affected by the
* current focus.
* Indicates that the result of the function depends on the current focus (see XPath 3.1
* <a href= "https://www.w3.org/TR/xpath-functions-31/#dt-focus-independent">focus-dependent</a>).
* If not assigned to a function definition, a call will not be affected by the current focus.
*/
FOCUS_DEPENDENT,
/**
Expand All @@ -70,6 +65,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 Expand Up @@ -118,8 +123,8 @@ default String getName() {
int arity();

/**
* Determines if the result of the function call will produce identical results
* when provided the same implicit or explicit arguments.
* Determines if the result of the function call will produce identical results when provided the
* same implicit or explicit arguments.
*
* @return {@code true} if function is deterministic or {@code false} otherwise
* @see FunctionProperty#DETERMINISTIC
Expand All @@ -129,11 +134,10 @@ default boolean isDeterministic() {
}

/**
* Determines if the result of the function call depends on property values
* within the static or dynamic context and the provided arguments.
* Determines if the result of the function call depends on property values within the static or
* dynamic context and the provided arguments.
*
* @return {@code true} if function is context dependent or {@code false}
* otherwise
* @return {@code true} if function is context dependent or {@code false} otherwise
* @see FunctionProperty#CONTEXT_DEPENDENT
*/
default boolean isContextDepenent() {
Expand All @@ -143,8 +147,7 @@ default boolean isContextDepenent() {
/**
* Determines if the result of the function call depends on the current focus.
*
* @return {@code true} if function is focus dependent or {@code false}
* otherwise
* @return {@code true} if function is focus dependent or {@code false} otherwise
* @see FunctionProperty#FOCUS_DEPENDENT
*/
default boolean isFocusDependent() {
Expand All @@ -154,8 +157,7 @@ default boolean isFocusDependent() {
/**
* Determines if the final argument can be repeated.
*
* @return {@code true} if the final argument can be repeated or {@code false}
* otherwise
* @return {@code true} if the final argument can be repeated or {@code false} otherwise
* @see FunctionProperty#UNBOUNDED_ARITY
*/
default boolean isArityUnbounded() {
Expand Down Expand Up @@ -242,8 +244,7 @@ static Builder builder() {
* Construct a new function signature builder.
*
* @param staticContext
* the static context used to lookup data types and function
* implementations
* the static context used to lookup data types and function implementations
*
* @return the new builder instance
*/
Expand Down Expand Up @@ -390,8 +391,8 @@ public Builder focusIndependent() {
* Indicate if the last argument can be repeated.
*
* @param allow
* if {@code true} then the the last argument can be repeated an
* unlimited number of times, or {@code false} otherwise
* if {@code true} then the the last argument can be repeated an unlimited number of times,
* or {@code false} otherwise
* @return this builder
*/
@NonNull
Expand Down Expand Up @@ -530,8 +531,7 @@ public Builder argument(@NonNull IArgument argument) {
* Specify the static function to call when executing the function.
*
* @param handler
* a method implementing the {@link IFunctionExecutor} functional
* interface
* a method implementing the {@link IFunctionExecutor} functional interface
* @return this builder
*/
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

/**
* Provides built-in Metapath functions based on the XPath 3.1
* <a href= "https://www.w3.org/TR/xpath-functions-31/">function
* specification</a>.
* <a href= "https://www.w3.org/TR/xpath-functions-31/">function specification</a>.
*/
@SuppressFBWarnings("UWF_UNWRITTEN_FIELD")
public class DefaultFunctionLibrary
Expand Down Expand Up @@ -268,7 +267,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,113 @@
/*
* 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 9728d2d

Please sign in to comment.