oxirs-rule 0.2.4

Forward/backward rule engine for RDFS, OWL, and SWRL reasoning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! # Temporal Reasoning Module
//!
//! This module provides temporal reasoning capabilities using Allen's Interval Algebra
//! for reasoning about time intervals and their relationships.
//!
//! ## Features
//!
//! - **Allen's Interval Algebra**: 13 basic relations between time intervals
//! - **Temporal Constraint Networks**: Reasoning about temporal constraints
//! - **Path Consistency**: Enforcing consistency in temporal networks
//! - **Temporal Rules**: Rules with temporal conditions
//! - **Event Ordering**: Reasoning about event sequences
//!
//! ## Allen's 13 Relations
//!
//! 1. Before (p < q)
//! 2. After (p > q)
//! 3. Meets (p m q)
//! 4. Met-by (p mi q)
//! 5. Overlaps (p o q)
//! 6. Overlapped-by (p oi q)
//! 7. During (p d q)
//! 8. Contains (p di q)
//! 9. Starts (p s q)
//! 10. Started-by (p si q)
//! 11. Finishes (p f q)
//! 12. Finished-by (p fi q)
//! 13. Equals (p = q)
//!
//! ## Example
//!
//! ```text
//! use oxirs_rule::temporal::*;
//!
//! // Create time intervals
//! let morning = TimeInterval::new(8.0, 12.0);
//! let lunch = TimeInterval::new(12.0, 13.0);
//! let afternoon = TimeInterval::new(13.0, 17.0);
//!
//! // Check Allen relations
//! assert_eq!(morning.allen_relation(&lunch), AllenRelation::Meets);
//! assert_eq!(lunch.allen_relation(&afternoon), AllenRelation::Meets);
//! assert_eq!(morning.allen_relation(&afternoon), AllenRelation::Before);
//!
//! // Create a temporal constraint network
//! let mut tcn = TemporalConstraintNetwork::new();
//! tcn.add_interval("morning".to_string(), morning);
//! tcn.add_interval("lunch".to_string(), lunch);
//! tcn.add_constraint("morning".to_string(), "lunch".to_string(), AllenRelation::Meets);
//!
//! // Check consistency
//! assert!(tcn.is_consistent().expect("should succeed"));
//! # Ok::<(), anyhow::Error>(())
//! ```

use crate::Rule;
use anyhow::{anyhow, Result};
use std::collections::{HashMap, HashSet};

/// Time interval represented by start and end points
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TimeInterval {
    /// Start time
    pub start: f64,
    /// End time
    pub end: f64,
}

impl TimeInterval {
    /// Create a new time interval
    pub fn new(start: f64, end: f64) -> Result<Self> {
        if start > end {
            return Err(anyhow!("Start time must be before end time"));
        }
        Ok(Self { start, end })
    }

    /// Get the duration of the interval
    pub fn duration(&self) -> f64 {
        self.end - self.start
    }

    /// Check if a point in time is within the interval
    pub fn contains_point(&self, point: f64) -> bool {
        point >= self.start && point <= self.end
    }

    /// Compute the Allen relation between two intervals
    pub fn allen_relation(&self, other: &TimeInterval) -> AllenRelation {
        let eps = 1e-10; // Epsilon for floating point comparison

        // Equal
        if (self.start - other.start).abs() < eps && (self.end - other.end).abs() < eps {
            return AllenRelation::Equals;
        }

        // Before
        if self.end < other.start - eps {
            return AllenRelation::Before;
        }

        // After
        if self.start > other.end + eps {
            return AllenRelation::After;
        }

        // Meets
        if (self.end - other.start).abs() < eps {
            return AllenRelation::Meets;
        }

        // Met-by
        if (self.start - other.end).abs() < eps {
            return AllenRelation::MetBy;
        }

        // During
        if self.start > other.start + eps && self.end < other.end - eps {
            return AllenRelation::During;
        }

        // Contains
        if self.start < other.start - eps && self.end > other.end + eps {
            return AllenRelation::Contains;
        }

        // Starts
        if (self.start - other.start).abs() < eps && self.end < other.end - eps {
            return AllenRelation::Starts;
        }

        // Started-by
        if (self.start - other.start).abs() < eps && self.end > other.end + eps {
            return AllenRelation::StartedBy;
        }

        // Finishes
        if self.start > other.start + eps && (self.end - other.end).abs() < eps {
            return AllenRelation::Finishes;
        }

        // Finished-by
        if self.start < other.start - eps && (self.end - other.end).abs() < eps {
            return AllenRelation::FinishedBy;
        }

        // Overlaps
        if self.start < other.start - eps
            && self.end > other.start + eps
            && self.end < other.end - eps
        {
            return AllenRelation::Overlaps;
        }

        // Overlapped-by
        if self.start > other.start + eps
            && self.start < other.end - eps
            && self.end > other.end + eps
        {
            return AllenRelation::OverlappedBy;
        }

        // Fallback (shouldn't happen with correct logic)
        AllenRelation::Before
    }

