private async Task <bool> HasAdjustmentAmountAsync(State state) { if (((Adjustment)state).AdjustmentAmount.HasValue) { var adjustmentDecision = state.Antecedent as AdjustmentDecision; var collateData = adjustmentDecision.Antecedent as CollateData; if ((((Adjustment)state).AdjustmentAmount.Value + collateData.RedressAmount.Value) > 100) { if (Transition == null) { Transition = Transitions.FirstOrDefault(); } await TaskRunner.DoAsyncStuff(); return(true); } var error = "The sum of the redress amount and the adjustment amount must be greater than 100."; state.Log.Add(new LogEntry(error)); throw new StateException(state, error); } else { var error = String.Format("{0} requires an adjustment amount before it can be completed.", state.Name); state.WriteLogEntry(error); throw new StateException(state, error); } }
private VisualTransition FindTransition(string oldStateName, string newStateName) { if (oldStateName.IsNullOrEmpty() || newStateName.IsNullOrEmpty()) { return(null); } var perfectMatch = Transitions.FirstOrDefault(vt => string.Equals(vt.From, oldStateName) && string.Equals(vt.To, newStateName)); if (perfectMatch != null) { return(perfectMatch); } var fromMatch = Transitions.FirstOrDefault(vt => string.Equals(vt.From, oldStateName) && vt.To == null); if (fromMatch != null) { return(fromMatch); } var toMatch = Transitions.FirstOrDefault(vt => vt.From == null && string.Equals(vt.To, newStateName)); return(toMatch); }
/// <summary> /// Handle an event. /// </summary> /// <param name="e">Event to handle.</param> /// <returns>Returns the new state of the machine.</returns> public IState <D> ReceiveEvent(IMachineEvent e) { var transition = Transitions.FirstOrDefault(t => t.Event.Equals(e) && (t.Guard?.Invoke(Data, e) ?? true)); if (transition == null) { return(null); } transition.Data = Data; transition.Action?.Invoke(Data, e); return(transition.To); }
public StateInfo TransitionTo(object state, object trigger) { if (Transitions.FirstOrDefault(x => x.State == (int)state && x.Trigger == (int)trigger) == null) { Transitions.Add(new Transition() { State = (int)state, Trigger = (int)trigger }); } return(this); }
protected bool CanTriggerFirstAble(out PetrinetTransition first) { var ret = false; first = null; if (GlobalTriggerAreaForAllTransitions == null) { } else if (GlobalTriggerAreaForAllTransitions.bounds.Contains(Controller.transform.position)) { ret = true; } else { var forward = Controller.transform.forward; switch (TransitionTrigger) { case Trigger.Point: RaycastHit info; if (GlobalTriggerAreaForAllTransitions.Raycast(new Ray(Controller.transform.position, forward), out info, 10f)) { ret = true; } break; case Trigger.Proximity: var distance = GlobalTriggerAreaForAllTransitions.bounds.Contains(Controller.transform.position) ? 0f : Vector3.Distance(Controller.transform.position, GlobalTriggerAreaForAllTransitions.ClosestPointOnBounds(Controller.transform.position)); if (distance < TriggerDistance) { ret = true; } break; } } if (!ret) { return(false); } first = Transitions.FirstOrDefault(t => t.GetConditionsFulfilled().Values.Any(v => v)); if (first == null) { ret = false; } return(ret); }
/// <summary> /// 获取当前执行的跳转路线 /// </summary> /// <param name="transitionID">跳转ID</param> /// <returns>跳转对象</returns> protected Transition GetExecuteTransition(string transitionID) { Transition executeTransition = Transitions .FirstOrDefault(t => t.NID == transitionID); ASTNode an = this.GetNode(executeTransition.Destination); Transition returnTransition = executeTransition; while (an.NodeType == Enums.WorkflowNodeCategory.Decision) { WorkflowDecision decision = WorkflowDecision.ConvertToReallyType(an); returnTransition = decision.GetTransition(); an = this.GetNode(returnTransition.Destination); } return(returnTransition); }
private async Task <bool> HasRedressRateAsync(State state) { if (((CollateData)state).RedressAmount.HasValue) { if (Transition == null) { Transition = Transitions.FirstOrDefault(); } await TaskRunner.DoAsyncStuff(); return(true); } var error = String.Format("{0} requires a redress amount before it can be completed.", state.Name); state.WriteLogEntry(error); throw new StateException(state, error); }
internal async Task CalculateFinalRedressAmountAsync(State context) { var states = context.Flatten(); var collateData = states.OfType <CollateData>().FirstOrDefault(s => s.Name.Equals("Collate Data")); var responseReceived = states.OfType <ResponseReceived>().FirstOrDefault(s => s.Name.Equals("Response Received")); var adjustment = states.OfType <Adjustment>().FirstOrDefault(s => s.Name.Equals("Adjustment")); decimal?finalRedressAmount = 0; if (collateData != null && collateData.RedressAmount.HasValue) { finalRedressAmount = collateData.RedressAmount; } if (adjustment != null && adjustment.AdjustmentAmount.HasValue) { finalRedressAmount += adjustment.AdjustmentAmount; } if (responseReceived != null && responseReceived.ConsequentialLossClaim.HasValue) { finalRedressAmount += responseReceived.ConsequentialLossClaim; } FinalRedressAmount = finalRedressAmount; if (Transition == null) { Transition = Transitions.FirstOrDefault(s => s.Name.Equals("Payment")); } await TaskRunner.DoAsyncStuff(); }
public int ProcessLine(string line) { currentState = StartState; var tempState = currentState; currentInput = line; int i; var tempI = 0; var currentWord = ""; var tempCurrentWord = ""; for (i = 0; i < currentInput.Length; i++) { var currentChar = currentInput[i]; if (char.IsWhiteSpace(currentChar)) { continue; } if (AcceptStates.Contains(currentState)) { tempState = currentState; tempI = i; tempCurrentWord = currentWord; } var transition = Transitions.FirstOrDefault(t => t.FromState == currentState && t.Symbol == currentChar); if (transition == null) { if (AcceptStates.Contains(currentState)) { currentState = StartState; i--; tempState = null; Console.WriteLine(currentWord + " " + LanguageWords.FirstOrDefault(kvp => kvp.Value == currentWord).Key); currentWord = ""; continue; } if (tempState == null || tempState == StartState) { return(i); } currentState = tempState; currentWord = tempCurrentWord; i = tempI - 1; i--; tempState = null; continue; } currentState = transition.ToState; currentWord += currentChar; } if (!AcceptStates.Contains(currentState)) { return(i); } Console.WriteLine(currentWord + " " + LanguageWords.FirstOrDefault(kvp => kvp.Value == currentWord).Key); return(-1); }
public State GetNextState(List <TuringTape> read) { var match = Transitions.FirstOrDefault(c => c.IsMatch(read)); return(match.Apply(read)); }
/// <summary> /// Returns the first element that matches the name. /// </summary> /// <param name="name"></param> /// <returns></returns> /// <author>Jannik Arndt</author> public Transition FindTransition(String name) { return(Transitions.FirstOrDefault(x => x.Name == name)); }
public void Minimize() { var distinct = new TriangularTable <int, State>(States.Count, f => f.StateNumber); distinct.Fill(-1); // Fill with empty states // Create a function for the distinct state pairs and performing an action on them Action <Action <State, State> > distinctStatePairs = action => { for (int i = 0; i < States.Count; ++i) { var p = States[i]; for (int j = i + 1; j < States.Count; ++j) { var q = States[j]; action(p, q); } } }; // Get a set of all valid input ranges that we have in the DFA ISet <CharRange> allValidInputs = new HashSet <CharRange>(); foreach (var transition in Transitions) { allValidInputs.UnionWith(transition.ValidInput.Ranges); } // For every distinct pair of states, if one of them is an accepting state // and the other one is not set the distinct distinctStatePairs((p, q) => { var pIsAcceptState = p.AcceptState; var bIsAcceptState = q.AcceptState; if (bIsAcceptState && pIsAcceptState) { // If both are accepting states, then we might have an issue merging them. // this is because we use multiple regular expressions with different endings when // constructing lexers. var pAcceptStates = p.NfaStates.Where(f => f.AcceptState).ToList(); var qAcceptStates = q.NfaStates.Where(f => f.AcceptState).ToList(); if (pAcceptStates.Count() == qAcceptStates.Count()) { foreach (var pAcceptState in pAcceptStates) { if (!qAcceptStates.Contains(pAcceptState)) { // Since the accepting states differ, its not cool to merge // these two states. distinct[p, q] = int.MaxValue; } } } else { // Not the same number of states, not cool to merge distinct[p, q] = int.MaxValue; } } if (pIsAcceptState ^ bIsAcceptState) { distinct[p, q] = int.MaxValue; } }); // Make a dictionary of from transitions. This is well worth the time, since // this gets accessed lots of times. var targetDict = new Dictionary <State, Dictionary <CharRange, State> >(); foreach (var transition in Transitions) { Dictionary <CharRange, State> toDict; targetDict.TryGetValue(transition.From, out toDict); if (toDict == null) { toDict = new Dictionary <CharRange, State>(); targetDict.Add(transition.From, toDict); } foreach (var range in transition.ValidInput.Ranges) { toDict.Add(range, transition.To); } } // Start iterating bool changes; do { changes = false; distinctStatePairs((p, q) => { if (distinct[p, q] == -1) { Func <State, CharRange, State> targetState = (state, c) => { Dictionary <CharRange, State> charDict; if (targetDict.TryGetValue(state, out charDict)) { State toState; if (charDict.TryGetValue(c, out toState)) { return(toState); } } return(null); }; foreach (var a in allValidInputs) { var qa = targetState(q, a); var pa = targetState(p, a); if (pa == null ^ qa == null) { // If one of them has a transition on this character range but the other one doesn't then // they are separate. distinct[p, q] = a.GetHashCode(); changes = true; break; } // If both are null, then we carry on. // The other one is null implictly since we have XOR checked it earlier if (qa == null) { continue; } if (distinct[qa, pa] != -1) { distinct[p, q] = a.GetHashCode(); changes = true; break; } } } }); } while (changes); // Merge states that still have blank square // To make this work we have to bunch states together since the indices will be screwed up var mergeSets = new List <ISet <State> >(); Func <State, ISet <State> > findMergeList = s => mergeSets.FirstOrDefault(m => m.Contains(s)); distinctStatePairs((p, q) => { // No need to check those that we have already determined to be distinct if (distinct[p, q] != -1) { return; } // These two states are supposed to merge! // See if p or q is already part of a merge list! var pMergeSet = findMergeList(p); var qMergeSet = findMergeList(q); if (pMergeSet == null && qMergeSet == null) { // No previous set for either // Add a new merge set mergeSets.Add(new HashSet <State> { p, q }); } else if (pMergeSet != null && qMergeSet == null) { // Add q to pMergeSet pMergeSet.Add(q); } else if (pMergeSet == null) { // Add p to qMergeSet qMergeSet.Add(p); } else { // Both previously have merge sets // If its not the same set (which it shoudln't be) then add their union if (pMergeSet != qMergeSet) { // Union everything into the pMergeSet pMergeSet.UnionWith(qMergeSet); // Remove the qMergeSet mergeSets.Remove(qMergeSet); } } }); // Armed with the merge sets, we can now do the actual merge foreach (var mergeSet in mergeSets) { // The lone state that should remain is the FIRST set in the mergeset var stateList = mergeSet.ToList(); var outputState = stateList[0]; // If this statelist contains the startstate, the new startstate will have to be // the new output state if (stateList.Contains(StartState)) { StartState = outputState; } // Iterate over all the states in the merge list except for the one we have decided // to merge everything into. for (int i = 1; i < stateList.Count; ++i) { var toRemove = stateList[i]; // Find all transitions that went to this state var toTransitions = Transitions.Where(f => f.To == toRemove).ToList(); foreach (var transition in toTransitions) { // There can be two cases here, either there already is a new transition to be found, in // which case we can merge the valid input instead. The alternative is that there is no prior // transition, in which case we repoint our transition to the output state. var existingTransition = Transitions.FirstOrDefault(f => f.From == transition.From && f.To == outputState); if (existingTransition != null) { existingTransition.ValidInput.UnionWith(transition.ValidInput); Transitions.Remove(transition); // Remove the old transition } else { transition.To = outputState; } } // Find all transitions that went from this state var fromTransitions = Transitions.Where(f => f.From == toRemove).ToList(); foreach (var transition in fromTransitions) { // Same two cases as the code above var existingTransition = Transitions.FirstOrDefault(f => f.From == outputState && f.To == transition.To); if (existingTransition != null) { existingTransition.ValidInput.UnionWith(transition.ValidInput); Transitions.Remove(transition); // Remove the old transition } else { transition.From = outputState; } } // Since before removing this state, we need to merge the list of NFA states that created both of these states foreach (var nfaState in toRemove.NfaStates) { if (!outputState.NfaStates.Contains(nfaState)) { outputState.NfaStates.Add(nfaState); } } // There should be no more references to this state. It can thus be removed. States.Remove(toRemove); } } // The states now need to be renumbered AssignStateNumbers(); }