lazhen
2025-01-09 8afe90b633046db39042aada36b88193062f8cff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Quintiq file version 2.0
#parent: #root
Method CompleteCombiForLastPeriod (
  OptCampaignCombi combi,
  output Campaign_MPs campaigns,
  output Transition_MPs transitions
)
{
  Description: 'added combi transition and campaigns for last period of multiperiod combi'
  TextBody:
  [*
    if ( not isnull( combi ) ) 
    {
      lastocusp := combi.LastCombiElement().OptCampaignUnitSubPeriod(); 
      assert( not isnull( lastocusp ), 'expecting last combi element sub period is set' ); 
      // keep adding campaign / transitions for as longs as they end within lastocusp
      done := false; 
      while( campaigns.Size() + transitions.Size() > 0 and not done ) 
      {
        firstcampaign := guard( campaigns.Element( 0 ), null( Campaign_MP ) );
        firsttransition := guard( transitions.Element( 0 ), null( Transition_MP ) );  
        if ( isnull( firstcampaign ) ) 
        {
          combi.AddCombiElementTransition( firsttransition.TransitionType_MP(), lastocusp ); 
          if ( firsttransition.End() <= lastocusp.EndTime() ) 
          {
            transitions.Remove( firsttransition ); // no need to continue anymore in next sub period 
          }
          done := true; 
        }
        else
        {
          if ( isnull( firsttransition ) ) 
          {
            combi.AddCombiElementCampaign( firstcampaign.CampaignType_MP(), lastocusp ); 
            if ( firstcampaign.End() <= lastocusp.EndTime() ) 
            {
              campaigns.Remove( firstcampaign ); 
            }
            done := true; 
          }
          else 
          {
            // we still have both campaigns and transitions
            if ( firstcampaign.Start() < firsttransition.Start() ) 
            {
              combi.AddCombiElementCampaign( firstcampaign.CampaignType_MP(), lastocusp ); 
              
              done := firstcampaign.End() >= lastocusp.EndTime(); 
              if ( firstcampaign.End() > lastocusp.EndTime() ) 
              {
                // don't remove campaign - need to continue with it next period(s). 
              }
              else if ( firstcampaign.End() = lastocusp.EndTime() )   // special case of ending right at period end. 
              {
                campaigns.Remove( firstcampaign ); 
                // add combi element so it matches with the next period combi - but disallowed nonzero duration because this will lead to a mismatch if current combi start with same elt
                elt := combi.AddCombiElementTransition( firsttransition.TransitionType_MP(), lastocusp ); 
                elt.DebugFixEndDuration( true ); 
                elt.DebugDurationOfCampaignElementTypeEndVariables( 0.0 ); 
              }
              else
              {
                campaigns.Remove( firstcampaign ); // campaign ending strictly before end of period, continue completing combi
              }
            }                                       
            else
            {
              combi.AddCombiElementTransition( firsttransition.TransitionType_MP(), lastocusp ); 
              done := firsttransition.End() >= lastocusp.EndTime(); 
              if ( firsttransition.End() > lastocusp.EndTime() )
              {
                // don't remove transition - need to continue with it next period(s)
              }
              else if ( firsttransition.End() = lastocusp.EndTime() )    // special case of ending right at period end. 
              {
                transitions.Remove( firsttransition ); 
                // add combi element so it matches with the next period combi - but disallowed nonzero duration because this will lead to a mismatch if current combi start with same elt
                elt := combi.AddCombiElementCampaign( firstcampaign.CampaignType_MP(), lastocusp );
                elt.DebugFixEndDuration( true ); 
                elt.DebugDurationOfCampaignElementTypeEndVariables( 0.0 );      
              }
              else
              {
                transitions.Remove( firsttransition ); // transition ending strictly before end of period, continue completing combi
              } 
            }
          }  
        }   
      }
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}