Ejemplo n.º 1
0
        public void GenerateGUIFiles(Dictionary <string, StringBuilder> outputFiles, Type targetType, string outNamespace, byte maxRecursionLevel)
        {
            if (targetType.IsGenericType)
            {
                throw new NotImplementedException("Generic Types are not supported.");
            }

            List <Type> renderedTypes = new List <Type>();

            WPFClass2GUIBuilder wpfGen = new WPFClass2GUIBuilder();

            wpfGen.BuildControls(targetType, targetType.GetProperties().ToList(), outNamespace, maxRecursionLevel);

            foreach (WPFClass2GUIBuilder resWpfGen in wpfGen.InnerBuilders)
            {
                resWpfGen.Render();

                string xamlFile = $"{CN_VIEW_FOLDER}\\{resWpfGen.TargetType.Name}Window.xaml";
                string xamlCS   = $"{CN_VIEW_FOLDER}\\{resWpfGen.TargetType.Name}Window.xaml.cs";
                string vmFile   = $"{CN_VM_FOLDER}\\{resWpfGen.TargetType.Name}ViewModel.cs";

                outputFiles.AddIfNotContainsKey($"{CN_VM_BASE_FILE}", resWpfGen.RenderOutput.ViewModelBase);
                outputFiles.AddIfNotContainsKey($"{CN_DISPLAYNAMECONVERTER_FILE}", resWpfGen.RenderOutput.DisplayNameConverter);

                outputFiles.AddIfNotContainsKey(xamlFile, resWpfGen.RenderOutput.Xaml);
                outputFiles.AddIfNotContainsKey(xamlCS, resWpfGen.RenderOutput.XamlCodebehind);
                outputFiles.AddIfNotContainsKey(vmFile, resWpfGen.RenderOutput.ViewModel);
            }
        }
Ejemplo n.º 2
0
        public void AddIfNotContainsKey()
        {
            var @this = new Dictionary <string, string>();

            @this.AddIfNotContainsKey("Fizz", "FizzBuzz");
            @this.AddIfNotContainsKey("Fizz", () => "Buzz");

            Assert.AreEqual("FizzBuzz", @this["Fizz"]);
        }
Ejemplo n.º 3
0
        public void AddIfNotContainsKey()
        {
            // Type
            var @this = new Dictionary <string, string>();

            // Examples
            @this.AddIfNotContainsKey("Fizz", "FizzBuzz");   // Add "FizzBuzz";
            @this.AddIfNotContainsKey("Fizz", () => "Buzz"); // Add Nothing;

            // Unit Test
            Assert.AreEqual("FizzBuzz", @this["Fizz"]);
        }
        public void AddIfNotContainsKey()
        {
            // Type
            var @this = new Dictionary<string, string>();

            // Examples
            @this.AddIfNotContainsKey("Fizz", "FizzBuzz"); // Add "FizzBuzz";
            @this.AddIfNotContainsKey("Fizz", () => "Buzz"); // Add Nothing;

            // Unit Test
            Assert.AreEqual("FizzBuzz", @this["Fizz"]);
        }
        public void AddIfNotContainsKeyTestCase2()
        {
            var key = RandomValueEx.GetRandomString();
            var dic = new Dictionary<String, String>();

            var result = dic.AddIfNotContainsKey( key, () => RandomValueEx.GetRandomString() );
            Assert.IsTrue( result );
            Assert.AreEqual( 1, dic.Count );

            result = dic.AddIfNotContainsKey( key, () => RandomValueEx.GetRandomString() );
            Assert.IsFalse( false );
            Assert.AreEqual( 1, dic.Count );
        }
        public void AddIfNotContainsKeyTest2()
        {
            var key = RandomValueEx.GetRandomString();
            var dic = new Dictionary <String, String>();

            var result = dic.AddIfNotContainsKey(key, RandomValueEx.GetRandomString);

            Assert.True(result);
            Assert.Single(dic);

            result = dic.AddIfNotContainsKey(key, RandomValueEx.GetRandomString);
            Assert.False(result);
            Assert.Single(dic);
        }
        public void AddIfNotContainsKeyTest()
        {
            var key = Extensions.GetRandomString();
            var dic = new Dictionary <String, String>();

            var result = dic.AddIfNotContainsKey(key, Extensions.GetRandomString());

            Assert.True(result);
            Assert.Single(dic);

            result = dic.AddIfNotContainsKey(key, Extensions.GetRandomString());
            Assert.False(result);
            Assert.Single(dic);
        }
        public void AddIfNotContainsKeyTest()
        {
            var key = RandomValueEx.GetRandomString();
            var dic = new Dictionary <String, String>();

            var result = dic.AddIfNotContainsKey(key, RandomValueEx.GetRandomString());

            Assert.True(result);
            Assert.Equal(1, dic.Count);

            result = dic.AddIfNotContainsKey(key, RandomValueEx.GetRandomString());
            Assert.False(result);
            Assert.Equal(1, dic.Count);
        }
        public bool AddProvider(ILogHelperProvider provider)
        {
            if (null == provider)
            {
                return(false);
            }

            return(_logHelperProviders.AddIfNotContainsKey(provider.GetType(), provider));
        }
