File indexing completed on 2025-01-18 10:17:43
0001
0002
0003
0004
0005
0006 #include <catch.hpp>
0007
0008 #include <JANA/JObject.h>
0009 #include <JANA/JEvent.h>
0010 #include <JANA/JEventProcessor.h>
0011 #include <JANA/Topology/JEventSourceArrow.h>
0012 #include <JANA/Topology/JEventMapArrow.h>
0013 #include <JANA/Topology/JSubeventArrow.h>
0014 #include <JANA/Topology/JTopologyBuilder.h>
0015
0016
0017 struct MyInput : public JObject {
0018 int x;
0019 float y;
0020 MyInput(int x, float y) : x(x), y(y) {}
0021 };
0022
0023 struct MyOutput : public JObject {
0024 float z;
0025 explicit MyOutput(float z) : z(z) {}
0026 };
0027
0028 struct MyProcessor : public JSubeventProcessor<MyInput, MyOutput> {
0029 MyProcessor() {
0030 inputTag = "";
0031 outputTag = "subeventted";
0032 }
0033 MyOutput* ProcessSubevent(MyInput* input) override {
0034 return new MyOutput(input->y + (float) input->x);
0035 }
0036 };
0037
0038 TEST_CASE("Create subevent processor") {
0039
0040 MyProcessor processor;
0041 MyInput input(22, 7.6);
0042 MyOutput* output = processor.ProcessSubevent(&input);
0043 REQUIRE(output->z == 29.6f);
0044 }
0045
0046
0047 TEST_CASE("Basic subevent arrow functionality") {
0048
0049 MyProcessor processor;
0050 JMailbox<JEvent*> events_in;
0051 JMailbox<JEvent*> events_out;
0052 JMailbox<SubeventWrapper<MyInput>> subevents_in;
0053 JMailbox<SubeventWrapper<MyOutput>> subevents_out;
0054
0055 auto split_arrow = new JSplitArrow<MyInput, MyOutput>("split", &processor, &events_in, &subevents_in);
0056 auto subprocess_arrow = new JSubeventArrow<MyInput, MyOutput>("subprocess", &processor, &subevents_in, &subevents_out);
0057 auto merge_arrow = new JMergeArrow<MyInput, MyOutput>("merge", &processor, &subevents_out, &events_out);
0058
0059 SECTION("No-op execute subevent arrows") {
0060 JArrowMetrics m;
0061 split_arrow->execute(m, 0);
0062 merge_arrow->execute(m, 0);
0063 subprocess_arrow->execute(m, 0);
0064 }
0065
0066 struct SimpleSource : public JEventSource {
0067 SimpleSource() {
0068 SetCallbackStyle(CallbackStyle::ExpertMode);
0069 }
0070 Result Emit(JEvent& event) override {
0071 if (GetEmittedEventCount() == 10) return Result::FailureFinished;
0072 std::vector<MyInput*> inputs;
0073 inputs.push_back(new MyInput(22,3.6));
0074 inputs.push_back(new MyInput(23,3.5));
0075 inputs.push_back(new MyInput(24,3.4));
0076 inputs.push_back(new MyInput(25,3.3));
0077 event.Insert(inputs);
0078 return Result::Success;
0079 }
0080 };
0081
0082 struct SimpleProcessor : public JEventProcessor {
0083 SimpleProcessor() {
0084 SetCallbackStyle(CallbackStyle::ExpertMode);
0085 }
0086 void Process(const JEvent& event) {
0087 auto outputs = event.Get<MyOutput>();
0088 REQUIRE(outputs.size() == 4);
0089 REQUIRE(outputs[0]->z == 25.6f);
0090 REQUIRE(outputs[1]->z == 26.5f);
0091 REQUIRE(outputs[2]->z == 27.4f);
0092 REQUIRE(outputs[3]->z == 28.3f);
0093 }
0094 };
0095
0096 SECTION("Execute subevent arrows end-to-end") {
0097
0098 JApplication app;
0099 app.SetTimeoutEnabled(false);
0100 app.SetTicker(false);
0101
0102 auto topology = app.GetService<JTopologyBuilder>();
0103 topology->set_configure_fn([&](JTopologyBuilder& topology) {
0104
0105 auto source_arrow = new JEventSourceArrow("simpleSource", {new SimpleSource});
0106 source_arrow->attach(topology.event_pool, JEventSourceArrow::EVENT_IN);
0107 source_arrow->attach(&events_in, JEventSourceArrow::EVENT_OUT);
0108
0109 auto proc_arrow = new JEventMapArrow("simpleProcessor");
0110 proc_arrow->attach(&events_out, 0);
0111 proc_arrow->attach(topology.event_pool, 1);
0112 proc_arrow->add_processor(new SimpleProcessor);
0113
0114 topology.arrows.push_back(source_arrow);
0115 topology.arrows.push_back(split_arrow);
0116 topology.arrows.push_back(subprocess_arrow);
0117 topology.arrows.push_back(merge_arrow);
0118 topology.arrows.push_back(proc_arrow);
0119
0120 source_arrow->attach(split_arrow);
0121 split_arrow->attach(subprocess_arrow);
0122 subprocess_arrow->attach(merge_arrow);
0123 merge_arrow->attach(proc_arrow);
0124 });
0125
0126 app.Run(true);
0127 }
0128
0129
0130 }
0131
0132