P icon indicating copy to clipboard operation
P copied to clipboard

"KeyNotFoundException" when instantiating machines from C# without "creates" declaration

Open KelvinLi2020 opened this issue 5 years ago • 1 comments

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 [...]

KelvinLi2020 avatar Sep 30 '20 15:09 KelvinLi2020

Thanks @KelvinLi2020, I will update the code to generate better error message to give hint to programmers to add the creates annotation.

ankushdesai avatar Sep 30 '20 16:09 ankushdesai

This has been fixed now. Thanks!

ankushdesai avatar Nov 29 '22 01:11 ankushdesai