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 cascading.flow.planner.PlannerContext;
025import cascading.flow.planner.graph.ElementGraph;
026import cascading.flow.planner.graph.ElementGraphs;
027import cascading.flow.planner.graph.ElementSubGraph;
028import cascading.flow.planner.iso.expression.ExpressionGraph;
029import cascading.flow.planner.iso.finder.GraphFinder;
030import cascading.flow.planner.iso.finder.Match;
031import cascading.flow.planner.rule.TransformException;
032
033/**
034 * Class SubGraphTransformer will return a bounded sub-graph after matching a sub-graph within a contracted graph.
035 */
036public class SubGraphTransformer extends GraphTransformer<ElementGraph, ElementSubGraph>
037  {
038  private GraphTransformer graphTransformer;
039  private ExpressionGraph subGraphMatcher;
040  private GraphFinder subGraphFinder;
041  private boolean findAllPrimaries = false;
042
043  public SubGraphTransformer( GraphTransformer graphTransformer, ExpressionGraph subGraphMatcher )
044    {
045    this.graphTransformer = graphTransformer;
046    this.subGraphMatcher = subGraphMatcher;
047    this.subGraphFinder = new GraphFinder( subGraphMatcher );
048    this.findAllPrimaries = subGraphMatcher.supportsNonRecursiveMatch();
049    }
050
051  public Transformed<ElementSubGraph> transform( PlannerContext plannerContext, ElementGraph rootGraph )
052    {
053    Transformed<ElementSubGraph> transformed = new Transformed<>( plannerContext, this, subGraphMatcher, rootGraph );
054
055    try
056      {
057      Transformed contractedTransformed = graphTransformer.transform( plannerContext, rootGraph ); // contracted graph transform
058
059      transformed.addChildTransform( contractedTransformed );
060
061      // apply contracted sub-graph matcher to get the bounded sub-graph of the original graph
062      ElementGraph contractedGraph = contractedTransformed.getEndGraph();
063
064      Match match = findAllPrimaries ? subGraphFinder.findAllMatches( plannerContext, contractedGraph ) : subGraphFinder.findFirstMatch( plannerContext, contractedGraph );
065
066      if( !match.foundMatch() )
067        return transformed;
068
069      ElementGraph contractedSubGraph = match.getMatchedGraph();
070
071      ElementSubGraph resultSubGraph = asSubGraphOf( rootGraph, contractedSubGraph ); // the bounded sub-graph of the rootGraph
072
073      transformed.setEndGraph( resultSubGraph );
074
075      return transformed;
076      }
077    catch( Throwable throwable )
078      {
079      throw new TransformException( throwable, transformed );
080      }
081    }
082
083  protected ElementSubGraph asSubGraphOf( ElementGraph rootGraph, ElementGraph contractedSubGraph )
084    {
085    return ElementGraphs.asSubGraph( rootGraph, contractedSubGraph, null );
086    }
087  }