我基于本地.csv
文件的信息,实现了有限状态机的简单实现:
TransitionTable.csv
就像下面这样简单:
q_src,eve,q_dst
q0,sig1,q1
q1,sig2,q2
这是我的主要课程:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BacktrackingPathFinder
{
class Program
{
static IEnumerable<string[]> transitions = File.ReadAllLines(@"TransitionTable.csv").Skip(1).Select(x => x.Split(','));
static void Main(string[] args)
{
State q0 = new State("q0");
Event e1 = new Event("sig1");
PathFinder pf = new PathFinder();
State s = pf.FindDestination(q0, e1, transitions);
Console.WriteLine(s.ID);
Console.ReadLine();
}
}
}
PathFinder.cs
定义如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BacktrackingPathFinder
{
public class PathFinder
{
public PathFinder()
{
}
public State FindDestination(State q0, Event s0, IEnumerable<string[]> transitions)
{
var newq = from t in transitions
where t[0] == q0.ID && t[1] == s0.ID
select t[2];
return new State(newq.FirstOrDefault().ToString());
}
}
}
根据我的.csv
文件,输出应为q1
,但会抛出以下异常,我无法弄清楚:
System.TypeInitializationException未处理消息:未处理 类型&#39; System.TypeInitializationException&#39;的异常发生在 mscorlib.dll附加信息:类型初始化程序 &#39; BacktrackingPathFinder.Program&#39;抛出异常。
更新
我做了@Poke说:
class Program
{
static void Main(string[] args)
{
IEnumerable<string[]> transitions = File.ReadAllLines(@"TransitionTable.csv").Skip(1).Select(x => x.Split(','));
State q0 = new State("q0");
Event e1 = new Event("sig1");
PathFinder pf = new PathFinder();
State s = pf.FindDestination(q0, e1, transitions);
Console.WriteLine(s.ID);
Console.ReadLine();
}
}