ScriptCanvasReporter.inl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ScriptCanvas/SystemComponent.h>
  9. #include <ScriptCanvas/Execution/ExecutionState.h>
  10. namespace ScriptCanvasEditor
  11. {
  12. using namespace ScriptCanvas;
  13. AZ_INLINE Reporter::Reporter()
  14. {
  15. m_parseDuration = m_translationDuration = 0;
  16. ScriptCanvas::ExecutionNotificationsBus::Handler::BusConnect();
  17. }
  18. AZ_INLINE Reporter::Reporter(const AZ::EntityId& entityID) : Reporter()
  19. {
  20. m_parseDuration = m_translationDuration = 0;
  21. SetEntity(entityID);
  22. ScriptCanvas::ExecutionNotificationsBus::Handler::BusConnect();
  23. }
  24. AZ_INLINE Reporter::~Reporter() {}
  25. AZ_INLINE void Reporter::Checkpoint(const Report& report)
  26. {
  27. if (m_isReportFinished)
  28. {
  29. return;
  30. }
  31. m_checkpoints.push_back(report);
  32. }
  33. AZ_INLINE void Reporter::CollectPerformanceTiming()
  34. {
  35. ScriptCanvas::SystemComponent::ModPerformanceTracker()->CalculateReports();
  36. m_performanceReport = ScriptCanvas::SystemComponent::ModPerformanceTracker()->GetSnapshotReport();
  37. }
  38. AZ_INLINE bool Reporter::ExpectsParseError() const
  39. {
  40. return m_expectParseError;
  41. }
  42. AZ_INLINE bool Reporter::ExpectsRuntimeFailure() const
  43. {
  44. return m_expectsRuntimeError;
  45. }
  46. AZ_INLINE void Reporter::FinishReport()
  47. {
  48. if (m_isReportFinished)
  49. {
  50. AZ_Error("ScriptCanvas", false, "The report is already finished");
  51. }
  52. m_isReportFinished = true;
  53. Bus::Handler::BusDisconnect(m_graph);
  54. AZ::EntityBus::Handler::BusDisconnect(m_entityId);
  55. }
  56. AZ_INLINE const AZStd::vector<Report>& Reporter::GetCheckpoints() const
  57. {
  58. if (!m_isReportFinished)
  59. {
  60. AZ_Error("ScriptCanvas", false, "The report must be finished before evaluation");
  61. }
  62. return m_checkpoints;
  63. }
  64. AZ_INLINE ExecutionConfiguration Reporter::GetExecutionConfiguration() const
  65. {
  66. return m_configuration;
  67. }
  68. AZ_INLINE ExecutionMode Reporter::GetExecutionMode() const
  69. {
  70. return m_mode;
  71. }
  72. AZ_INLINE const AZStd::vector<Report>& Reporter::GetFailure() const
  73. {
  74. if (!m_isReportFinished)
  75. {
  76. AZ_Error("ScriptCanvas", false, "The report must be finished before evaluation");
  77. }
  78. return m_failures;
  79. }
  80. AZ_INLINE const AZ::Data::AssetId& Reporter::GetGraph() const
  81. {
  82. return m_graph;
  83. }
  84. AZ_INLINE AZStd::sys_time_t Reporter::GetParseDuration() const
  85. {
  86. return m_parseDuration;
  87. }
  88. AZ_INLINE const Execution::PerformanceTrackingReport& Reporter::GetPerformanceReport() const
  89. {
  90. return m_performanceReport;
  91. }
  92. AZ_INLINE const AZStd::vector<Report>& Reporter::GetSuccess() const
  93. {
  94. if (!m_isReportFinished)
  95. {
  96. AZ_Error("ScriptCanvas", false, "The report must be finished before evaluation");
  97. }
  98. return m_successes;
  99. }
  100. AZ_INLINE AZStd::sys_time_t Reporter::GetTranslateDuration() const
  101. {
  102. return m_translationDuration;
  103. }
  104. AZ_INLINE const AZ::IO::Path& Reporter::GetFilePath() const
  105. {
  106. return m_filePath;
  107. }
  108. AZ_INLINE bool Reporter::IsActivated() const
  109. {
  110. return m_graphIsActivated;
  111. }
  112. AZ_INLINE bool Reporter::IsCompiled() const
  113. {
  114. return m_graphIsCompiled;
  115. }
  116. AZ_INLINE bool Reporter::IsComplete() const
  117. {
  118. return m_graphIsComplete;
  119. }
  120. AZ_INLINE bool Reporter::IsDeactivated() const
  121. {
  122. if (!m_isReportFinished)
  123. {
  124. AZ_Error("ScriptCanvas", false, "The report must be finished before evaluation");
  125. }
  126. return m_graphIsDeactivated;
  127. }
  128. AZ_INLINE bool Reporter::IsErrorFree() const
  129. {
  130. if (!m_isReportFinished)
  131. {
  132. AZ_Error("ScriptCanvas", false, "The report must be finished before evaluation");
  133. }
  134. return m_failures.empty();
  135. }
  136. AZ_INLINE bool Reporter::IsGraphLoaded() const
  137. {
  138. return m_isGraphLoaded;
  139. }
  140. AZ_INLINE bool Reporter::IsGraphObserved([[maybe_unused]] const AZ::EntityId& entityId, const GraphIdentifier& identifier)
  141. {
  142. return m_configuration == ExecutionConfiguration::Traced && identifier.m_assetId == m_graph;
  143. }
  144. AZ_INLINE void Reporter::RuntimeError([[maybe_unused]] const ExecutionState& executionState, const AZStd::string_view& description)
  145. {
  146. m_failures.push_back(AZStd::string::format("ScriptCanvas runtime error: %s", description.data()));
  147. }
  148. AZ_INLINE bool Reporter::IsParseAttemptMade() const
  149. {
  150. return m_isParseAttemptMade;
  151. }
  152. AZ_INLINE bool Reporter::IsProcessOnly() const
  153. {
  154. return m_processOnly;
  155. }
  156. AZ_INLINE bool Reporter::IsReportFinished() const
  157. {
  158. return m_isReportFinished;
  159. }
  160. AZ_INLINE void Reporter::MarkCompiled()
  161. {
  162. m_graphIsCompiled = true;
  163. }
  164. AZ_INLINE void Reporter::MarkExpectParseError()
  165. {
  166. m_expectParseError = true;
  167. }
  168. AZ_INLINE void Reporter::MarkExpectRuntimeFailure()
  169. {
  170. m_expectsRuntimeError = true;
  171. }
  172. AZ_INLINE void Reporter::MarkGraphLoaded()
  173. {
  174. m_isGraphLoaded = true;
  175. }
  176. AZ_INLINE void Reporter::MarkParseAttemptMade()
  177. {
  178. m_isParseAttemptMade = true;
  179. }
  180. AZ_INLINE bool Reporter::operator==(const Reporter& other) const
  181. {
  182. return m_graphIsActivated == other.m_graphIsActivated
  183. && m_graphIsDeactivated == other.m_graphIsDeactivated
  184. && m_graphIsComplete == other.m_graphIsComplete
  185. && m_isReportFinished == other.m_isReportFinished
  186. && m_failures == other.m_failures
  187. && m_successes == other.m_successes;
  188. }
  189. AZ_INLINE void Reporter::OnEntityActivated(const AZ::EntityId& entity)
  190. {
  191. if (m_entityId != entity)
  192. {
  193. AZ_Error("ScriptCanvas", false, "This reporter is listening to the wrong entity");
  194. }
  195. if (m_isReportFinished)
  196. {
  197. return;
  198. }
  199. m_graphIsActivated = true;
  200. }
  201. AZ_INLINE void Reporter::OnEntityDeactivated(const AZ::EntityId& entity)
  202. {
  203. if (m_entityId != entity)
  204. {
  205. AZ_Error("ScriptCanvas", false, "This reporter is listening to the wrong entity");
  206. }
  207. if (m_isReportFinished)
  208. {
  209. return;
  210. }
  211. m_graphIsDeactivated = true;
  212. }
  213. AZ_INLINE void Reporter::SetDurations(AZStd::sys_time_t parse, AZStd::sys_time_t translate)
  214. {
  215. m_parseDuration = parse;
  216. m_translationDuration = translate;
  217. }
  218. AZ_INLINE void Reporter::SetExecutionConfiguration(ExecutionConfiguration configuration)
  219. {
  220. m_configuration = configuration;
  221. }
  222. AZ_INLINE void Reporter::SetExecutionMode(ExecutionMode mode)
  223. {
  224. m_mode = mode;
  225. }
  226. AZ_INLINE void Reporter::SetEntity(const AZ::EntityId& entityID)
  227. {
  228. m_entityId = entityID;
  229. AZ::EntityBus::Handler::BusConnect(m_entityId);
  230. }
  231. AZ_INLINE void Reporter::SetGraph(const AZ::Data::AssetId& graph)
  232. {
  233. m_graph = graph;
  234. Bus::Handler::BusConnect(graph);
  235. }
  236. AZ_INLINE void Reporter::SetProcessOnly(bool processOnly)
  237. {
  238. m_processOnly = processOnly;
  239. }
  240. AZ_INLINE void Reporter::SetFilePath(const AZ::IO::PathView& filePath)
  241. {
  242. m_filePath = filePath;
  243. }
  244. // Handler
  245. AZ_INLINE void Reporter::MarkComplete(const Report& report)
  246. {
  247. if (m_isReportFinished)
  248. {
  249. return;
  250. }
  251. if (!report.empty())
  252. {
  253. Checkpoint(AZStd::string::format("MarkComplete: %s", report.data()));
  254. }
  255. if (m_graphIsComplete)
  256. {
  257. AddFailure("MarkComplete was called twice!");
  258. }
  259. else
  260. {
  261. m_graphIsComplete = true;
  262. }
  263. }
  264. AZ_INLINE void Reporter::AddFailure(const Report& report)
  265. {
  266. if (m_isReportFinished)
  267. {
  268. return;
  269. }
  270. m_failures.push_back(report);
  271. Checkpoint(AZStd::string::format("AddFailure: %s", report.data()));
  272. }
  273. AZ_INLINE void Reporter::AddSuccess(const Report& report)
  274. {
  275. if (m_isReportFinished)
  276. {
  277. return;
  278. }
  279. m_successes.push_back(report);
  280. if (!report.empty())
  281. {
  282. Checkpoint(AZStd::string::format("AddSuccess: %s", report.data()));
  283. }
  284. }
  285. AZ_INLINE void Reporter::ExpectFalse(const bool value, const Report& report)
  286. {
  287. if (value)
  288. {
  289. AddFailure(AZStd::string::format("Error | Expected false.: %s", report.data()));
  290. }
  291. if (!report.empty())
  292. {
  293. Checkpoint(AZStd::string::format("ExpectFalse: %s", report.data()));
  294. }
  295. }
  296. AZ_INLINE void Reporter::ExpectTrue(const bool value, const Report& report)
  297. {
  298. if (!value)
  299. {
  300. AddFailure(AZStd::string::format("Error | Expected true.: %s", report.data()));
  301. }
  302. if (!report.empty())
  303. {
  304. Checkpoint(AZStd::string::format("ExpectTrue: %s", report.data()));
  305. }
  306. }
  307. AZ_INLINE void Reporter::ExpectEqual(const Data::NumberType lhs, const Data::NumberType rhs, const Report& report)
  308. {
  309. if (!AZ::IsClose(lhs, rhs, 0.001))
  310. {
  311. AddFailure(AZStd::string::format("Error | Expected(candidate: %s) == (reference: %s): %s", Datum(lhs).ToString().data(), Datum(rhs).ToString().data(), report.data()));
  312. }
  313. if (!report.empty())
  314. {
  315. Checkpoint(AZStd::string::format("ExpectEqual: %s", report.data()));
  316. }
  317. }
  318. AZ_INLINE void Reporter::ExpectNotEqual(const Data::NumberType lhs, const Data::NumberType rhs, const Report& report)
  319. {
  320. if (AZ::IsClose(lhs, rhs, 0.001))
  321. {
  322. AddFailure(AZStd::string::format("Error | Expected(candidate: %s) != (reference: %s): %s", Datum(lhs).ToString().data(), Datum(rhs).ToString().data(), report.data()));
  323. }
  324. if (!report.empty())
  325. {
  326. Checkpoint(AZStd::string::format("ExpectNotEqual: %s", report.data()));
  327. }
  328. }
  329. SCRIPT_CANVAS_UNIT_TEST_EQUALITY_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_EQ)
  330. SCRIPT_CANVAS_UNIT_TEST_EQUALITY_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectNotEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_NE)
  331. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectGreaterThan, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_GT)
  332. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectGreaterThanEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_GE)
  333. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectLessThan, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_LT)
  334. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectLessThanEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_LE)
  335. // Vector Implementations
  336. SCRIPT_CANVAS_UNIT_TEST_VECTOR_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectGreaterThan, SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_GT)
  337. SCRIPT_CANVAS_UNIT_TEST_VECTOR_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectGreaterThanEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_GE)
  338. SCRIPT_CANVAS_UNIT_TEST_VECTOR_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectLessThan, SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_LT)
  339. SCRIPT_CANVAS_UNIT_TEST_VECTOR_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectLessThanEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_LE)
  340. }