@@ -11,12 +11,20 @@ namespace SharpPDDL
1111 public delegate void ListOfString ( List < List < string > > planGenerated ) ;
1212
1313 internal class DomainPlanner
14+
15+ // | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|
16+ // |Clean the mess!|
17+ // |________|
18+ // (\__/) ||
19+ // (•ㅅ•) ||
20+ // / づ
21+
1422 {
1523 protected Dictionary < FoungingGoalDetail , SortedSet < Crisscross > > FoundedGoals ;
1624 protected Dictionary < Crisscross , List < GoalPDDL > > FoundedCrisscrosses ;
1725 protected Crisscross CurrentBuilded ;
1826 internal CrisscrossGenerator CurrentBuilder { get ; private set ; }
19- protected PlanImplementor PlanImplementor ;
27+ protected Executor DomainExecutor ;
2028 protected readonly DomeinPDDL Owner ;
2129 internal Action < uint > currentMinCumulativeCostUpdate ;
2230 protected Action < KeyValuePair < Crisscross , List < GoalPDDL > > > FoundSols ;
@@ -36,7 +44,7 @@ internal DomainPlanner(DomeinPDDL Owner)
3644 Content = Owner . CurrentState
3745 } ;
3846
39- PlanImplementor = new PlanImplementor ( Owner ) ;
47+ DomainExecutor = new Executor ( Owner ) ;
4048 CurrentBuilder = new CrisscrossGenerator ( CurrentBuilded , Owner , FoundSols , currentMinCumulativeCostUpdate ) ;
4149 FoundedGoals = new Dictionary < FoungingGoalDetail , SortedSet < Crisscross > > ( ) ;
4250 FoundedCrisscrosses = new Dictionary < Crisscross , List < GoalPDDL > > ( Crisscross . IContentEqualityComparer ) ;
@@ -61,16 +69,27 @@ internal void Start(ParallelOptions options)
6169
6270 internal void DomainGoals_CollectionChanged ( object sender , System . Collections . Specialized . NotifyCollectionChangedEventArgs e )
6371 {
72+ if ( e . Action != System . Collections . Specialized . NotifyCollectionChangedAction . Add &&
73+ e . Action != System . Collections . Specialized . NotifyCollectionChangedAction . Remove )
74+ return ;
75+
76+ Task CurrentBuilderStopping = null ;
77+ if ( ! CurrentBuilder . IsCrisscrossGeneratorCancellationRequested )
78+ CurrentBuilderStopping = CurrentBuilder . Stop ( ) ;
79+
6480 if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Remove )
6581 {
82+ if ( ! ( CurrentBuilderStopping is null ) )
83+ CurrentBuilderStopping . Wait ( ) ;
84+
6685 foreach ( var RemGoal in e . OldItems )
86+ {
87+ RemoveFoundedGoal ( ( GoalPDDL ) RemGoal ) ;
6788 GloCla . Tracer ? . TraceEvent ( TraceEventType . Verbose , 141 , GloCla . ResMan . GetString ( "V10" ) , ( ( GoalPDDL ) RemGoal ) . Name ) ;
68- }
89+ }
6990
70- if ( e . Action != System . Collections . Specialized . NotifyCollectionChangedAction . Add )
71- {
72- //if (!Owner.domainGoals.Any())
73- // InternalCancellationDomeinSrc.Cancel();
91+ if ( ! ( CurrentBuilderStopping is null ) )
92+ CurrentBuilder . ReStart ( ) ;
7493
7594 return ;
7695 }
@@ -93,8 +112,6 @@ internal void DomainGoals_CollectionChanged(object sender, System.Collections.Sp
93112 throw new Exception ( GloCla . ResMan . GetString ( "E5" ) ) ;
94113 }
95114
96- Task CurrentBuilderStopping = CurrentBuilder . Stop ( ) ;
97-
98115 foreach ( GoalPDDL ToCheckGoal in ToCheckGoals )
99116 ToCheckGoal . BuildIt ( Owner ) ;
100117
@@ -130,10 +147,10 @@ private void FoundSolsVoid(KeyValuePair<Crisscross, List<GoalPDDL>> Found)
130147 }
131148 }
132149
133- if ( this . PlanImplementor . ImplementorTask ? . Status == TaskStatus . Running )
150+ if ( this . DomainExecutor . ImplementorTask ? . Status == TaskStatus . Running )
134151 return ;
135152
136- if ( Found . Value . Any ( G => G . goalPriority == GoalPriority . TopHihtPriority ) || Found . Value . Count ( ) == Owner . domainGoals . Count ( ) )
153+ if ( Found . Value . Any ( G => G . goalPriority == GoalPriority . TopHihtPriority ) )
137154 {
138155 this . GenList ( Found ) ;
139156 }
@@ -154,7 +171,7 @@ private void CheckingIfGenerateActionList(uint ActMinCumulativeCost)
154171 }
155172
156173 var FoundedChipStates = NOTIsFoundingChippest . Where ( FG => ( 1.05 * FG . Value . First ( ) . CumulativedTransitionCharge < ActMinCumulativeCost ) ) ;
157- if ( FoundedChipStates is null )
174+ if ( ! FoundedChipStates . Any ( ) )
158175 return ;
159176
160177 int MaxCountSolution = FoundedChipStates . Max ( FG => FG . Value . Count ) ;
@@ -230,49 +247,22 @@ private void RealizeGoalsPlanifFound()
230247 }
231248 }
232249
233- private void SetObjToMigrate ( GoalPDDL goalPDDL , Crisscross GainedCrisscross )
250+ internal void RemoveFoundedGoal ( GoalPDDL goalPDDL )
234251 {
235- foreach ( IGoalObject goalObject in goalPDDL . GoalObjects )
236- {
237- if ( ! goalObject . MigrateIntheEnd )
238- continue ;
239-
240- if ( ! ( goalObject . OriginalObj is null ) )
241- continue ;
242-
243- foreach ( ThumbnailObject ThObj in GainedCrisscross . Content . ChangedThumbnailObjects )
244- foreach ( IGoalObject goalObj in goalPDDL . GoalObjects )
245- if ( ( bool ) goalObj . GoalPDDL . DynamicInvoke ( ThObj ) )
246- goalObj . OriginalObj = ThObj . OriginalObj ;
247- }
248- }
249-
250- internal List < GoalPDDL > RemoveRealizedGoalsOfCrisscross ( Crisscross GainedCrisscross )
251- {
252- if ( ! FoundedCrisscrosses . ContainsKey ( GainedCrisscross ) )
253- return null ;
252+ if ( ! FoundedGoals . Any ( FG => FG . Key . GoalPDDL . Equals ( goalPDDL ) ) )
253+ return ;
254254
255- //copy list of realized goals
256- List < GoalPDDL > goals = new List < GoalPDDL > ( FoundedCrisscrosses [ GainedCrisscross ] ) ;
255+ KeyValuePair < FoungingGoalDetail , SortedSet < Crisscross > > toRem = FoundedGoals . First ( FG => FG . Key . GoalPDDL . Equals ( goalPDDL ) ) ;
257256
258- foreach ( GoalPDDL goalPDDL in goals )
257+ foreach ( Crisscross crisscross in toRem . Value )
259258 {
260- SetObjToMigrate ( goalPDDL , GainedCrisscross ) ;
261- KeyValuePair < FoungingGoalDetail , SortedSet < Crisscross > > FoGo = FoundedGoals . First ( FG => FG . Key . GoalPDDL . Name == goalPDDL . Name ) ;
262- FoGo . Value . Remove ( GainedCrisscross ) ;
263- this . Owner . domainGoals . Remove ( goalPDDL ) ;
264-
265- //Let external program know, some part is realized
266- goalPDDL . GoalRealized ? . Invoke ( goalPDDL , null ) ;
267-
268- if ( FoGo . Value . Any ( ) )
269- continue ;
270-
271- FoundedGoals . Remove ( FoGo . Key ) ;
259+ List < GoalPDDL > r = FoundedCrisscrosses [ crisscross ] ;
260+ bool us = r . Remove ( goalPDDL ) ;
261+ if ( ! r . Any ( ) )
262+ FoundedCrisscrosses . Remove ( crisscross ) ;
272263 }
273264
274- FoundedCrisscrosses . Remove ( GainedCrisscross ) ;
275- return goals ;
265+ FoundedGoals . Remove ( toRem . Key ) ;
276266 }
277267
278268 internal void GenList ( KeyValuePair < Crisscross , List < GoalPDDL > > Found )
@@ -294,31 +284,24 @@ internal void GenList(KeyValuePair<Crisscross, List<GoalPDDL>> Found)
294284 for ( int j = 0 ; j != arg . Length ; j ++ )
295285 arg [ j ] = state . Content . ThumbnailObjects . First ( ThOb => ThOb . OriginalObj . Equals ( FoKePo [ i ] . ActionArgOryg [ j ] ) ) ;
296286
297- Plan . Add ( new List < string > { String . Format ( GloCla . ResMan . GetString ( "Txt1" ) , Owner . actions [ FoKePo [ i ] . ActionNr ] . Name ) , ( string ) Owner . actions [ FoKePo [ i ] . ActionNr ] . InstantActionSententia . DynamicInvoke ( arg ) , String . Format ( GloCla . ResMan . GetString ( "Txt2" ) , FoKePo [ i ] . ActionCost ) } ) ;
287+ Plan . Add ( new List < string > { String . Format ( GloCla . ResMan . GetString ( "Txt1" ) , Owner . actions [ FoKePo [ i ] . ActionNr ] . Name ) , ( string ) Owner . actions [ FoKePo [ i ] . ActionNr ] . InstantActionSententia . DynamicInvoke ( arg ) , String . Format ( GloCla . ResMan . GetString ( "Txt2" ) , FoKePo [ i ] . ActionCost ) } ) ;
298288
299289 state = FoKePo [ i ] . Child ;
300290 }
301291
302292 PlanGeneratedInDomainPlanner ? . Invoke ( Plan ) ;
303293
304- List < Task > ToWait = new List < Task > ( ) ;
305-
306294 Task Stopping = CurrentBuilder . Stop ( ) ;
307- if ( Stopping . Status == TaskStatus . Running )
308- ToWait . Add ( Stopping ) ;
309-
310- Task Realizing = PlanImplementor . RealizeIt ( FoKePo , CancellationDomein ) ;
311- if ( Realizing . Status == TaskStatus . Running )
312- ToWait . Add ( Realizing ) ;
313-
314295 Task < ( Crisscross NewRoot , SortedSet < Crisscross > , SortedList < string , Crisscross > NewIndexedStates ) > Transcribing = CurrentBuilder . TranscribeState ( FoKePo . Last ( ) . Child , CancellationDomein ) ;
315- if ( Transcribing . Status == TaskStatus . Running )
316- ToWait . Add ( Transcribing ) ;
296+ Task . WaitAll ( Stopping , Transcribing ) ;
317297
318- Task . WaitAll ( ToWait . ToArray ( ) ) ;
298+ CurrentBuilded = Transcribing . Result . NewRoot ;
299+
300+ Task Realizing = DomainExecutor . RealizeIt ( FoKePo , Found . Value , CancellationDomein ) ;
301+ Realizing . Wait ( ) ;
319302
320303 //internal fault
321- if ( Realizing . IsFaulted )
304+ if ( Realizing . IsFaulted )
322305 {
323306 if ( Realizing . Exception . InnerException is PrecondExecutionException )
324307 {
@@ -331,10 +314,10 @@ internal void GenList(KeyValuePair<Crisscross, List<GoalPDDL>> Found)
331314 RefreshedThumbnails . Add ( UpdatedThObj ) ;
332315 }
333316
334- CurrentBuilded = new Crisscross ( )
317+ /* CurrentBuilded = new Crisscross()
335318 {
336319 Content = new PossibleState(RefreshedThumbnails)
337- } ;
320+ };*/
338321
339322 var ToGoalCheck = new ConcurrentQueue < Crisscross > ( ) ;
340323 ToGoalCheck . Enqueue ( CurrentBuilded ) ;
@@ -354,10 +337,10 @@ internal void GenList(KeyValuePair<Crisscross, List<GoalPDDL>> Found)
354337 else
355338 {
356339 CurrentBuilded = Transcribing . Result . NewRoot ;
357- CurrentBuilder . InitBuffors ( null , Transcribing . Result . Item2 , null , Transcribing . Result . NewIndexedStates ) ;
358-
340+ CurrentBuilder . InitBuffors ( Transcribing . Result . Item2 , null , null , Transcribing . Result . NewIndexedStates ) ;
341+
359342 if ( Owner . domainGoals . Except ( Found . Value ) . Any ( ) )
360- CurrentBuilder . ReStart ( ) ;
343+ CurrentBuilder . ReStart ( ) ;
361344 }
362345 }
363346 }
0 commit comments