mockforge_world_state/
query.rs

1//! World State Query - Flexible querying of world state
2//!
3//! This module provides a query builder for filtering and searching
4//! world state snapshots.
5
6use crate::model::{NodeType, StateEdge, StateLayer, StateNode};
7use serde::{Deserialize, Serialize};
8use std::collections::HashSet;
9
10/// Query for filtering world state
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct WorldStateQuery {
13    /// Filter by node types
14    #[serde(default)]
15    pub node_types: Option<HashSet<NodeType>>,
16    /// Filter by layers
17    #[serde(default)]
18    pub layers: Option<HashSet<StateLayer>>,
19    /// Filter by node IDs
20    #[serde(default)]
21    pub node_ids: Option<HashSet<String>>,
22    /// Filter by relationship types
23    #[serde(default)]
24    pub relationship_types: Option<HashSet<String>>,
25    /// Include edges in results
26    #[serde(default = "default_true")]
27    pub include_edges: bool,
28    /// Maximum depth for traversal (for graph queries)
29    #[serde(default)]
30    pub max_depth: Option<usize>,
31}
32
33fn default_true() -> bool {
34    true
35}
36
37impl Default for WorldStateQuery {
38    fn default() -> Self {
39        Self {
40            node_types: None,
41            layers: None,
42            node_ids: None,
43            relationship_types: None,
44            include_edges: true,
45            max_depth: None,
46        }
47    }
48}
49
50impl WorldStateQuery {
51    /// Create a new empty query
52    pub fn new() -> Self {
53        Self::default()
54    }
55
56    /// Filter by node types
57    pub fn with_node_types(mut self, types: HashSet<NodeType>) -> Self {
58        self.node_types = Some(types);
59        self
60    }
61
62    /// Filter by layers
63    pub fn with_layers(mut self, layers: HashSet<StateLayer>) -> Self {
64        self.layers = Some(layers);
65        self
66    }
67
68    /// Filter by node IDs
69    pub fn with_node_ids(mut self, ids: HashSet<String>) -> Self {
70        self.node_ids = Some(ids);
71        self
72    }
73
74    /// Filter by relationship types
75    pub fn with_relationship_types(mut self, types: HashSet<String>) -> Self {
76        self.relationship_types = Some(types);
77        self
78    }
79
80    /// Set whether to include edges
81    pub fn include_edges(mut self, include: bool) -> Self {
82        self.include_edges = include;
83        self
84    }
85
86    /// Set maximum traversal depth
87    pub fn with_max_depth(mut self, depth: usize) -> Self {
88        self.max_depth = Some(depth);
89        self
90    }
91
92    /// Check if a node matches this query
93    pub fn matches_node(&self, node: &StateNode) -> bool {
94        // Check node type filter
95        if let Some(ref types) = self.node_types {
96            if !types.contains(&node.node_type) {
97                return false;
98            }
99        }
100
101        // Check layer filter
102        if let Some(ref layers) = self.layers {
103            if !layers.contains(&node.layer) {
104                return false;
105            }
106        }
107
108        // Check node ID filter
109        if let Some(ref ids) = self.node_ids {
110            if !ids.contains(&node.id) {
111                return false;
112            }
113        }
114
115        true
116    }
117
118    /// Check if an edge matches this query
119    pub fn matches_edge(&self, edge: &StateEdge) -> bool {
120        // Check relationship type filter
121        if let Some(ref types) = self.relationship_types {
122            if !types.contains(&edge.relationship_type) {
123                return false;
124            }
125        }
126
127        true
128    }
129}