001/* 002 * Copyright (c) 2007-2016 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.assertion; 022 023import java.util.Iterator; 024import java.util.LinkedList; 025import java.util.List; 026import java.util.Set; 027 028import cascading.flow.FlowElement; 029import cascading.flow.planner.PlannerContext; 030import cascading.flow.planner.graph.ElementGraph; 031import cascading.flow.planner.graph.ElementSubGraph; 032import cascading.flow.planner.iso.GraphResult; 033import cascading.flow.planner.iso.expression.ElementCapture; 034import cascading.flow.planner.iso.finder.Match; 035import cascading.flow.planner.iso.transformer.Transformed; 036import cascading.flow.planner.rule.Rule; 037import cascading.util.Util; 038 039/** 040 * 041 */ 042public class Asserted extends GraphResult 043 { 044 PlannerContext plannerContext; 045 GraphAssert graphAssert; 046 ElementGraph beginGraph; 047 final String message; 048 private GraphAssert.AssertionType assertionType; 049 final Match match; 050 051 List<Transformed> childTransforms; 052 053 public Asserted( PlannerContext plannerContext, GraphAssert graphAssert, ElementGraph beginGraph, String message, GraphAssert.AssertionType assertionType, Match match ) 054 { 055 this.plannerContext = plannerContext; 056 this.graphAssert = graphAssert; 057 this.beginGraph = beginGraph; 058 this.message = message; 059 this.assertionType = assertionType; 060 this.match = match; 061 } 062 063 public GraphAssert getGraphAssert() 064 { 065 return graphAssert; 066 } 067 068 @Override 069 public ElementGraph getBeginGraph() 070 { 071 return beginGraph; 072 } 073 074 public String getMessage() 075 { 076 String result = message; 077 078 for( ElementCapture capture : ElementCapture.values() ) 079 { 080 Iterator<FlowElement> iterator = match.getCapturedElements( capture ).iterator(); 081 082 while( result.contains( "{" + capture + "}" ) && iterator.hasNext() ) 083 result = result.replaceFirst( "\\{" + capture + "\\}", iterator.next().toString() ); 084 } 085 086 return result; 087 } 088 089 public GraphAssert.AssertionType getAssertionType() 090 { 091 return assertionType; 092 } 093 094 @Override 095 public String getRuleName() 096 { 097 if( getGraphAssert() instanceof Rule ) 098 return ( (Rule) getGraphAssert() ).getRuleName(); 099 100 return "none"; 101 } 102 103 public ElementSubGraph getMatched() 104 { 105 return match.getMatchedGraph(); 106 } 107 108 public Set<FlowElement> getAnchors() 109 { 110 return match.getCapturedElements( ElementCapture.Primary ); 111 } 112 113 public FlowElement getFirstAnchor() 114 { 115 return Util.getFirst( getAnchors() ); 116 } 117 118 @Override 119 public ElementGraph getEndGraph() 120 { 121 return null; 122 } 123 124 public List<Transformed> getChildTransforms() 125 { 126 if( childTransforms == null ) 127 childTransforms = new LinkedList<>(); 128 129 return childTransforms; 130 } 131 132 public void addChildTransform( Transformed transformed ) 133 { 134 if( plannerContext.isTransformTracingEnabled() ) 135 getChildTransforms().add( transformed ); 136 } 137 138 @Override 139 public void writeDOTs( String path ) 140 { 141 int count = 0; 142 143 for( int i = 0; i < getChildTransforms().size(); i++ ) 144 { 145 Transformed transformed = getChildTransforms().get( i ); 146 String name = transformed.getTransformerName(); 147 transformed.writeDOTs( path + "/child-" + i + "-" + name + "/" ); 148 } 149 150 count = writeBeginGraph( path, count ); 151 152 writeEndGraph( path, count ); 153 } 154 }