    001/*
    002 * Copyright (c) 2016 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
    003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
    004 *
    005 * Project and contact information: http://www.cascading.org/
    006 *
    007 * This file is part of the Cascading project.
    008 *
    009 * Licensed under the Apache License, Version 2.0 (the "License");
    010 * you may not use this file except in compliance with the License.
    011 * You may obtain a copy of the License at
    012 *
    013 *     http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing, software
    016 * distributed under the License is distributed on an "AS IS" BASIS,
    017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    018 * See the License for the specific language governing permissions and
    019 * limitations under the License.
    020 */
    021
    022package cascading.flow.planner.iso.transformer;
    023
    024import java.io.File;
    025import java.util.LinkedList;
    026import java.util.List;
    027
    028import cascading.flow.planner.PlannerContext;
    029import cascading.flow.planner.graph.ElementGraph;
    030import cascading.flow.planner.graph.ElementMultiGraph;
    031import cascading.flow.planner.iso.GraphResult;
    032import cascading.flow.planner.iso.expression.ExpressionGraph;
    033import cascading.flow.planner.rule.Rule;
    034
    035/**
    036 *
    037 */
    038public class Transformed<E extends ElementGraph> extends GraphResult
    039  {
    040  PlannerContext plannerContext;
    041  GraphTransformer graphTransformer;
    042  ExpressionGraph expressionGraph;
    043  ElementGraph beginGraph;
    044  int recursionCount = 0;
    045  List<ElementGraph> recursions;
    046  List<Transformed> childTransforms;
    047  E endGraph;
    048
    049  public Transformed( PlannerContext plannerContext, GraphTransformer graphTransformer, ElementGraph beginGraph )
    050    {
    051    this.plannerContext = plannerContext;
    052    this.graphTransformer = graphTransformer;
    053
    054    if( plannerContext.isTransformTracingEnabled() )
    055      beginGraph = new ElementMultiGraph( beginGraph );
    056
    057    this.beginGraph = beginGraph;
    058    }
    059
    060  public Transformed( PlannerContext plannerContext, GraphTransformer graphTransformer, ExpressionGraph expressionGraph, ElementGraph beginGraph )
    061    {
    062    this.plannerContext = plannerContext;
    063    this.graphTransformer = graphTransformer;
    064    this.expressionGraph = expressionGraph;
    065
    066    if( plannerContext.isTransformTracingEnabled() )
    067      beginGraph = new ElementMultiGraph( beginGraph );
    068
    069    this.beginGraph = beginGraph;
    070    }
    071
    072  public PlannerContext getPlannerContext()
    073    {
    074    return plannerContext;
    075    }
    076
    077  public String getRuleName()
    078    {
    079    if( getGraphTransform() instanceof Rule )
    080      return ( (Rule) getGraphTransform() ).getRuleName();
    081
    082    return "none";
    083    }
    084
    085  public String getTransformerName()
    086    {
    087    return getGraphTransform().getClass().getSimpleName();
    088    }
    089
    090  public GraphTransformer getGraphTransform()
    091    {
    092    return graphTransformer;
    093    }
    094
    095  @Override
    096  public ElementGraph getBeginGraph()
    097    {
    098    return beginGraph;
    099    }
    100
    101  public void setEndGraph( E endGraph )
    102    {
    103    this.endGraph = endGraph;
    104    }
    105
    106  @Override
    107  public E getEndGraph()
    108    {
    109    return endGraph;
    110    }
    111
    112  public int getNumRecursions()
    113    {
    114    return recursionCount;
    115    }
    116
    117  public List<ElementGraph> getRecursions()
    118    {
    119    if( recursions == null )
    120      recursions = new LinkedList<>();
    121
    122    return recursions;
    123    }
    124
    125  public List<Transformed> getChildTransforms()
    126    {
    127    if( childTransforms == null )
    128      childTransforms = new LinkedList<>();
    129
    130    return childTransforms;
    131    }
    132
    133  void addRecursionTransform( ElementGraph transformed )
    134    {
    135    recursionCount++;
    136
    137    if( plannerContext.isTransformTracingEnabled() )
    138      getRecursions().add( new ElementMultiGraph( transformed ) );
    139    }
    140
    141  public void addChildTransform( Transformed transformed )
    142    {
    143    if( plannerContext.isTransformTracingEnabled() )
    144      getChildTransforms().add( transformed );
    145    }
    146
    147  @Override
    148  public void writeDOTs( String path )
    149    {
    150    int count = 0;
    151
    152    if( expressionGraph != null )
    153      {
    154      String fileName = String.format( "expression-graph-%s.dot", expressionGraph.getClass().getSimpleName() );
    155      expressionGraph.writeDOT( new File( path, fileName ).toString() );
    156      }
    157
    158    for( int i = 0; i < getChildTransforms().size(); i++ )
    159      {
    160      Transformed transformed = getChildTransforms().get( i );
    161      String name = transformed.getTransformerName();
    162      String pathName = String.format( "%s/child-%d-%s/", path, i, name );
    163      transformed.writeDOTs( pathName );
    164      }
    165
    166    count = writeBeginGraph( path, count );
    167
    168    for( ElementGraph recursion : getRecursions() )
    169      {
    170      String name = recursion.getClass().getSimpleName();
    171      recursion.writeDOT( new File( path, makeFileName( count++, name, "recursion" ) ).toString() );
    172      }
    173
    174    writeEndGraph( path, count );
    175    }
    176  }