Esempio n. 1
0
 public static Object DeserializeFromFile(RPath filePath)
 {
     using (Stream stream = File.Open(filePath, FileMode.Open)) {
         BinaryFormatter formatter = new BinaryFormatter();
         return(formatter.Deserialize(stream));
     }
 }
Esempio n. 2
0
        public static string?GenerateNnReport(RPath path, CancellationToken _, NnType type)
        {
            try {
                string exePath = "";
                switch (type)
                {
                case NnType.Nn3: exePath = nn3Path; break;

                case NnType.NnPP: exePath = nnPPPath; break;
                }

                string timeTotal = "", convergence = "";

                string   content = File.ReadAllText(path.SubPath(logFileName));
                string[] lines   = content.Splitter("([\r\n|\r|\n]+)");

                foreach (string line in lines)
                {
                    if (Regex.IsMatch(line, "Simulator Run Time:"))
                    {
                        // timeTotal = Regex.Match(line, @"(\(.*\[h\]\))").Result("$1\n");
                        timeTotal = line + "\n";
                    }
                    else if (Regex.IsMatch(line, "OUTER-ITERATION failed to converge!!"))
                    {
                        convergence = "OUTER-ITERATION failed to converge!!\n";
                    }
                }

                return(timeTotal + convergence);
            } catch {
                return(null);
            }
        }
Esempio n. 3
0
 public static void SerializeToFile(Object obj, RPath filePath)
 {
     using (Stream stream = File.Open(filePath, FileMode.Create)) {
         BinaryFormatter formatter = new BinaryFormatter();
         formatter.Serialize(stream, obj);
     }
 }
Esempio n. 4
0
        bool NnMainExecute(CancellationToken ct, ImmutableDictionary <string, string> options)
        {
            RPath logFilePath = NnMainPath.SubPath(NnAgent.logFileName);

            var tsLog = Util.StartLogParser <NnMainLog>(logFilePath, ct, SetStatus);

            NnAgent.RunNnStructure(
                NnMainPath, Content, ct, Type
                );
            NnAgent.RunNn(
                NnMainPath, Content, ct, Type
                );

            tsLog.Cancel();

            string?report = NnAgent.GenerateNnReport(
                NnMainPath, ct, Type
                );

            if (report != null)
            {
                File.WriteAllText(NnMainReportPath, report);
            }

            // if (File.Exists(NnMainNonSCToken))
            //     File.Delete(NnMainNonSCToken);

            return(!ct.IsCancellationRequested);
        }
Esempio n. 5
0
        bool TestOpenNotepadExecute(CancellationToken ct, ImmutableDictionary <string, string> options)
        {
            RPath logFilePath = TestOpenNotepadPath.SubPath(NnAgent.logFileName);

            var tsLog = Util.StartLogParser <NnMainLog>(logFilePath, ct, SetStatus);

            Util.StartAndWaitProcess(
                "notepad",
                "",
                ct
                );

            tsLog.Cancel();

            return(!ct.IsCancellationRequested);
        }
Esempio n. 6
0
        public static CancellationTokenSource StartLogParser <Log>(RPath logFilePath, CancellationToken ct, Action <string?> status)
            where Log : LogBase, new()
        {
            if (!File.Exists(logFilePath))
            {
                File.Create(logFilePath).Close();
            }

            var tsLog = CancellationTokenSource.CreateLinkedTokenSource(ct);

            Task logTask = Task.Run(
                () => LogParser <Log>(logFilePath, tsLog.Token, status),
                tsLog.Token
                );

            return(tsLog);
        }
Esempio n. 7
0
        NnTemplate(
            string name,
            NnType type,
            RPath path,
            List <Element> elements,
            ImmutableDictionary <string, string?> variables,
            ImmutableDictionary <string, string> derivedVariables
            )
        {
            this.Name             = name;
            this.Type             = type;
            this.FSPath           = path;
            this.Elements         = elements;
            this.Variables        = variables;
            this.DerivedVariables = derivedVariables;

            Save();
        }
Esempio n. 8
0
        public NnPlan(
            string name,
            RPath path,
            NnTemplate template,
            PlanType type = PlanType.NoPlan
            )
        {
            this.Name     = name;
            this.FSPath   = path;
            this.Template = template;
            this.Type     = type;

            tasks = new Dictionary <NnParam, NnTask>();

            KernelInitialize();

            Save();
        }
