2 prototypes ensureNamespace: #relations &delegate: True.
3 relations define: #Relation &parents: {Cloneable}.
4 "Relations are objects that map objects to an arbitrary number of other
5 objects, and include a protocol of relational algebra."
7 r@(Relation traits) applyTo: x
10 r@(Relation traits) inverseApplyTo: x
13 r@(Relation traits) applyToAll: x
16 r@(Relation traits) inverseApplyToAll: x
19 relations define: #Top &builder: [Relation clone].
21 relations define: #Bottom &builder: [Relation clone].
23 relations define: #IdentityRelation &parents: {Relation}.
25 ir@(IdentityRelation traits) applyTo: x
26 "Identities return the element input."
29 ir@(IdentityRelation traits) inverseApplyTo: x
30 "Identities return the element input."
33 ir@(IdentityRelation traits) applyToAll: objs
36 ir@(IdentityRelation traits) inverseApplyToAll: objs
39 ir@(IdentityRelation traits) invert
40 "The inverse of an identity is that identity."
43 ir@(IdentityRelation traits) transitiveClosure
44 "The transitive closure of an identity is that identity."
47 ir@(IdentityRelation traits) reflexiveClosure
48 "The reflexive closure of an identity is that identity."
51 relations define: #UnaryArgumentRelation &parents: {Relation}
54 uar@(UnaryArgumentRelation traits) newFor: rel
55 [uar cloneSettingSlots: #{#relation} to: {rel}].
57 relations define: #InvertedRelation &parents: {UnaryArgumentRelation}.
58 "This represents the result of an inversion of a relation."
60 r@(Relation traits) invert
64 ir@(InvertedRelation traits) applyTo: obj
66 ir relation inverseApplyTo: obj
69 ir@(InvertedRelation traits) inverseApplyTo: obj
71 ir relation applyTo: obj
74 ir@(InvertedRelation traits) invert
75 "The inverse of an inverse is the original relation."
80 relations define: #TransitiveClosureRelation &parents: {UnaryArgumentRelation}.
81 "This represents the transitive closure (applying * times to oneself) of a
84 tc@(TransitiveClosureRelation traits) applyTo: obj
86 nextStep := (tc relation applyTo: obj).
87 temp union: (tc applyToAll: nextStep)
90 tc@(TransitiveClosureRelation traits) transitiveClosure
95 relations define: #MappingRelation &parents: {Relation}
96 &slots: {#mapping -> EmptyMapping}.
98 mr@(MappingRelation traits) applyTo: obj
100 (mr mapping at: obj) as: SingleSet
103 mr@(MappingRelation traits) inverseApplyTo: obj
106 mr mapping doWithIndex: [| :each :index |
107 each = obj ifTrue: [result add: index]]
111 relations define: #AssociationTable &parents: {Relation}
112 &slots: {#associations -> Set new}.
113 "A Set of Associations with no organizational restrictions."
115 at@(AssociationTable traits) applyTo: obj
119 do: [| :each | each key = obj ifTrue: [result add: each value]].
123 at@(AssociationTable traits) inverseApplyTo: obj
127 do: [| :each | each value = obj ifTrue: [result nextPut: each key]].
131 relations define: #NAryRelation &parents: {Relation}
132 &slots: {#elements -> {}}.
134 r@(NAryRelation traits) newFor: rels
135 [r cloneSettingSlots: #{#elements} to: {rels}].
137 relations define: #RelationIntersection &parents: {NAryRelation}.
139 r1@(Relation traits) /\ r2@(Relation traits)
140 [RelationIntersection newFor: {r1. r2}].
142 r@(RelationIntersection traits) applyTo: obj
144 r elements gather: [| :a :b | (a applyTo: obj) /\ (b applyTo: obj)] &initial: {}
147 r@(RelationIntersection traits) inverseApplyTo: obj
149 r elements gather: [| :a :b | (a inverseApplyTo: obj) /\ (b inverseApplyTo: obj)] &initial: {}
152 r@(RelationIntersection traits) applyToAll: obj
154 r elements gather: [| :a :b | (a applyToAll: obj) /\ (b applyToAll: obj)] &initial: {}
157 r@(RelationIntersection traits) inverseApplyToAll: obj
159 r elements gather: [| :a :b | (a inverseApplyToAll: obj) /\ (b inverseApplyToAll: obj)] &initial: {}
162 relations define: #RelationUnion &parents: {NAryRelation}.
164 r1@(Relation traits) \/ r2@(Relation traits)
165 [RelationUnion newFor: {r1. r2}].
167 r@(RelationUnion traits) applyTo: obj
169 r elements gather: [| :a :b | (a applyTo: obj) \/ (b applyTo: obj)] &initial: {}
172 r@(RelationUnion traits) inverseApplyTo: obj
174 r elements gather: [| :a :b | (a inverseApplyTo: obj) \/ (b inverseApplyTo: obj)] &initial: {}
177 r@(RelationUnion traits) applyToAll: obj
179 r elements gather: [| :a :b | (a applyToAll: obj) \/ (b applyToAll: obj)] &initial: {}
182 r@(RelationUnion traits) inverseApplyToAll: obj
184 r elements gather: [| :a :b | (a inverseApplyToAll: obj) \/ (b inverseApplyToAll: obj)] &initial: {}