text: # Copyright 2021 Google LLC. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  """Test pipeline using custom components with channel.union()."""
  
  import os
  
  from tfx.components import CsvExampleGen
  from tfx.components import StatisticsGen
  from tfx.dsl.component.experimental import annotations
  from tfx.dsl.component.experimental.decorators import component
  from tfx.dsl.components.common import resolver
  from tfx.dsl.input_resolution.strategies import latest_artifact_strategy
  from tfx.orchestration import pipeline
  from tfx.types import channel
  from tfx.types import standard_artifacts
  
  _pipeline_name = 'channel_union_pipeline'
  
  _data_root = 'channel_union'
  _data_root_1 = os.path.join(_data_root, 'data1')
  _data_root_2 = os.path.join(_data_root, 'data2')
  
  _tfx_root = os.path.join(_data_root, 'tfx')
  _pipeline_root = os.path.join(_tfx_root, 'pipelines', _pipeline_name)
  
  
  @component
  def ChannelUnionComponent(  # pylint: disable=invalid-name
      input_data: annotations.InputArtifact[standard_artifacts.Examples],
      name: annotations.Parameter[str],
      output_data: annotations.OutputArtifact[standard_artifacts.Examples]):
    del input_data, name, output_data
  
  
  def _create_pipeline(pipeline_name: str, pipeline_root: str, data_root_1: str,
                       data_root_2: str) -> pipeline.Pipeline:
    """Implements a pipeline with channel.union()."""
    # Brings data into the pipeline or otherwise joins/converts training data.
    example_gen_1 = CsvExampleGen(input_base=data_root_1).with_id('example_gen_1')
    example_gen_2 = CsvExampleGen(input_base=data_root_2).with_id('example_gen_2')
  
    # pylint: disable=no-value-for-parameter
    channel_union = ChannelUnionComponent(
        input_data=channel.union([
            example_gen_1.outputs['examples'], example_gen_2.outputs['examples']
        ]),
        name='channel_union_input')
  
    # Get the latest channel.
    latest_artifacts_resolver = resolver.Resolver(
        strategy_class=latest_artifact_strategy.LatestArtifactStrategy,
        resolved_channels=channel.union([
            example_gen_1.outputs['examples'],
            channel_union.outputs['output_data']
        ])).with_id('latest_artifacts_resolver')
  
    # Computes statistics over data for visualization and example validation.
    statistics_gen = StatisticsGen(
        examples=latest_artifacts_resolver.outputs['resolved_channels'])
    return pipeline.Pipeline(
        pipeline_name=pipeline_name,
        pipeline_root=pipeline_root,
        components=[
            example_gen_1, example_gen_2, channel_union,
            latest_artifacts_resolver, statistics_gen
        ])
  
  
  def create_test_pipeline():
    return _create_pipeline(
        pipeline_name=_pipeline_name,
        pipeline_root=_pipeline_root,
        data_root_1=_data_root_1,
        data_root_2=_data_root_2)
  