Esempio n. 9
0
        public static void LogParser <Log>(RPath logFilePath, CancellationToken ct, Action <string?> status)
            where Log : LogBase, new()
        {
            Log log = new Log();

            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path   = Path.GetDirectoryName(logFilePath);
            watcher.Filter = Path.GetFileName(logFilePath);

            StreamReader sr = new StreamReader(
                new FileStream(
                    logFilePath,
                    FileMode.OpenOrCreate,
                    FileAccess.Read,
                    FileShare.ReadWrite
                    )
                );

            while (!ct.IsCancellationRequested)
            {
                watcher.WaitForChanged(WatcherChangeTypes.All, 1000);
                while (!sr.EndOfStream)
                {
                    string?str = log.Push(sr.ReadLine());
                    if (str != null)
                    {
                        status(str);
                    }
                }
            }
            while (!sr.EndOfStream)
            {
                string?str = log.Push(sr.ReadLine());
                if (str != null)
                {
                    status(str);
                }
            }

            status(null);

            sr.Close();
        }
Esempio n. 10
0
        public static void RunNn(RPath path, string content, CancellationToken ct, NnType type)
        {
            try {
                File.WriteAllText(
                    path.SubPath(inputFileName),
                    content);

                File.WriteAllText(
                    path.SubPath(inputRefFileName),
                    content);

                string exePath = "";
                string dBPath  = "";
                switch (type)
                {
                case NnType.Nn3:
                    exePath = nn3Path;
                    dBPath  = nn3DBPath;
                    break;

                case NnType.NnPP:
                    exePath = nnPPPath;
                    dBPath  = nnPPDBPath;
                    break;
                }
                if (exePath == "")
                {
                    throw new Exception();
                }

                Util.StartAndWaitProcess(
                    nnMainPath + exePath,
                    " --license \"" + nnMainPath + "License\\license.txt\"" +
                    " --database \"" + nnMainPath + dBPath +
                    " --outputdirectory \"" + path + "\"" +
                    " --noautooutdir -log \"" + path.SubPath(inputFileName),
                    ct
                    );
            } catch {
                Util.ErrorHappend("Exception encountered in RunNn (NnAgent)!");
            }
        }
Esempio n. 11
0
        public static NnPlan?Load(RPath path)
        {
            try {
                var planData =
                    (SaveData)Util.DeserializeFromFile(
                        path.SubPath(NnAgent.planFileName)
                        );

                Dictionary <NnParam, NnTask> tasks = new Dictionary <NnParam, NnTask>();
                foreach (var taskId in planData.taskIds)
                {
                    NnTask?task = NnTask.Load(
                        path.SubPath("tasks").SubPath(taskId.Value)
                        );
                    if (task != null)
                    {
                        tasks[taskId.Key] = task;
                    }
                }

                NnTemplate?template = NnTemplate.Load(path);

                if (template == null)
                {
                    throw new Exception();
                }

                NnPlan plan = new NnPlan(
                    planData.name,
                    path,
                    template,
                    tasks,
                    // planData.consts?.ToImmutableDictionary(),
                    planData.Type
                    );

                return(plan);
            } catch {
                Util.ErrorHappend($"Error while loading plan!");
                return(null);
            }
        }
Esempio n. 12
0
        NnPlan(
            string name,
            RPath path,
            NnTemplate template,
            Dictionary <NnParam, NnTask> tasks,
            // ImmutableDictionary<string, string> ? consts,
            PlanType type
            )
        {
            this.Name     = name;
            this.FSPath   = path;
            this.Template = template;
            this.tasks    = tasks;
            // this.Consts = consts;
            this.Type = type;

            KernelInitialize();

            Save();
        }
Esempio n. 13
0
        public static (RPath?data, RPath?coord, RPath?fld, RPath?v) GetCoordAndDat(RPath path, string entry, bool ignoreExist = false)
        {
            RPath coord = path.SubPath(entry + ".coord");
            RPath data  = path.SubPath(entry + ".dat");
            RPath fld   = path.SubPath(entry + ".fld");
            RPath v     = path.SubPath(entry + ".v");

            if (!ignoreExist)
            {
                if (!(
                        File.Exists(coord) &&
                        File.Exists(data) &&
                        File.Exists(fld) &&
                        File.Exists(v)
                        ))
                {
                    return(null, null, null, null);
                }
            }

            return(data, coord, fld, v);
        }
