Skip to main content

rudof_rdf/rdf_core/visualizer/
rdf_visualizer_config.rs

1use crate::rdf_core::visualizer::style::{
2    ArrowStyle, DEFAULT_OBJECT_ARROW_STYLE, DEFAULT_PREDICATE_ARROW_STYLE, DEFAULT_SUBJECT_ARROW_STYLE, Style, UmlColor,
3};
4use serde::{Deserialize, Serialize};
5
6/// Default text subject label used when no custom values are provided.
7const DEFAULT_SUBJECT_TEXT: &str = "subj";
8/// Default text predicate label used when no custom values are provided.
9const DEFAULT_PREDICATE_TEXT: &str = "pred";
10/// Default text object label used when no custom values are provided.
11const DEFAULT_OBJECT_TEXT: &str = "obj";
12
13/// Enum representing the available UML node shapes for visualization.
14#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
15pub enum UmlShape {
16    /// Cloud shape.
17    Cloud,
18    /// Rectangle shape.
19    Rectangle,
20}
21
22/// Configuration object controlling the visual appearance of RDF graphs.
23///
24/// This struct allows customization of node and edge styles, labels, and shapes for different RDF term types.
25/// All fields are optional and will fall back to default values if not specified.
26#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
27pub struct RDFVisualizationConfig {
28    // === URI node styling ===
29    /// Stroke color of URI nodes.
30    uri_line_color: Option<UmlColor>,
31    /// Stroke thickness of URI nodes.
32    uri_line_thickness: Option<u32>,
33    /// Background color of URI nodes.
34    uri_background_color: Option<UmlColor>,
35    /// Corner radius of URI node shapes.
36    uri_round_corner: Option<u32>,
37
38    // === Blank node styling ===
39    /// Stroke color of blank nodes.
40    bnode_line_color: Option<UmlColor>,
41    /// Stroke thickness of blank nodes.
42    bnode_line_thickness: Option<u32>,
43    /// Background color of blank nodes.
44    bnode_background_color: Option<UmlColor>,
45    /// Corner radius of blank node shapes.
46    bnode_round_corner: Option<u32>,
47
48    // === Literal node styling ===
49    /// Stroke color of literal nodes.
50    literal_line_color: Option<UmlColor>,
51    /// Stroke thickness of literal nodes.
52    literal_line_thickness: Option<u32>,
53    /// Background color of literal nodes.
54    literal_background_color: Option<UmlColor>,
55    /// Corner radius of literal node shapes.
56    literal_round_corner: Option<u32>,
57
58    // === Reifier node styling ===
59    /// Stroke color of reifier nodes.
60    reifier_line_color: Option<UmlColor>,
61    /// Stroke thickness of reifier nodes.
62    reifier_line_thickness: Option<u32>,
63    /// Background color of reifier nodes.
64    reifier_background_color: Option<UmlColor>,
65    /// Corner radius of reifier node shapes.
66    reifier_round_corner: Option<u32>,
67
68    // === Asserted triple term styling ===
69    /// Stroke color of asserted triple terms.
70    asserted_line_color: Option<UmlColor>,
71    /// Stroke thickness of asserted triple terms.
72    asserted_line_thickness: Option<u32>,
73    /// Background color of asserted triple terms.
74    asserted_background_color: Option<UmlColor>,
75    /// Corner radius of asserted triple term shapes.
76    asserted_round_corner: Option<u32>,
77
78    // === Non-asserted triple term styling ===
79    /// Stroke color of non-asserted triple terms.
80    non_asserted_line_color: Option<UmlColor>,
81    /// Stroke thickness of non-asserted triple terms.
82    non_asserted_line_thickness: Option<u32>,
83    /// Background color of non-asserted triple terms.
84    non_asserted_background_color: Option<UmlColor>,
85    /// Corner radius of non-asserted triple term shapes.
86    non_asserted_round_corner: Option<u32>,
87
88    // === Labels and shapes ===
89    /// Label for subject triple term.
90    triple_term_subject_label: Option<String>,
91    /// Label for predicate triple term.
92    triple_term_predicate_label: Option<String>,
93    /// Label for object triple term.
94    triple_term_object_label: Option<String>,
95    /// Label for reification.
96    reifies_label: Option<String>,
97    /// Shape for unasserted triple.
98    unasserted_triple_shape: Option<UmlShape>,
99    /// Shape for asserted triple.
100    asserted_triple_shape: Option<UmlShape>,
101
102    // === Arrow styles ===
103    /// Arrow style for subject.
104    subject_arrow_style: Option<ArrowStyle>,
105    /// Arrow style for predicate.
106    predicate_arrow_style: Option<ArrowStyle>,
107    /// Arrow style for object.
108    object_arrow_style: Option<ArrowStyle>,
109
110    // === Text for subject, predicate, object ===
111    /// Text for subject.
112    subject_text: Option<String>,
113    /// Text for predicate.
114    predicate_text: Option<String>,
115    /// Text for object.
116    object_text: Option<String>,
117}
118
119impl RDFVisualizationConfig {
120    /// Creates a new configuration with default values.
121    pub fn new() -> Self {
122        Self::default()
123    }
124
125    /// Sets the background color for literal nodes.
126    pub fn with_literal_background_color(mut self, color: UmlColor) -> Self {
127        self.literal_background_color = Some(color);
128        self
129    }
130
131    /// Sets the label for the subject triple term.
132    pub fn with_triple_term_subject_label(mut self, label: String) -> Self {
133        self.triple_term_subject_label = Some(label);
134        self
135    }
136
137    /// Sets the label for the predicate triple term.
138    pub fn with_triple_term_predicate_label(mut self, label: String) -> Self {
139        self.triple_term_predicate_label = Some(label);
140        self
141    }
142
143    /// Sets the label for the object triple term.
144    pub fn with_triple_term_object_label(mut self, label: String) -> Self {
145        self.triple_term_object_label = Some(label);
146        self
147    }
148
149    /// Sets the label for reification.
150    pub fn with_reifies_label(mut self, label: String) -> Self {
151        self.reifies_label = Some(label);
152        self
153    }
154
155    /// Gets the stroke color for URI nodes.
156    pub fn uri_line_color(&self) -> UmlColor {
157        self.uri_line_color.clone().unwrap_or(URI_LINE_COLOR)
158    }
159
160    /// Gets the stroke thickness for URI nodes.
161    pub fn uri_line_thickness(&self) -> u32 {
162        self.uri_line_thickness.unwrap_or(URI_LINE_THICKNESS)
163    }
164
165    /// Gets the background color for URI nodes.
166    pub fn uri_background_color(&self) -> UmlColor {
167        self.uri_background_color.clone().unwrap_or(URI_BACKGROUND_COLOR)
168    }
169
170    /// Gets the corner radius for URI node shapes.
171    pub fn uri_round_corner(&self) -> u32 {
172        self.uri_round_corner.unwrap_or(URI_ROUND_CORNER)
173    }
174
175    /// Gets the stroke color for blank nodes.
176    pub fn bnode_line_color(&self) -> UmlColor {
177        self.bnode_line_color.clone().unwrap_or(BNODE_LINE_COLOR)
178    }
179
180    /// Gets the stroke thickness for blank nodes.
181    pub fn bnode_line_thickness(&self) -> u32 {
182        self.bnode_line_thickness.unwrap_or(BNODE_LINE_THICKNESS)
183    }
184
185    /// Gets the background color for blank nodes.
186    pub fn bnode_background_color(&self) -> UmlColor {
187        self.bnode_background_color.clone().unwrap_or(BNODE_BACKGROUND_COLOR)
188    }
189
190    /// Gets the corner radius for blank node shapes.
191    pub fn bnode_round_corner(&self) -> u32 {
192        self.bnode_round_corner.unwrap_or(BNODE_ROUND_CORNER)
193    }
194
195    /// Gets the stroke color for literal nodes.
196    pub fn literal_line_color(&self) -> UmlColor {
197        self.literal_line_color.clone().unwrap_or(LITERAL_LINE_COLOR)
198    }
199
200    /// Gets the stroke thickness for literal nodes.
201    pub fn literal_line_thickness(&self) -> u32 {
202        self.literal_line_thickness.unwrap_or(LITERAL_LINE_THICKNESS)
203    }
204
205    /// Gets the background color for literal nodes.
206    pub fn literal_background_color(&self) -> UmlColor {
207        self.literal_background_color
208            .clone()
209            .unwrap_or(LITERAL_BACKGROUND_COLOR)
210    }
211
212    /// Gets the corner radius for literal node shapes.
213    pub fn literal_round_corner(&self) -> u32 {
214        self.literal_round_corner.unwrap_or(LITERAL_ROUND_CORNER)
215    }
216
217    /// Gets the stroke color for reifier nodes.
218    pub fn reifier_line_color(&self) -> UmlColor {
219        self.reifier_line_color.clone().unwrap_or(REIFIER_LINE_COLOR)
220    }
221
222    /// Gets the stroke thickness for reifier nodes.
223    pub fn reifier_line_thickness(&self) -> u32 {
224        self.reifier_line_thickness.unwrap_or(REIFIER_LINE_THICKNESS)
225    }
226
227    /// Gets the background color for reifier nodes.
228    pub fn reifier_background_color(&self) -> UmlColor {
229        self.reifier_background_color
230            .clone()
231            .unwrap_or(REIFIER_BACKGROUND_COLOR)
232    }
233
234    /// Gets the corner radius for reifier node shapes.
235    pub fn reifier_round_corner(&self) -> u32 {
236        self.reifier_round_corner.unwrap_or(REIFIER_ROUND_CORNER)
237    }
238
239    /// Gets the stroke color for asserted triple terms.
240    pub fn asserted_line_color(&self) -> UmlColor {
241        self.asserted_line_color.clone().unwrap_or(URI_LINE_COLOR)
242    }
243
244    /// Gets the stroke thickness for asserted triple terms.
245    pub fn asserted_line_thickness(&self) -> u32 {
246        self.asserted_line_thickness.unwrap_or(URI_LINE_THICKNESS)
247    }
248
249    /// Gets the background color for asserted triple terms.
250    pub fn asserted_background_color(&self) -> UmlColor {
251        self.asserted_background_color.clone().unwrap_or(URI_BACKGROUND_COLOR)
252    }
253
254    /// Gets the corner radius for asserted triple term shapes.
255    pub fn asserted_round_corner(&self) -> u32 {
256        self.asserted_round_corner.unwrap_or(URI_ROUND_CORNER)
257    }
258
259    /// Gets the stroke color for non-asserted triple terms.
260    pub fn non_asserted_line_color(&self) -> UmlColor {
261        self.non_asserted_line_color.clone().unwrap_or(BNODE_LINE_COLOR)
262    }
263
264    /// Gets the stroke thickness for non-asserted triple terms.
265    pub fn non_asserted_line_thickness(&self) -> u32 {
266        self.non_asserted_line_thickness.unwrap_or(BNODE_LINE_THICKNESS)
267    }
268
269    /// Gets the background color for non-asserted triple terms.
270    pub fn non_asserted_background_color(&self) -> UmlColor {
271        self.non_asserted_background_color
272            .clone()
273            .unwrap_or(BNODE_BACKGROUND_COLOR)
274    }
275
276    /// Gets the corner radius for non-asserted triple term shapes.
277    pub fn non_asserted_round_corner(&self) -> u32 {
278        self.non_asserted_round_corner.unwrap_or(BNODE_ROUND_CORNER)
279    }
280
281    /// Returns a `Style` object constructed from this configuration.
282    pub fn get_style(&self) -> Style {
283        Style::from_config(self)
284    }
285
286    /// Gets the arrow style for subject edges.
287    pub fn get_subject_arrow_style(&self) -> ArrowStyle {
288        self.subject_arrow_style.clone().unwrap_or(DEFAULT_SUBJECT_ARROW_STYLE)
289    }
290
291    /// Gets the label text for subject edges.
292    pub fn get_subject_text(&self) -> String {
293        self.subject_text.clone().unwrap_or(DEFAULT_SUBJECT_TEXT.into())
294    }
295
296    /// Gets the arrow style for predicate edges.
297    pub fn get_predicate_arrow_style(&self) -> ArrowStyle {
298        self.predicate_arrow_style
299            .clone()
300            .unwrap_or(DEFAULT_PREDICATE_ARROW_STYLE)
301    }
302
303    /// Gets the label text for predicate edges.
304    pub fn get_predicate_text(&self) -> String {
305        self.predicate_text.clone().unwrap_or(DEFAULT_PREDICATE_TEXT.into())
306    }
307
308    /// Gets the arrow style for object edges.
309    pub fn get_object_arrow_style(&self) -> ArrowStyle {
310        self.object_arrow_style.clone().unwrap_or(DEFAULT_OBJECT_ARROW_STYLE)
311    }
312
313    /// Gets the label text for object edges.
314    pub fn get_object_text(&self) -> String {
315        self.object_text.clone().unwrap_or(DEFAULT_OBJECT_TEXT.into())
316    }
317}
318
319// === Default values for visualizer configuration ===
320
321/// Default stroke color for URI nodes.
322const URI_LINE_COLOR: UmlColor = UmlColor::Blue;
323/// Default stroke thickness for URI nodes.
324const URI_LINE_THICKNESS: u32 = 1;
325/// Default background color for URI nodes.
326const URI_BACKGROUND_COLOR: UmlColor = UmlColor::White;
327/// Default corner radius for URI node shapes.
328const URI_ROUND_CORNER: u32 = 25;
329
330/// Default stroke color for blank nodes.
331const BNODE_LINE_COLOR: UmlColor = UmlColor::Blue;
332/// Default stroke thickness for blank nodes.
333const BNODE_LINE_THICKNESS: u32 = 1;
334/// Default background color for blank nodes.
335const BNODE_BACKGROUND_COLOR: UmlColor = UmlColor::Gray;
336/// Default corner radius for blank node shapes.
337const BNODE_ROUND_CORNER: u32 = 25;
338
339/// Default stroke color for literal nodes.
340const LITERAL_LINE_COLOR: UmlColor = UmlColor::Black;
341/// Default stroke thickness for literal nodes.
342const LITERAL_LINE_THICKNESS: u32 = 1;
343/// Default background color for literal nodes.
344const LITERAL_BACKGROUND_COLOR: UmlColor = UmlColor::Cyan;
345/// Default corner radius for literal node shapes.
346const LITERAL_ROUND_CORNER: u32 = 0;
347
348/// Default stroke color for reifier nodes.
349const REIFIER_LINE_COLOR: UmlColor = UmlColor::Black;
350/// Default stroke thickness for reifier nodes.
351const REIFIER_LINE_THICKNESS: u32 = 1;
352/// Default background color for reifier nodes.
353const REIFIER_BACKGROUND_COLOR: UmlColor = UmlColor::Yellow;
354/// Default corner radius for reifier node shapes.
355const REIFIER_ROUND_CORNER: u32 = 0;
356
357/// Default stroke color for asserted triple terms.
358const ASSERTED_LINE_COLOR: UmlColor = UmlColor::Black;
359/// Default stroke thickness for asserted triple terms.
360const ASSERTED_LINE_THICKNESS: u32 = 2;
361/// Default background color for asserted triple terms.
362const ASSERTED_BACKGROUND_COLOR: UmlColor = UmlColor::White;
363/// Default corner radius for asserted triple term shapes.
364const ASSERTED_ROUND_CORNER: u32 = 0;
365
366/// Default stroke color for non-asserted triple terms.
367const NON_ASSERTED_LINE_COLOR: UmlColor = UmlColor::Blue;
368/// Default stroke thickness for non-asserted triple terms.
369const NON_ASSERTED_LINE_THICKNESS: u32 = 2;
370/// Default background color for non-asserted triple terms.
371const NON_ASSERTED_BACKGROUND_COLOR: UmlColor = UmlColor::White;
372/// Default corner radius for non-asserted triple term shapes.
373const NON_ASSERTED_ROUND_CORNER: u32 = 0;
374
375/// Default label for subject triple term.
376const TRIPLE_TERM_SUBJECT_LABEL: &str = "subject";
377/// Default label for predicate triple term.
378const TRIPLE_TERM_PREDICATE_LABEL: &str = "predicate";
379/// Default label for object triple term.
380const TRIPLE_TERM_OBJECT_LABEL: &str = "object";
381/// Default label for reification.
382const REIFIES_LABEL: &str = "reifies";
383
384/// Default shape for asserted triple.
385const ASSERTED_TRIPLE_SHAPE: UmlShape = UmlShape::Rectangle;
386/// Default shape for non-asserted triple.
387const NON_ASSERTED_TRIPLE_SHAPE: UmlShape = UmlShape::Cloud;
388
389impl Default for RDFVisualizationConfig {
390    /// Returns a configuration with all fields set to their default values.
391    fn default() -> Self {
392        RDFVisualizationConfig {
393            uri_line_color: Some(URI_LINE_COLOR),
394            uri_line_thickness: Some(URI_LINE_THICKNESS),
395            uri_background_color: Some(URI_BACKGROUND_COLOR),
396            uri_round_corner: Some(URI_ROUND_CORNER),
397
398            bnode_line_color: Some(BNODE_LINE_COLOR),
399            bnode_line_thickness: Some(BNODE_LINE_THICKNESS),
400            bnode_background_color: Some(BNODE_BACKGROUND_COLOR),
401            bnode_round_corner: Some(BNODE_ROUND_CORNER),
402
403            literal_line_color: Some(LITERAL_LINE_COLOR),
404            literal_line_thickness: Some(LITERAL_LINE_THICKNESS),
405            literal_background_color: Some(LITERAL_BACKGROUND_COLOR),
406            literal_round_corner: Some(LITERAL_ROUND_CORNER),
407
408            reifier_line_color: Some(REIFIER_LINE_COLOR),
409            reifier_line_thickness: Some(REIFIER_LINE_THICKNESS),
410            reifier_background_color: Some(REIFIER_BACKGROUND_COLOR),
411            reifier_round_corner: Some(REIFIER_ROUND_CORNER),
412
413            asserted_line_color: Some(ASSERTED_LINE_COLOR),
414            asserted_line_thickness: Some(ASSERTED_LINE_THICKNESS),
415            asserted_background_color: Some(ASSERTED_BACKGROUND_COLOR),
416            asserted_round_corner: Some(ASSERTED_ROUND_CORNER),
417
418            non_asserted_line_color: Some(NON_ASSERTED_LINE_COLOR),
419            non_asserted_line_thickness: Some(NON_ASSERTED_LINE_THICKNESS),
420            non_asserted_background_color: Some(NON_ASSERTED_BACKGROUND_COLOR),
421            non_asserted_round_corner: Some(NON_ASSERTED_ROUND_CORNER),
422
423            triple_term_subject_label: Some(TRIPLE_TERM_SUBJECT_LABEL.into()),
424            triple_term_predicate_label: Some(TRIPLE_TERM_PREDICATE_LABEL.into()),
425            triple_term_object_label: Some(TRIPLE_TERM_OBJECT_LABEL.into()),
426
427            reifies_label: Some(REIFIES_LABEL.into()),
428            unasserted_triple_shape: Some(NON_ASSERTED_TRIPLE_SHAPE),
429            asserted_triple_shape: Some(ASSERTED_TRIPLE_SHAPE),
430
431            subject_arrow_style: Some(DEFAULT_SUBJECT_ARROW_STYLE),
432            predicate_arrow_style: Some(DEFAULT_PREDICATE_ARROW_STYLE),
433            object_arrow_style: Some(DEFAULT_OBJECT_ARROW_STYLE),
434
435            subject_text: Some(DEFAULT_SUBJECT_TEXT.into()),
436            predicate_text: Some(DEFAULT_PREDICATE_TEXT.into()),
437            object_text: Some(DEFAULT_OBJECT_TEXT.into()),
438        }
439    }
440}