-
Notifications
You must be signed in to change notification settings - Fork 28
Add functional conversions for non-proportional units #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ntn
wants to merge
5
commits into
main
Choose a base branch
from
functional-conversions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a21fe1
feat: extend Unit to accept proc-based functional conversions
ntn 4056a03
feat: add FunctionalConversionTableBuilder for proc-based conversions
ntn 328f579
feat: wire FunctionalConversionTableBuilder into UnitSystem
ntn 970c87a
feat: add Temperature acceptance tests as proof of concept
ntn 1631f10
refactor: extract shared builder module, add cleaner DSL, fix type sa…
ntn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # frozen_string_literal: true | ||
| module Measured | ||
| class CacheError < StandardError; end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # frozen_string_literal: true | ||
| module Measured::ConversionTableBuilderBase | ||
| attr_reader :units | ||
|
|
||
| private | ||
|
|
||
| def validate_no_cycles | ||
| graph = units.select { |unit| unit.conversion_unit.present? }.group_by(&:name) | ||
| validate_acyclic_graph(graph, from: graph.keys[0]) | ||
| end | ||
|
|
||
| # This uses a depth-first search algorithm: https://en.wikipedia.org/wiki/Depth-first_search | ||
| def validate_acyclic_graph(graph, from:, visited: []) | ||
| graph[from]&.each do |edge| | ||
| adjacent_node = edge.conversion_unit | ||
| if visited.include?(adjacent_node) | ||
| raise Measured::CycleDetected.new(edge) | ||
| else | ||
| validate_acyclic_graph(graph, from: adjacent_node, visited: visited + [adjacent_node]) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def find_direct_conversion_cached(to:, from:) | ||
| @direct_conversion_cache ||= {} | ||
| @direct_conversion_cache[to] ||= {} | ||
|
|
||
| if @direct_conversion_cache[to].key?(from) | ||
| @direct_conversion_cache[to][from] | ||
| else | ||
| @direct_conversion_cache[to][from] = find_direct_conversion(to: to, from: from) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # frozen_string_literal: true | ||
| class Measured::FunctionalConversionTableBuilder | ||
| include Measured::ConversionTableBuilderBase | ||
|
|
||
| IDENTITY = ->(x) { x } | ||
|
|
||
| def initialize(units, cache: nil) | ||
| @units = units | ||
| cache ||= { class: Measured::Cache::Null } | ||
| cache_instance = cache[:class].new(*cache[:args]) | ||
| unless cache_instance.is_a?(Measured::Cache::Null) | ||
| raise Measured::CacheError, "Functional unit systems cannot be cached" | ||
| end | ||
| end | ||
|
|
||
| def to_h | ||
| @table ||= generate_table | ||
| end | ||
|
|
||
| def update_cache | ||
| raise Measured::CacheError, "Functional unit systems cannot be cached" | ||
| end | ||
|
|
||
| def cached? | ||
| false | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def generate_table | ||
| validate_no_cycles | ||
|
|
||
| units.map(&:name).each_with_object({}) do |to_unit, table| | ||
| to_table = { to_unit => IDENTITY } | ||
|
|
||
| table.each do |from_unit, from_table| | ||
| conversion, inverse = find_conversion(to: from_unit, from: to_unit) | ||
| to_table[from_unit] = conversion | ||
| from_table[to_unit] = inverse | ||
| end | ||
|
|
||
| table[to_unit] = to_table | ||
| end | ||
| end | ||
|
|
||
| def find_conversion(to:, from:) | ||
| result = find_direct_conversion_cached(to: to, from: from) || find_tree_traversal_conversion(to: to, from: from) | ||
| raise Measured::MissingConversionPath.new(from, to) unless result | ||
| result | ||
| end | ||
|
|
||
| def find_direct_conversion(to:, from:) | ||
| units.each do |unit| | ||
| if unit.name == from && unit.conversion_unit == to | ||
| forward = wrap_conversion(unit.conversion_amount) | ||
| backward = wrap_conversion(unit.inverse_conversion_amount) | ||
| return [forward, backward] | ||
| end | ||
|
|
||
| if unit.name == to && unit.conversion_unit == from | ||
| backward = wrap_conversion(unit.conversion_amount) | ||
| forward = wrap_conversion(unit.inverse_conversion_amount) | ||
| return [forward, backward] | ||
| end | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def wrap_conversion(amount) | ||
| return amount if amount.is_a?(Proc) | ||
|
|
||
| ->(x) { x * amount } | ||
| end | ||
|
|
||
| def find_tree_traversal_conversion(to:, from:) | ||
| identity = ->(x) { x } | ||
| traverse(from: from, to: to, units_remaining: units.map(&:name), forward: identity, backward: identity) | ||
| end | ||
|
|
||
| def traverse(from:, to:, units_remaining:, forward:, backward:) | ||
| units_remaining = units_remaining - [from] | ||
|
|
||
| units_remaining.each do |name| | ||
| pair = find_direct_conversion_cached(from: from, to: name) | ||
| next unless pair | ||
|
|
||
| step_forward, step_backward = pair | ||
| new_forward = compose(step_forward, forward) | ||
| new_backward = compose(backward, step_backward) | ||
|
|
||
| if name == to | ||
| return [new_forward, new_backward] | ||
| else | ||
| result = traverse(from: name, to: to, units_remaining: units_remaining, forward: new_forward, backward: new_backward) | ||
| return result if result | ||
| end | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def compose(outer, inner) | ||
| ->(x) { outer.call(inner.call(x)) } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good refactor.