Class: RGen::Instantiator::ReferenceResolver
- Inherits:
-
Object
- Object
- RGen::Instantiator::ReferenceResolver
- Defined in:
- lib/rgen/instantiator/reference_resolver.rb
Overview
The ReferenceResolver can be used to resolve unresolved references, i.e. instances of class UnresolvedReference
There are two ways how this can be used:
1. the identifiers and associated model elements are added upfront using +add_identifier+
2. register an :identifier_resolver with the constructor, which will be invoked
for every unresolved identifier
Defined Under Namespace
Classes: UnresolvedReference
Instance Method Summary collapse
-
#add_identifier(ident, element) ⇒ Object
Add an
identifer
/element
pair which will be used for looking up unresolved identifers. -
#initialize(options = {}) ⇒ ReferenceResolver
constructor
Create a reference resolver, options:.
-
#resolve(unresolved_refs, options = {}) ⇒ Object
Tries to resolve the given
unresolved_refs
.
Constructor Details
#initialize(options = {}) ⇒ ReferenceResolver
Create a reference resolver, options:
:identifier_resolver:
a proc which is called with an identifier and which should return the associated element
in case the identifier is not uniq, the proc may return multiple values
default: lookup element in internal map
38 39 40 41 |
# File 'lib/rgen/instantiator/reference_resolver.rb', line 38 def initialize(={}) @identifier_resolver = [:identifier_resolver] @identifier_map = {} end |
Instance Method Details
#add_identifier(ident, element) ⇒ Object
Add an identifer
/ element
pair which will be used for looking up unresolved identifers
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rgen/instantiator/reference_resolver.rb', line 44 def add_identifier(ident, element) map_entry = @identifier_map[ident] if map_entry if map_entry.is_a?(Array) map_entry << element else @identifier_map[ident] = [map_entry, element] end else @identifier_map[ident] = element end end |
#resolve(unresolved_refs, options = {}) ⇒ Object
Tries to resolve the given unresolved_refs
. If resolution is successful, the proxy object will be removed, otherwise there will be an error description in the problems array. In case the resolved target element’s type is not valid for the given feature, the target_type_error
flag will be set on the unresolved reference. Returns an array of the references which are still unresolved. Options:
:problems
an array to which problems will be appended
:on_resolve
a proc which will be called for every sucessful resolution, receives the unresolved
reference as well as to new target element
:use_target_type
use the expected target type to narrow the set of possible targets
(i.e. ignore targets with wrong type)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rgen/instantiator/reference_resolver.rb', line 74 def resolve(unresolved_refs, ={}) problems = [:problems] || [] still_unresolved_refs = [] unresolved_refs.each do |ur| if @identifier_resolver target = @identifier_resolver.call(ur.proxy.targetIdentifier) else target = @identifier_map[ur.proxy.targetIdentifier] end target = [target].compact unless target.is_a?(Array) if [:use_target_type] feature = ur.element.class.ecore.eAllReferences.find{|r| r.name == ur.feature_name} target = target.select{|e| e.is_a?(feature.eType.instanceClass)} end if target.size == 1 refs = ur.element.getGeneric(ur.feature_name) if refs.is_a?(Array) index = refs.index(ur.proxy) ur.element.removeGeneric(ur.feature_name, ur.proxy) begin ur.element.addGeneric(ur.feature_name, target[0], index) [:on_resolve] && [:on_resolve].call(ur, target[0]) rescue StandardError => e if is_type_error?(e) ur.element.addGeneric(ur.feature_name, ur.proxy, index) ur.target_type_error = true problems << (target[0]) still_unresolved_refs << ur else raise end end else begin # this will replace the proxy ur.element.setGeneric(ur.feature_name, target[0]) [:on_resolve] && [:on_resolve].call(ur, target[0]) rescue StandardError => e if is_type_error?(e) ur.target_type_error = true problems << (target[0]) still_unresolved_refs << ur else raise end end end elsif target.size > 1 problems << "identifier #{ur.proxy.targetIdentifier} not uniq" still_unresolved_refs << ur else problems << "identifier #{ur.proxy.targetIdentifier} not found" still_unresolved_refs << ur end end still_unresolved_refs end |