-
Notifications
You must be signed in to change notification settings - Fork 5
/
add-remove-match-ss-strings.metta
executable file
·66 lines (57 loc) · 2.38 KB
/
add-remove-match-ss-strings.metta
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
;;;;;;;;;;;;;;;;;;;;;;
; Adding Atoms to the Knowledge Base
;;;;;;;;;;;;;;;;;;;;;;
!(bind! &kb0 (new-space))
!(add-atom &kb0 (("S" "a")))
!(add-atom &kb0 (("S" "b")))
;MeTTaLog Only: !(assertEqual (atom-count &kb0) 2)
!(assertEqualToResult (get-atoms &kb0) ((("S" "a")) (("S" "b"))))
;;;;;;;;;;;;;;;;;;;;;;
; Removing Atoms from the Knowledge Base
;;;;;;;;;;;;;;;;;;;;;;
!(bind! &kb1 (new-space))
!(add-atom &kb1 (("S" "a")))
!(add-atom &kb1 (("S" "b")))
!(add-atom &kb1 (("S" "c")))
;; Remove an atom and test for success
!(assertTrue (remove-atom &kb1 (("S" "b")))) ; "remove_atom on a present atom should return true"
;; Attempt to remove a non-existent atom and test for failure
!(assertFalse (remove-atom &kb1 (("S" "bogus")))) ; "remove_atom on a missing atom should return false"
;; Verify the current state of the knowledge base
!(assertEqualToResult (get-atoms &kb1) ((("S" "a")) (("S" "c"))))
;;;;;;;;;;;;;;;;;;;;;;
; Replacing Atoms in the Knowledge Base
;;;;;;;;;;;;;;;;;;;;;;
!(bind! &kb2 (new-space))
(= (replace-atom $space $before $after) (if (remove-atom $space $before) (add-atom $space $after) False))
!(add-atom &kb2 (("S" "a")))
!(add-atom &kb2 (("S" "b")))
!(add-atom &kb2 (("S" "c")))
;; Replace an atom and verify the operation was successful
!(assertTrue (replace-atom &kb2 (("S" "b")) (("S" "d"))))
;; Check the new set of atoms in the knowledge base
!(assertEqualToResult (get-atoms &kb2) ((("S" "a")) (("S" "d")) (("S" "c"))))
;;;;;;;;;;;;;;;;;;;;;;
; Querying Atoms in the Knowledge Base
;;;;;;;;;;;;;;;;;;;;;;
!(bind! &kb3 (new-space))
!(add-atom &kb3 (E (("S" "A")) (("S" "B"))))
!(add-atom &kb3 (E (("S" "C")) (("S" "D"))))
;; Adding a duplicate pattern for testing multiple matches
!(add-atom &kb3 (E (("S" "A")) (("S" "E"))))
;; Verify that the query returns the expected matches
!(assertEqualToResult (match &kb3 (E (("S" "A")) $XX) $XX) ((("S" "B")) (("S" "E"))))
;;;;;;;;;;;;;;;;;;;;;;
; Comprehensive Test with Add, Remove, Query
;;;;;;;;;;;;;;;;;;;;;;
!(bind! &kb4 (new-space))
!(add-atom &kb4 (("S" "initial-state")))
;; Perform a sequence of operations
!(add-atom &kb4 (("S" "x")))
!(remove-atom &kb4 (("S" "initial-state")))
!(add-atom &kb4 (("S" "y")))
;; Final state should have "x" and "y" only
!(assertEqualToResult (get-atoms &kb4) ((("S" "x")) (("S" "y"))))
;; Query to test the presence of "x" and "y"
!(assertTrue (query &kb4 (("S" "x"))))
!(assertTrue (query &kb4 (("S" "y"))))