Skip to content

Commit

Permalink
Implemented a Metaschema module binding-based constraint loader: Bind…
Browse files Browse the repository at this point in the history
…ingConstraintLoader. This loader is capable of loading the legacy external constraint format and the new meta constraint format.
  • Loading branch information
david-waltermire committed Feb 14, 2024
1 parent 08b0517 commit 7b4baf5
Show file tree
Hide file tree
Showing 35 changed files with 763 additions and 134 deletions.
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) {
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 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 @@ -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>
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
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.IFieldDefinition;
import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;
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,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.IFlagDefinition;
import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;
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 @@ -34,7 +34,7 @@

public interface IConstraintSet {
@NonNull
Iterable<ITargetedConstaints> getTargetedConstraintsForModule(@NonNull IModule module);
Iterable<ITargetedConstraints> getTargetedConstraintsForModule(@NonNull IModule module);

@NonNull
Collection<IConstraintSet> getImportedConstraintSets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

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

import gov.nist.secauto.metaschema.core.model.constraint.ICardinalityConstraint;
import gov.nist.secauto.metaschema.core.model.constraint.IIndexConstraint;
import gov.nist.secauto.metaschema.core.model.constraint.IModelConstrained;
import gov.nist.secauto.metaschema.core.model.constraint.IUniqueConstraint;
package gov.nist.secauto.metaschema.core.model.constraint;

import java.util.List;

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

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

import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValuesConstraint;
import gov.nist.secauto.metaschema.core.model.constraint.IConstraint;
import gov.nist.secauto.metaschema.core.model.constraint.IExpectConstraint;
import gov.nist.secauto.metaschema.core.model.constraint.IIndexHasKeyConstraint;
import gov.nist.secauto.metaschema.core.model.constraint.ILet;
import gov.nist.secauto.metaschema.core.model.constraint.IMatchesConstraint;
import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;
package gov.nist.secauto.metaschema.core.model.constraint;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ public interface IScopedContraints {
String getModuleShortName();

@NonNull
List<ITargetedConstaints> getTargetedContraints();
List<ITargetedConstraints> getTargetedContraints();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Represents a set of constraints that target a given definition using a target
* Metapath expression.
*/
public interface ITargetedConstaints extends IValueConstrained {
public interface ITargetedConstraints extends IValueConstrained {
@NonNull
MetapathExpression getTargetExpression();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import gov.nist.secauto.metaschema.core.model.IDefinition;
import gov.nist.secauto.metaschema.core.model.IFieldDefinition;
import gov.nist.secauto.metaschema.core.model.IFlagDefinition;
import gov.nist.secauto.metaschema.core.model.constraint.AbstractTargetedConstraints;
import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;

import java.util.Locale;
Expand Down
Loading

0 comments on commit 7b4baf5

Please sign in to comment.