PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
Chain Class Reference

#include <Chain.h>

Classes

struct  ValidationResult
 

Public Member Functions

 Chain ()
 
 ~Chain ()
 
bool Save ()
 
const std::string & GetName () const
 
const std::string & GetDescription () const
 
const std::string & GetFilePath () const
 
const std::vector< ChainItem > & GetItems () const
 
int GetItemCount () const
 
int GetParticipantCounter () const
 
bool GetUploadEnabled () const
 
bool GetLSLEnabled () const
 
const std::string & GetLSLStreamName () const
 
void SetName (const std::string &name)
 
void SetDescription (const std::string &desc)
 
void SetParticipantCounter (int counter)
 
void SetUploadEnabled (bool enabled)
 
void SetLSLEnabled (bool enabled)
 
void SetLSLStreamName (const std::string &name)
 
void IncrementParticipantCounter ()
 
void AddItem (const ChainItem &item)
 
void InsertItem (int index, const ChainItem &item)
 
bool RemoveItem (int index)
 
bool MoveItem (int fromIndex, int toIndex)
 
ChainItemGetItem (int index)
 
const ChainItemGetItem (int index) const
 
ValidationResult Validate (const class Study *study=nullptr) const
 

Static Public Member Functions

static std::shared_ptr< ChainLoadFromFile (const std::string &path)
 
static std::shared_ptr< ChainCreateNew (const std::string &path, const std::string &name, const std::string &description="")
 

Detailed Description

Definition at line 60 of file Chain.h.

Constructor & Destructor Documentation

◆ Chain()

Chain::Chain ( )

Definition at line 118 of file Chain.cpp.

119 : mParticipantCounter(1001), mUploadEnabled(false),
120 mLSLEnabled(false), mLSLStreamName("PEBL_{test}")
121{
122}

◆ ~Chain()

Chain::~Chain ( )

Definition at line 124 of file Chain.cpp.

124 {
125}

Member Function Documentation

◆ AddItem()

void Chain::AddItem ( const ChainItem item)

Definition at line 153 of file Chain.cpp.

153 {
154 mItems.push_back(item);
155}

◆ CreateNew()

std::shared_ptr< Chain > Chain::CreateNew ( const std::string &  path,
const std::string &  name,
const std::string &  description = "" 
)
static

Definition at line 142 of file Chain.cpp.

144 {
145 auto chain = std::make_shared<Chain>();
146 chain->mFilePath = path;
147 chain->mName = name;
148 chain->mDescription = description;
149
150 return chain;
151}

Referenced by Study::CreateNew(), and ScaleManager::CreateStudyFromScale().

◆ GetDescription()

const std::string & Chain::GetDescription ( ) const
inline

Definition at line 78 of file Chain.h.

78{ return mDescription; }

◆ GetFilePath()

const std::string & Chain::GetFilePath ( ) const
inline

Definition at line 79 of file Chain.h.

79{ return mFilePath; }

◆ GetItem() [1/2]

ChainItem * Chain::GetItem ( int  index)

Definition at line 204 of file Chain.cpp.

204 {
205 if (index < 0 || index >= static_cast<int>(mItems.size())) {
206 return nullptr;
207 }
208
209 return &mItems[index];
210}

◆ GetItem() [2/2]

const ChainItem * Chain::GetItem ( int  index) const

Definition at line 212 of file Chain.cpp.

212 {
213 if (index < 0 || index >= static_cast<int>(mItems.size())) {
214 return nullptr;
215 }
216
217 return &mItems[index];
218}

◆ GetItemCount()

int Chain::GetItemCount ( ) const
inline

Definition at line 81 of file Chain.h.

81{ return static_cast<int>(mItems.size()); }

◆ GetItems()

const std::vector< ChainItem > & Chain::GetItems ( ) const
inline

Definition at line 80 of file Chain.h.

80{ return mItems; }

◆ GetLSLEnabled()

bool Chain::GetLSLEnabled ( ) const
inline

Definition at line 84 of file Chain.h.

84{ return mLSLEnabled; }

◆ GetLSLStreamName()

const std::string & Chain::GetLSLStreamName ( ) const
inline

Definition at line 85 of file Chain.h.

85{ return mLSLStreamName; }

◆ GetName()

const std::string & Chain::GetName ( ) const
inline

Definition at line 77 of file Chain.h.

77{ return mName; }

◆ GetParticipantCounter()

int Chain::GetParticipantCounter ( ) const
inline

Definition at line 82 of file Chain.h.

82{ return mParticipantCounter; }

◆ GetUploadEnabled()

bool Chain::GetUploadEnabled ( ) const
inline

Definition at line 83 of file Chain.h.

83{ return mUploadEnabled; }

◆ IncrementParticipantCounter()

void Chain::IncrementParticipantCounter ( )

Definition at line 412 of file Chain.cpp.

412 {
413 mParticipantCounter++;
414 Save();
415}
bool Save()
Definition Chain.cpp:138

References Save().

◆ InsertItem()

void Chain::InsertItem ( int  index,
const ChainItem item 
)

Definition at line 157 of file Chain.cpp.

157 {
158 if (index < 0 || index > static_cast<int>(mItems.size())) {
159 return; // Invalid index
160 }
161
162 mItems.insert(mItems.begin() + index, item);
163}

◆ LoadFromFile()

std::shared_ptr< Chain > Chain::LoadFromFile ( const std::string &  path)
static

Definition at line 127 of file Chain.cpp.

127 {
128 auto chain = std::make_shared<Chain>();
129 chain->mFilePath = path;
130
131 if (!chain->LoadFromJSON(path)) {
132 return nullptr;
133 }
134
135 return chain;
136}

◆ MoveItem()

bool Chain::MoveItem ( int  fromIndex,
int  toIndex 
)

Definition at line 174 of file Chain.cpp.

174 {
175 if (fromIndex < 0 || fromIndex >= static_cast<int>(mItems.size())) {
176 return false; // Invalid source index
177 }
178
179 if (toIndex < 0 || toIndex >= static_cast<int>(mItems.size())) {
180 return false; // Invalid destination index
181 }
182
183 if (fromIndex == toIndex) {
184 return true; // No-op
185 }
186
187 // Extract item
188 ChainItem item = mItems[fromIndex];
189
190 // Remove from old position
191 mItems.erase(mItems.begin() + fromIndex);
192
193 // Adjust toIndex if needed (removing item may shift indices)
194 if (toIndex > fromIndex) {
195 toIndex--;
196 }
197
198 // Insert at new position
199 mItems.insert(mItems.begin() + toIndex, item);
200
201 return true;
202}

◆ RemoveItem()

bool Chain::RemoveItem ( int  index)

Definition at line 165 of file Chain.cpp.

165 {
166 if (index < 0 || index >= static_cast<int>(mItems.size())) {
167 return false; // Invalid index
168 }
169
170 mItems.erase(mItems.begin() + index);
171 return true;
172}

◆ Save()

bool Chain::Save ( )

Definition at line 138 of file Chain.cpp.

138 {
139 return SaveToJSON(mFilePath);
140}

Referenced by IncrementParticipantCounter().

◆ SetDescription()

void Chain::SetDescription ( const std::string &  desc)
inline

Definition at line 89 of file Chain.h.

89{ mDescription = desc; }

◆ SetLSLEnabled()

void Chain::SetLSLEnabled ( bool  enabled)
inline

Definition at line 92 of file Chain.h.

92{ mLSLEnabled = enabled; }

◆ SetLSLStreamName()

void Chain::SetLSLStreamName ( const std::string &  name)
inline

Definition at line 93 of file Chain.h.

93{ mLSLStreamName = name; }

◆ SetName()

