private void AssertRuleDoesNotMatchCondtitional( string ruleText, int[] sourceSymbols = null, float[][] sourceParameters = null, string axiom = null, int ruleParamMemoryStartIndex = 0, int matchIndex = 0, int paramTempMemorySize = 0, float[] globalParams = null, string[] globalParamNames = null, string includedSymbols = "[]ABCDE" ) { var totalIncluded = new HashSet <int>(includedSymbols.Select(x => (int)x)); globalParamNames = globalParamNames ?? new string[0]; using var symbols = new DependencyTracker <SymbolString <float> >( axiom == null ? new SymbolString <float>(sourceSymbols, sourceParameters) : SymbolString <float> .FromString(axiom, Allocator.Persistent) ); var ruleFromString = new BasicRule( RuleParser.ParseToRule(ruleText, x => x, globalParameters: globalParamNames), '[', ']'); using var ruleNativeData = new SystemLevelRuleNativeData(new[] { ruleFromString }); var nativeWriter = new SymbolSeriesMatcherNativeDataWriter(); ruleFromString.WriteDataIntoMemory(ruleNativeData, nativeWriter); globalParams = globalParams ?? new float[0]; using var globalNative = new NativeArray <float>(globalParams, Allocator.Persistent); using var paramMemory = new NativeArray <float>(paramTempMemorySize, Allocator.Persistent); using var branchCache = new SymbolStringBranchingCache('[', ']', new[] { totalIncluded }, ruleNativeData); branchCache.BuildJumpIndexesFromSymbols(symbols); var random = new Unity.Mathematics.Random(); var matchSingleData = new LSystemSingleSymbolMatchData { isTrivial = false, tmpParameterMemorySpace = JaggedIndexing.GetWithNoLength(ruleParamMemoryStartIndex) }; var potentialMatch = ruleFromString.AsBlittable().PreMatchCapturedParametersWithoutConditional( branchCache, symbols.Data, matchIndex, paramMemory, matchSingleData.tmpParameterMemorySpace.index, ref matchSingleData, new TmpNativeStack <SymbolStringBranchingCache.BranchEventData>(5), globalNative, ruleNativeData.dynamicOperatorMemory, ref random, ruleNativeData.ruleOutcomeMemorySpace ); Assert.IsFalse(potentialMatch); }
public void FindsBranchForwardLinksCorrectlyWhenBranchingAtSameIndexAsCharacterCodeForBranchingSymbol() { using var targetString = new DependencyTracker <SymbolString <float> >(SymbolString <float> .FromString("[&E][&&E]&EEEE[&[EE]E][&&[EE]E]&EEEE[&[EEEE]EE][&&[EEEE]EE]&EEEA[&[EEEEEE]E[E]E[E]][&&[EEEEEE]E[E]E[E]]")); using var branchingCache = new SymbolStringBranchingCache('[', ']', new SystemLevelRuleNativeData()); branchingCache.BuildJumpIndexesFromSymbols(targetString); Assert.AreEqual(64, branchingCache.FindOpeningBranchIndexReadonly(82)); Assert.AreEqual(86, branchingCache.FindOpeningBranchIndexReadonly(93)); }
public void FindsClosingBranchLinks() { using var targetString = new DependencyTracker <SymbolString <float> >(SymbolString <float> .FromString("A[AA]AAA[A[AAA[A]]]A")); using var branchingCache = new SymbolStringBranchingCache('[', ']', new SystemLevelRuleNativeData()); branchingCache.BuildJumpIndexesFromSymbols(targetString); Assert.AreEqual(4, branchingCache.FindClosingBranchIndexReadonly(1)); Assert.AreEqual(17, branchingCache.FindClosingBranchIndexReadonly(10)); Assert.AreEqual(18, branchingCache.FindClosingBranchIndexReadonly(8)); }
public SymbolString <float> GetAxiom(Allocator allocator = Allocator.Persistent) { if (!fileIndexesByFullIdentifier.ContainsKey(originFile)) { throw new LinkException(LinkExceptionType.BAD_ORIGIN_FILE, $"could not find origin file '{originFile}'"); } var originFileData = allFiles.data[fileIndexesByFullIdentifier[originFile]]; return(SymbolString <float> .FromString(originFileData.axiom, allocator, chr => originFileData.GetSymbolInFile(chr))); }
private void AssertBackwardsMatch( string target, string matcher, bool shouldMatch, string includeSymbols = "[]ABCDEFJ", int parameterMemorySize = 10, float[] expectedCapturedParameters = null, int indexInTarget = -1) { var includedSymbolSet = includeSymbols.Select(x => (int)x); var prefixBuilder = SymbolSeriesPrefixBuilder.Parse(matcher); using var nativeData = new SystemLevelRuleNativeData(new RuleDataRequirements { prefixNodes = prefixBuilder.RequiredNodeMemorySpace }); var writer = new SymbolSeriesMatcherNativeDataWriter(); var seriesMatcher = prefixBuilder.BuildIntoManagedMemory(nativeData, writer); using var targetString = new DependencyTracker <SymbolString <float> >(SymbolString <float> .FromString(target)); using var branchingCache = new SymbolStringBranchingCache( '[', ']', includedSymbolSet == null ? new HashSet <int> [0] : new[] { new HashSet <int>(includedSymbolSet) }, nativeData); branchingCache.BuildJumpIndexesFromSymbols(targetString); using var parameterMemory = new NativeArray <float>(parameterMemorySize, Allocator.Persistent); var realIndex = indexInTarget < 0 ? indexInTarget + targetString.Data.Length : indexInTarget; var hasMatched = branchingCache.MatchesBackwards( branchingCache.includeSymbols[0], realIndex, seriesMatcher, targetString.Data, 0, parameterMemory, out var copiedParams); if (shouldMatch != hasMatched) { Assert.Fail($"Expected '{matcher}' to {(shouldMatch ? "" : "not ")}match backwards from {indexInTarget} in '{target}'"); } if (shouldMatch && expectedCapturedParameters != null) { Assert.AreEqual(expectedCapturedParameters.Length, copiedParams); for (int i = 0; i < expectedCapturedParameters.Length; i++) { Assert.AreEqual(expectedCapturedParameters[i], parameterMemory[i]); } } }
public void SymbolStringConstructorConvertsToInts() { var symbolFromString = SymbolString <float> .FromString("AABA"); var convertedSymbols = symbolFromString.symbols; Assert.AreEqual(convertedSymbols[0], 65); Assert.AreEqual(convertedSymbols[1], 65); Assert.AreEqual(convertedSymbols[2], 66); Assert.AreEqual(convertedSymbols[3], 65); symbolFromString.Dispose(); }
private void AssertForwardsMatch( string target, string matcher, bool shouldMatch = true, int indexInTarget = 0, int parameterMemorySize = 10, float[] expectedCapturedParameters = null, string message = null, string includeSymbols = "[]ABCDEFGJKZ") { var seriesMatcher = SymbolSeriesGraphTests.GenerateSingleMatcher(matcher, out var nativeData); using var nativeDataDisposable = nativeData; using var targetString = new DependencyTracker <SymbolString <float> >(SymbolString <float> .FromString(target)); using var branchingCache = new SymbolStringBranchingCache( '[', ']', includeSymbols == null ? new HashSet <int> [0] : new[] { new HashSet <int>(includeSymbols.Select(x => (int)x)) }, nativeDataDisposable); branchingCache.BuildJumpIndexesFromSymbols(targetString); using var parameterMemory = new NativeArray <float>(parameterMemorySize, Allocator.Persistent); var matches = branchingCache.MatchesForward( branchingCache.includeSymbols[0], indexInTarget, seriesMatcher, targetString.Data, 0, parameterMemory, out var copiedParams, new TmpNativeStack <SymbolStringBranchingCache.BranchEventData>(5)); if (shouldMatch != matches) { Assert.Fail($"Expected '{matcher}' to {(shouldMatch ? "" : "not ")}match forwards retaining order from {indexInTarget} in '{target}'{(message == null ? "" : '\n' + message)}"); } if (shouldMatch && expectedCapturedParameters != null) { Assert.AreEqual(expectedCapturedParameters.Length, copiedParams, "captured parameter length mismatch"); for (int i = 0; i < expectedCapturedParameters.Length; i++) { Assert.AreEqual(expectedCapturedParameters[i], parameterMemory[i]); } } }
public DefaultLSystemState(string axiom, uint seed = 1) { currentSymbols = new DependencyTracker <SymbolString <float> >(SymbolString <float> .FromString(axiom)); randomProvider = new Unity.Mathematics.Random(seed); }