    /// Get the intersection of two intervals (if they overlap)
    pub fn intersection(&self, other: &TimeInterval) -> Option<TimeInterval> {
        let start = self.start.max(other.start);
        let end = self.end.min(other.end);

        if start <= end {
            TimeInterval::new(start, end).ok()
        } else {
            None
        }
    }

    /// Get the union of two intervals (if they overlap or meet)
    pub fn union(&self, other: &TimeInterval) -> Option<TimeInterval> {
        // Check if they overlap or meet
        let relation = self.allen_relation(other);
        match relation {
            AllenRelation::Before | AllenRelation::After => None,
            _ => {
                let start = self.start.min(other.start);
                let end = self.end.max(other.end);
                TimeInterval::new(start, end).ok()
            }
        }
    }
}

/// Allen's 13 basic relations between time intervals
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AllenRelation {
    /// p before q: p.end < q.start
    Before,
    /// p after q: p.start > q.end
    After,
    /// p meets q: p.end = q.start
    Meets,
    /// p met-by q: p.start = q.end
    MetBy,
    /// p overlaps q: p.start < q.start < p.end < q.end
    Overlaps,
    /// p overlapped-by q: q.start < p.start < q.end < p.end
    OverlappedBy,
    /// p during q: q.start < p.start < p.end < q.end
    During,
    /// p contains q: p.start < q.start < q.end < p.end
    Contains,
    /// p starts q: p.start = q.start < p.end < q.end
    Starts,
    /// p started-by q: p.start = q.start < q.end < p.end
    StartedBy,
    /// p finishes q: q.start < p.start < p.end = q.end
    Finishes,
    /// p finished-by q: p.start < q.start < p.end = q.end
    FinishedBy,
    /// p equals q: p.start = q.start, p.end = q.end
    Equals,
}

impl AllenRelation {
    /// Get the inverse relation
    pub fn inverse(&self) -> AllenRelation {
        match self {
            AllenRelation::Before => AllenRelation::After,
            AllenRelation::After => AllenRelation::Before,
            AllenRelation::Meets => AllenRelation::MetBy,
            AllenRelation::MetBy => AllenRelation::Meets,
            AllenRelation::Overlaps => AllenRelation::OverlappedBy,
            AllenRelation::OverlappedBy => AllenRelation::Overlaps,
            AllenRelation::During => AllenRelation::Contains,
            AllenRelation::Contains => AllenRelation::During,
            AllenRelation::Starts => AllenRelation::StartedBy,
            AllenRelation::StartedBy => AllenRelation::Starts,
            AllenRelation::Finishes => AllenRelation::FinishedBy,
            AllenRelation::FinishedBy => AllenRelation::Finishes,
            AllenRelation::Equals => AllenRelation::Equals,
        }
    }

    /// Get all possible relations
    pub fn all() -> Vec<AllenRelation> {
        vec![
            AllenRelation::Before,
            AllenRelation::After,
            AllenRelation::Meets,
            AllenRelation::MetBy,
            AllenRelation::Overlaps,
            AllenRelation::OverlappedBy,
            AllenRelation::During,
            AllenRelation::Contains,
            AllenRelation::Starts,
            AllenRelation::StartedBy,
            AllenRelation::Finishes,
            AllenRelation::FinishedBy,
            AllenRelation::Equals,
        ]
    }
}

/// Temporal constraint: a set of possible Allen relations
#[derive(Debug, Clone)]
pub struct TemporalConstraint {
    /// Possible relations between two intervals
    pub relations: HashSet<AllenRelation>,
}

impl TemporalConstraint {
    /// Create a constraint with all possible relations
    pub fn universal() -> Self {
        Self {
            relations: AllenRelation::all().into_iter().collect(),
        }
    }

    /// Create a constraint with a single relation
    pub fn single(relation: AllenRelation) -> Self {
        let mut relations = HashSet::new();
        relations.insert(relation);
        Self { relations }
    }

    /// Intersect two constraints
    pub fn intersect(&self, other: &TemporalConstraint) -> TemporalConstraint {
        TemporalConstraint {
            relations: self
                .relations
                .intersection(&other.relations)
                .copied()
                .collect(),
        }
    }

    /// Check if the constraint is empty (no possible relations)
    pub fn is_empty(&self) -> bool {
        self.relations.is_empty()
    }

    /// Compose two constraints using Allen's composition table
    pub fn compose(&self, other: &TemporalConstraint) -> TemporalConstraint {
        let mut result = HashSet::new();

        for r1 in &self.relations {
            for r2 in &other.relations {
                // Add all possible compositions
                result.extend(Self::compose_relations(*r1, *r2));
            }
        }

        TemporalConstraint { relations: result }
    }

    /// Compose two Allen relations (simplified version)
    fn compose_relations(r1: AllenRelation, r2: AllenRelation) -> Vec<AllenRelation> {
        // This is a simplified composition table
        // Full implementation would use Allen's complete composition table
        use AllenRelation::*;

        match (r1, r2) {
            (Before, Before) => vec![Before],
            (Before, Meets) => vec![Before],
            (Meets, Before) => vec![Before],
            (Meets, Meets) => vec![Meets],
            (Equals, r) => vec![r],
            (r, Equals) => vec![r],
            _ => AllenRelation::all(), // Conservative: all possible relations
        }
    }
}

/// Temporal Constraint Network
#[derive(Debug, Clone)]
pub struct TemporalConstraintNetwork {
    /// Named intervals
    intervals: HashMap<String, TimeInterval>,
    /// Constraints between intervals
    constraints: HashMap<(String, String), TemporalConstraint>,
}

impl TemporalConstraintNetwork {
    /// Create a new temporal constraint network
    pub fn new() -> Self {
        Self {
            intervals: HashMap::new(),
            constraints: HashMap::new(),
        }
    }

    /// Add an interval to the network
    pub fn add_interval(&mut self, name: String, interval: TimeInterval) {
        self.intervals.insert(name, interval);
    }

    /// Add a constraint between two intervals
    pub fn add_constraint(&mut self, from: String, to: String, relation: AllenRelation) {
        let constraint = TemporalConstraint::single(relation);
        self.constraints
            .insert((from.clone(), to.clone()), constraint.clone());

        // Add inverse constraint
        let inverse_constraint = TemporalConstraint::single(relation.inverse());
        self.constraints.insert((to, from), inverse_constraint);
    }

    /// Get constraint between two intervals
    pub fn get_constraint(&self, from: &str, to: &str) -> TemporalConstraint {
        self.constraints
            .get(&(from.to_string(), to.to_string()))
            .cloned()
            .unwrap_or_else(TemporalConstraint::universal)
    }

