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