Skip to content

Commit

Permalink
Added basic support for array construction. Cleaned up ISequence impl…
Browse files Browse the repository at this point in the history
…ementations, moving getFirstItem to be a method member.
  • Loading branch information
david-waltermire committed May 21, 2024
1 parent d4d2b85 commit cffe487
Show file tree
Hide file tree
Showing 41 changed files with 458 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
* the Java type of the items in a sequence
*/
@SuppressWarnings("PMD.ShortMethodName")
public interface ISequence<ITEM extends IItem> extends List<ITEM>, IArrayMember {
public interface ISequence<ITEM extends IItem> extends List<ITEM>, IArrayMember, IStringValued {
/**
* Get an empty sequence.
*
Expand Down Expand Up @@ -97,6 +97,63 @@ default Iterator<ITEM> iterator() {
@NonNull
Stream<ITEM> stream();

/**
* Retrieves the first item in a sequence. If the sequence is empty, a
* {@code null} result is returned. If requireSingleton is {@code true} and the
* sequence contains more than one item, a {@link TypeMetapathException} is
* thrown.
*
* @param <T>
* the item type to return derived from the provided sequence
* @param items
* the sequence to retrieve the first item from
* @param requireSingleton
* if {@code true} then a {@link TypeMetapathException} is thrown if
* the sequence contains more than one item
* @return {@code null} if the sequence is empty, or the item otherwise
* @throws TypeMetapathException
* if the sequence contains more than one item and requireSingleton is
* {@code true}
*/
static <T extends IItem> T getFirstItem(@NonNull ISequence<T> items, boolean requireSingleton) {
return getFirstItem(items.stream(), requireSingleton);
}

/**
* Retrieves the first item in a sequence. If the sequence is empty, a
* {@code null} result is returned. If requireSingleton is {@code true} and the
* sequence contains more than one item, a {@link TypeMetapathException} is
* thrown.
*
* @param <T>
* the item type to return derived from the provided sequence
* @param items
* the sequence to retrieve the first item from
* @param requireSingleton
* if {@code true} then a {@link TypeMetapathException} is thrown if
* the sequence contains more than one item
* @return {@code null} if the sequence is empty, or the item otherwise
* @throws TypeMetapathException
* if the sequence contains more than one item and requireSingleton is
* {@code true}
*/
static <T extends IItem> T getFirstItem(@NonNull Stream<T> items, boolean requireSingleton) {
return items.limit(2)
.reduce((t, u) -> {
if (requireSingleton) {
throw new InvalidTypeMetapathException(
null,
String.format("sequence expected to contain only one item, but found multiple"));
}
return t;
}).orElse(null);
}

@Nullable
default ITEM getFirstItem(boolean requireSingleton) {
return getFirstItem(this, requireSingleton);
}

/**
* Get a stream guaranteed to be backed by a list.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Portions of this software was developed by employees of the National Institute
* of Standards and Technology (NIST), an agency of the Federal Government and is
* being made available as a public service. Pursuant to title 17 United States
* Code Section 105, works of NIST employees are not subject to copyright
* protection in the United States. This software may be subject to foreign
* copyright. Permission in the United States and in foreign countries, to the
* extent that NIST may hold copyright, to use, copy, modify, create derivative
* works, and distribute this software and its documentation without fee is hereby
* granted on a non-exclusive basis, provided that this notice and disclaimer
* of warranty appears in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.core.metapath;

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

public interface IStringValued {
/**
* Get the string value.
*
* @return the string value
*/
@NonNull
String asString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public final class MetapathConstants {
public static final URI NS_METAPATH_FUNCTIONS_MATH = ObjectUtils.requireNonNull(
URI.create("http://csrc.nist.gov/ns/metaschema/metapath-functions/math"));
@NonNull
public static final URI NS_METAPATH_FUNCTIONS_ARRAY = ObjectUtils.requireNonNull(
URI.create("http://csrc.nist.gov/ns/metaschema/metapath-functions/array"));
@NonNull
public static final URI NS_METAPATH_FUNCTIONS_EXTENDED = NS_METAPATH;

@NonNull
Expand All @@ -62,9 +65,10 @@ public final class MetapathConstants {
public static final String PREFIX_XPATH_FUNCTIONS = "mp";
@NonNull
public static final String PREFIX_XPATH_FUNCTIONS_MATH = "math";
@NonNull
public static final String PREFIX_XPATH_FUNCTIONS_ARRAY = "array";

private MetapathConstants() {
// disable construction
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ protected <T> T toResultType(@NonNull ISequence<?> sequence, @NonNull ResultType
result = FnBoolean.fnBoolean(sequence).toBoolean();
break;
case NODE:
result = FunctionUtils.getFirstItem(sequence, true);
result = sequence.getFirstItem(true);
break;
case NUMBER:
INumericItem numeric = FunctionUtils.toNumeric(sequence, true);
Expand All @@ -356,8 +356,8 @@ protected <T> T toResultType(@NonNull ISequence<?> sequence, @NonNull ResultType
result = sequence;
break;
case STRING:
IItem item = FunctionUtils.getFirstItem(sequence, true);
result = item == null ? "" : FnData.fnDataItem(item).asString();
IAnyAtomicItem item = FnData.fnData(sequence).getFirstItem(true);
result = item == null ? "" : item.asString();
break;
default:
throw new InvalidTypeMetapathException(null, String.format("unsupported result type '%s'", resultType.name()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public final class StaticContext {
knownNamespaces.put(
MetapathConstants.PREFIX_XPATH_FUNCTIONS_MATH,
MetapathConstants.NS_METAPATH_FUNCTIONS_MATH);
knownNamespaces.put(
MetapathConstants.PREFIX_XPATH_FUNCTIONS_ARRAY,
MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY);
WELL_KNOWN_NAMESPACES = CollectionUtil.unmodifiableMap(knownNamespaces);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.TypeMetapathException;
import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
import gov.nist.secauto.metaschema.core.metapath.function.library.FnData;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;

import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -54,9 +52,7 @@ public abstract class AbstractExpression implements IExpression {
@Nullable
public static IAnyAtomicItem getFirstDataItem(@NonNull ISequence<?> sequence,
boolean requireSingleton) {
IItem item = FunctionUtils.getFirstItem(sequence, requireSingleton);

return item == null ? null : FnData.fnDataItem(item);
return FnData.fnData(sequence).getFirstItem(requireSingleton);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ protected IExpression handleLet(LetexprContext context) {

@Override
protected IExpression handleArrayConstructor(SquarearrayconstructorContext context) {
if (context.getChildCount() == 2) {
// empty
return new ArrayMembers(CollectionUtil.emptyList());
}

return nAiryToCollection(context, 1, 2,
(ctx, idx) -> {
int pos = (idx - 1) / 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -43,6 +44,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand Down Expand Up @@ -205,7 +207,7 @@ public static List<ISequence<?>> convertArguments(
String.format("a sequence of one expected, but found '%d'", size));
}

IItem item = FunctionUtils.getFirstItem(parameter, true);
IItem item = parameter.getFirstItem(true);
parameter = item == null ? ISequence.empty() : ISequence.of(item);
break;
}
Expand All @@ -216,7 +218,7 @@ public static List<ISequence<?>> convertArguments(
String.format("a sequence of zero or one expected, but found '%d'", size));
}

IItem item = FunctionUtils.getFirstItem(parameter, false);
IItem item = parameter.getFirstItem(false);
parameter = item == null ? ISequence.empty() : ISequence.of(item);
break;
}
Expand Down Expand Up @@ -279,35 +281,34 @@ protected static ISequence<?> convertSequence(@NonNull IArgument argument, @NonN
ISequenceType requiredSequenceType = argument.getSequenceType();
Class<? extends IItem> requiredSequenceTypeClass = requiredSequenceType.getType();

List<IItem> result = new ArrayList<>(sequence.size());
Stream<? extends IItem> stream = sequence.safeStream();

boolean atomize = IAnyAtomicItem.class.isAssignableFrom(requiredSequenceTypeClass);
if (IAnyAtomicItem.class.isAssignableFrom(requiredSequenceTypeClass)) {
Stream<? extends IAnyAtomicItem> atomicStream = stream.flatMap(item -> FnData.atomize(item));

for (IItem item : sequence.getValue()) {
assert item != null;
if (atomize) {
item = FnData.fnDataItem(item); // NOPMD - intentional

// if (IUntypedAtomicItem.class.isInstance(item)) { // NOPMD
// // TODO: apply cast to atomic type
// }
// if (IUntypedAtomicItem.class.isInstance(item)) { // NOPMD
// // TODO: apply cast to atomic type
// }

if (IStringItem.class.equals(requiredSequenceTypeClass)) {
// promote URIs to strings if a string is required
if (IStringItem.class.equals(requiredSequenceTypeClass) && IAnyUriItem.class.isInstance(item)) {
item = IStringItem.cast((IAnyUriItem) item); // NOPMD - intentional
}
atomicStream = atomicStream.map(item -> IAnyUriItem.class.isInstance(item) ? IStringItem.cast(item) : item);
}

// item = requiredSequenceType.
stream = atomicStream;
}

stream = stream.peek(item -> {
if (!requiredSequenceTypeClass.isInstance(item)) {
throw new InvalidTypeMetapathException(
item,
String.format("The type '%s' is not a subtype of '%s'", item.getClass().getName(),
String.format("The type '%s' is not a subtype of '%s'",
item.getClass().getName(),
requiredSequenceTypeClass.getName()));
}
result.add(item);
}
retval = ISequence.of(result);
});

retval = ISequence.of(stream);
}
return retval;
}
Expand All @@ -320,7 +321,7 @@ public ISequence<?> execute(
try {
List<ISequence<?>> convertedArguments = convertArguments(this, arguments);

IItem contextItem = isFocusDepenent() ? FunctionUtils.requireFirstItem(focus, true) : null;
IItem contextItem = isFocusDepenent() ? ObjectUtils.requireNonNull(focus.getFirstItem(true)) : null;

CallingContext callingContext = null;
ISequence<?> result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,74 +112,6 @@ public static long asLong(@NonNull BigInteger value) {
return value.longValueExact();
}

/**
* Retrieves the first item in a sequence. If the sequence is empty, a
* {@link TypeMetapathException} exception is thrown. If requireSingleton is
* {@code true} and the sequence contains more than one item, a
* {@link TypeMetapathException} is thrown.
*
* @param <ITEM>
* the item type to return derived from the provided sequence
* @param sequence
* the sequence to retrieve the first item from
* @param requireSingleton
* if {@code true} then a {@link TypeMetapathException} is thrown if
* the sequence contains more than one item
* @return {@code null} if the sequence is empty, or the item otherwise
* @throws TypeMetapathException
* if the sequence is empty, or contains more than one item and
* requireSingleton is {@code true}
*/
@NonNull
public static <ITEM extends IItem> ITEM requireFirstItem(@NonNull ISequence<ITEM> sequence,
boolean requireSingleton) {
if (sequence.isEmpty()) {
throw new InvalidTypeMetapathException(
null,
"Expected a non-empty sequence, but sequence was empty.");
}
List<ITEM> items = sequence.getValue();
if (requireSingleton && items.size() != 1) {
throw new InvalidTypeMetapathException(
null,
String.format("sequence expected to contain one item, but found '%d'", items.size()));
}
return ObjectUtils.notNull(items.iterator().next());
}

/**
* Retrieves the first item in a sequence. If the sequence is empty, a
* {@code null} result is returned. If requireSingleton is {@code true} and the
* sequence contains more than one item, a {@link TypeMetapathException} is
* thrown.
*
* @param <ITEM>
* the item type to return derived from the provided sequence
* @param sequence
* the sequence to retrieve the first item from
* @param requireSingleton
* if {@code true} then a {@link TypeMetapathException} is thrown if
* the sequence contains more than one item
* @return {@code null} if the sequence is empty, or the item otherwise
* @throws TypeMetapathException
* if the sequence contains more than one item and requireSingleton is
* {@code true}
*/
@Nullable
public static <ITEM extends IItem> ITEM getFirstItem(@NonNull ISequence<ITEM> sequence, boolean requireSingleton) {
@Nullable ITEM retval = null;
if (!sequence.isEmpty()) {
List<ITEM> items = sequence.getValue();
if (requireSingleton && items.size() != 1) {
throw new InvalidTypeMetapathException(
null,
String.format("sequence expected to contain one item, but found '%d'", items.size()));
}
retval = items.iterator().next();
}
return retval;
}

/**
* Gets the first item of the provided sequence as a {@link INumericItem} value.
* If the sequence is empty, then a {@code null} value is returned.
Expand All @@ -198,7 +130,7 @@ public static <ITEM extends IItem> ITEM getFirstItem(@NonNull ISequence<ITEM> se
*/
@Nullable
public static INumericItem toNumeric(@NonNull ISequence<?> sequence, boolean requireSingleton) {
IItem item = getFirstItem(sequence, requireSingleton);
IItem item = sequence.getFirstItem(requireSingleton);
return item == null ? null : toNumeric(item);
}

Expand All @@ -215,7 +147,7 @@ public static INumericItem toNumeric(@NonNull ISequence<?> sequence, boolean req
@NonNull
public static INumericItem toNumeric(@NonNull IItem item) {
// atomize
IAnyAtomicItem atomicItem = FnData.fnDataItem(item);
IAnyAtomicItem atomicItem = ISequence.getFirstItem(FnData.atomize(item), true);
return toNumeric(atomicItem);
}

Expand All @@ -229,7 +161,7 @@ public static INumericItem toNumeric(@NonNull IItem item) {
* if the item cannot be cast to a numeric value
*/
@NonNull
public static INumericItem toNumeric(@NonNull IAnyAtomicItem item) {
public static INumericItem toNumeric(@Nullable IAnyAtomicItem item) {
try {
return IDecimalItem.cast(item);
} catch (InvalidValueForCastFunctionException ex) {
Expand Down
Loading

0 comments on commit cffe487

Please sign in to comment.