IConstraintSet.java

  1. /*
  2.  * Portions of this software was developed by employees of the National Institute
  3.  * of Standards and Technology (NIST), an agency of the Federal Government and is
  4.  * being made available as a public service. Pursuant to title 17 United States
  5.  * Code Section 105, works of NIST employees are not subject to copyright
  6.  * protection in the United States. This software may be subject to foreign
  7.  * copyright. Permission in the United States and in foreign countries, to the
  8.  * extent that NIST may hold copyright, to use, copy, modify, create derivative
  9.  * works, and distribute this software and its documentation without fee is hereby
  10.  * granted on a non-exclusive basis, provided that this notice and disclaimer
  11.  * of warranty appears in all copies.
  12.  *
  13.  * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
  14.  * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
  15.  * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
  16.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
  17.  * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
  18.  * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
  19.  * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
  20.  * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
  21.  * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
  22.  * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
  23.  * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
  24.  * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
  25.  */

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

  27. import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
  28. import gov.nist.secauto.metaschema.core.metapath.MetapathExpression.ResultType;
  29. import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem;
  30. import gov.nist.secauto.metaschema.core.metapath.item.node.IModuleNodeItem;
  31. import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
  32. import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory;
  33. import gov.nist.secauto.metaschema.core.model.IDefinition;
  34. import gov.nist.secauto.metaschema.core.model.IModule;
  35. import gov.nist.secauto.metaschema.core.model.MetaschemaException;
  36. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  37. import java.util.Collection;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.Set;
  41. import java.util.stream.Collectors;
  42. import java.util.stream.Stream;

  43. import javax.xml.namespace.QName;

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

  45. public interface IConstraintSet {

  46.   @NonNull
  47.   static Set<IConstraintSet> resolveConstraintSets(@NonNull Set<IConstraintSet> constraintSets) {
  48.     return ObjectUtils.notNull(constraintSets.stream()
  49.         .flatMap(set -> resolveConstraintSet(ObjectUtils.notNull(set)))
  50.         .distinct()
  51.         .collect(Collectors.toUnmodifiableSet()));
  52.   }

  53.   @NonNull
  54.   private static Stream<IConstraintSet> resolveConstraintSet(@NonNull IConstraintSet constraintSet) {
  55.     return ObjectUtils.notNull(Stream.concat(
  56.         Stream.of(constraintSet),
  57.         constraintSet.getImportedConstraintSets().stream()));
  58.   }

  59.   @NonNull
  60.   static List<ITargetedConstaints> getTargetedConstraintsForMetaschema(
  61.       @NonNull Set<IConstraintSet> constraintSets,
  62.       @NonNull IModule module) {
  63.     return ObjectUtils.notNull(resolveConstraintSets(constraintSets).stream()
  64.         .flatMap(set -> set.getTargetedConstraintsForModule(module))
  65.         .collect(Collectors.toUnmodifiableList()));
  66.   }

  67.   @NonNull
  68.   Stream<ITargetedConstaints> getTargetedConstraintsForModule(@NonNull IModule module);

  69.   static void applyConstraintSetToModule(
  70.       @NonNull Set<IConstraintSet> constraintSets,
  71.       @NonNull IModule module) throws MetaschemaException {
  72.     Set<IConstraintSet> resolvedConstraintSets = resolveConstraintSets(constraintSets);

  73.     ConstraintComposingVisitor visitor = new ConstraintComposingVisitor();
  74.     IModuleNodeItem item = INodeItemFactory.instance().newModuleNodeItem(module);

  75.     for (ITargetedConstaints targeted : getTargetedConstraintsForMetaschema(resolvedConstraintSets, module)) {
  76.       MetapathExpression targetExpression = targeted.getTargetExpression();
  77.       INodeItem node = targetExpression.evaluateAs(item, ResultType.NODE);
  78.       if (node == null) {
  79.         throw new MetaschemaException(String.format("Target not found for expression '%s' on metaschema '%s'.",
  80.             targetExpression.getPath(),
  81.             module.getQName()));
  82.       } else if (node instanceof IDefinitionNodeItem) {
  83.         IDefinition nodeDefinition = ((IDefinitionNodeItem<?, ?>) node).getDefinition();
  84.         IModule nodeModule = nodeDefinition.getContainingModule();
  85.         if (!module.equals(nodeModule)) {
  86.           throw new MetaschemaException(
  87.               String.format("Target definition '%s' in metaschema '%s' is not in the scoped metaschema '%s'.",
  88.                   nodeDefinition.getName(),
  89.                   nodeModule.getQName(),
  90.                   module.getQName()));
  91.         }
  92.       }
  93.       node.accept(visitor, targeted);
  94.     }

  95.   }

  96.   Collection<IConstraintSet> getImportedConstraintSets();

  97.   /**
  98.    * Get the set of Metaschema scoped constraints to apply by a {@link QName}
  99.    * formed from the Metaschema namespace and short name.
  100.    *
  101.    * @return the mapping of QName to scoped constraints
  102.    */
  103.   @NonNull
  104.   Map<QName, List<IScopedContraints>> getScopedContraints();
  105. }