Why does IContext.RetractLinked require two parameters?

83 views Asked by At

I added a linked fact using:

context.InsertLinked(longOrderKey, longOrder);

At some point later, I want to remove this fact. It's easy for me to construct the key without having the record:

var longOrderKey = (managedAccount.AccountId, PositionType.Long, fungible.FungibleId);

So why do I need the record when removing a linked fact using the method:

context.RetractLinked(longOrderKey, longOrder);

Why can't this method just use the longOrderKey? What if I don't have the 'longOrder' record. Do I really need to look it up before I can remove it?

1

There are 1 answers

3
Sergiy Nikolayev On BEST ANSWER

Linked facts are tied to an activation that created them. The purpose of the key is to be able to identify the specific fact if the activation produced more than one linked fact. If you are inserting just one linked fact in the RHS of a rule, you can really set the key to anything, e.g. "1"; if you were to insert two facts, you could set keys to "1" and "2", and so on. In essence, the key is to identify the linked fact within the activation. The fact itself is needed, so that the engine can find the corresponding entries in the working memory. Much like ISession.Retract requires the fact object, so that it can find it in the working memory. Another point is that in most scenarios you should not need to retract the linked facts as they would get retracted automatically, once the activation gets deleted (i.e. the conditions that created the activation become false).