|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace SharpPDDL |
| 9 | +{ |
| 10 | + internal class Transcriber |
| 11 | + { |
| 12 | + //New root of states |
| 13 | + internal readonly Crisscross NewOne; |
| 14 | + |
| 15 | + private readonly List<ActionPDDL> Actions; |
| 16 | + |
| 17 | + //Crisscroses without child in prev. run, spots to start run alg. |
| 18 | + internal readonly SortedSet<Crisscross> ChildlessCrisscrosses; |
| 19 | + |
| 20 | + //List of all Crisscrosses in NewOne |
| 21 | + internal readonly SortedList<string, Crisscross> NewIndexedStates; |
| 22 | + |
| 23 | + //Collection of Crisscrosses waiting to pin into NewOne "tree" |
| 24 | + //private SortedSet<KeyValuePair<uint, Crisscross>> NotTranscribedChildYet; |
| 25 | + |
| 26 | + private SortedSet<KeyValuePair<Crisscross, List<CrisscrossChildrenCon>>> NotTranscribedChildYet; |
| 27 | + |
| 28 | + //List of Crisscrosses pined into NewOne with alternatives Root, candidates to reduce |
| 29 | + private List<Crisscross> PossibleToCrisscrossReduce; |
| 30 | + |
| 31 | + internal Transcriber(Crisscross NewRoot, List<ActionPDDL> actions) |
| 32 | + { |
| 33 | + this.Actions = actions; |
| 34 | + |
| 35 | + List<ThumbnailObject> NewThumbnailObjects = new List<ThumbnailObject>(); |
| 36 | + foreach (ThumbnailObject TO in NewRoot.Content.ThumbnailObjects) |
| 37 | + NewThumbnailObjects.Add(new ThumbnailObjectPrecursor<object>(TO, false) as ThumbnailObject); |
| 38 | + |
| 39 | + NewOne = new Crisscross |
| 40 | + { |
| 41 | + Content = new PossibleState(NewThumbnailObjects) |
| 42 | + }; |
| 43 | + |
| 44 | + NotTranscribedChildYet = new SortedSet<KeyValuePair<Crisscross, List<CrisscrossChildrenCon>>>(new TupleCo()) |
| 45 | + { |
| 46 | + new KeyValuePair<Crisscross, List<CrisscrossChildrenCon>>(NewOne, NewRoot.Children) |
| 47 | + }; |
| 48 | + |
| 49 | + ChildlessCrisscrosses = new SortedSet<Crisscross>(Crisscross.SortCumulativedTransitionCharge()); |
| 50 | + |
| 51 | + PossibleToCrisscrossReduce = new List<Crisscross>() { NewOne }; |
| 52 | + |
| 53 | + NewIndexedStates = new SortedList<string, Crisscross> |
| 54 | + { |
| 55 | + { NewOne.Content.CheckSum, NewOne } |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + private Task TranscribeOne(CancellationToken cancellationToken) |
| 60 | + { |
| 61 | + Task TranscribeTask = new Task(() => |
| 62 | + { |
| 63 | + while (NotTranscribedChildYet.Any() && !cancellationToken.IsCancellationRequested) |
| 64 | + { |
| 65 | + KeyValuePair<Crisscross, List<CrisscrossChildrenCon>> keyValuePair = NotTranscribedChildYet.First(); |
| 66 | + Crisscross WorkWithIt = keyValuePair.Key; |
| 67 | + |
| 68 | + foreach (CrisscrossChildrenCon C in keyValuePair.Value) |
| 69 | + { |
| 70 | + ThumbnailObject[] SetToCheck = new ThumbnailObject[C.ActionArgThOb.Length]; |
| 71 | + |
| 72 | + for (int i = 0; i != C.ActionArgThOb.Length; i++) |
| 73 | + SetToCheck[i] = WorkWithIt.Content.ThumbnailObjects.First(TO => TO.OriginalObj.Equals(C.ActionArgThOb[i].OriginalObj)); |
| 74 | + |
| 75 | + object ResultOfCheck = Actions[C.ActionNr].InstantActionPDDLSimplified.DynamicInvoke(SetToCheck); |
| 76 | + |
| 77 | + if (ResultOfCheck is null) |
| 78 | + continue; |
| 79 | + |
| 80 | + var ResultOfCheckasList = (List<List<KeyValuePair<ushort, ValueType>>>)ResultOfCheck; |
| 81 | + var ChangedThObs = new List<ThumbnailObject>(); |
| 82 | + |
| 83 | + for (int j = 0; j < SetToCheck.Length; j++) |
| 84 | + { |
| 85 | + var ChangedThumbnailObject = SetToCheck[j].CreateChild(ResultOfCheckasList[j]); |
| 86 | + ChangedThObs.Add(ChangedThumbnailObject); |
| 87 | + } |
| 88 | + |
| 89 | + PossibleState possibleState = new PossibleState(WorkWithIt.Content, ChangedThObs); |
| 90 | + WorkWithIt.Add(possibleState, C.ActionNr, SetToCheck, C.ActionCost, out Crisscross AddedItem); |
| 91 | + |
| 92 | + if (NewIndexedStates.Any(s => s.Key == AddedItem.Content.CheckSum)) |
| 93 | + { |
| 94 | + if (NewIndexedStates.Any(s => s.Value.Content.Compare(ref AddedItem.Content))) |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if (WorkWithIt.AlternativeRoots.Any()) |
| 99 | + { |
| 100 | + lock (PossibleToCrisscrossReduce) |
| 101 | + { |
| 102 | + PossibleToCrisscrossReduce.Add(AddedItem); |
| 103 | + } |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + try |
| 108 | + { |
| 109 | + NewIndexedStates.Add(AddedItem.Content.CheckSum, AddedItem); |
| 110 | + } |
| 111 | + catch { } |
| 112 | + } |
| 113 | + |
| 114 | + if (C.Child.Children.Any()) |
| 115 | + NotTranscribedChildYet.Add(new KeyValuePair<Crisscross, List<CrisscrossChildrenCon>>(AddedItem, C.Child.Children)); |
| 116 | + else |
| 117 | + ChildlessCrisscrosses.Add(AddedItem); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + NotTranscribedChildYet.Remove(keyValuePair); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + TranscribeTask.Start(); |
| 126 | + |
| 127 | + return TranscribeTask; |
| 128 | + } |
| 129 | + |
| 130 | + internal Task TranscribeState(CancellationToken cancellationToken) |
| 131 | + { |
| 132 | + Task Transcribing = new Task(() => |
| 133 | + { |
| 134 | + GloCla.Tracer?.TraceEvent(TraceEventType.Start, 122, GloCla.ResMan.GetString("Sa10")); |
| 135 | + AutoResetEvent autoResetEvent = new AutoResetEvent(true); |
| 136 | + object CrisscrossReduceLocker = new Object(); |
| 137 | + CrisscrossReducer crisscrossReducer = new CrisscrossReducer(NewOne, autoResetEvent, PossibleToCrisscrossReduce, CrisscrossReduceLocker, null, null); |
| 138 | + crisscrossReducer.IndexStates(NewIndexedStates); |
| 139 | + |
| 140 | + //Task transcribeOne = TranscribeOne(cancellationToken); |
| 141 | + crisscrossReducer.Start(cancellationToken); |
| 142 | + //transcribeOne.Wait(); |
| 143 | + ChildlessCrisscrosses.Add(NewOne); |
| 144 | + |
| 145 | + if (cancellationToken.IsCancellationRequested) |
| 146 | + ChildlessCrisscrosses.UnionWith(NotTranscribedChildYet.Select(NT => NT.Key)); |
| 147 | + |
| 148 | + GloCla.Tracer?.TraceEvent(TraceEventType.Stop, 123, GloCla.ResMan.GetString("Sp10")); |
| 149 | + }, cancellationToken); |
| 150 | + |
| 151 | + Transcribing.Start(); |
| 152 | + |
| 153 | + return Transcribing; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments