Newer
Older
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
//TODO? PERF? (often inserts) SortedDict <-> Dict (often reads)
//TODO? hide base dict
//TODO: MMT: move some functionality there
//TODO: consequent!= samestep != dependent
//TODO! use URI as key
//PERF: avoid string as key (hash -> colission? -> strcmp[!])
public class FactOrganizer: Dictionary<int, Fact>
{
private Dictionary<int, meta> MetaInf = new Dictionary<int, meta>();
private List<stepnote> Workflow = new List<stepnote>();
// notes position in Workflow for un-/redo; the pointed to element is non-acitve
private int marker = 0;
// backlock logic for convinience
private int worksteps = 0;
private int backlog = 0;
// set if recently been resetted
private bool resetted = false;
private struct stepnote
{
// Fact.Id
public int Id;
// true if this Fact has been created in the same step as the last one
// steproot[false] (=> steptail[true])*
public bool samestep;
// reference to steproot/ after steptail-end
public int steplink;
// distincts creation and deletion
public bool creation;
public stepnote(int Id, bool samestep, bool creation, FactOrganizer that)
{
this.Id = Id;
this.samestep = samestep;
this.steplink = samestep ? that.Workflow[that.Workflow.Count - 1].steplink : that.Workflow.Count + 1;
this.creation = creation;
if (samestep)
// steplink = first steptail ? steproot : previous.steplink
{
stepnote prev = that.Workflow[that.Workflow.Count - 1];
this.steplink = prev.samestep ? prev.steplink : that.Workflow.Count - 1;
}
else
// steproot sets steplink after itself (end of steptail)
this.steplink = that.Workflow.Count + 1;
}
}
private struct meta
{
// TODO? -> last occurence for safe_dependencies
// reference to first occurrence in Workflow
public int workflow_id;
// keeps track wether Fact is currently in Scene
public bool active;
public meta(int workflow_id, bool active = true)
{
this.workflow_id = workflow_id;
this.active = active;
}
}
public FactOrganizer() : base() { }
public FactOrganizer(IDictionary<int, Fact> dictionary) : base(dictionary) { }
//TODO: PERF: better way than string search? -> use string as key!
public bool searchURI(string uri, out int found)
{
foreach(var element in this)
if (element.Value.backendURI.Equals(uri))
{
found = element.Key;
return true;
}
found = -1;
return false;
}
//TODO? MMT? PERF: O(n), every Fact-insertion
private bool FindEquivalent(Fact search, out Fact found)
// Looks for existent facts (found) which are very similar to prposed fact (search)
// does not check active state
{
foreach (var entry in this)
{
if (entry.Value.GetType() == search.GetType() &&
entry.Value.Equivalent(search))
{
found = entry.Value;
return true;
}
}
found = null;
return false;
}
private void WorkflowAdd(stepnote note)
// adds Workflow; updates meta struct; Invokes Events
{
if (note.samestep)
// update steplink of steproot
{
stepnote tmp = Workflow[note.steplink];
tmp.steplink = Workflow.Count + 1;
Workflow[note.steplink] = tmp;
}
Workflow.Add(note);
marker = Workflow.Count;
// update active info
meta info = MetaInf[note.Id];
info.active = note.creation;
MetaInf[note.Id] = info;
InvokeFactEvent(note.creation, note.Id);
}
public new void Add(int key, Fact value)
// hide
{
this.Add(value, out bool obsolete);
}
public int Add(Fact value, out bool exists, bool samestep = false)
// also checks for duplicates and active state
// returns key of actual Fact
{
if (resetted)
this.hardreset(false);
else if (backlog > 0)
{
worksteps -= backlog;
backlog = 0;
//TODO! incorporate deletion/ multiples in Workflow
for (int i = Workflow.Count - 1; i >= marker; i--)
{
stepnote last = Workflow[i];
this[last.Id].delete();
base.Remove(last.Id);
MetaInf.Remove(last.Id);
}
Workflow.RemoveRange(marker, Workflow.Count - marker);
}
if (!samestep)
worksteps++;
int key;
if (exists = FindEquivalent(value, out Fact found))
{
//TODO: MMT: del 'fact' (value) in MMT (alt.: s.TODO in addFact)
key = found.Id;
if (MetaInf[key].active)
return key;
}
else
{
//TODO: MMT: alt: insert in MMT if needed here/ on Invoke() (see WorkflowAdd)
key = value.Id;
base.Add(key, value);
MetaInf.Add(key, new meta(Workflow.Count, true));
}
WorkflowAdd(new stepnote(key, samestep, true, this));
return key;
}
public new bool Remove(int key)
// hide
{
return this.Remove(key, false);
}
public bool Remove(Fact value, bool samestep = false)
{
if (!this.ContainsKey(value.Id))
return false;
this.Remove(value.Id, samestep);
return true;
}
//TODO! test
public bool Remove(int key, bool samestep = false)
//no reset check (impossible)
{
if (!base.ContainsKey(key))
return false;
//TODO: see issue #58
safe_dependencies(key, out List<int> deletethis);
yeetusdeletus(deletethis, samestep);
return true;
}
// TODO: MMT: decide dependencies there
// TODO? handle deletions better
// TODO? decrease runtime from O(n/2)
private bool safe_dependencies(int key, out List<int> dependencies)
// searches for dependencies of a Fact; returns false if any dependencies are steproots
// int key: Fact to be deleted
// out List<int> dependencies: dependencyList
{
dependencies = new List<int>();
int c_unsafe = 0;
int pos = MetaInf[key].workflow_id;
dependencies.Add(key);
/* consequent!= samestep != dependent
// get steproot
if (Workflow[pos].samestep)
pos = Workflow[pos].steplink;
// add entire step
for (int i = pos; i < Workflow[pos].steplink; i++)
dependencies.Add(Workflow[i].Id);
pos = Workflow[pos].steplink;
*/
// accumulate facts that are dependent of dependencies
for (int i = pos; i < Workflow.Count; i++)
{
if (!Workflow[i].creation)
{
// just try
if (dependencies.Remove(Workflow[i].Id) && !Workflow[i].samestep)
c_unsafe--;
}
else if (0 < this[Workflow[i].Id].getDependentFactIds().Intersect(dependencies).Count())
{
dependencies.Add(Workflow[i].Id);
if (!Workflow[i].samestep)
c_unsafe++;
}
}
return c_unsafe == 0;
}
private void yeetusdeletus(List<int> deletereverse, bool samestep = false)
{
for(int i = deletereverse.Count - 1; i >= 0; i--, samestep = true)
{
WorkflowAdd(new stepnote(deletereverse[i], samestep, false, this));
}
}
private void reversestep(int pos, bool samestep = false)
// reverses any entire step; adds process to Workflow!
// int pos: position after steptail-end of the step to be reversed
{
pos--;
// check for valid step (implicit reset check)
if (pos >= marker)
return;
for (int i = pos, stop = Workflow[pos].samestep ? Workflow[pos].steplink : pos;
i >= stop; i--, samestep = true)
{
WorkflowAdd(new stepnote(Workflow[i].Id, samestep, !Workflow[i].creation, this));
}
}
public void undo()
{
if (resetted)
fastforward(); // revert softreset
else if (backlog < worksteps) {
backlog++;
stepnote last = Workflow[--marker];
int stop = last.samestep ? last.steplink : marker;
for (int i = marker; i >= stop; i--)
{
last = Workflow[i];
InvokeFactEvent(!last.creation, last.Id);
}
marker = stop;
}
}
public void redo()
{
resetted = false;
if (backlog > 0)
{
backlog--;
stepnote last = Workflow[marker];
int stop = last.samestep ? Workflow[last.steplink].steplink : last.steplink;
for (int i = marker; i < stop; i++)
{
last = Workflow[i];
InvokeFactEvent(last.creation, last.Id);
}
marker = stop;
}
}
public new void Clear()
// Does not Invoke RemoveFactEvent(s)!
{
base.Clear();
Workflow.Clear();
marker = 0;
worksteps = 0;
backlog = 0;
resetted = false;
}
public void hardreset(bool invoke_event = true)
{
foreach(var entry in this)
{
if (invoke_event) //TODO: check if removed?
CommunicationEvents.RemoveFactEvent.Invoke(entry.Value);
entry.Value.delete();
}
this.Clear();
}
public void softreset()
{
if (resetted)
{
fastforward();
return;
}
// TODO: PREF: alt: EventResetAll
// marker = 0; backlog = worksteps;
while (marker > 0)
undo();
resetted = true;
}
public void fastforward()
{
while (backlog > 0)
// also sets resetted = false;
redo();
}
public void store()
{
// TODO: save state of all of this?
}
public void load()
{
// TODO: see issue #58
}
private void InvokeFactEvent(bool creation, int Id)
{
if (creation)
CommunicationEvents.AddFactEvent.Invoke(this[Id]);
else
CommunicationEvents.RemoveFactEvent.Invoke(this[Id]);
}
}