    /// Check if the network is consistent
    pub fn is_consistent(&self) -> Result<bool> {
        // Use path consistency algorithm (PC-2)
        let mut network = self.clone();

        // Iterate until no changes
        let mut changed = true;
        let mut iterations = 0;
        const MAX_ITERATIONS: usize = 100;

        while changed && iterations < MAX_ITERATIONS {
            changed = false;
            iterations += 1;

            let interval_names: Vec<String> = network.intervals.keys().cloned().collect();

            for i in &interval_names {
                for j in &interval_names {
                    if i == j {
                        continue;
                    }

                    for k in &interval_names {
                        if k == i || k == j {
                            continue;
                        }

                        // Path consistency: C[i,j] = C[i,j] ∩ (C[i,k] ∘ C[k,j])
                        let c_ij = network.get_constraint(i, j);
                        let c_ik = network.get_constraint(i, k);
                        let c_kj = network.get_constraint(k, j);

                        let composed = c_ik.compose(&c_kj);
                        let new_c_ij = c_ij.intersect(&composed);

                        if new_c_ij.is_empty() {
                            return Ok(false); // Inconsistent
                        }

                        if new_c_ij.relations != c_ij.relations {
                            changed = true;
                            network
                                .constraints
                                .insert((i.clone(), j.clone()), new_c_ij.clone());

                            // Update inverse
                            let inverse_relations: HashSet<AllenRelation> =
                                new_c_ij.relations.iter().map(|r| r.inverse()).collect();
                            network.constraints.insert(
                                (j.clone(), i.clone()),
                                TemporalConstraint {
                                    relations: inverse_relations,
                                },
                            );
                        }
                    }
                }
            }
        }

        Ok(true)
    }

    /// Get all intervals
    pub fn get_intervals(&self) -> &HashMap<String, TimeInterval> {
        &self.intervals
    }

    /// Query temporal relationships
    pub fn query_relation(&self, from: &str, to: &str) -> Option<HashSet<AllenRelation>> {
        self.constraints
            .get(&(from.to_string(), to.to_string()))
            .map(|c| c.relations.clone())
    }
}

impl Default for TemporalConstraintNetwork {
    fn default() -> Self {
        Self::new()
    }
}

/// Temporal rule with time-based conditions
#[derive(Debug, Clone)]
pub struct TemporalRule {
    /// Base rule
    pub rule: Rule,
    /// Temporal constraints on rule variables
    pub temporal_constraints: HashMap<String, TimeInterval>,
}

impl TemporalRule {
    /// Create a new temporal rule
    pub fn new(rule: Rule) -> Self {
        Self {
            rule,
            temporal_constraints: HashMap::new(),
        }
    }

    /// Add a temporal constraint to a variable
    pub fn add_temporal_constraint(&mut self, variable: String, interval: TimeInterval) {
        self.temporal_constraints.insert(variable, interval);
    }

    /// Check if the rule is applicable at a given time
    pub fn is_applicable_at(&self, time: f64) -> bool {
        self.temporal_constraints
            .values()
            .all(|interval| interval.contains_point(time))
    }
}

/// Event with a time point or interval
#[derive(Debug, Clone)]
pub struct TemporalEvent {
    /// Event name
    pub name: String,
    /// Time interval when the event occurs
    pub interval: TimeInterval,
    /// Event properties
    pub properties: HashMap<String, String>,
}

impl TemporalEvent {
    /// Create a new temporal event
    pub fn new(name: String, interval: TimeInterval) -> Self {
        Self {
            name,
            interval,
            properties: HashMap::new(),
        }
    }

    /// Add a property to the event
    pub fn add_property(&mut self, key: String, value: String) {
        self.properties.insert(key, value);
    }

    /// Check the Allen relation with another event
    pub fn relation_with(&self, other: &TemporalEvent) -> AllenRelation {
        self.interval.allen_relation(&other.interval)
    }
}

/// Temporal reasoning engine
#[derive(Debug)]
pub struct TemporalReasoningEngine {
    /// Temporal constraint network
    tcn: TemporalConstraintNetwork,
    /// Temporal rules
    rules: Vec<TemporalRule>,
    /// Events
    events: Vec<TemporalEvent>,
}

impl TemporalReasoningEngine {
    /// Create a new temporal reasoning engine
    pub fn new() -> Self {
        Self {
            tcn: TemporalConstraintNetwork::new(),
            rules: Vec::new(),
            events: Vec::new(),
        }
    }