Esempio n. 14
0
        public static NnTemplate?Load(RPath path)
        {
            try {
                var tempData =
                    (SaveData)Util.DeserializeFromFile(
                        path.SubPath(NnAgent.tempFileName)
                        );

                NnTemplate temp = new NnTemplate(
                    tempData.name,
                    tempData.type,
                    path,
                    tempData.elements,
                    tempData.variables.ToImmutableDictionary(),
                    tempData.derivedVariables.ToImmutableDictionary()
                    );

                return(temp);
            } catch {
                Util.ErrorHappend($"Error while loading template!");
                return(null);
            }
        }
Esempio n. 15
0
 public static void SaveLog(RPath path, string log)
 {
     // FIXME:
 }
Esempio n. 16
0
        public static NnTemplate?NewTemplate(
            string name,
            string content,
            RPath path
            )
        {
            List <Element> elements = new List <Element>();

            string[] lines =
                content.Splitter("([\r\n|\r|\n]+)");

            // Test parse (to identify NnType)
            NnType type = NnType.Nn3;

            foreach (string line in lines)
            {
                if (Regex.IsMatch(line, @"^[ |\t]*}.*"))
                {
                    type = NnType.NnPP; break;
                }
            }

            string tokenVariable = "\\$", tokenComment = "#";

            switch (type)
            {
            case NnType.Nn3:
                tokenVariable = "%";
                tokenComment  = "!";
                break;

            case NnType.NnPP:
                tokenVariable = "\\$";
                tokenComment  = "#";
                break;
            }

            Dictionary <string, string> defaultValues     = new Dictionary <string, string>();

            Dictionary <string, string>  derivedVariables = new Dictionary <string, string>();
            Dictionary <string, string?> variables        = new Dictionary <string, string?>();

            List <string> variableKeys                    = new List <string>();

            foreach (string oriline in lines)
            {
                string line = Regex.Replace(oriline, $"{tokenComment}.*$", "");
                if (Regex.IsMatch(
                        line,
                        $"^[ \\t]*{tokenVariable}[0-9A-Za-z_]+[ \\t]*=.*"))
                {
                    // Regex.Replace(line, @"[ \t]+", "");
                    string[] tokens = line.TrimSpaces().Splitter("=");

                    string var = tokens[0].Substring(1);
                    string val = tokens.Count() >= 2 ? tokens[1] : "";

                    if (defaultValues.ContainsKey(var))
                    {
                        Util.ErrorHappend($"Multiple definition of key \"{tokens[0]}\"!");
                        return(null);
                    }

                    defaultValues.Add(var, val);

                    if (!val.Contains(tokenVariable.Last()))
                    {
                        variables.Add(var, val);
                    }
                }
                else
                {
                    if (Regex.IsMatch(line, "directory"))
                    {
                        if (!Util.WarnAndDecide($"Output directory specification {line} in template file will be discarded.\nContinue parsing?"))
                        {
                            return(null);
                        }
                        else
                        {
                            continue;
                        }
                    }

                    string[] tokens =
                        line.Splitter($"({tokenVariable}[0-9A-Za-z_]+)");

                    foreach (string token in tokens)
                    {
                        string var;
                        if (token[0] == tokenComment.Last())
                        {
                            elements.Add(Element.NewContent(token));
                        }
                        else if (token[0] == tokenVariable.Last())
                        {
                            var = token.Substring(1);
                            // var = token;
                            variableKeys.Add(var);
                            elements.Add(Element.NewVariable(var));
                        }
                        else
                        {
                            elements.Add(Element.NewContent(token));
                        }
                    }
                }
            }

            // FIXME: clutter!
            // foreach (string key in variableKeys) {
            //     if (defaultValues.ContainsKey(key))
            //         if (defaultValues[key].Contains(tokenVariable.Last()))
            //             derivedVariables[key] = defaultValues[key];
            //         else
            //             variables[key] = defaultValues[key];
            //     else
            //         variables[key] = null;
            // }

            foreach (string key in defaultValues.Keys)
            {
                if (defaultValues[key].Contains(tokenVariable.Last()))
                {
                    derivedVariables[key] = defaultValues[key];
                }
                else
                {
                    variables[key] = defaultValues[key];
                }
            }

            foreach (string key in variableKeys)
            {
                if (!derivedVariables.ContainsKey(key) && !variables.ContainsKey(key))
                {
                    variables[key] = null;
                }
            }

            return(new NnTemplate(
                       name,
                       type,
                       path,
                       elements,
                       variables.ToImmutableDictionary(),
                       derivedVariables.ToImmutableDictionary()
                       ));
        }