Ejemplo n.º 10
0
        static int Fravik(int[] a)
        {
            Dictionary<int, int> t = new Dictionary<int, int>();
            foreach (int n in a)
            {
                t.AddIfNotContainsKey(n, 1, 1);
            }

            return (from n in t
                   where n.Value != 1
                   select n).ToArray()[0].Key;
        }
Ejemplo n.º 11
0
        public void DictionaryAddIfNotContainsKey()
        {
            var dic = new Dictionary <string, string>();

            var addResult1 = dic.AddIfNotContainsKey("key1", "value1");

            Assert.True(addResult1);
            Assert.Single(dic);

            var addResult2 = dic.AddIfNotContainsKey("key1", "key1");

            Assert.False(addResult2);
            Assert.Single(dic);

            var addResult3 = dic.AddIfNotContainsKey("key2", () => "value2");

            Assert.True(addResult3);
            Assert.Equal(2, dic.Count);

            var addResult4 = dic.AddIfNotContainsKey("key2", () => "value2");

            Assert.False(addResult4);
            Assert.Equal(2, dic.Count);

            var addResult5 = dic.AddIfNotContainsKey("key3", s => "value3");

            Assert.True(addResult5);
            Assert.Equal(3, dic.Count);

            var addResult6 = dic.AddIfNotContainsKey("key3", s => "value3");

            Assert.False(addResult6);
            Assert.Equal(3, dic.Count);
        }
Ejemplo n.º 12
0
        public Dictionary <int, List <Dictionary <string, List <string> > > > CreateBuildPlan(BuildSourceFilesCollection scripts, int threads = BuildTargetCommand.DefaultThreads)
        {
            Dictionary <string, string> codeScripts = new Dictionary <string, string>();

            /*
             * Mapping script names to build names
             */
            Dictionary <string, string> scriptToBuild = new Dictionary <string, string>();

            foreach (var kvp in scripts)
            {
                var buildName    = kvp.Key;
                var buildScripts = kvp.Value;
                foreach (var script in buildScripts)
                {
                    string scriptName    = script.Substring(0, script.Length - 4);
                    string scriptNameKey = scriptName.ToLower();
                    codeScripts.Add(scriptNameKey, scriptName);
                    scriptToBuild.Add(scriptNameKey, buildName);
                }
            }

            List <Dictionary <string, List <string> > > preparedChunks = new List <Dictionary <string, List <string> > >();
            List <string> nonpairedScripts = new List <string>();
            int           previousCount    = codeScripts.Count;

            /*
             * Prepare chunks of scripts and push lone scripts into a different array
             */
            while (codeScripts.Any())
            {
                var      currentScript = codeScripts.First().Key;
                string[] preparedChunk = this.graph.getScriptsToCompile(currentScript);
                if (preparedChunk.Length > 1)
                {
                    /*
                     * Chunk mapped per-build
                     */
                    Dictionary <string, List <string> > preparedMappedChunk = new Dictionary <string, List <string> >();
                    foreach (var chunkScript in preparedChunk)
                    {
                        string chunkScriptKey = chunkScript.ToLower();
                        codeScripts.Remove(chunkScriptKey);
                        preparedMappedChunk.AddNewListIfNotContainsKeyAndAddValueToList(scriptToBuild[chunkScriptKey], chunkScript);
                    }

                    preparedChunks.Add(preparedMappedChunk);
                }
                else
                {
                    string nonpairedChunkScript = preparedChunk[0];
                    nonpairedScripts.Add(nonpairedChunkScript);
                    string nonpairedChunkScriptKey = nonpairedChunkScript.ToLower();
                    codeScripts.Remove(nonpairedChunkScriptKey);
                }

                if (codeScripts.Count >= previousCount)
                {
                    throw new InvalidOperationException("Error in planning build, circuit breaker on.");
                }
                else
                {
                    previousCount = codeScripts.Count;
                }
            }

            Dictionary <int, List <Dictionary <string, List <string> > > > threadBuckets = new Dictionary <int, List <Dictionary <string, List <string> > > >();
            Dictionary <int, int> threadBucketsSizes = new Dictionary <int, int>();
            int bucket = 0;

            foreach (var chunk in preparedChunks)
            {
                threadBucketsSizes.AddIfNotContainsKey(bucket, 0);
                threadBuckets.GetOrAddNewIfNotContainsKey(bucket).Add(chunk);
                foreach (var kvp in chunk)
                {
                    var chunkBuild   = kvp.Key;
                    var chunkScripts = kvp.Value;
                    threadBucketsSizes[bucket] += chunkScripts.Count;
                }

                bucket++;
                if (bucket == threads)
                {
                    bucket = 0;
                }
            }

            //Evening the buckets
            int biggestBucket = threadBucketsSizes.Max(kvp => kvp.Value);

            foreach (var kvp in threadBuckets)
            {
                var bucketKey     = kvp.Key;
                int bucketSize    = threadBucketsSizes[bucketKey];
                int neededScripts = biggestBucket - bucketSize;
                Dictionary <string, List <string> > eveningChunk = new Dictionary <string, List <string> >();
                if (neededScripts >= nonpairedScripts.Count)
                {
                    foreach (var nonpairedScript in nonpairedScripts)
                    {
                        string chunkScriptBuild = scriptToBuild[nonpairedScript.ToLower()];
                        eveningChunk.GetOrAddNewIfNotContainsKey(chunkScriptBuild).Add(nonpairedScript);
                    }

                    threadBuckets[bucketKey].Add(eveningChunk);
                    //Not sure if should be here but prolly yes?
                    threadBucketsSizes[bucketKey] += nonpairedScripts.Count;
                    nonpairedScripts = new List <string>();
                    break;
                }

                string[] sliceOfNonpairedScripts = nonpairedScripts.Take(neededScripts).ToArray();
                foreach (var sliceOfNonpairedScript in sliceOfNonpairedScripts)
                {
                    string chunkScriptBuild = scriptToBuild[sliceOfNonpairedScript.ToLower()];
                    eveningChunk.GetOrAddNewIfNotContainsKey(chunkScriptBuild).Add(sliceOfNonpairedScript);
                }

                threadBuckets[bucketKey].Add(eveningChunk);
                threadBucketsSizes[bucketKey] += neededScripts;
                nonpairedScripts = nonpairedScripts.Skip(neededScripts).ToList();
            }

            Dictionary <int, List <Dictionary <string, List <string> > > > restChunks = new Dictionary <int, List <Dictionary <string, List <string> > > >();
            int restChunkBucket = 0;

            foreach (var nonpairedScript in nonpairedScripts)
            {
                Dictionary <string, List <string> > singleScriptChunk = new Dictionary <string, List <string> >();
                string chunkScriptBuild = scriptToBuild[nonpairedScript.ToLower()];
                singleScriptChunk.AddNewListIfNotContainsKey(chunkScriptBuild);
                restChunks.AddNewListIfNotContainsKey(restChunkBucket);

                singleScriptChunk[chunkScriptBuild].Add(nonpairedScript);
                restChunks[restChunkBucket].Add(singleScriptChunk);
                restChunkBucket++;
                if (restChunkBucket == threads)
                {
                    restChunkBucket = 0;
                }
            }

            foreach (var kvp in restChunks)
            {
                var bucketKey           = kvp.Key;
                var restOfScriptsChunks = kvp.Value;
                foreach (var restOfScriptsChunk in restOfScriptsChunks)
                {
                    threadBuckets[bucketKey].Add(restOfScriptsChunk);
                }
            }

            return(threadBuckets);
        }
            public void WhenSourceIsNull_ThenThrowException()
            {
                Dictionary <string, string> sut = null;

                Assert.Throws <ArgumentNullException>(() => sut.AddIfNotContainsKey("key1", "value1"));
            }
        public void Execute(string targets)
        {
            if (!PreExecutionChecks(true, true, false, false))
            {
                return;
            }
            Directory.CreateDirectory(DataDirectory.GetGraphDirectoryPath());
            Build build = new Build(Build.DEFAULT_BUILD_PATH); //This argument might well not be important in this case

            BuildTarget[]       buildTargets       = BuildTargetFactory.ParseCollection(targets, build);
            BuildTargetSimple[] buildTargetsSimple = BuildTargetFactory.GetCollection(buildTargets);
            ProgressWriter?     progressWriter     = null;
            Dictionary <string, List <string> > dependencyGraph = new Dictionary <string, List <string> >();
            Dictionary <string, List <string> > usageGraph      = new Dictionary <string, List <string> >();

            using (ESMAnalyzer esmAnalyzer = ESMAnalyzer.Load())
            {
                progressWriter = new ProgressWriter("Building Interoperable Compilation Graph", buildTargets.GetTotalSourceFiles());
                BuildSourceFilesCollection <BuildTargetSimple> sourceFiles = buildTargetsSimple.GetSourceFiles();
                using (StreamWriter errorLog = new StreamWriter(TES5ScriptDependencyGraph.ErrorLogPath, false))
                {
                    using (StreamWriter debugLog = new StreamWriter(TES5ScriptDependencyGraph.DebugLogPath, false))
                    {
                        foreach (var kvp in sourceFiles)
                        {
                            BuildTargetSimple buildTarget = kvp.Key;
                            var sourceBuildFiles          = kvp.Value;
                            foreach (var sourceFile in sourceBuildFiles)
                            {
                                string scriptName = sourceFile.Substring(0, sourceFile.Length - 4);//remove extension
                                string source     = buildTarget.GetSourceFromPath(scriptName);
                                ITES4CodeFilterable ast;
                                try
                                {
                                    ast = buildTarget.GetAST(source);
                                }
                                catch (EOFOnlyException) { continue; }//Ignore files that are only whitespace or comments.

                                /*catch (UnexpectedTokenException ex)
                                 * {//UnexpectedTokenExceptions no longer occur, so this code should not be invoked.
                                 *  errorLog.WriteLine(sourceFile + ":  " + ex.Message + Environment.NewLine);
                                 *  continue;
                                 * }*/
                                List <TES4ObjectProperty> propertiesAccesses = new List <TES4ObjectProperty>();
                                ast.Filter((data) =>
                                {
                                    TES4ObjectProperty?property = data as TES4ObjectProperty;
                                    if (property == null)
                                    {
                                        return(false);
                                    }
                                    propertiesAccesses.Add(property);
                                    return(true);
                                });
                                Dictionary <string, ITES5Type> preparedProperties = new Dictionary <string, ITES5Type>();
                                foreach (var property in propertiesAccesses)
                                {
                                    Match  match           = TES5ReferenceFactory.ReferenceAndPropertyNameRegex.Match(property.StringValue);
                                    string propertyName    = match.Groups[1].Value;
                                    string propertyKeyName = propertyName.ToLower();
                                    preparedProperties.AddIfNotContainsKey(propertyKeyName, () => esmAnalyzer.GetScriptTypeByEDID(propertyName));
                                }

                                debugLog.WriteLine(scriptName + " - " + preparedProperties.Count + " prepared");
                                string lowerScriptType = scriptName.ToLower();
                                foreach (var kvp2 in preparedProperties)
                                {
                                    var    preparedPropertyKey = kvp2.Key;
                                    string propertyTypeName    = preparedProperties[preparedPropertyKey].OriginalName;
                                    //Only keys are lowercased.
                                    string lowerPropertyType = propertyTypeName.ToLower();
                                    dependencyGraph.AddNewListIfNotContainsKeyAndAddValueToList(lowerPropertyType, lowerScriptType);
                                    usageGraph.AddNewListIfNotContainsKeyAndAddValueToList(lowerScriptType, lowerPropertyType);
                                    debugLog.WriteLine("Registering a dependency from " + scriptName + " to " + propertyTypeName);
                                }

                                progressWriter.IncrementAndWrite();
                            }
                        }
                    }
                }
            }
            progressWriter.Write("Saving");
            TES5ScriptDependencyGraph graph = new TES5ScriptDependencyGraph(dependencyGraph, usageGraph);

            buildTargets.WriteGraph(graph);
            progressWriter.WriteLast();
        }