void Chain::SetName ( const std::string &  name)
inline

Definition at line 88 of file Chain.h.

88{ mName = name; }

◆ SetParticipantCounter()

void Chain::SetParticipantCounter ( int  counter)
inline

Definition at line 90 of file Chain.h.

90{ mParticipantCounter = counter; }

◆ SetUploadEnabled()

void Chain::SetUploadEnabled ( bool  enabled)
inline

Definition at line 91 of file Chain.h.

91{ mUploadEnabled = enabled; }

◆ Validate()

Chain::ValidationResult Chain::Validate ( const class Study study = nullptr) const

Definition at line 220 of file Chain.cpp.

220 {
221 ValidationResult result;
222
223 // Check required fields
224 if (mName.empty()) {
225 result.errors.push_back("Chain name is required");
226 }
227
228 if (mFilePath.empty()) {
229 result.warnings.push_back("Chain file path not set");
230 }
231
232 // Check items
233 if (mItems.empty()) {
234 result.warnings.push_back("Chain has no items");
235 }
236
237 for (size_t i = 0; i < mItems.size(); i++) {
238 const ChainItem& item = mItems[i];
239 std::string prefix = "Item " + std::to_string(i + 1) + ": ";
240
241 if (item.IsPageItem()) {
242 // Validate page items
243 if (item.title.empty()) {
244 result.warnings.push_back(prefix + "Page has no title");
245 }
246
247 if (item.content.empty()) {
248 result.warnings.push_back(prefix + "Page has no content");
249 }
250
251 } else {
252 // Validate test items
253 if (item.testName.empty()) {
254 result.errors.push_back(prefix + "Test name is required");
255 }
256
257 // If study is provided, check if test exists in study
258 if (study != nullptr) {
259 const Test* test = study->GetTest(item.testName);
260 if (test == nullptr) {
261 result.errors.push_back(prefix + "Test not found in study: " + item.testName);
262 } else {
263 // Check if test is included
264 if (!test->included) {
265 result.warnings.push_back(prefix + "Test is marked as not included: " + item.testName);
266 }
267
268 // Check parameter variant
269 if (!item.paramVariant.empty() && item.paramVariant != "default") {
270 const ParameterVariant* variant = test->GetVariant(item.paramVariant);
271 if (variant == nullptr) {
272 result.errors.push_back(prefix + "Parameter variant not found: " + item.paramVariant);
273 }
274 }
275
276 // Check language
277 if (!item.language.empty()) {
278 std::vector<std::string> availableLangs = test->GetAvailableLanguages(study->GetPath());
279 bool langFound = false;
280 for (const auto& lang : availableLangs) {
281 if (lang == item.language) {
282 langFound = true;
283 break;
284 }
285 }
286 if (!langFound && !availableLangs.empty()) {
287 result.warnings.push_back(prefix + "Language not found in translations: " + item.language);
288 }
289 }
290 }
291 }
292 }
293 }
294
295 return result;
296}
std::string testName
Definition Chain.h:33
bool IsPageItem() const
Definition Chain.h:52
std::string title
Definition Chain.h:29
std::string content
Definition Chain.h:30
std::string paramVariant
Definition Chain.h:34
std::string language
Definition Chain.h:35
Definition Study.h:24
std::vector< std::string > GetAvailableLanguages(const std::string &studyPath) const
Definition Study.cpp:48
const ParameterVariant * GetVariant(const std::string &variantName) const
Definition Study.cpp:80
bool included
Definition Study.h:28

References ChainItem::content, Chain::ValidationResult::errors, Test::GetAvailableLanguages(), Study::GetPath(), Study::GetTest(), Test::GetVariant(), Test::included, ChainItem::IsPageItem(), ChainItem::language, ChainItem::paramVariant, ChainItem::testName, ChainItem::title, and Chain::ValidationResult::warnings.


The documentation for this class was generated from the following files: