Class: Archsight::Annotations::ComputedEvaluator
- Inherits:
-
Object
- Object
- Archsight::Annotations::ComputedEvaluator
- Defined in:
- lib/archsight/annotations/computed.rb
Overview
ComputedEvaluator provides the DSL context for computing annotation values. It exposes aggregation functions and relation traversal methods.
Instance Method Summary collapse
-
#annotation(key) ⇒ Object
Access a regular annotation value from the current instance.
-
#avg(instances, key) ⇒ Float?
Average numeric annotation values.
-
#collect(instances, key) ⇒ Array
Collect unique annotation values.
-
#computed(key) ⇒ Object
Access a computed annotation value (triggers computation if needed).
-
#count(instances, key = nil) ⇒ Integer
Count instances or non-nil annotation values.
-
#first(instances, key) ⇒ Object?
Get first non-nil annotation value.
-
#get(instance, key) ⇒ Object?
Get an annotation value from an instance, triggering computation if needed.
-
#incoming(kind = nil) ⇒ Object
Get direct incoming relations (<- Kind).
-
#incoming_transitive(kind = nil, max_depth: 10) ⇒ Object
Get transitive incoming relations (<~ Kind).
-
#initialize(instance, database, manager) ⇒ ComputedEvaluator
constructor
A new instance of ComputedEvaluator.
-
#max(instances, key) ⇒ Float?
Maximum numeric annotation value.
-
#min(instances, key) ⇒ Float?
Minimum numeric annotation value.
-
#most_common(instances, key) ⇒ Object?
Get most common annotation value (mode).
-
#outgoing(kind = nil) ⇒ Object
Get direct outgoing relations (-> Kind).
-
#outgoing_transitive(kind = nil, max_depth: 10) ⇒ Object
Get transitive outgoing relations (~> Kind).
-
#sum(instances, key) ⇒ Float?
Sum numeric annotation values from instances.
Constructor Details
#initialize(instance, database, manager) ⇒ ComputedEvaluator
Returns a new instance of ComputedEvaluator.
27 28 29 30 31 32 |
# File 'lib/archsight/annotations/computed.rb', line 27 def initialize(instance, database, manager) @instance = instance @database = database @manager = manager @resolver = Archsight::Annotations::ComputedRelationResolver.new(instance, database) end |
Instance Method Details
#annotation(key) ⇒ Object
Access a regular annotation value from the current instance
35 36 37 |
# File 'lib/archsight/annotations/computed.rb', line 35 def annotation(key) @instance.annotations[key] end |
#avg(instances, key) ⇒ Float?
Average numeric annotation values
94 95 96 97 |
# File 'lib/archsight/annotations/computed.rb', line 94 def avg(instances, key) values = extract_values(instances, key) Archsight::Annotations::ComputedAggregators.avg(values) end |
#collect(instances, key) ⇒ Array
Collect unique annotation values
121 122 123 124 |
# File 'lib/archsight/annotations/computed.rb', line 121 def collect(instances, key) values = extract_values(instances, key) Archsight::Annotations::ComputedAggregators.collect(values) end |
#computed(key) ⇒ Object
Access a computed annotation value (triggers computation if needed)
40 41 42 |
# File 'lib/archsight/annotations/computed.rb', line 40 def computed(key) @manager.compute_for_key(@instance, key) end |
#count(instances, key = nil) ⇒ Integer
Count instances or non-nil annotation values
81 82 83 84 85 86 87 88 |
# File 'lib/archsight/annotations/computed.rb', line 81 def count(instances, key = nil) if key values = extract_values(instances, key) Archsight::Annotations::ComputedAggregators.count(values) else instances.length end end |
#first(instances, key) ⇒ Object?
Get first non-nil annotation value
130 131 132 133 |
# File 'lib/archsight/annotations/computed.rb', line 130 def first(instances, key) values = extract_values(instances, key) Archsight::Annotations::ComputedAggregators.first(values) end |
#get(instance, key) ⇒ Object?
Get an annotation value from an instance, triggering computation if needed
148 149 150 151 |
# File 'lib/archsight/annotations/computed.rb', line 148 def get(instance, key) @manager.compute_for_key(instance, key) if instance.class.computed_annotations.any? { |d| d.matches?(key) } instance.annotations[key] end |
#incoming(kind = nil) ⇒ Object
Get direct incoming relations (<- Kind)
57 58 59 |
# File 'lib/archsight/annotations/computed.rb', line 57 def incoming(kind = nil) @resolver.incoming(kind) end |
#incoming_transitive(kind = nil, max_depth: 10) ⇒ Object
Get transitive incoming relations (<~ Kind)
62 63 64 |
# File 'lib/archsight/annotations/computed.rb', line 62 def incoming_transitive(kind = nil, max_depth: 10) @resolver.incoming_transitive(kind, max_depth: max_depth) end |
#max(instances, key) ⇒ Float?
Maximum numeric annotation value
112 113 114 115 |
# File 'lib/archsight/annotations/computed.rb', line 112 def max(instances, key) values = extract_values(instances, key) Archsight::Annotations::ComputedAggregators.max(values) end |
#min(instances, key) ⇒ Float?
Minimum numeric annotation value
103 104 105 106 |
# File 'lib/archsight/annotations/computed.rb', line 103 def min(instances, key) values = extract_values(instances, key) Archsight::Annotations::ComputedAggregators.min(values) end |
#most_common(instances, key) ⇒ Object?
Get most common annotation value (mode)
139 140 141 142 |
# File 'lib/archsight/annotations/computed.rb', line 139 def most_common(instances, key) values = extract_values(instances, key) Archsight::Annotations::ComputedAggregators.most_common(values) end |
#outgoing(kind = nil) ⇒ Object
Get direct outgoing relations (-> Kind)
47 48 49 |
# File 'lib/archsight/annotations/computed.rb', line 47 def outgoing(kind = nil) @resolver.outgoing(kind) end |
#outgoing_transitive(kind = nil, max_depth: 10) ⇒ Object
Get transitive outgoing relations (~> Kind)
52 53 54 |
# File 'lib/archsight/annotations/computed.rb', line 52 def outgoing_transitive(kind = nil, max_depth: 10) @resolver.outgoing_transitive(kind, max_depth: max_depth) end |
#sum(instances, key) ⇒ Float?
Sum numeric annotation values from instances
72 73 74 75 |
# File 'lib/archsight/annotations/computed.rb', line 72 def sum(instances, key) values = extract_values(instances, key) Archsight::Annotations::ComputedAggregators.sum(values) end |