/// <summary> /// Get next word. If the next natural word does not obey given predicate, throw exception /// If we reach the end of the list get random key matching end condition passed in Reset /// </summary> /// <param name="condition"> Predicate next word must obey </param> /// <returns></returns> public string GetNextWord(Predicate <string> condition) { //If word generator has never been reset, reset with default values if (!SubchainsInitialized) { ResetSubchains(); } Predicate <string> realCondition = condition; if (realCondition == null) { realCondition = _alwaysTrueCondition.WordCondition; } ChainKey currKey = _currKey; string currWord = CurrentWord; string[] newChainKeyVals = new string[currKey.Words.Length]; for (int i = 0; i < currKey.Words.Length - 1; i++) { newChainKeyVals[i] = currKey.Words[i + 1]; } newChainKeyVals[currKey.Words.Length - 1] = currWord; currKey = new ChainKey(newChainKeyVals); List <string> potentialWords = null; try { potentialWords = _chains.GetValues(currKey); } catch (Exceptions.InvalidKey) { //if we get here that means we hit the end of the list newChainKeyVals = GetRandomKey(_onEndSubChains, ck => !ck.Equals(currKey)).Words; currKey = new ChainKey(newChainKeyVals); potentialWords = _chains.GetValues(currKey); } bool candidateFound = GetCandidate(potentialWords, condition, out currWord); if (!candidateFound) { throw new Exceptions.NoPossibleElements("Unable to find a state matching given conditions"); } _currKey = currKey; CurrentWord = currWord; return(currWord); }
/// <summary> /// Reset using currently existing initialCondition and onEndCondition /// This should be a low cpu usage operation /// </summary> public void ResetReadOnly() { //verify if reset has never been called if (!SubchainsInitialized) { throw new Exceptions.InitializationRequired("ResetReadOnly should be called after a regular reset"); } //All chains and words should match condition so we can just get random ChainKey currKey = GetRandomKey(_initialConditionSubKeys); string currWord; GetCandidate(_initialConditionSubChains.GetValues(currKey), null, out currWord); CurrentWord = currWord; _currKey = currKey; }
private ChainKey[] CreateKeysArray(ChainMap map, Predicate <ChainKey> keyCondition) { List <ChainKey> newKeys = new List <ChainKey>(); List <ChainKey> origKeys = map.GetAllKeys(); foreach (ChainKey origKey in origKeys) { if (keyCondition.Invoke(origKey)) { //Add key multiple times to ensure probability of picking it is proportional to the //number of values foreach (string val in map.GetValues(origKey)) { newKeys.Add(origKey); } } } return(newKeys.ToArray()); }
public static Dictionary<ChainKey, List<string>> GetUnderlyingDictionary(ChainMap inputChainMap) { Dictionary<ChainKey, List<string>> retValues = new Dictionary<ChainKey, List<string>>() ; List<ChainKey> keys = inputChainMap.GetAllKeys() ; foreach(ChainKey key in keys) { retValues.Add(key, inputChainMap.GetValues(key)) ; } return retValues; }
private ChainKey[] CreateKeysArray(ChainMap map, Predicate<ChainKey> keyCondition) { List<ChainKey> newKeys = new List<ChainKey>() ; List<ChainKey> origKeys = map.GetAllKeys() ; foreach(ChainKey origKey in origKeys) { if(keyCondition.Invoke(origKey)) { //Add key multiple times to ensure probability of picking it is proportional to the //number of values foreach(string val in map.GetValues(origKey)) { newKeys.Add(origKey) ; } } } return newKeys.ToArray() ; }