    /// Add an interval to the network
    pub fn add_interval(&mut self, name: String, interval: TimeInterval) {
        self.tcn.add_interval(name, interval);
    }

    /// Add a temporal constraint
    pub fn add_constraint(&mut self, from: String, to: String, relation: AllenRelation) {
        self.tcn.add_constraint(from, to, relation);
    }

    /// Add a temporal rule
    pub fn add_temporal_rule(&mut self, rule: TemporalRule) {
        self.rules.push(rule);
    }

    /// Add an event
    pub fn add_event(&mut self, event: TemporalEvent) {
        self.events.push(event);
    }

    /// Get applicable rules at a given time
    pub fn get_applicable_rules(&self, time: f64) -> Vec<&TemporalRule> {
        self.rules
            .iter()
            .filter(|rule| rule.is_applicable_at(time))
            .collect()
    }

    /// Get events occurring at a given time
    pub fn get_events_at(&self, time: f64) -> Vec<&TemporalEvent> {
        self.events
            .iter()
            .filter(|event| event.interval.contains_point(time))
            .collect()
    }

    /// Find events with a specific relation to a given event
    pub fn find_events_with_relation(
        &self,
        event_name: &str,
        relation: AllenRelation,
    ) -> Vec<&TemporalEvent> {
        if let Some(target_event) = self.events.iter().find(|e| e.name == event_name) {
            self.events
                .iter()
                .filter(|e| e.name != event_name && e.relation_with(target_event) == relation)
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Check network consistency
    pub fn is_consistent(&self) -> Result<bool> {
        self.tcn.is_consistent()
    }

    /// Get the temporal constraint network
    pub fn get_network(&self) -> &TemporalConstraintNetwork {
        &self.tcn
    }
}

impl Default for TemporalReasoningEngine {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_time_interval_creation() -> Result<(), Box<dyn std::error::Error>> {
        let interval = TimeInterval::new(1.0, 5.0)?;
        assert_eq!(interval.start, 1.0);
        assert_eq!(interval.end, 5.0);
        assert_eq!(interval.duration(), 4.0);

        // Invalid interval
        assert!(TimeInterval::new(5.0, 1.0).is_err());
        Ok(())
    }

    #[test]
    fn test_allen_before() -> Result<(), Box<dyn std::error::Error>> {
        let i1 = TimeInterval::new(1.0, 3.0)?;
        let i2 = TimeInterval::new(5.0, 7.0)?;

        assert_eq!(i1.allen_relation(&i2), AllenRelation::Before);
        assert_eq!(i2.allen_relation(&i1), AllenRelation::After);
        Ok(())
    }

    #[test]
    fn test_allen_meets() -> Result<(), Box<dyn std::error::Error>> {
        let i1 = TimeInterval::new(1.0, 3.0)?;
        let i2 = TimeInterval::new(3.0, 5.0)?;

        assert_eq!(i1.allen_relation(&i2), AllenRelation::Meets);
        assert_eq!(i2.allen_relation(&i1), AllenRelation::MetBy);
        Ok(())
    }

    #[test]
    fn test_allen_overlaps() -> Result<(), Box<dyn std::error::Error>> {
        let i1 = TimeInterval::new(1.0, 4.0)?;
        let i2 = TimeInterval::new(3.0, 6.0)?;

        assert_eq!(i1.allen_relation(&i2), AllenRelation::Overlaps);
        assert_eq!(i2.allen_relation(&i1), AllenRelation::OverlappedBy);
        Ok(())
    }

    #[test]
    fn test_allen_during() -> Result<(), Box<dyn std::error::Error>> {
        let i1 = TimeInterval::new(2.0, 4.0)?;
        let i2 = TimeInterval::new(1.0, 5.0)?;

        assert_eq!(i1.allen_relation(&i2), AllenRelation::During);
        assert_eq!(i2.allen_relation(&i1), AllenRelation::Contains);
        Ok(())
    }

    #[test]
    fn test_allen_equals() -> Result<(), Box<dyn std::error::Error>> {
        let i1 = TimeInterval::new(1.0, 5.0)?;
        let i2 = TimeInterval::new(1.0, 5.0)?;

        assert_eq!(i1.allen_relation(&i2), AllenRelation::Equals);
        Ok(())
    }

    #[test]
    fn test_interval_intersection() -> Result<(), Box<dyn std::error::Error>> {
        let i1 = TimeInterval::new(1.0, 5.0)?;
        let i2 = TimeInterval::new(3.0, 7.0)?;

        let intersection = i1.intersection(&i2).ok_or("expected Some value")?;
        assert_eq!(intersection.start, 3.0);
        assert_eq!(intersection.end, 5.0);
        Ok(())
    }

    #[test]
    fn test_interval_union() -> Result<(), Box<dyn std::error::Error>> {
        let i1 = TimeInterval::new(1.0, 5.0)?;
        let i2 = TimeInterval::new(3.0, 7.0)?;

        let union = i1.union(&i2).ok_or("expected Some value")?;
        assert_eq!(union.start, 1.0);
        assert_eq!(union.end, 7.0);
        Ok(())
    }

    #[test]
    fn test_temporal_constraint_network() -> Result<(), Box<dyn std::error::Error>> {
        let mut tcn = TemporalConstraintNetwork::new();

        let morning = TimeInterval::new(8.0, 12.0)?;
        let lunch = TimeInterval::new(12.0, 13.0)?;
        let afternoon = TimeInterval::new(13.0, 17.0)?;

        tcn.add_interval("morning".to_string(), morning);
        tcn.add_interval("lunch".to_string(), lunch);
        tcn.add_interval("afternoon".to_string(), afternoon);

        tcn.add_constraint(
            "morning".to_string(),
            "lunch".to_string(),
            AllenRelation::Meets,
        );
        tcn.add_constraint(
            "lunch".to_string(),
            "afternoon".to_string(),
            AllenRelation::Meets,
        );

        assert!(tcn.is_consistent()?);
        Ok(())
    }

    #[test]
    fn test_temporal_rule() -> Result<(), Box<dyn std::error::Error>> {
        let rule = Rule {
            name: "test_rule".to_string(),
            body: vec![],
            head: vec![],
        };

        let mut temporal_rule = TemporalRule::new(rule);
        let interval = TimeInterval::new(9.0, 17.0)?;
        temporal_rule.add_temporal_constraint("working_hours".to_string(), interval);

        assert!(temporal_rule.is_applicable_at(10.0));
        assert!(!temporal_rule.is_applicable_at(20.0));
        Ok(())
    }

    #[test]
    fn test_temporal_event() -> Result<(), Box<dyn std::error::Error>> {
        let meeting =
            TemporalEvent::new("team_meeting".to_string(), TimeInterval::new(14.0, 15.0)?);

        let lunch = TemporalEvent::new("lunch_break".to_string(), TimeInterval::new(12.0, 13.0)?);

        assert_eq!(lunch.relation_with(&meeting), AllenRelation::Before);
        Ok(())
    }

    #[test]
    fn test_temporal_reasoning_engine() -> Result<(), Box<dyn std::error::Error>> {
        let mut engine = TemporalReasoningEngine::new();

        let event1 = TemporalEvent::new("event1".to_string(), TimeInterval::new(1.0, 3.0)?);

        let event2 = TemporalEvent::new("event2".to_string(), TimeInterval::new(2.0, 4.0)?);

        engine.add_event(event1);
        engine.add_event(event2);

        let events_at_2_5 = engine.get_events_at(2.5);
        assert_eq!(events_at_2_5.len(), 2);
        Ok(())
    }

    #[test]
    fn test_allen_relation_inverse() {
        assert_eq!(AllenRelation::Before.inverse(), AllenRelation::After);
        assert_eq!(AllenRelation::Meets.inverse(), AllenRelation::MetBy);
        assert_eq!(
            AllenRelation::Overlaps.inverse(),
            AllenRelation::OverlappedBy
        );
        assert_eq!(AllenRelation::Equals.inverse(), AllenRelation::Equals);
    }
}