    001/*
    002 * Copyright (c) 2007-2017 Xplenty, 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 cascading.flow.planner.PlannerContext;
    024import cascading.flow.planner.graph.ElementGraph;
    025import cascading.flow.planner.iso.assertion.GraphAssert;
    026import cascading.flow.planner.iso.transformer.ContractedTransformer;
    027import cascading.flow.planner.iso.transformer.SubGraphTransformer;
    028import cascading.flow.planner.iso.transformer.Transformed;
    029
    030/**
    031 * The RuleAssert class is responsible for asserting the structure of a element graph.
    032 */
    033public class RuleAssert extends GraphAssert<ElementGraph> implements Rule
    034  {
    035  private final PlanPhase phase;
    036  private final RuleExpression ruleExpression;
    037  private ContractedTransformer contractedTransformer;
    038  private SubGraphTransformer subGraphTransformer;
    039
    040  public RuleAssert( PlanPhase phase, RuleExpression ruleExpression, String message )
    041    {
    042    this( phase, ruleExpression, message, null );
    043    }
    044
    045  public RuleAssert( PlanPhase phase, RuleExpression ruleExpression, String message, AssertionType assertionType )
    046    {
    047    super( ruleExpression.getMatchExpression(), message, assertionType );
    048    this.phase = phase;
    049    this.ruleExpression = ruleExpression;
    050
    051    if( ruleExpression.getContractionExpression() != null )
    052      contractedTransformer = new ContractedTransformer( ruleExpression.getContractionExpression() );
    053    else
    054      contractedTransformer = null;
    055
    056    if( ruleExpression.getContractedMatchExpression() != null )
    057      {
    058      if( contractedTransformer == null )
    059        throw new IllegalArgumentException( "must have contracted expression if given contracted match expression" );
    060
    061      subGraphTransformer = new SubGraphTransformer( contractedTransformer, ruleExpression.getContractedMatchExpression() );
    062      }
    063    else
    064      {
    065      subGraphTransformer = null;
    066      }
    067    }
    068
    069  @Override
    070  public PlanPhase getRulePhase()
    071    {
    072    return phase;
    073    }
    074
    075  @Override
    076  public String getRuleName()
    077    {
    078    return getClass().getSimpleName().replaceAll( "^(.*)[A-Z][a-z]*Rule$", "$1" );
    079    }
    080
    081  @Override
    082  protected Transformed<ElementGraph> transform( PlannerContext plannerContext, ElementGraph graph )
    083    {
    084    Transformed transformed = null;
    085
    086    if( contractedTransformer != null )
    087      transformed = contractedTransformer.transform( plannerContext, graph );
    088    else if( subGraphTransformer != null )
    089      transformed = subGraphTransformer.transform( plannerContext, graph );
    090
    091    return transformed;
    092    }
    093
    094  @Override
    095  public String toString()
    096    {
    097    return getRuleName();
    098    }
    099  }