private ExecutionStrategyHelper readExecutionStrategy(XPathNavigator nav) { if (nav != null) { ExecutionStrategyHelper strategy = null; ExecutionStrategyHelper exs = null; XPathNodeIterator it = nav.Select("rule | choice | max | arb"); string currentItem = null; while (it.MoveNext()) { currentItem = it.Current.Name; ExecutionStrategyHelper ex; if (currentItem == "rule") { if (exs == null) { exs = new ExecutionStrategyHelper(StrategyOperator.SEQUENCE); } else if (exs.Operator != StrategyOperator.SEQUENCE) { ex = new ExecutionStrategyHelper(StrategyOperator.SEQUENCE); exs.Next = ex; exs = ex; } string x = it.Current.GetAttribute("name", String.Empty); if (!String.IsNullOrEmpty(x)) { exs.Rules.Add(x); } } else if (currentItem == "choice") { ex = new ExecutionStrategyHelper(StrategyOperator.CHOICE); if (exs == null) { exs = ex; } else { exs.Next = ex; exs = ex; } readRuleList(it.Current, exs.Rules); } else if (currentItem == "max") { ex = new ExecutionStrategyHelper(StrategyOperator.MAX); if (exs == null) { exs = ex; } else { exs.Next = ex; exs = ex; } readRuleList(it.Current, exs.Rules); } else if (currentItem == "arb") { ex = new ExecutionStrategyHelper(StrategyOperator.ARBITRARY); if (exs == null) { exs = ex; } else { exs.Next = ex; exs = ex; } readRuleList(it.Current, exs.Rules); } if (strategy == null) { strategy = exs; } } return(strategy); } return(new ExecutionStrategyHelper(StrategyOperator.SEQUENCE)); }
public KPsystem Read() { kp = new KPsystem(); XPathDocument doc = new XPathDocument(FileName); XPathNavigator nav = doc.CreateNavigator(); XPathNavigator root = nav.SelectSingleNode("/kpsystem"); XPathNavigator meta = root.SelectSingleNode("meta"); readPItem(meta, kp); if (namedInstances == null) { namedInstances = new Dictionary <string, MInstance>(); } else { namedInstances.Clear(); } //iterate through all nodes that contain XPathNodeIterator mtype = root.SelectChildren("mtype", String.Empty); while (mtype.MoveNext()) { string mtypeName = mtype.Current.GetAttribute("name", String.Empty); MType mt = kp[mtypeName]; currentType = mt; readPItem(mtype.Current, mt); XPathNodeIterator it = mtype.Current.SelectChildren("instance", String.Empty); while (it.MoveNext()) { mt.Instances.Add(readInstance(it.Current)); } Dictionary <string, Rule> rules = new Dictionary <string, Rule>(); it = mtype.Current.SelectChildren("rule", String.Empty); while (it.MoveNext()) { Rule r = readRule(it.Current); rules.Add(r.Name, r); } ExecutionStrategyHelper sh = null; XPathNavigator s = mtype.Current.SelectSingleNode("strategy"); if (s != null) { sh = readExecutionStrategy(s); } ExecutionStrategy ex = null; ExecutionStrategy exit = null; ExecutionStrategyHelper shit = sh; while (shit != null) { if (exit == null) { exit = new ExecutionStrategy(shit.Operator); ex = exit; } else { exit.Next = new ExecutionStrategy(shit.Operator); exit = exit.Next; } foreach (string r in shit.Rules) { Rule rule = null; rules.TryGetValue(r, out rule); if (rule != null) { exit.Rules.Add(rules[r]); } } shit = shit.Next; } mt.ExecutionStrategy = ex; } XPathNodeIterator links = root.SelectChildren("link", String.Empty); while (links.MoveNext()) { XPathNavigator lnav = links.Current; InstanceIdentifier lhs = readInstanceIdentifier(lnav); if (lhs.Indicator == InstanceIndicator.NAME) { MInstance lhsVal = null; if (namedInstances.TryGetValue(lhs.Value, out lhsVal)) { InstanceIdentifier rhs = readInstanceIdentifier(lnav.SelectSingleNode("with")); if (rhs.Indicator == InstanceIndicator.NAME) { MInstance rhsVal = null; if (namedInstances.TryGetValue(rhs.Value, out rhsVal)) { if (!Object.ReferenceEquals(rhsVal, lhsVal)) { lhsVal.ConnectBidirectionalTo(rhsVal); } } } else if (rhs.Indicator == InstanceIndicator.TYPE) { if (!String.IsNullOrEmpty(rhs.Value)) { MType t = kp[rhs.Value]; foreach (MInstance inst in t.Instances) { if (!Object.ReferenceEquals(inst, lhsVal)) { lhsVal.ConnectBidirectionalTo(inst); } } } } } } else if (lhs.Indicator == InstanceIndicator.TYPE) { if (!String.IsNullOrEmpty(lhs.Value)) { MType t = kp[lhs.Value]; InstanceIdentifier rhs = readInstanceIdentifier(lnav.SelectSingleNode("with")); if (rhs.Indicator == InstanceIndicator.NAME) { MInstance rhsVal = null; if (namedInstances.TryGetValue(rhs.Value, out rhsVal)) { foreach (MInstance inst in t.Instances) { if (!Object.ReferenceEquals(inst, rhsVal)) { rhsVal.ConnectBidirectionalTo(inst); } } } } else if (rhs.Indicator == InstanceIndicator.TYPE) { if (!String.IsNullOrEmpty(rhs.Value)) { MType t2 = kp[rhs.Value]; foreach (MInstance inst in t.Instances) { foreach (MInstance inst2 in t2.Instances) { if (!Object.ReferenceEquals(inst, inst2)) { inst.ConnectBidirectionalTo(inst2); } } } } } } } } return(kp); }