P
P copied to clipboard
"KeyNotFoundException" when instantiating machines from C# without "creates" declaration
When C# code instantiates a state machine, and if the corresponding P function signature does not declare creates, then an internal P error is returned.
P code:
test TestCase [main=Test]: { Test };
machine Test {
start state Init {
entry {
StartAsync();
}
}
}
// wrong!
fun StartAsync();
// correct!
// fun StartAsync() creates Test;
fun MakeATest(): Test {
return new Test();
}
C# code:
using System;
using Plang.CoyoteRuntime.Values;
using Plang.CoyoteRuntime;
namespace PGlobalFunctions {
public partial class GlobalFunctions {
public static void StartAsync(PMachine machine) {
MakeATest(machine);
}
}
}
Resulting exception:
<ErrorLog> Exception 'System.Collections.Generic.KeyNotFoundException' was thrown in [...]: The given key 'I_Test' was not present in the dictionary.
from location 'System.Private.CoreLib':
The stack trace is:
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Plang.CoyoteRuntime.PMachine.CreateInterface[T](PMachine creator, IPrtValue payload) in [...]
Thanks @KelvinLi2020, I will update the code to generate better error message to give hint to programmers to add the creates annotation.
This has been fixed now. Thanks!