Repository navigation
Rework the 'Math' expression family as 'Math Function' / 'Math Formula', and add 'Math Running' / 'Math Aggregate' #4917
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| https://github.com/Keavon/graphite-branded-assets/archive/4e42efe8dbf2ae2d82e12123aa9c103d5740a465.tar.gz | ||
| 6b7883a26952c77aad56758a11207761c2fcd1b61bf6dd5996c3cf9d4eb1cb5e | ||
| https://github.com/Keavon/graphite-branded-assets/archive/70b260c21afad29140bbc63ddab952fec3dd024c.tar.gz | ||
| 5b05e7f4449b5f962755542e9b156891bbf2e0a87b7fc90798b8352d9225757d |
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
107 changes: 107 additions & 0 deletions
107
document/migrations/nodes/src/math_nodes/math_formula_from_math_f.rs
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,107 @@ | ||
| use migration_core::{Node, Target, decl, literal_of, migration}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| const EXPRESSION: usize = 1; | ||
|
|
||
| /// Frozen mirror of the `TaggedValue` variant this migration reads. | ||
| #[derive(Serialize, Deserialize)] | ||
| enum Tagged { | ||
| String(String), | ||
| } | ||
|
|
||
| /// The lone operators 'Math f(…)' applied across its items, in every spelling its classifier read when the node was split. | ||
| const OPERATORS: &[&str] = &["+", "-", "*", "/", "^", "&&", "∧", "||", "∨", "<", "<=", "≤", ">", ">=", "≥", "==", "!=", "≠"]; | ||
|
|
||
| /// The variadic functions 'Math f(…)' called with all its items when given a lone name, also spelled with a leading `\`. | ||
| const FUNCTIONS: &[&str] = &[ | ||
| "hypot", | ||
| "min", | ||
| "max", | ||
| "mean", | ||
| "median", | ||
| "variance", | ||
| "variancepop", | ||
| "stdev", | ||
| "stdevpop", | ||
| "geomean", | ||
| "harmmean", | ||
| "rms", | ||
| "mode", | ||
| "count", | ||
| "xor", | ||
| "gcd", | ||
| "lcm", | ||
| ]; | ||
|
|
||
| /// 'Math f(…)' split by the kind of string it held: a lone operator or function name, which it applied across its items, moves to | ||
| /// 'Math Aggregate', and an expression stays as 'Math Formula'. A wired string can't be read before it runs, so it stays an expression. | ||
| #[migration("math_nodes::MathFNode"@1 => "math_nodes::MathFormulaNode"@2)] | ||
| pub fn math_formula_from_math_f(node: &Node, target: &mut Target) { | ||
| let Some(slot) = node.inputs().get(EXPRESSION) else { return }; | ||
| let Some(Tagged::String(expression)) = literal_of::<Tagged>(&slot.input) else { return }; | ||
|
|
||
| let token = expression.trim(); | ||
| let function = token.strip_prefix('\\').unwrap_or(token); | ||
| if OPERATORS.contains(&token) || FUNCTIONS.contains(&function) { | ||
| target.set(decl!("math_nodes::MathAggregateNode"@1)); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use migration_core::{Declaration, NodeInput, literal}; | ||
| use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; | ||
|
|
||
| const MATH_F: Declaration = decl!("math_nodes::MathFNode"@1); | ||
|
|
||
| fn contract() -> SourceContract { | ||
| // Only the items can read another 'Math f(…)', since the expression and output type don't take its number | ||
| SourceContract { | ||
| reads_own_output: |slot| slot == 0, | ||
| ..SourceContract::new(3, |random, slot| match slot { | ||
| EXPRESSION => literal(Tagged::String(["+", "a * b", "\\max", " ≤ ", "min(a, b)"][random.below(5)].to_string())), | ||
| _ => literal(random.below(4) as f64), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| fn migrated_declaration(expression: NodeInput) -> Option<Declaration> { | ||
| let mut fixture = Fixture::new(&MathFormulaFromMathF); | ||
| let anchor = fixture.add(MATH_F, vec![literal(1.), expression, literal(0.)]); | ||
| let migrated = fixture.migrate(&MathFormulaFromMathF).unwrap(); | ||
| fixture.declaration_of(&migrated, anchor) | ||
| } | ||
|
|
||
| #[test] | ||
| fn rewrites_history() { | ||
| check_commutation(&MathFormulaFromMathF, &contract()); | ||
| check_undo(&MathFormulaFromMathF, &contract()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn lone_operators_and_functions_become_aggregates() { | ||
| let aggregate = Some(decl!("math_nodes::MathAggregateNode"@1)); | ||
| let formula = Some(decl!("math_nodes::MathFormulaNode"@2)); | ||
| let expression = |text: &str| literal(Tagged::String(text.to_string())); | ||
|
|
||
| assert_eq!(migrated_declaration(expression("+")), aggregate); | ||
| assert_eq!(migrated_declaration(expression(" ≠ ")), aggregate); | ||
| assert_eq!(migrated_declaration(expression("max")), aggregate); | ||
| assert_eq!(migrated_declaration(expression("\\mean")), aggregate); | ||
| assert_eq!(migrated_declaration(expression("a + b")), formula); | ||
| assert_eq!(migrated_declaration(expression("max(a, b)")), formula); | ||
| assert_eq!(migrated_declaration(expression("")), formula); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_wired_expression_becomes_a_formula() { | ||
| let mut fixture = Fixture::new(&MathFormulaFromMathF); | ||
| let source = fixture.add(MATH_F, vec![literal(1.), literal(Tagged::String("+".to_string())), literal(0.)]); | ||
| let anchor = fixture.add(MATH_F, vec![literal(1.), NodeInput::Node { id: source, index: 0 }, literal(0.)]); | ||
| let migrated = fixture.migrate(&MathFormulaFromMathF).unwrap(); | ||
|
|
||
| assert_eq!(fixture.declaration_of(&migrated, anchor), Some(decl!("math_nodes::MathFormulaNode"@2))); | ||
| assert_eq!(fixture.declaration_of(&migrated, source), Some(decl!("math_nodes::MathAggregateNode"@1))); | ||
| } | ||
| } | ||
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 @@ | ||
| //! Migrations of the nodes in `math_nodes`. | ||
|
|
||
| mod math_formula_from_math_f; | ||
| mod renames; |
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,5 @@ | ||
| //! Nodes that only moved to a new identifier, or changed version with stored nodes staying as they are. | ||
|
|
||
| use migration_core::rename; | ||
|
|
||
| rename!("math_nodes::MathFxNode"@1 => "math_nodes::MathFunctionNode"@2); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.