Skip to main content

rudof_rdf/rdf_core/visualizer/utils/
usage_count.rs

1use std::fmt::Display;
2
3/// Tracks how often an entity is used in different roles within triples.
4pub struct UsageCount {
5    /// Number of times the entity is used as a predicate.
6    as_predicate: usize,
7    /// Number of times the entity is used as a predicate in a triple.
8    as_predicate_in_triple: usize,
9    /// Number of times the entity is used as a subject.
10    as_subject: usize,
11    /// Number of times the entity is used as a subject in a triple.
12    as_subject_in_triple: usize,
13    /// Number of times the entity is used as an object.
14    as_object: usize,
15    /// Number of times the entity is used as an object in a triple.
16    as_object_in_triple: usize,
17}
18
19impl UsageCount {
20    /// Creates a new [`UsageCount`] with all counters initialized to zero.
21    pub fn new() -> Self {
22        UsageCount {
23            as_predicate: 0,
24            as_subject: 0,
25            as_object: 0,
26            as_predicate_in_triple: 0,
27            as_subject_in_triple: 0,
28            as_object_in_triple: 0,
29        }
30    }
31
32    /// Returns the number of times the entity was used as a predicate.
33    pub fn as_predicate(&self) -> usize {
34        self.as_predicate
35    }
36
37    /// Returns the number of times the entity was used as a subject.
38    pub fn as_subject(&self) -> usize {
39        self.as_subject
40    }
41
42    /// Returns the number of times the entity was used as an object.
43    pub fn as_object(&self) -> usize {
44        self.as_object
45    }
46
47    /// Returns `true` if the entity appears in at least one triple.
48    pub fn in_triple(&self) -> bool {
49        self.as_predicate_in_triple > 0 || self.as_subject_in_triple > 0 || self.as_object_in_triple > 0
50    }
51
52    /// Increments the predicate usage counter.
53    pub fn increment_as_predicate(&mut self) {
54        self.as_predicate += 1;
55    }
56
57    /// Increments the subject usage counter.
58    pub fn increment_as_subject(&mut self) {
59        self.as_subject += 1;
60    }
61
62    /// Increments the object usage counter.
63    pub fn increment_as_object(&mut self) {
64        self.as_object += 1;
65    }
66
67    /// Increments the predicate-in-triple counter.
68    pub fn increment_as_predicate_in_triple(&mut self) {
69        self.as_predicate_in_triple += 1;
70    }
71
72    /// Increments the subject-in-triple counter.
73    pub fn increment_as_subject_in_triple(&mut self) {
74        self.as_subject_in_triple += 1;
75    }
76
77    /// Increments the object-in-triple counter.
78    pub fn increment_as_object_in_triple(&mut self) {
79        self.as_object_in_triple += 1;
80    }
81}
82
83impl Display for UsageCount {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(
86            f,
87            "UsageCount {{ as_predicate: {}, as_subject: {}, as_object: {}, as_predicate_in_triple: {}, as_subject_in_triple: {}, as_object_in_triple: {} }}",
88            self.as_predicate,
89            self.as_subject,
90            self.as_object,
91            self.as_predicate_in_triple,
92            self.as_subject_in_triple,
93            self.as_object_in_triple
94        )
95    }
96}
97
98impl Default for UsageCount {
99    fn default() -> Self {
100        UsageCount::new()
101    }
102}