class Puppet::Pops::Evaluator::RelationshipOperator
The RelationshipOperator implements the semantics of the -> <- ~> <~ operators creating relationships or notification relationships between the left and right hand side's references to resources.
This is separate class since a second level of evaluation is required that transforms string in left or right hand to type references. The task of “making a relationship” is delegated to the “runtime support” class that is included. This is done to separate the concerns of the new evaluator from the 3x runtime; messy logic goes into the runtime support module. Later when more is cleaned up this can be simplified further.
Constants
- RELATIONSHIP_OPERATORS
- RELATION_TYPE
- REVERSE_OPERATORS
Public Class Methods
# File lib/puppet/pops/evaluator/relationship_operator.rb 33 def initialize 34 @type_transformer_visitor = Visitor.new(self, "transform", 1, 1) 35 @type_calculator = Types::TypeCalculator.new() 36 37 tf = Types::TypeFactory 38 @catalog_type = tf.variant(tf.catalog_entry, tf.type_type(tf.catalog_entry)) 39 end
Public Instance Methods
Asserts (and returns) the type if it is a PCatalogEntryType (A PCatalogEntryType is the base class of PClassType, and PResourceType).
# File lib/puppet/pops/evaluator/relationship_operator.rb 97 def assert_catalog_type(o, scope) 98 unless @type_calculator.assignable?(@catalog_type, o) 99 raise NotCatalogTypeError.new(o) 100 end 101 # TODO must check if this is an abstract PResourceType (i.e. without a type_name) - which should fail ? 102 # e.g. File -> File (and other similar constructs) - maybe the catalog protects against this since references 103 # may be to future objects... 104 o 105 end
Evaluate a relationship. TODO: The error reporting is not fine grained since evaluation has already taken place There is no references to the original source expressions at this point, only the overall relationship expression. (e.g.. the expression may be ['string', func_call(), etc.] -> func_call()) To implement this, the general evaluator needs to be able to track each evaluation result and associate it with a corresponding expression. This structure should then be passed to the relationship operator.
# File lib/puppet/pops/evaluator/relationship_operator.rb 123 def evaluate(left_right_evaluated, relationship_expression, scope) 124 # assert operator (should have been validated, but this logic makes assumptions which would 125 # screw things up royally). Better safe than sorry. 126 unless RELATIONSHIP_OPERATORS.include?(relationship_expression.operator) 127 fail(Issues::UNSUPPORTED_OPERATOR, relationship_expression, {:operator => relationship_expression.operator}) 128 end 129 130 begin 131 # Turn each side into an array of types (this also asserts their type) 132 # (note wrap in array first if value is not already an array) 133 # 134 # TODO: Later when objects are Puppet Runtime Objects and know their type, it will be more efficient to check/infer 135 # the type first since a chained operation then does not have to visit each element again. This is not meaningful now 136 # since inference needs to visit each object each time, and this is what the transformation does anyway). 137 # 138 # real is [left, right], and both the left and right may be a single value or an array. In each case all content 139 # should be flattened, and then transformed to a type. left or right may also be a value that is transformed 140 # into an array, and thus the resulting left and right must be flattened individually 141 # Once flattened, the operands should be sets (to remove duplicate entries) 142 # 143 real = left_right_evaluated.collect {|x| [x].flatten.collect {|y| transform(y, scope) }} 144 real[0].flatten! 145 real[1].flatten! 146 real[0].uniq! 147 real[1].uniq! 148 149 # reverse order if operator is Right to Left 150 source, target = reverse_operator?(relationship_expression) ? real.reverse : real 151 152 # Add the relationships to the catalog 153 source.each {|s| target.each {|t| add_relationship(s, t, RELATION_TYPE[relationship_expression.operator], scope) }} 154 155 # The result is the transformed source RHS unless it is empty, in which case the transformed LHS is returned. 156 # This closes the gap created by an empty set of references in a chain of relationship 157 # such that X -> [ ] -> Y results in X -> Y. 158 #result = real[1].empty? ? real[0] : real[1] 159 if real[1].empty? 160 # right side empty, simply use the left (whatever it may be) 161 result = real[0] 162 else 163 right = real[1] 164 if right.size == 1 && right[0].is_a?(Puppet::Pops::Evaluator::Collectors::AbstractCollector) 165 # the collector when evaluated later may result in an empty set, if so, the 166 # lazy relationship forming logic needs to have access to the left value. 167 adapter = Puppet::Pops::Adapters::EmptyAlternativeAdapter.adapt(right[0]) 168 adapter.empty_alternative = real[0] 169 end 170 result = right 171 end 172 result 173 174 rescue NotCatalogTypeError => e 175 fail(Issues::NOT_CATALOG_TYPE, relationship_expression, {:type => @type_calculator.string(e.type)}) 176 rescue IllegalRelationshipOperandError => e 177 fail(Issues::ILLEGAL_RELATIONSHIP_OPERAND_TYPE, relationship_expression, {:operand => e.operand}) 178 end 179 end
# File lib/puppet/pops/evaluator/relationship_operator.rb 181 def reverse_operator?(o) 182 REVERSE_OPERATORS.include?(o.operator) 183 end
# File lib/puppet/pops/evaluator/relationship_operator.rb 41 def transform(o, scope) 42 @type_transformer_visitor.visit_this_1(self, o, scope) 43 end
# File lib/puppet/pops/evaluator/relationship_operator.rb 85 def transform_AbstractCollector(o, scope) 86 o 87 end
Array content needs to be transformed
# File lib/puppet/pops/evaluator/relationship_operator.rb 90 def transform_Array(o, scope) 91 o.map{|x| transform(x, scope) } 92 end
This transforms a 3x Collector (the result of evaluating a 3x AST::Collection). It is passed through verbatim since it is evaluated late by the compiler. At the point where the relationship is evaluated, it is simply recorded with the compiler for later evaluation. If one of the sides of the relationship is a Collector it is evaluated before the actual relationship is formed. (All of this happens at a later point in time.
# File lib/puppet/pops/evaluator/relationship_operator.rb 81 def transform_Collector(o, scope) 82 o 83 end
Catch all non transformable objects @api private
# File lib/puppet/pops/evaluator/relationship_operator.rb 47 def transform_Object(o, scope) 48 raise IllegalRelationshipOperandError.new(o) 49 end
Types are what they are, just check the type @api private
# File lib/puppet/pops/evaluator/relationship_operator.rb 71 def transform_PAnyType(o, scope) 72 assert_catalog_type(o, scope) 73 end
A qualified name is short hand for a class with this name @api private
# File lib/puppet/pops/evaluator/relationship_operator.rb 65 def transform_QualifiedName(o, scope) 66 Types::TypeFactory.host_class(o.value) 67 end
A Resource is by definition a Catalog type, but of 3.x type @api private
# File lib/puppet/pops/evaluator/relationship_operator.rb 53 def transform_Resource(o, scope) 54 Types::TypeFactory.resource(o.type, o.title) 55 end
A string must be a type reference in string format @api private
# File lib/puppet/pops/evaluator/relationship_operator.rb 59 def transform_String(o, scope) 60 assert_catalog_type(Types::TypeParser.singleton.parse(o), scope) 61 end