@@ -64,17 +64,19 @@ public Propagator getPropagator() {
6464 return propagationQueue ;
6565 }
6666
67- public void insert (@ Nullable Object a ) {
67+ @ Override
68+ public void insert (Object a ) {
6869 if (tupleMap .containsKey (a )) {
6970 return ;
7071 }
71- tupleMap . values (). stream (). flatMap ( List :: stream ). forEach ( this :: retractExisting );
72+ invalidateCache ( );
7273 tupleMap .put (a , new ArrayList <>());
7374 insertIntoNodeNetwork (a );
7475 recalculateTuples ();
7576 }
7677
77- public void update (@ Nullable Object a ) {
78+ @ Override
79+ public void update (Object a ) {
7880 for (var mappedTuple : tupleMap .get (a )) {
7981 updateExisting (a , mappedTuple );
8082 }
@@ -93,11 +95,12 @@ private void updateExisting(@Nullable Object a, UniTuple<A> tuple) {
9395 }
9496 }
9597
96- public void retract (@ Nullable Object a ) {
98+ @ Override
99+ public void retract (Object a ) {
97100 if (!tupleMap .containsKey (a )) {
98101 return ;
99102 }
100- tupleMap . values (). stream (). flatMap ( List :: stream ). forEach ( this :: retractExisting );
103+ invalidateCache ( );
101104 tupleMap .remove (a );
102105 retractFromNodeNetwork (a );
103106 recalculateTuples ();
@@ -125,29 +128,30 @@ private void retractExisting(UniTuple<A> tuple) {
125128 }
126129
127130 private void insertIntoNodeNetwork (Object toInsert ) {
128- nodeNetwork .getTupleSourceRootNodes (toInsert .getClass ()).forEach (node -> {
129- ((AbstractForEachUniNode ) node ).insert (toInsert );
130- });
131+ nodeNetwork .getTupleSourceRootNodes (toInsert .getClass ())
132+ .forEach (node -> ((AbstractForEachUniNode ) node ).insert (toInsert ));
131133 }
132134
133135 private void retractFromNodeNetwork (Object toRetract ) {
134- nodeNetwork .getTupleSourceRootNodes (toRetract .getClass ()).forEach (node -> {
135- ((AbstractForEachUniNode ) node ).retract (toRetract );
136- });
136+ nodeNetwork .getTupleSourceRootNodes (toRetract .getClass ())
137+ .forEach (node -> ((AbstractForEachUniNode ) node ).retract (toRetract ));
138+ }
139+
140+ private void invalidateCache () {
141+ tupleMap .values ().stream ().flatMap (List ::stream ).forEach (this ::retractExisting );
142+ recordingTupleNode .getTupleRecorder ().reset ();
137143 }
138144
139145 private void recalculateTuples () {
140146 var recorder = recordingTupleNode .getTupleRecorder ();
141- recorder .reset ();
142147 for (var mappedTupleEntry : tupleMap .entrySet ()) {
143148 mappedTupleEntry .getValue ().clear ();
144149 var invalidated = mappedTupleEntry .getKey ();
145150 recorder .recordingInto (mappedTupleEntry .getValue (), this ::remapTuple , () -> {
146151 // Do an update on the object and settle the network; this will update precisely the
147152 // tuples mapped to this node, which will then be recorded
148- nodeNetwork .getTupleSourceRootNodes (invalidated .getClass ()).forEach (node -> {
149- ((AbstractForEachUniNode ) node ).update (invalidated );
150- });
153+ nodeNetwork .getTupleSourceRootNodes (invalidated .getClass ())
154+ .forEach (node -> ((AbstractForEachUniNode ) node ).update (invalidated ));
151155 nodeNetwork .settle ();
152156 });
153157 }
0 commit comments