Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record Metapath expression text #330

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import gov.nist.secauto.metaschema.core.mdm.impl.IDMModelNodeItem;
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IAtomicValuedNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IFieldNodeItem;
import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
import gov.nist.secauto.metaschema.core.model.IFieldDefinition;
Expand All @@ -26,7 +25,7 @@
* is orphaned from a document model.
*/
public interface IDMFieldNodeItem
extends IFieldNodeItem, IDMModelNodeItem<IFieldDefinition, IFieldInstance>, IAtomicValuedNodeItem, IDMNodeItem {
extends IFieldNodeItem, IDMModelNodeItem<IFieldDefinition, IFieldInstance> {
/**
* Create new field node item that is detached from a parent node item.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

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

import gov.nist.secauto.metaschema.core.metapath.item.node.IAtomicValuedNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem;

/**
* Represents a Metapath flag node item that is backed by a simple Metaschema
* module-based data model.
*/
public interface IDMFlagNodeItem extends IFlagNodeItem, IDMNodeItem, IAtomicValuedNodeItem {
public interface IDMFlagNodeItem extends IFlagNodeItem, IDMNodeItem {
// no additional methods
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package gov.nist.secauto.metaschema.core.mdm.impl;

import gov.nist.secauto.metaschema.core.mdm.IDMFieldNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.model.IFieldDefinition;
Expand All @@ -18,7 +17,7 @@
*/
public class ChildFieldNodeItem
extends AbstractDMFieldNodeItem
implements IDMFieldNodeItem, IFeatureChildNodeItem<IAssemblyNodeItem> {
implements IFeatureChildNodeItem<IAssemblyNodeItem> {
@NonNull
private final IFieldInstance instance;
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import gov.nist.secauto.metaschema.core.mdm.IDMFlagNodeItem;
import gov.nist.secauto.metaschema.core.mdm.IDMNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IModelNodeItem;
import gov.nist.secauto.metaschema.core.model.IFlagInstance;
import gov.nist.secauto.metaschema.core.model.IModelDefinition;
Expand All @@ -33,7 +32,7 @@
* the Java type of the instance associated with a Metaschema module
*/
public interface IDMModelNodeItem<D extends IModelDefinition, I extends INamedModelInstance>
extends IModelNodeItem<D, I>, IDMNodeItem, IDefinitionNodeItem<D, I> {
extends IModelNodeItem<D, I>, IDMNodeItem {
/**
* Create and add a new flag to the underlying data model.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,26 @@ public static IItemType lookupItemType(Class<? extends IItem> clazz) {
return retval;
}

/**
* Parse a function name.
* <p>
* This method will attempt to identify the namespace corresponding to a given
* prefix.
* <p>
* The prefix will be resolved using the following lookup order, advancing to
* the next when a {@code null} value is returned:
* <ol>
* <li>Lookup the prefix using the namespaces registered with the static
* context.</li>
* <li>Lookup the prefix in the well-known namespaces.</li>
* </ol>
* If an empty prefix is provided, the
* {@link Builder#defaultFunctionNamespace(String)} namespace will be used.</li>
*
* @param name
* the name
* @return the parsed qualified name
*/
@NonNull
public IEnhancedQName parseFunctionName(@NonNull String name) {
return EQNameFactory.instance().parseName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ public abstract class AbstractBinaryExpression<L extends IExpression, R extends
/**
* Construct a new binary expression.
*
* @param text
* the parsed text of the expression
* @param left
* the first sub-expression to evaluate
* @param right
* the second sub-expression to evaluate
*/
public AbstractBinaryExpression(@NonNull L left, @NonNull R right) {
public AbstractBinaryExpression(@NonNull String text, @NonNull L left, @NonNull R right) {
super(text);
this.left = Objects.requireNonNull(left);
this.right = Objects.requireNonNull(right);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,31 @@

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

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

/**
* A common base class for Metapath expression implementations, providing common
* utility functions.
*/
public abstract class AbstractExpression implements IExpression {
@NonNull
private final String text;

/**
* Construct a new expression.
*
* @param text
* the parsed text of the expression
*/
public AbstractExpression(@NonNull String text) {
this.text = text;
}

@Override
public String getText() {
return text;
}

@Override
public String toString() {
return CSTPrinter.toString(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import gov.nist.secauto.metaschema.core.metapath.cst.math.Multiplication;
import gov.nist.secauto.metaschema.core.metapath.cst.math.Negate;
import gov.nist.secauto.metaschema.core.metapath.cst.math.Subtraction;
import gov.nist.secauto.metaschema.core.metapath.cst.path.Axis;
import gov.nist.secauto.metaschema.core.metapath.cst.path.ContextItem;
import gov.nist.secauto.metaschema.core.metapath.cst.path.FlagStep;
import gov.nist.secauto.metaschema.core.metapath.cst.path.KindNodeTest;
Expand Down Expand Up @@ -165,11 +164,6 @@ public RESULT visitAnd(And expr, CONTEXT context) {
return visitChildren(expr, context);
}

@Override
public RESULT visitAxis(@NonNull Axis expr, @NonNull CONTEXT context) {
return visitChildren(expr, context);
}

@Override
public RESULT visitStep(Step expr, CONTEXT context) {
return visitChildren(expr, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ public abstract class AbstractNAryExpression
/**
* Construct a new n-ary expression.
*
* @param text
* the parsed text of the expression
* @param children
* the sub-expression children
*/
public AbstractNAryExpression(@NonNull List<IExpression> children) {
public AbstractNAryExpression(@NonNull String text, @NonNull List<IExpression> children) {
super(text);
this.children = Objects.requireNonNull(children);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ public abstract class AbstractUnaryExpression
/**
* Construct a new unary expression.
*
* @param text
* the parsed text of the expression
* @param expr
* the single sub-expression
*/
public AbstractUnaryExpression(@NonNull IExpression expr) {
public AbstractUnaryExpression(@NonNull String text, @NonNull IExpression expr) {
super(text);
this.expr = Objects.requireNonNull(expr, "expr");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,14 @@
* expression that is declared inline within a Metapath expression.
*/
public class AnonymousFunctionCall
extends AbstractFunction
implements IExpression, IFunction {
@NonNull
private static final Set<FunctionProperty> PROPERTIES = ObjectUtils.notNull(EnumSet.of(
FunctionProperty.DETERMINISTIC));
@NonNull
private final ISequenceType result;
@NonNull
private final IExpression body;
extends AbstractExpression {
private final AnonymousFunction function;

/**
* Construct a new function call expression.
*
* @param text
* the parsed text of the expression
* @param arguments
* the parameter declarations for the function call
* @param result
Expand All @@ -49,17 +44,26 @@ public class AnonymousFunctionCall
* the Metapath expression that implements the logic of the function
*/
public AnonymousFunctionCall(
@NonNull String text,
@NonNull List<IArgument> arguments,
@NonNull ISequenceType result,
@NonNull IExpression body) {
super("(anonymous)-" + UUID.randomUUID().toString(), "", arguments);
this.result = result;
this.body = body;
super(text);
this.function = new AnonymousFunction(arguments, result, body);
}

/**
* Get the function associated with this Metapath expression.
*
* @return the function
*/
protected AnonymousFunction getFunction() {
return function;
}

@Override
public List<IExpression> getChildren() {
return ObjectUtils.notNull(List.of(body));
return ObjectUtils.notNull(List.of(getFunction().getBody()));
}

@Override
Expand All @@ -74,49 +78,75 @@ public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visit

@Override
public ISequence<?> accept(DynamicContext dynamicContext, ISequence<?> focus) {
return ISequence.of(this);
return ISequence.of(getFunction());
}

@SuppressWarnings("null")
@Override
public String toASTString() {
IFunction function = getFunction();
return String.format("%s[arguments=%s,return=%s]",
getClass().getName(), getName(),
getArguments(),
result.toSignature());
getClass().getName(),
function.getArguments(),
function.getResult().toSignature());
}

@Override
public Set<FunctionProperty> getProperties() {
return PROPERTIES;
}

@Override
public ISequenceType getResult() {
return result;
}
private static final class AnonymousFunction
extends AbstractFunction {
@NonNull
private static final Set<FunctionProperty> PROPERTIES = ObjectUtils.notNull(EnumSet.of(
FunctionProperty.DETERMINISTIC));
@NonNull
private final ISequenceType result;
@NonNull
private final IExpression body;

public AnonymousFunction(
@NonNull List<IArgument> arguments,
@NonNull ISequenceType result,
@NonNull IExpression body) {
super("(anonymous)-" + UUID.randomUUID().toString(), "", arguments);
this.result = result;
this.body = body;
}

@Override
@NonNull
protected ISequence<?> executeInternal(
@NonNull List<ISequence<?>> arguments,
@NonNull DynamicContext dynamicContext,
@Nullable IItem focus) {

DynamicContext subContext = dynamicContext.subContext();
if (arguments.size() != getArguments().size()) {
throw new IllegalArgumentException("Number of arguments does not match the number of parameters.");
@Override
public ISequenceType getResult() {
return result;
}

Iterator<? extends ISequence<?>> args = arguments.iterator();
Iterator<IArgument> params = getArguments().iterator();
while (args.hasNext() && params.hasNext()) {
ISequence<?> sequence = args.next();
IArgument param = params.next();
@NonNull
protected IExpression getBody() {
return body;
}

subContext.bindVariableValue(param.getName(), ObjectUtils.notNull(sequence));
@Override
public Set<FunctionProperty> getProperties() {
return PROPERTIES;
}

return body.accept(subContext, ISequence.of(focus));
@Override
@NonNull
protected ISequence<?> executeInternal(
@NonNull List<ISequence<?>> arguments,
@NonNull DynamicContext dynamicContext,
@Nullable IItem focus) {

DynamicContext subContext = dynamicContext.subContext();
if (arguments.size() != getArguments().size()) {
throw new IllegalArgumentException("Number of arguments does not match the number of parameters.");
}

Iterator<? extends ISequence<?>> args = arguments.iterator();
Iterator<IArgument> params = getArguments().iterator();
while (args.hasNext() && params.hasNext()) {
ISequence<?> sequence = args.next();
IArgument param = params.next();

subContext.bindVariableValue(param.getName(), ObjectUtils.notNull(sequence));
}

return body.accept(subContext, ISequence.of(focus));
}
}
}
Loading
Loading