40 int Jigsaw::m_class_key = 0;
42 Jigsaw::Jigsaw(
const std::string& sname,
43 const std::string& stitle,
44 int Nchild,
int Ndependancy)
45 : RFBase(sname, stitle, Jigsaw::m_class_key++),
46 m_Nchild(Nchild), m_Ndeps(Ndependancy)
48 m_Log.SetSource(
"Jigsaw "+
GetName());
50 m_ParentStatePtr =
nullptr;
51 for(
int i = 0; i < m_Nchild; i++){
52 m_ChildFrames.push_back(ConstRestFrameList());
55 for(
int i = 0; i < m_Ndeps; i++){
56 m_DependancyFrames.push_back(ConstRestFrameList());
57 m_DependancyStates.push_back(StateList());
61 Jigsaw::Jigsaw() : RFBase(), m_Nchild(0), m_Ndeps(0){
62 m_Type = kVanillaJigsaw;
63 m_Log.SetSource(
"Jigsaw "+
GetName());
70 Jigsaw& Jigsaw::Empty(){
71 return InvisibleJigsaw::Empty();
79 for(
int i = 0; i < m_Nchild; i++){
80 m_ChildFrames[i].Clear();
81 m_ChildStates[i].Clear();
84 for(
int i = 0; i < m_Ndeps; i++){
85 m_DependancyStates[i].Clear();
86 m_DependancyFrames[i].Clear();
89 m_DependancyJigsaws.Clear();
92 bool Jigsaw::IsInvisibleJigsaw()
const {
93 return m_Type == kInvisibleJigsaw;
96 bool Jigsaw::IsCombinatoricJigsaw()
const {
97 return m_Type == kCombinatoricJigsaw;
102 if(IsInvisibleJigsaw())
103 output +=
" Type: Invisible \n";
104 if(IsCombinatoricJigsaw())
105 output +=
" Type: Combinatoric \n";
109 void Jigsaw::SetGroup(
Group& group){
116 if(*m_GroupPtr == group){
119 Group* groupPtr = m_GroupPtr;
120 m_GroupPtr =
nullptr;
121 groupPtr->RemoveJigsaw(*
this);
125 m_GroupPtr =
nullptr;
130 Group& Jigsaw::GetGroup()
const {
134 return Group::Empty();
137 bool Jigsaw::CanResolve(
const ConstRestFrameList& frames)
const {
139 return SetBody(
false);
141 return GetParentFrames() == frames;
144 bool Jigsaw::CanResolve(
const State& state)
const {
145 if(!state)
return false;
146 return CanResolve(state.GetListFrames());
149 void Jigsaw::SetParentState(){
150 SetParentState(State::Empty());
153 void Jigsaw::SetParentState(State& state){
157 if(m_ParentStatePtr){
158 if(*m_ParentStatePtr == state)
161 m_ParentStatePtr =
nullptr;
165 m_ParentStatePtr =
nullptr;
167 m_ParentStatePtr = &state;
168 state.SetChildJigsaw(*
this);
172 State
const& Jigsaw::GetParentState()
const {
174 return *m_ParentStatePtr;
176 return State::Empty();
179 int Jigsaw::GetNChildren()
const {
183 int Jigsaw::GetNDependancyStates()
const {
187 State& Jigsaw::GetChildState(
int i)
const {
188 return m_ChildStates[i];
191 StateList
const& Jigsaw::GetChildStates()
const {
192 return m_ChildStates;
195 StateList
const& Jigsaw::GetDependancyStates(
int i)
const {
196 if(i < 0 || i >= m_Ndeps)
197 return State::EmptyList();
198 return m_DependancyStates[i];
201 ConstRestFrameList Jigsaw::GetParentFrames()
const {
202 ConstRestFrameList frames;
203 for(
int i = 0; i < m_Nchild; i++)
204 frames += GetChildFrames(i);
208 ConstRestFrameList
const& Jigsaw::GetChildFrames(
int i)
const {
209 if(i < 0 || i >= m_Nchild)
210 return RestFrame::EmptyList();
211 return m_ChildFrames[i];
214 ConstRestFrameList
const& Jigsaw::GetDependancyFrames(
int i)
const {
215 if(i < 0 || i >= m_Ndeps)
216 return RestFrame::EmptyList();
217 return m_DependancyFrames[i];
220 bool Jigsaw::InitializeTree(){
222 return SetMind(
false);
224 if(!GetParentState()){
226 m_Log <<
"Unable to initialize Jigsaw. ";
227 m_Log <<
"No parent State set." << LogEnd;
228 return SetMind(
false);
231 if(!CanResolve(GetParentState())){
233 m_Log <<
"Unable to resolve input parent State. ";
234 m_Log <<
" Frames (capable): " << Log(GetParentFrames()) << std::endl;
235 m_Log <<
" Frames (requested): " << Log(GetParentState().GetListFrames());
237 return SetMind(
false);
240 for(
int i = 0; i < m_Nchild; i++){
241 m_ChildStates[i].Clear();
242 m_ChildStates[i].SetParentJigsaw(*
this);
243 m_ChildStates[i].AddFrames(GetChildFrames(i));
246 return SetMind(
true);
249 bool Jigsaw::InitializeAnalysis(){
251 return SetMind(
false);
254 return SetMind(
false);
257 const LabRecoFrame& lab_frame =
258 static_cast<const LabRecoFrame&
>(GetGroup().GetLabFrame());
259 StateList states = lab_frame.GetTreeStates();
260 GroupList groups = lab_frame.GetListGroups();
262 int Ngroup = groups.GetN();
263 std::vector<ConstRestFrameList> group_frames;
264 for(
int i = 0; i < Ngroup; i++)
265 group_frames.push_back(ConstRestFrameList());
267 for(
int d = 0; d < m_Ndeps; d++){
268 m_DependancyStates[d].Clear();
270 for(
int i = 0; i < Ngroup; i++)
271 group_frames[i].
Clear();
273 int Nf = m_DependancyFrames[d].GetN();
274 for(
int f = 0; f < Nf; f++){
275 const RestFrame& frame = m_DependancyFrames[d][f];
277 bool no_group =
true;
278 for(
int g = 0; g < Ngroup; g++){
279 if(groups[g].ContainsFrame(frame)){
280 group_frames[g].Add(frame);
287 State& state = states.GetFrame(frame);
290 m_Log <<
"Cannot find State corresponding to frame: " << std::endl;
291 m_Log << Log(frame) <<
" " << Log(states) << LogEnd;
292 return SetMind(
false);
295 m_Log <<
"Adding dependancy State for index " << d;
296 m_Log <<
" corresponding to frame:";
297 m_Log << Log(frame) << LogEnd;
298 m_DependancyStates[d] += state;
301 for(
int g = 0; g < Ngroup; g++){
302 if(group_frames[g].GetN() == 0)
continue;
303 StateList group_states = groups[g].GetChildStates(group_frames[g]);
304 if(group_states.GetN() == 0){
305 m_Log <<
"Cannot find States in Group:" << std::endl;
306 m_Log <<
" Frames:" << std::endl <<
" ";
307 m_Log << Log(group_frames[g]) << std::endl;
308 m_Log <<
" Group:" << std::endl;
309 m_Log << Log(groups.Get(g)) << LogEnd;
310 return SetMind(
false);
313 m_Log <<
"Sucessfully found dependancy States for index " << d << std::endl;
314 int Ns = group_states.GetN();
315 m_Log <<
" Frames:" << std::endl <<
" ";
316 m_Log << Log(group_frames[g]) << std::endl;
317 m_Log <<
" States:" << std::endl;
318 for(
int s = 0; s < Ns; s++){
319 m_Log <<
" state " << s <<
": ";
320 m_Log << Log(group_states[s].GetListFrames()) << std::endl;
323 m_DependancyStates[d] += group_states;
327 return SetMind(
true);
330 bool Jigsaw::InitializeDependancyJigsaws(){
334 m_DependancyJigsaws.Clear();
337 FillStateJigsawDependancies(jigsaws);
339 m_DependancyJigsaws.Add(jigsaws);
342 FillGroupJigsawDependancies(jigsaws);
344 m_DependancyJigsaws += jigsaws;
349 void Jigsaw::AddChildFrame(
const RestFrame& frame,
int i){
351 if(!frame.IsRecoFrame())
return;
352 if(i < 0 || i >= m_Nchild)
return;
356 m_ChildFrames[i] += frame;
359 void Jigsaw::AddDependancyFrame(
const RestFrame& frame,
int i){
361 if(!frame.IsRecoFrame())
return;
362 if(i < 0 || i >= m_Ndeps)
return;
366 m_DependancyFrames[i] += frame;
369 void Jigsaw::RemoveFrame(
const RestFrame& frame){
374 for(
int i = 0; i < m_Nchild; i++)
375 m_ChildFrames[i].Remove(frame);
377 for(
int i = 0; i < m_Ndeps; i++)
378 m_DependancyFrames[i].Remove(frame);
381 void Jigsaw::RemoveFrames(
const ConstRestFrameList& frames){
382 int N = frames.GetN();
383 for(
int i = 0; i < N; i++)
384 RemoveFrame(frames[i]);
387 bool Jigsaw::IsSoundBody()
const {
388 if(RFBase::IsSoundBody())
391 for(
int i = 0; i < m_Nchild-1; i++){
392 for(
int j = i+1; j < m_Nchild; j++){
393 if(m_ChildFrames[i].Intersection(m_ChildFrames[j]).GetN() > 0){
395 m_Log <<
"Child frames are repeated between ";
396 m_Log <<
"more than one output index: ";
397 m_Log << Log(m_ChildFrames[i].Intersection(m_ChildFrames[j]));
399 return SetBody(
false);
403 for(
int i = 0; i < m_Nchild; i++){
404 if(m_ChildFrames[i].GetN() == 0){
406 m_Log <<
"No child frames at index ";
407 m_Log << i << LogEnd;
408 return SetBody(
false);
412 return SetBody(
true);
415 bool Jigsaw::DependsOnJigsaw(
const Jigsaw& jigsaw)
const {
416 return m_DependancyJigsaws.Contains(jigsaw);
419 void Jigsaw::FillGroupJigsawDependancies(JigsawList& jigsaws)
const {
420 if(jigsaws.Contains(*
this))
return;
421 jigsaws.Add((Jigsaw&)(*m_This));
423 m_ParentStatePtr->GetParentJigsaw().FillGroupJigsawDependancies(jigsaws);
426 void Jigsaw::FillStateJigsawDependancies(JigsawList& jigsaws)
const {
427 if(jigsaws.Contains(*
this))
return;
428 jigsaws.Add((Jigsaw&)(*m_This));
430 for(
int i = 0; i < m_Ndeps; i++){
431 int M = m_DependancyStates[i].GetN();
432 for(
int j = 0; j < M; j++){
433 m_DependancyStates[i][j].GetParentJigsaw().
434 FillStateJigsawDependancies(jigsaws);
virtual std::string PrintString(LogType type) const
String of information associated with object.
std::string GetName() const
Returns object name.
bool IsEmpty() const
Checks whether this is default (empty) instance of class.
abstract base class for all Group objects
virtual std::string PrintString(LogType type) const
String of information associated with Jigsaw.
virtual void Clear()
Clears Jigsaw of all connections to other objects.