pattern_core/graph/
graph_view.rs1use std::hash::Hash;
10
11use crate::graph::graph_classifier::{GraphClass, GraphClassifier, GraphValue};
12use crate::graph::graph_query::GraphQuery;
13use crate::pattern::Pattern;
14use crate::pattern_graph::PatternGraph;
15use crate::reconcile::{HasIdentity, Mergeable, ReconciliationPolicy, Refinable};
16use crate::subject::Symbol;
17
18pub struct GraphView<Extra, V: GraphValue> {
32 pub view_query: GraphQuery<V>,
33 pub view_elements: Vec<(GraphClass<Extra>, Pattern<V>)>,
34}
35
36impl<Extra: Clone, V: GraphValue + Clone> Clone for GraphView<Extra, V> {
37 fn clone(&self) -> Self {
38 GraphView {
39 view_query: self.view_query.clone(),
40 view_elements: self.view_elements.clone(),
41 }
42 }
43}
44
45fn view_elements_from_graph<Extra, V>(
51 classifier: &GraphClassifier<Extra, V>,
52 graph: &PatternGraph<Extra, V>,
53) -> Vec<(GraphClass<Extra>, Pattern<V>)>
54where
55 Extra: Clone,
56 V: GraphValue + Clone,
57{
58 let mut view_elements: Vec<(GraphClass<Extra>, Pattern<V>)> = Vec::new();
59 for p in graph.pg_nodes.values() {
60 view_elements.push(((classifier.classify)(p), p.clone()));
61 }
62 for p in graph.pg_relationships.values() {
63 view_elements.push(((classifier.classify)(p), p.clone()));
64 }
65 for p in graph.pg_walks.values() {
66 view_elements.push(((classifier.classify)(p), p.clone()));
67 }
68 for p in graph.pg_annotations.values() {
69 view_elements.push(((classifier.classify)(p), p.clone()));
70 }
71 for (_, p) in graph.pg_other.values() {
72 view_elements.push(((classifier.classify)(p), p.clone()));
73 }
74 view_elements
75}
76
77#[cfg(not(feature = "thread-safe"))]
83pub fn from_pattern_graph<Extra, V>(
84 classifier: &GraphClassifier<Extra, V>,
85 graph: &PatternGraph<Extra, V>,
86) -> GraphView<Extra, V>
87where
88 Extra: Clone + 'static,
89 V: GraphValue + Clone + 'static,
90 V::Id: Clone + Eq + Hash + 'static,
91{
92 use std::rc::Rc;
93
94 let rc_graph = Rc::new(PatternGraph {
95 pg_nodes: graph.pg_nodes.clone(),
96 pg_relationships: graph.pg_relationships.clone(),
97 pg_walks: graph.pg_walks.clone(),
98 pg_annotations: graph.pg_annotations.clone(),
99 pg_other: graph.pg_other.clone(),
100 pg_conflicts: graph.pg_conflicts.clone(),
101 });
102 let view_query = crate::pattern_graph::from_pattern_graph(rc_graph);
103 let view_elements = view_elements_from_graph(classifier, graph);
104 GraphView {
105 view_query,
106 view_elements,
107 }
108}
109
110#[cfg(feature = "thread-safe")]
111pub fn from_pattern_graph<Extra, V>(
112 classifier: &GraphClassifier<Extra, V>,
113 graph: &PatternGraph<Extra, V>,
114) -> GraphView<Extra, V>
115where
116 Extra: Clone + Send + Sync + 'static,
117 V: GraphValue + Clone + Send + Sync + 'static,
118 V::Id: Clone + Eq + Hash + Send + Sync + 'static,
119{
120 use std::sync::Arc;
121
122 let arc_graph = Arc::new(PatternGraph {
123 pg_nodes: graph.pg_nodes.clone(),
124 pg_relationships: graph.pg_relationships.clone(),
125 pg_walks: graph.pg_walks.clone(),
126 pg_annotations: graph.pg_annotations.clone(),
127 pg_other: graph.pg_other.clone(),
128 pg_conflicts: graph.pg_conflicts.clone(),
129 });
130 let view_query = crate::pattern_graph::from_pattern_graph(arc_graph);
131 let view_elements = view_elements_from_graph(classifier, graph);
132 GraphView {
133 view_query,
134 view_elements,
135 }
136}
137
138#[allow(dead_code)]
149pub fn from_graph_lens<Extra, V, L>(
150 _classifier: &GraphClassifier<Extra, V>,
151 _lens: L,
152) -> GraphView<Extra, V>
153where
154 V: GraphValue,
155{
156 todo!("from_graph_lens: deferred until GraphLens is ported to pattern-rs")
157}
158
159pub fn materialize<Extra, V>(
169 classifier: &GraphClassifier<Extra, V>,
170 policy: &ReconciliationPolicy<V::MergeStrategy>,
171 view: GraphView<Extra, V>,
172) -> PatternGraph<Extra, V>
173where
174 V: GraphValue<Id = Symbol>
175 + HasIdentity<V, Symbol>
176 + Mergeable
177 + Refinable
178 + PartialEq
179 + Clone
180 + 'static,
181 Extra: 'static,
182{
183 crate::pattern_graph::from_patterns_with_policy(
184 classifier,
185 policy,
186 view.view_elements.into_iter().map(|(_, p)| p),
187 )
188}