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.rule;
022
023import java.util.Collection;
024
025import cascading.flow.FlowElement;
026import cascading.flow.planner.PlannerContext;
027import cascading.flow.planner.graph.ElementGraph;
028import cascading.flow.planner.iso.subgraph.GraphPartitioner;
029import cascading.flow.planner.iso.subgraph.Partitions;
030
031/**
032 * The RulePartitioner class is responsible for partitioning an element graph into smaller sub-graphs.
033 * <p/>
034 * It may also re-partition a given graph, in place replacing it with its children, if any.
035 */
036public abstract class RulePartitioner implements Rule
037  {
038  public abstract Enum[] getAnnotationExcludes();
039
040  public enum PartitionSource
041    {
042      /**
043       * Partition the parent into children.
044       */
045      PartitionParent,
046
047      /**
048       * Partition a given child into more children, removing the original child.
049       */
050      PartitionCurrent
051    }
052
053  protected PlanPhase phase;
054  protected PartitionSource partitionSource = PartitionSource.PartitionParent;
055  protected GraphPartitioner graphPartitioner;
056
057  public RulePartitioner( PlanPhase phase, PartitionSource partitionSource, GraphPartitioner graphPartitioner )
058    {
059    this.phase = phase;
060    this.partitionSource = partitionSource;
061    this.graphPartitioner = graphPartitioner;
062    }
063
064  public RulePartitioner()
065    {
066    }
067
068  @Override
069  public PlanPhase getRulePhase()
070    {
071    return phase;
072    }
073
074  public PartitionSource getPartitionSource()
075    {
076    return partitionSource;
077    }
078
079  protected GraphPartitioner getGraphPartitioner()
080    {
081    return graphPartitioner;
082    }
083
084  public Partitions partition( PlannerContext plannerContext, ElementGraph elementGraph )
085    {
086    return partition( plannerContext, elementGraph, null );
087    }
088
089  public Partitions partition( PlannerContext plannerContext, ElementGraph elementGraph, Collection<FlowElement> excludes )
090    {
091    Partitions partition = getGraphPartitioner().partition( plannerContext, elementGraph, excludes );
092
093    if( partition != null )
094      partition.setRulePartitioner( this );
095
096    return partition;
097    }
098
099  @Override
100  public String getRuleName()
101    {
102    return getClass().getSimpleName().replaceAll( "^(.*)[]A-Z][a-z]*Rule$", "$1" );
103    }
104
105  @Override
106  public String toString()
107    {
108    return getRuleName();
109    }
110  }