[go: up one dir, main page]

File: main.cpp

package info (click to toggle)
dyssol 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 18,188 kB
  • sloc: cpp: 53,643; sh: 85; python: 55; makefile: 12
file content (152 lines) | stat: -rw-r--r-- 4,467 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Copyright (c) 2020, Dyssol Development Team.
 * Copyright (c) 2024, DyssolTEC GmbH.
 * All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "ArgumentsParser.h"
#include "ScriptParser.h"
#include "ScriptRunner.h"
#include "ThreadPool.h"
#include "DyssolSystemDefines.h"
#include <iomanip>

// Prints information about command line arguments.
void PrintArgumentsInfo(const CArgumentsParser& _parser)
{
	std::cout << "Usage: DyssolC --key[=value] [--key[=value]] [...]" << std::endl;
	for (const auto& k : _parser.AllAllowedKeys())
	{
		for (const auto& s : k.keysS)
			std::cout << "-" << s << " ";
		std::cout << '\t';
		for (const auto& l : k.keysL)
			std::cout << "--" << l << " ";
		std::cout << k.description << std::endl;
	}
}

// Prints information about the current version.
void PrintVersionInfo()
{
	std::cout << "Version: " << CURRENT_VERSION_STR << std::endl;
}

// Prints information about available models.
void PrintModelsInfo(const std::vector<std::filesystem::path>& _modelPaths)
{
	// Helper function to print a formatted entry justified left and filled with spaces until the given length.
	constexpr auto PrintUnitEntry = [](const auto& _entry, std::streamsize _width)
	{
		std::cout << std::left << std::setw(_width) << std::setfill(' ') << _entry;
	};

	// create models manager
	CModelsManager manager;
	manager.AddDir(L".");				// add current directory as a path to units/solvers
	for (const auto& dir : _modelPaths)	// add other directories as a path to units/solvers
		manager.AddDir(dir);

	// get available models
	const auto units   = manager.GetAvailableUnits();
	const auto solvers = manager.GetAvailableSolvers();

	// print info
	constexpr size_t nameLen = 50;
	constexpr size_t typeLen = 30;
	constexpr size_t authLen = 50;
	PrintUnitEntry("Name"  , nameLen);
	PrintUnitEntry("Type"  , typeLen);
	PrintUnitEntry("Author", authLen);
	std::cout << std::endl;
	for (const auto& m : units)
	{
		PrintUnitEntry(m.name, nameLen);
		PrintUnitEntry(m.isDynamic ? StrConst::MMT_Dynamic : StrConst::MMT_SteadyState, typeLen);
		PrintUnitEntry(m.author, authLen);
		std::cout << std::endl;
	}
	for (const auto& m : solvers)
	{
		PrintUnitEntry(m.name, nameLen);
		PrintUnitEntry(std::vector<std::string>{ SOLVERS_TYPE_NAMES }[static_cast<unsigned>(m.solverType)] + " " + StrConst::MMT_Solver, typeLen);
		PrintUnitEntry(m.author, authLen);
		std::cout << std::endl;
	}
}

bool RunDyssol(const std::filesystem::path& _script)
{
	InitializeThreadPool();

	std::cout << "Parsing script file: \n\t" << _script.string() << std::endl;

	const CScriptParser parser{ _script };
	std::cout << "Jobs found: \n\t" << parser.JobsCount() << std::endl;

	CScriptRunner runner;
	size_t counter = 0;
	bool success = true;
	for (const auto& job : parser.Jobs())
	{
		std::cout << " ===== Starting job: " << counter++ + 1 << " ===== " << std::endl;
		success &= runner.RunJob(*job);
	}

	return success;
}

void HandleException(const std::exception_ptr& _exceptionPtr)
{
	try
	{
		if (_exceptionPtr)
			std::rethrow_exception(_exceptionPtr);
	}
	catch (const std::exception& e)
	{
		std::cout << "Unknown unhandled exception caught: '" << e.what() << "'\n";
	}
}

int main(int argc, const char* argv[])
{
	std::exception_ptr exceptionPtr;
	try
	{
		// possible keys with aliases and descriptions
		const std::vector<CArgumentsParser::SKey> keys{
			{ { "script"      }, { "s"  }, { "path to script file"                          } },
			{ { "version"     }, { "v"  }, { "print information about current version"      } },
			{ { "models"      }, { "m"  }, { "print information about available models"     } },
			{ { "models_path" }, { "mp" }, { "additional path to look for available models" } },
			{ { "help"        }, { "h"  }, { "give this help list"                          } },
		};

		const CArgumentsParser parser(argc, argv, keys);

		if (parser.TokensCount() == 0 || parser.HasKey("h"))
		{
			PrintArgumentsInfo(parser);
			return 0;
		}

		if (parser.HasKey("v"))
			PrintVersionInfo();
		if (parser.HasKey("m"))
		{
			std::vector<std::filesystem::path> fsPaths;
			for (const auto& p : parser.GetValues("mp"))
				fsPaths.emplace_back(p);
			PrintModelsInfo(fsPaths);
		}
		if (parser.HasKey("s"))
			if (!RunDyssol(parser.GetValue("s")))
				return 1;
	}
	catch (...)
	{
		exceptionPtr = std::current_exception(); // capture current exception
	}
	HandleException(exceptionPtr);

	return 0;
}