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

Add metaschema models for constraints #320

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 @@ -745,14 +745,16 @@ protected IExpression handleAndexpr(AndexprContext ctx) {
* ====================================================================
*/

@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override
protected IExpression handleForexpr(ForexprContext ctx) {
SimpleforclauseContext simpleForClause = ctx.simpleforclause();

// for SimpleForBinding ("," SimpleForBinding)*
int bindingCount = simpleForClause.getChildCount() / 2;

@NonNull IExpression retval = ObjectUtils.notNull(ctx.exprsingle().accept(this));
@NonNull
IExpression retval = ObjectUtils.notNull(ctx.exprsingle().accept(this));

// step through in reverse
for (int idx = bindingCount - 1; idx >= 0; idx--) {
Expand Down Expand Up @@ -781,7 +783,8 @@ protected IExpression handleForexpr(ForexprContext ctx) {

@Override
protected IExpression handleLet(LetexprContext context) {
@NonNull IExpression retval = ObjectUtils.notNull(context.exprsingle().accept(this));
@NonNull
IExpression retval = ObjectUtils.notNull(context.exprsingle().accept(this));

SimpleletclauseContext letClause = context.simpleletclause();
List<SimpleletbindingContext> clauses = letClause.simpleletbinding();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import gov.nist.secauto.metaschema.core.model.IFieldDefinition;
import gov.nist.secauto.metaschema.core.model.IFieldInstance;
import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute;
import gov.nist.secauto.metaschema.core.model.IModelInstance;
import gov.nist.secauto.metaschema.core.model.IModule;
import gov.nist.secauto.metaschema.core.model.INamedModelInstance;
import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute;
Expand Down Expand Up @@ -249,18 +250,44 @@ private IAssemblyNodeItem getCycledInstance(
* @return the stream of descendant instances
*/
@NonNull
protected Stream<INamedModelInstanceAbsolute> getNamedModelInstances(@NonNull IContainerModelAbsolute container) {
protected Stream<? extends INamedModelInstance> getNamedModelInstances(@NonNull IContainerModelAbsolute container) {
aj-stein-nist marked this conversation as resolved.
Show resolved Hide resolved
return ObjectUtils.notNull(container.getModelInstances().stream()
.flatMap(instance -> {
Stream<INamedModelInstanceAbsolute> retval;
Stream<? extends INamedModelInstance> retval;
if (instance instanceof IAssemblyInstanceAbsolute || instance instanceof IFieldInstanceAbsolute) {
retval = Stream.of((INamedModelInstanceAbsolute) instance);
} else if (instance instanceof IChoiceInstance) {
// descend into the choice
retval = getNamedModelInstances((IChoiceInstance) instance);
} else if (instance instanceof IChoiceGroupInstance) {
throw new UnsupportedOperationException("implement");
// retval = Stream.of(instance);
IChoiceGroupInstance choiceGroupInstance = (IChoiceGroupInstance) instance;
retval = choiceGroupInstance.getNamedModelInstances().stream();
} else {
throw new UnsupportedOperationException("unsupported instance type: " + instance.getClass().getName());
}
return retval;
}));
}

/**
* Get the descendant model instances of the provided {@code container}.
*
* @param container
* the container to get descendant instances for
* @return the stream of descendant instances
*/
@NonNull
protected Stream<? extends IModelInstance> getValuedModelInstances(@NonNull IContainerModelAbsolute container) {
return ObjectUtils.notNull(container.getModelInstances().stream()
.flatMap(instance -> {
Stream<? extends IModelInstance> retval;
if (instance instanceof IAssemblyInstanceAbsolute || instance instanceof IFieldInstanceAbsolute) {
retval = Stream.of((INamedModelInstanceAbsolute) instance);
} else if (instance instanceof IChoiceInstance) {
// descend into the choice
retval = getNamedModelInstances((IChoiceInstance) instance);
} else if (instance instanceof IChoiceGroupInstance) {
retval = Stream.of((IChoiceGroupInstance) instance);
} else {
throw new UnsupportedOperationException("unsupported instance type: " + instance.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
import gov.nist.secauto.metaschema.core.metapath.item.node.IFeatureFlagContainerItem.FlagContainer;
import gov.nist.secauto.metaschema.core.metapath.item.node.IFeatureModelContainerItem.ModelContainer;
import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped;
import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance;
import gov.nist.secauto.metaschema.core.model.IFlagDefinition;
import gov.nist.secauto.metaschema.core.model.IFlagInstance;
import gov.nist.secauto.metaschema.core.model.IModelInstance;
import gov.nist.secauto.metaschema.core.model.IModule;
import gov.nist.secauto.metaschema.core.model.INamedModelInstance;
import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute;
import gov.nist.secauto.metaschema.core.model.INamedModelInstanceGrouped;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

Expand Down Expand Up @@ -136,18 +139,50 @@ protected Map<String, IFlagNodeItem> generateFlags(@NonNull IModelNodeItem<?, ?>

Object parentValue = parent.getValue();
assert parentValue != null;
for (INamedModelInstanceAbsolute instance : CollectionUtil
.toIterable(getNamedModelInstances(parent.getDefinition()))) {
Object instanceValue = instance.getValue(parentValue);

// the item values will be all non-null items
Stream<? extends Object> itemValues = instance.getItemValues(instanceValue).stream();
AtomicInteger index = new AtomicInteger(); // NOPMD - intentional
List<IModelNodeItem<?, ?>> items = itemValues.map(itemValue -> {
assert itemValue != null;
return newModelItem(instance, parent, index.incrementAndGet(), itemValue);
}).collect(Collectors.toUnmodifiableList());
retval.put(instance.getEffectiveName(), items);
for (IModelInstance instance : CollectionUtil.toIterable(getValuedModelInstances(parent.getDefinition()))) {
if (instance instanceof INamedModelInstanceAbsolute) {
INamedModelInstanceAbsolute namedInstance = (INamedModelInstanceAbsolute) instance;

Object instanceValue = namedInstance.getValue(parentValue);

// the item values will be all non-null items
Stream<? extends Object> itemValues = namedInstance.getItemValues(instanceValue).stream();
AtomicInteger index = new AtomicInteger(); // NOPMD - intentional
List<IModelNodeItem<?, ?>> items = itemValues.map(itemValue -> {
assert itemValue != null;
return newModelItem(namedInstance, parent, index.incrementAndGet(), itemValue);
}).collect(Collectors.toUnmodifiableList());
retval.put(namedInstance.getEffectiveName(), items);
} else if (instance instanceof IChoiceGroupInstance) {
IChoiceGroupInstance choiceInstance = (IChoiceGroupInstance) instance;

Object instanceValue = choiceInstance.getValue(parentValue);

Map<INamedModelInstanceGrouped, List<Object>> instanceMap = choiceInstance.getItemValues(instanceValue).stream()
.map(item -> {
assert item != null;
INamedModelInstanceGrouped itemInstance = choiceInstance.getItemInstance(item);
return Map.entry(itemInstance, item);
})
.collect(Collectors.groupingBy(
entry -> entry.getKey(),
LinkedHashMap::new,
Collectors.mapping(entry -> entry.getValue(), Collectors.toUnmodifiableList())));

for (Map.Entry<INamedModelInstanceGrouped, List<Object>> entry : instanceMap.entrySet()) {
INamedModelInstanceGrouped namedInstance = entry.getKey();
assert namedInstance != null;

AtomicInteger index = new AtomicInteger(); // NOPMD - intentional
List<IModelNodeItem<?, ?>> items = entry.getValue().stream()
.map(itemValue -> {
assert itemValue != null;
return newModelItem(namedInstance, parent, index.incrementAndGet(), itemValue);
}).collect(Collectors.toUnmodifiableList());
retval.put(namedInstance.getEffectiveName(), items);
}
}

}
return retval.isEmpty() ? CollectionUtil.emptyMap() : CollectionUtil.unmodifiableMap(retval);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

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

import gov.nist.secauto.metaschema.core.model.constraint.impl.IFeatureModelConstrained;
import gov.nist.secauto.metaschema.core.model.constraint.IFeatureModelConstrained;

import javax.xml.namespace.QName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ default boolean isEffectiveValueWrappedInXml() {
return true;
}

/**
* Get the named model instance for the provided choice group item.
*
* @param item
* the item to get the instance for
* @return the named model instance for the provided choice group item
*/
@NonNull
default INamedModelInstanceGrouped getItemInstance(@NonNull Object item) {
throw new UnsupportedOperationException("no value");
}

@Override
default MarkupMultiline getRemarks() {
// no remarks
Expand All @@ -93,9 +105,9 @@ default MarkupMultiline getRemarks() {
@SuppressWarnings("null")
@Override
default String toCoordinates() {
return String.format("%s:%s-instance:%s/%s@%d",
getContainingDefinition().getContainingModule().getShortName(),
return String.format("%s-instance:%s:%s/%s@%d",
getModelType().toString().toLowerCase(Locale.ROOT),
getContainingDefinition().getContainingModule().getShortName(),
getContainingDefinition().getName(),
getGroupAsName(),
hashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ default boolean isEffectiveValueWrappedInXml() {
@SuppressWarnings("null")
@Override
default String toCoordinates() {
return String.format("%s:%s-instance:%s@%d",
getContainingDefinition().getContainingModule().getShortName(),
return String.format("%s-instance:%s:%s@%d",
getModelType().toString().toLowerCase(Locale.ROOT),
getContainingDefinition().getContainingModule().getShortName(),
getContainingDefinition().getName(),
hashCode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.model;

import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;

public interface IConstraintLoader extends ILoader<IConstraintSet> {
// no additional methods
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

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

import gov.nist.secauto.metaschema.core.model.constraint.impl.IFeatureValueConstrained;
import gov.nist.secauto.metaschema.core.model.constraint.IFeatureValueConstrained;

import java.util.Locale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ default Object getEffectiveDefaultValue() {
@Override
default String toCoordinates() {
IModule module = getContainingModule();
return String.format("%s:%s-inline-definition:%s@%d",
module.getShortName(),
return String.format("%s-inline-definition:%s:%s/%s@%d",
getModelType().toString().toLowerCase(Locale.ROOT),
module.getShortName(),
getContainingDefinition().getName(),
getName(),
hashCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ default Object getEffectiveDefaultValue() {
@Override
default String toCoordinates() {
IDefinition definition = getDefinition();
return String.format("%s:%s-instance:%s@%d(%d)",
getContainingDefinition().getContainingModule().getShortName(),
return String.format("%s-instance:%s:%s/%s@%d(%d)",
getModelType().toString().toLowerCase(Locale.ROOT),
getContainingDefinition().getContainingModule().getShortName(),
definition.getName(),
getName(),
hashCode(),
definition.hashCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.core.model.constraint.impl;
package gov.nist.secauto.metaschema.core.model.constraint;

import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
import gov.nist.secauto.metaschema.core.model.constraint.ITargetedConstaints;
import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;

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

Expand All @@ -40,7 +38,7 @@
* the Java type of the constraint container
*/
public abstract class AbstractTargetedConstraints<T extends IValueConstrained>
aj-stein-nist marked this conversation as resolved.
Show resolved Hide resolved
implements ITargetedConstaints, IFeatureValueConstrained {
implements ITargetedConstraints, IFeatureValueConstrained {
@NonNull
private final MetapathExpression targetExpression;
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.core.model.constraint.impl;
package gov.nist.secauto.metaschema.core.model.constraint;

import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
import gov.nist.secauto.metaschema.core.model.constraint.IModelConstrained;
import gov.nist.secauto.metaschema.core.model.constraint.impl.AbstractDefinitionTargetedConstraints;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.core.model.constraint.impl;
package gov.nist.secauto.metaschema.core.model.constraint;

import gov.nist.secauto.metaschema.core.model.IModule;
import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
import gov.nist.secauto.metaschema.core.model.constraint.IScopedContraints;
import gov.nist.secauto.metaschema.core.model.constraint.ITargetedConstaints;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

Expand Down Expand Up @@ -105,7 +102,7 @@ public Set<IConstraintSet> getImportedConstraintSets() {
}

@Override
public Iterable<ITargetedConstaints> getTargetedConstraintsForModule(@NonNull IModule module) {
public Iterable<ITargetedConstraints> getTargetedConstraintsForModule(@NonNull IModule module) {
QName qname = module.getQName();

Map<QName, List<IScopedContraints>> map = getScopedContraints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.core.model.constraint.impl;
package gov.nist.secauto.metaschema.core.model.constraint;

import gov.nist.secauto.metaschema.core.model.IModule;
import gov.nist.secauto.metaschema.core.model.constraint.IScopedContraints;
import gov.nist.secauto.metaschema.core.model.constraint.ITargetedConstaints;

import java.net.URI;
import java.util.List;
Expand All @@ -41,7 +39,7 @@ public class DefaultScopedContraints implements IScopedContraints {
@NonNull
private final String shortName;
@NonNull
private final List<ITargetedConstaints> targetedConstraints;
private final List<ITargetedConstraints> targetedConstraints;

/**
* Construct a new set of scoped constraints.
Expand All @@ -58,7 +56,7 @@ public class DefaultScopedContraints implements IScopedContraints {
public DefaultScopedContraints(
@NonNull URI namespace,
@NonNull String shortName,
@NonNull List<ITargetedConstaints> targetedConstraints) {
@NonNull List<ITargetedConstraints> targetedConstraints) {
this.namespace = namespace;
this.shortName = shortName;
this.targetedConstraints = targetedConstraints;
Expand All @@ -75,7 +73,7 @@ public String getModuleShortName() {
}

@Override
public List<ITargetedConstaints> getTargetedContraints() {
public List<ITargetedConstraints> getTargetedContraints() {
return targetedConstraints;
}

Expand Down
Loading
Loading