ScriptCanvasReporter.inl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 bool Reporter::IsActivated() const
  105. {
  106. return m_graphIsActivated;
  107. }
  108. AZ_INLINE bool Reporter::IsCompiled() const
  109. {
  110. return m_graphIsCompiled;
  111. }
  112. AZ_INLINE bool Reporter::IsComplete() const
  113. {
  114. return m_graphIsComplete;
  115. }
  116. AZ_INLINE bool Reporter::IsDeactivated() const
  117. {
  118. if (!m_isReportFinished)
  119. {
  120. AZ_Error("ScriptCanvas", false, "The report must be finished before evaluation");
  121. }
  122. return m_graphIsDeactivated;
  123. }
  124. AZ_INLINE bool Reporter::IsErrorFree() const
  125. {
  126. if (!m_isReportFinished)
  127. {
  128. AZ_Error("ScriptCanvas", false, "The report must be finished before evaluation");
  129. }
  130. return m_failures.empty();
  131. }
  132. AZ_INLINE bool Reporter::IsGraphLoaded() const
  133. {
  134. return m_isGraphLoaded;
  135. }
  136. AZ_INLINE bool Reporter::IsGraphObserved(const ExecutionState& executionState)
  137. {
  138. return m_configuration == ExecutionConfiguration::Traced && executionState.GetAssetId() == m_graph;
  139. }
  140. AZ_INLINE void Reporter::RuntimeError([[maybe_unused]] const ExecutionState& executionState, const AZStd::string_view& description)
  141. {
  142. m_failures.push_back(AZStd::string::format("ScriptCanvas runtime error: %s", description.data()));
  143. }
  144. AZ_INLINE bool Reporter::IsParseAttemptMade() const
  145. {
  146. return m_isParseAttemptMade;
  147. }
  148. AZ_INLINE bool Reporter::IsProcessOnly() const
  149. {
  150. return m_processOnly;
  151. }
  152. AZ_INLINE bool Reporter::IsReportFinished() const
  153. {
  154. return m_isReportFinished;
  155. }
  156. AZ_INLINE void Reporter::MarkCompiled()
  157. {
  158. m_graphIsCompiled = true;
  159. }
  160. AZ_INLINE void Reporter::MarkExpectParseError()
  161. {
  162. m_expectParseError = true;
  163. }
  164. AZ_INLINE void Reporter::MarkExpectRuntimeFailure()
  165. {
  166. m_expectsRuntimeError = true;
  167. }
  168. AZ_INLINE void Reporter::MarkGraphLoaded()
  169. {
  170. m_isGraphLoaded = true;
  171. }
  172. AZ_INLINE void Reporter::MarkParseAttemptMade()
  173. {
  174. m_isParseAttemptMade = true;
  175. }
  176. AZ_INLINE bool Reporter::operator==(const Reporter& other) const
  177. {
  178. return m_graphIsActivated == other.m_graphIsActivated
  179. && m_graphIsDeactivated == other.m_graphIsDeactivated
  180. && m_graphIsComplete == other.m_graphIsComplete
  181. && m_isReportFinished == other.m_isReportFinished
  182. && m_failures == other.m_failures
  183. && m_successes == other.m_successes;
  184. }
  185. AZ_INLINE void Reporter::OnEntityActivated(const AZ::EntityId& entity)
  186. {
  187. if (m_entityId != entity)
  188. {
  189. AZ_Error("ScriptCanvas", false, "This reporter is listening to the wrong entity");
  190. }
  191. if (m_isReportFinished)
  192. {
  193. return;
  194. }
  195. m_graphIsActivated = true;
  196. }
  197. AZ_INLINE void Reporter::OnEntityDeactivated(const AZ::EntityId& entity)
  198. {
  199. if (m_entityId != entity)
  200. {
  201. AZ_Error("ScriptCanvas", false, "This reporter is listening to the wrong entity");
  202. }
  203. if (m_isReportFinished)
  204. {
  205. return;
  206. }
  207. m_graphIsDeactivated = true;
  208. }
  209. AZ_INLINE void Reporter::SetDurations(AZStd::sys_time_t parse, AZStd::sys_time_t translate)
  210. {
  211. m_parseDuration = parse;
  212. m_translationDuration = translate;
  213. }
  214. AZ_INLINE void Reporter::SetExecutionConfiguration(ExecutionConfiguration configuration)
  215. {
  216. m_configuration = configuration;
  217. }
  218. AZ_INLINE void Reporter::SetExecutionMode(ExecutionMode mode)
  219. {
  220. m_mode = mode;
  221. }
  222. AZ_INLINE void Reporter::SetEntity(const AZ::EntityId& entityID)
  223. {
  224. m_entityId = entityID;
  225. AZ::EntityBus::Handler::BusConnect(m_entityId);
  226. }
  227. AZ_INLINE void Reporter::SetGraph(const AZ::Data::AssetId& graph)
  228. {
  229. m_graph = graph;
  230. Bus::Handler::BusConnect(graph);
  231. }
  232. AZ_INLINE void Reporter::SetProcessOnly(bool processOnly)
  233. {
  234. m_processOnly = processOnly;
  235. }
  236. // Handler
  237. AZ_INLINE void Reporter::MarkComplete(const Report& report)
  238. {
  239. if (m_isReportFinished)
  240. {
  241. return;
  242. }
  243. if (!report.empty())
  244. {
  245. Checkpoint(AZStd::string::format("MarkComplete: %s", report.data()));
  246. }
  247. if (m_graphIsComplete)
  248. {
  249. AddFailure("MarkComplete was called twice!");
  250. }
  251. else
  252. {
  253. m_graphIsComplete = true;
  254. }
  255. }
  256. AZ_INLINE void Reporter::AddFailure(const Report& report)
  257. {
  258. if (m_isReportFinished)
  259. {
  260. return;
  261. }
  262. m_failures.push_back(report);
  263. Checkpoint(AZStd::string::format("AddFailure: %s", report.data()));
  264. }
  265. AZ_INLINE void Reporter::AddSuccess(const Report& report)
  266. {
  267. if (m_isReportFinished)
  268. {
  269. return;
  270. }
  271. m_successes.push_back(report);
  272. if (!report.empty())
  273. {
  274. Checkpoint(AZStd::string::format("AddSuccess: %s", report.data()));
  275. }
  276. }
  277. AZ_INLINE void Reporter::ExpectFalse(const bool value, const Report& report)
  278. {
  279. if (value)
  280. {
  281. AddFailure(AZStd::string::format("Error | Expected false.: %s", report.data()));
  282. }
  283. if (!report.empty())
  284. {
  285. Checkpoint(AZStd::string::format("ExpectFalse: %s", report.data()));
  286. }
  287. }
  288. AZ_INLINE void Reporter::ExpectTrue(const bool value, const Report& report)
  289. {
  290. if (!value)
  291. {
  292. AddFailure(AZStd::string::format("Error | Expected true.: %s", report.data()));
  293. }
  294. if (!report.empty())
  295. {
  296. Checkpoint(AZStd::string::format("ExpectTrue: %s", report.data()));
  297. }
  298. }
  299. AZ_INLINE void Reporter::ExpectEqual(const Data::NumberType lhs, const Data::NumberType rhs, const Report& report)
  300. {
  301. if (!AZ::IsClose(lhs, rhs, 0.001))
  302. {
  303. AddFailure(AZStd::string::format("Error | Expected(candidate: %s) == (reference: %s): %s", Datum(lhs).ToString().data(), Datum(rhs).ToString().data(), report.data()));
  304. }
  305. if (!report.empty())
  306. {
  307. Checkpoint(AZStd::string::format("ExpectEqual: %s", report.data()));
  308. }
  309. }
  310. AZ_INLINE void Reporter::ExpectNotEqual(const Data::NumberType lhs, const Data::NumberType rhs, const Report& report)
  311. {
  312. if (AZ::IsClose(lhs, rhs, 0.001))
  313. {
  314. AddFailure(AZStd::string::format("Error | Expected(candidate: %s) != (reference: %s): %s", Datum(lhs).ToString().data(), Datum(rhs).ToString().data(), report.data()));
  315. }
  316. if (!report.empty())
  317. {
  318. Checkpoint(AZStd::string::format("ExpectNotEqual: %s", report.data()));
  319. }
  320. }
  321. SCRIPT_CANVAS_UNIT_TEST_EQUALITY_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_EQ)
  322. SCRIPT_CANVAS_UNIT_TEST_EQUALITY_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectNotEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_NE)
  323. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectGreaterThan, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_GT)
  324. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectGreaterThanEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_GE)
  325. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectLessThan, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_LT)
  326. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectLessThanEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_LE)
  327. // Vector Implementations
  328. SCRIPT_CANVAS_UNIT_TEST_VECTOR_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectGreaterThan, SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_GT)
  329. SCRIPT_CANVAS_UNIT_TEST_VECTOR_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectGreaterThanEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_GE)
  330. SCRIPT_CANVAS_UNIT_TEST_VECTOR_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectLessThan, SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_LT)
  331. SCRIPT_CANVAS_UNIT_TEST_VECTOR_COMPARE_OVERLOAD_IMPLEMENTATIONS(Reporter, ExpectLessThanEqual, SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_LE)
  332. }