Class ModelTransformer


  • public final class ModelTransformer
    extends java.lang.Object
    Class used to transform Models.
    • Method Detail

      • create

        public static ModelTransformer create()
        Creates a ModelTransformer using ModelTransformerPlugin instances discovered using the com.software.smithy.transform class loader and any modules found in the module path.
        Returns:
        Returns the created ModelTransformer.
      • createWithPlugins

        public static ModelTransformer createWithPlugins​(java.util.List<ModelTransformerPlugin> plugins)
        Creates a ModelTransformer using a list of ModelTransformer plugins.
        Parameters:
        plugins - Plugins to use with the transformer.
        Returns:
        Returns the created ModelTransformer.
      • createWithServiceProviders

        public static ModelTransformer createWithServiceProviders​(java.lang.ClassLoader classLoader)
        Creates a ModelTransformer that finds ModelTransformerPlugin service providers using the given ClassLoader.
        Parameters:
        classLoader - ClassLoader used to find ModelTransformerPlugin instances.
        Returns:
        Returns the created ModelTransformer.
      • replaceShapes

        public Model replaceShapes​(Model model,
                                   java.util.Collection<Shape> shapes)
        Adds or replaces shapes into the model while ensuring that the model is in a consistent state.
        Parameters:
        model - Model to transform.
        shapes - Shapes to add or replace in the model.base.
        Returns:
        Returns the transformed model.base.
      • removeShapes

        public Model removeShapes​(Model model,
                                  java.util.Collection<Shape> shapes)
        Removes shapes from the model while ensuring that the model is in a consistent state.
        Parameters:
        model - Model to transform.
        shapes - Shapes to add or replace in the model.base.
        Returns:
        Returns the transformed model.base.
      • removeShapesIf

        public Model removeShapesIf​(Model model,
                                    java.util.function.Predicate<Shape> predicate)
        Removes shapes from the model that match the given predicate.
        Parameters:
        model - Model to transform.
        predicate - Predicate that accepts a shape and returns true to remove it.
        Returns:
        Returns the transformed model.base.
      • renameShapes

        public Model renameShapes​(Model model,
                                  java.util.Map<ShapeId,​ShapeId> renamed)
        Renames shapes using ShapeId pairs while ensuring that the transformed model is in a consistent state.

        This transformer ensures that when an aggregate shape is renamed, all members are updated in the model.

        Parameters:
        model - Model to transform.
        renamed - Map of shapeIds
        Returns:
        Returns the transformed model.base.
      • renameShapes

        public Model renameShapes​(Model model,
                                  java.util.Map<ShapeId,​ShapeId> renamed,
                                  java.util.function.Supplier<ModelAssembler> modelAssemblerSupplier)
        Renames shapes using ShapeId pairs while ensuring that the transformed model is in a consistent state.

        This transformer ensures that when an aggregate shape is renamed, all members are updated in the model.

        Parameters:
        model - Model to transform.
        renamed - Map of shapeIds
        modelAssemblerSupplier - Supplier used to create ModelAssemblers in each transform.
        Returns:
        Returns the transformed model.
      • filterTraits

        public Model filterTraits​(Model model,
                                  java.util.function.BiPredicate<Shape,​Trait> predicate)
        Filters traits out of the model that do not match the given predicate.

        The predicate function accepts the shape that a trait is attached to and the trait. If the predicate returns false, then the trait is removed from the shape.

        Parameters:
        model - Model to transform.
        predicate - Predicate that accepts a (Shape, Trait) and returns false if the trait should be removed.
        Returns:
        Returns the transformed model.base.
      • removeTraitsIf

        public Model removeTraitsIf​(Model model,
                                    java.util.function.BiPredicate<Shape,​Trait> predicate)
        Filters traits out of the model that match a predicate function.

        The predicate function accepts the shape that a trait is attached to and the trait. If the predicate returns true, then the trait is removed from the shape.

        Parameters:
        model - Model to transform.
        predicate - Predicate that accepts a (Shape, Trait) and returns true if the trait should be removed.
        Returns:
        Returns the transformed model.base.
      • filterMetadata

        public Model filterMetadata​(Model model,
                                    java.util.function.BiPredicate<java.lang.String,​Node> predicate)
        Filters out metadata key-value pairs from a model that do not match a predicate.
        Parameters:
        model - Model to transform.
        predicate - A predicate that accepts a metadata key-value pair. If the predicate returns true, then the metadata key-value pair is kept. Otherwise, it is removed.
        Returns:
        Returns the transformed model.base.
      • mapTraits

        public Model mapTraits​(Model model,
                               java.util.function.BiFunction<Shape,​Trait,​Trait> mapper)
        Maps over all traits in the model using a mapping function that accepts the shape the trait is applied to, a trait, and returns a trait, possibly even a different kind of trait.

        An exception is thrown if a trait is returned that targets a different shape than the Shape passed into the mapper function.

        Parameters:
        model - Model to transform.
        mapper - Mapping function that accepts a (Shape, Trait) and returns the mapped Trait.
        Returns:
        Returns the transformed model.base.
      • mapTraits

        public Model mapTraits​(Model model,
                               java.util.List<java.util.function.BiFunction<Shape,​Trait,​Trait>> mappers)
        Maps over all traits in the model using multiple mapping functions.

        Note: passing in a list of mappers is much more efficient than invoking mapTraits multiple times because it reduces the number of intermediate models that are needed to perform the transformation.

        Parameters:
        model - Model to transform.
        mappers - Mapping functions that accepts a (Shape, Trait) and returns the mapped Trait.
        Returns:
        Returns the transformed model.base.
        See Also:
        for more information.
      • mapShapes

        public Model mapShapes​(Model model,
                               java.util.function.Function<Shape,​Shape> mapper)
        Maps over all shapes in the model using a mapping function, allowing shapes to be replaced with completely different shapes or slightly modified shapes.

        An exception is thrown if a mapper returns a shape with a different shape ID or a different type.

        Parameters:
        model - Model to transform.
        mapper - Mapping function that accepts a shape and returns a shape with the same ID.
        Returns:
        Returns the transformed model.base.
      • mapShapes

        public Model mapShapes​(Model model,
                               java.util.List<java.util.function.Function<Shape,​Shape>> mappers)
        Maps over all shapes in the model using multiple mapping functions.

        Note: passing in a list of mappers is much more efficient than invoking mapShapes multiple times because it reduces the number of intermediate models that are needed to perform the transformation.

        Parameters:
        model - Model to transform.
        mappers - Mapping functions that accepts a shape and returns a shape with the same ID.
        Returns:
        Returns the transformed model.base.
        See Also:
        for more information.
      • removeUnreferencedShapes

        public Model removeUnreferencedShapes​(Model model)
        Removes shapes (excluding service shapes) that are not referenced by any other shapes.
        Parameters:
        model - Model to transform.
        Returns:
        Returns the transformed model.base.
      • removeUnreferencedShapes

        public Model removeUnreferencedShapes​(Model model,
                                              java.util.function.Predicate<Shape> keepFilter)
        Removes shapes (excluding service shapes) that are not referenced by any other shapes. Shapes that are part of the prelude or that act as the shape of any trait, regardless of if the trait is in use in the model, are never considered unreferenced.
        Parameters:
        model - Model to transform.
        keepFilter - Predicate function that accepts an unreferenced shape and returns true to remove the shape or false to keep the shape in the model.base.
        Returns:
        Returns the transformed model.base.
      • removeUnreferencedTraitDefinitions

        public Model removeUnreferencedTraitDefinitions​(Model model)
        Removes definitions for traits that are not used by any shape in the model.base. Trait definitions that are part of the prelude will not be removed.
        Parameters:
        model - Model to transform
        Returns:
        Returns the transformed model.base.
      • removeUnreferencedTraitDefinitions

        public Model removeUnreferencedTraitDefinitions​(Model model,
                                                        java.util.function.Predicate<Shape> keepFilter)
        Removes trait definitions for traits that are not used by any shape in the model.

        Trait definitions that are part of the prelude will not be removed.

        Parameters:
        model - Model to transform
        keepFilter - Predicate function that accepts an unreferenced trait shape (that has the TraitDefinition trait) and returns true to remove the definition or false to keep the definition in the model.base.
        Returns:
        Returns the transformed model.base.
      • scrubTraitDefinitions

        public Model scrubTraitDefinitions​(Model model)
        Removes all trait definitions from a model and all shapes that are only connected to the graph either directly or transitively by a trait definition shape.

        This can be useful when serializing a Smithy model to a format that does not include trait definitions and the shapes used by trait definitions would have no meaning (e.g., Swagger).

        Parameters:
        model - Model to transform.
        Returns:
        Returns the transformed model.base.
      • getNonTraitShapes

        @Deprecated
        public ShapeIndex getNonTraitShapes​(Model model)
        Deprecated.
      • getModelWithoutTraitShapes

        public Model getModelWithoutTraitShapes​(Model model)
        Gets all shapes from a model where shapes that define traits or shapes that are only used as part of a trait definition have been removed.
        Parameters:
        model - Model that contains shapes.
        Returns:
        Returns a model that contains matching shapes.