### Querying MORK Hypergraph Space with Pattern Matching Source: https://context7.com/egoran2/mork/llms.txt This snippet illustrates how to query MORK's hypergraph space using pattern matching. It first loads sample data into the space and then defines a query pattern to find specific relationships, such as all children of a given parent. The `query` method iterates over matching expressions, and the results are collected and printed. ```rust use mork::space::Space; use mork::{expr, sexpr}; fn main() { let mut space = Space::new(); let data = "(parent alice bob)\n(parent bob charlie)\n(age bob 25)\n"; space.load_sexpr(data.as_bytes(), expr!(space, "$"), expr!(space, "_1")).unwrap(); // Query pattern: (parent alice $x) - find all children of alice let query = expr!(space, "[3] parent alice $"); let mut results = Vec::new(); space.query(query, |_refs, expr| { results.push(sexpr!(space, expr)); }); for result in results { println!("Match: {}", result); } // Output: // Match: (parent alice bob) } ``` -------------------------------- ### Transforming Data in MORK with Pattern Templates Source: https://context7.com/egoran2/mork/llms.txt This code demonstrates data transformation within MORK's hypergraph space using pattern templates. It loads initial data and then applies a transformation rule: changing a '(parent X Y)' relation to a '(child Y X)' relation. The `transform` method modifies the space in place, and subsequent queries can retrieve the restructured data, showcasing the effect of the transformation. ```rust use mork::space::Space; use mork::expr; fn main() { let mut space = Space::new(); let data = "(parent alice bob)\n(parent bob charlie)\n"; space.load_sexpr(data.as_bytes(), expr!(space, "$"), expr!(space, "_1")).unwrap(); // Transform (parent X Y) -> (child Y X) let pattern = expr!(space, "[3] parent $ $"); let template = expr!(space, "[3] child _2 _1"); space.transform(pattern, template); // Query for transformed data let query = expr!(space, "[2] child $"); space.query(query, |_refs, expr| { println!("Child relation: {}", sexpr!(space, expr)); }); // Output: // Child relation: (child bob alice) // Child relation: (child charlie bob) } ``` -------------------------------- ### Loading S-Expression Data with Pattern Matching in MORK Source: https://context7.com/egoran2/mork/llms.txt This Rust code parses MeTTa S-expressions from text, applying pattern-based filtering and prefix insertion. It loads data into the MORK space, using a wildcard pattern and a specified prefix for transformation. The snippet also shows how to dump the filtered results, demonstrating the effect of the pattern and prefix on the loaded data. ```rust use mork::space::Space; use mork::{expr, sexpr}; use mork_bytestring::Expr; fn main() { let mut space = Space::new(); let data = r#"( parent alice bob) (parent alice charlie) (parent bob dana) (age bob 25) (age dana 3) "#; // Load all S-expressions with pattern $ (wildcard) and prefix "family" let pattern = expr!(space, "$"); let prefix = expr!(space, "[2] family _1"); match space.load_sexpr(data.as_bytes(), pattern, prefix) { Ok(count) => { println!("Loaded {} expressions", count); // Dump filtered results let mut output = Vec::new(); space.dump_sexpr(expr!(space, "$"), expr!(space, "_1"), &mut output); println!("{}", String::from_utf8(output).unwrap()); } Err(e) => eprintln!("Parse error: {}", e) } } ``` -------------------------------- ### Mork Space Composition Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Combines multiple space paths or structures into a more compact representation. It uses '.' for path concatenation and '|' for alternative paths within a structure. This is useful for simplifying complex space definitions. ```Mork Syntax x.y Foo.(bar | baz | cux) (x | y | z).(a | b) (Foo.(Bar | Baz)).(A.(1 | 2)) ``` -------------------------------- ### Loading JSON Data into MORK Space Source: https://context7.com/egoran2/mork/llms.txt This snippet demonstrates how to load JSON data into MORK's S-expression hypergraph space. It parses JSON, flattens it into key-path tuples, and stores them. The function returns the count of extracted paths and handles potential errors during loading. The loaded data can then be dumped as S-expressions. ```rust use mork::space::Space; fn main() { let mut space = Space::new(); let json = r#"{ "name": "Alice", "age": 30, "tags": ["rust", "ai"], "address": { "city": "NYC", "zip": "10001" } }"#; // Load JSON - returns count of extracted paths match space.load_json(json.as_bytes()) { Ok(count) => println!("Loaded {} paths", count), Err(e) => eprintln!("Error: {}", e) } // Dump as S-expressions let mut output = Vec::new(); space.dump_all_sexpr(&mut output).unwrap(); println!("{}", String::from_utf8(output).unwrap()); // Output: // (name Alice) // (age 30) // (tags (0 rust)) // (tags (1 ai)) // (address (city NYC)) // (address (zip 10001)) } ``` -------------------------------- ### Mork Instantiation Operation Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Filters a space to include only elements that are not instances of any other element in the space. An element 'e' is an instance of 'e'' if 'e'' is lexicographically smaller than 'e'. Formula: {e | e∈s and no e'∈s such that e' < e}. ```Mork Syntax v s ``` -------------------------------- ### Mork Transform Operation Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Transforms paths within a space based on a pattern and a replacement. It takes a space 'x', a pattern path 'p', and a replacement path 'q'. Paths in 'x' are matched against 'p', and if they match, they are transformed into 'q'. The wildcard '_' can be used in paths. ```Mork Syntax x \p -> q // Example: x = Foo.(Bar.(1 | 2 | 3) | Baz.(A | B | C) | Cux.(Red | Blue)) // p = $_.Cux.$c // q = Result.Color.$c // Result: Result.Color.Red | Result.Color.Blue ``` -------------------------------- ### Mork Space Union Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Creates a new space that contains all elements from the participating spaces. It is denoted by the '|' operator. This operation is analogous to a set union. ```Mork Syntax x | y a | b | c ``` -------------------------------- ### Mork Space Restriction Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Filters a space to include only paths that have a corresponding prefix in the right-hand space. It is denoted by the '<|' operator. This is useful for selecting subsets of a space based on prefix matching. ```Mork Syntax x <| y // Example: x = Foo.(Bar.(1 | 2 | 3) | Baz.(A | B | C) | Cux.(Red | Blue)) // y = Foo.(Bar | Baz) // Result: Foo.(Bar.(1 | 2 | 3) | Baz.(A | B | C)) ``` -------------------------------- ### Mork Space Subtraction Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Creates a new space containing elements from the first space that are not present in the second space. It is denoted by the '\' operator. This operation is analogous to a set difference. ```Mork Syntax x \ y // Example: x = a | b | c; y = c | e -> result = a | b ``` -------------------------------- ### Mork Drophead Operation Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Removes the common prefix from all elements within a space. It is denoted by 'drophead s'. This operation simplifies paths by removing leading components that are common to all elements. ```Mork Syntax drophead s // Example: s = Foo.(Bar.(1 | 2 | 3) | Baz.(A | B | C)) | Cuz.(A | B) // Result: Bar.(1 | 2 | 3) | Baz.(A | B | C) | A | B ``` -------------------------------- ### Mork Subspace Operation Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Extracts a subspace from a given space 's' based on a prefix path 'p'. It returns the elements of 's' that have 'p' as a prefix, with 'p' removed from the beginning of each element. Formula: {e dropPrefix p | e∈s e hasPrefix p}. ```Mork Syntax s(p) // Example: s = Foo.(Bar.(1 | 2 | 3) | Baz.(A | B | C)), p = Foo.Baz // Result: (A | B | C) ``` -------------------------------- ### Mork Space Intersection Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Creates a new space containing only the elements that are common to both participating spaces. It is denoted by the '&' operator. This operation is analogous to a set intersection. ```Mork Syntax x & y // Example: x = a | b | c; y = a | c | e -> result = a | c ``` -------------------------------- ### Mork Subsumption Operation Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Filters a space to include only elements that are not subsumed by any other element in the space. An element 'e' is subsumed by 'e'' if 'e'' is lexicographically greater than 'e'. Formula: {e | e∈s and no e'∈s such that e' > e}. ```Mork Syntax ^ s ``` -------------------------------- ### Mork Space Sharing Operation Source: https://github.com/egoran2/mork/blob/main/notes/space_ops.md Finds the longest shared prefixes between elements of two spaces, analogous to the Greatest Common Divisor (GCD). It is denoted by '**'. The laws state that x is a prefix of (x ** y) and y is a prefix of (x ** y). ```Mork Syntax x ** y // Laws: x = x <| (x ** y), y = y <| (x ** y) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.