Skip to main content

link_cli/
query_processor_substitution.rs

1//! The substitution half of [`GenericQueryProcessor`]: turning a matched restriction
2//! plus a substitution pattern into the links to write.
3
4use anyhow::Result;
5use std::collections::{HashMap, HashSet};
6
7use crate::error::LinkError;
8use crate::link::GenericLink as Link;
9use crate::named_type_links::NamedTypeLinks;
10use crate::query_address::QueryAddress;
11use crate::query_processor::GenericQueryProcessor;
12use crate::query_types::Pattern;
13
14impl<T: QueryAddress> GenericQueryProcessor<T> {
15    pub fn preserve_existing_substitution_parts(
16        storage: &mut impl NamedTypeLinks<T>,
17        pattern: &Pattern,
18        solution: &mut HashMap<String, T>,
19        index: T,
20        source: &mut T,
21        target: &mut T,
22        visited_indexes: &mut HashSet<T>,
23    ) -> Result<()> {
24        if !is_normal_index(index) || !storage.exists(index) {
25            return Ok(());
26        }
27
28        if !visited_indexes.insert(index) {
29            return Ok(());
30        }
31
32        let existing_link = storage.get_link(index).ok_or(LinkError::not_found(index))?;
33
34        if should_preserve_existing_part(pattern.source.as_deref(), solution)
35            && can_preserve_existing_part(existing_link, existing_link.source, visited_indexes)
36        {
37            *source = existing_link.source;
38            assign_variable_from_pattern(pattern.source.as_deref(), *source, solution);
39        } else if let Some(bound_source) =
40            resolved_variable_part(pattern.source.as_deref(), solution)
41        {
42            *source = bound_source;
43        }
44
45        if should_preserve_existing_part(pattern.target.as_deref(), solution)
46            && can_preserve_existing_part(existing_link, existing_link.target, visited_indexes)
47        {
48            *target = existing_link.target;
49            assign_variable_from_pattern(pattern.target.as_deref(), *target, solution);
50        } else if let Some(bound_target) =
51            resolved_variable_part(pattern.target.as_deref(), solution)
52        {
53            *target = bound_target;
54        }
55
56        visited_indexes.remove(&index);
57        Ok(())
58    }
59}
60
61fn should_preserve_existing_part<T: QueryAddress>(
62    pattern: Option<&Pattern>,
63    solution: &HashMap<String, T>,
64) -> bool {
65    pattern.is_some_and(|pattern| {
66        pattern.is_leaf() && is_variable(&pattern.index) && !solution.contains_key(&pattern.index)
67    })
68}
69
70fn resolved_variable_part<T: QueryAddress>(
71    pattern: Option<&Pattern>,
72    solution: &HashMap<String, T>,
73) -> Option<T> {
74    pattern.and_then(|pattern| {
75        if pattern.is_leaf() && is_variable(&pattern.index) {
76            solution.get(&pattern.index).copied()
77        } else {
78            None
79        }
80    })
81}
82
83fn assign_variable_from_pattern<T: QueryAddress>(
84    pattern: Option<&Pattern>,
85    value: T,
86    solution: &mut HashMap<String, T>,
87) {
88    if let Some(pattern) = pattern {
89        assign_variable(&pattern.index, value, solution);
90    }
91}
92
93fn assign_variable<T: QueryAddress>(id: &str, value: T, assignments: &mut HashMap<String, T>) {
94    if is_variable(id) {
95        assignments.insert(id.to_string(), value);
96    }
97}
98
99fn can_preserve_existing_part<T: QueryAddress>(
100    existing_link: Link<T>,
101    part: T,
102    visited_indexes: &HashSet<T>,
103) -> bool {
104    existing_link.is_full_point()
105        || existing_link.is_partial_point()
106        || !visited_indexes.contains(&part)
107}
108
109fn is_variable(identifier: &str) -> bool {
110    !identifier.is_empty() && identifier.starts_with('$')
111}
112
113fn is_normal_index<T: QueryAddress>(value: T) -> bool {
114    value != T::from_byte(0) && value != T::MAX
115}