HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_WorkItemSerialization.h
Go to the documentation of this file.
1  /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  */
8 
9 #ifndef __PDG_WORKITEM_SERIALIZATION_H__
10 #define __PDG_WORKITEM_SERIALIZATION_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_AttributeMap.h"
15 #include "PDG_File.h"
16 #include "PDG_WorkItemTypes.h"
17 
18 #include <UT/UT_Array.h>
19 #include <UT/UT_ArrayStringMap.h>
20 #include <UT/UT_StringArray.h>
21 
22 #include <SYS/SYS_TypeDecorate.h>
23 
24 #include <iosfwd>
25 
26 class PDG_GraphContext;
27 class PDG_Graph;
29 class PDG_WorkItem;
30 
31 class UT_AutoJSONParser;
32 class UT_IStream;
33 class UT_JSONValueMap;
34 class UT_JSONValue;
35 class UT_JSONWriter;
36 class UT_Options;
37 class UT_OptionsHolder;
38 class UT_WorkBuffer;
39 
40 /**
41  * Helper class for loading and storing work items to serialization
42  * formats
43  */
45 {
46 public:
49 
50  /// Returned by the statJSON methods, to provide information about
51  /// the type and number of work items in a .json file or string
52  struct Stat
53  {
54  int myVersion;
56 
57  bool myIsArray;
59  };
60 
61  /// Enumeration of checkpoint save types, which are used to determine
62  /// which set of fields to write to the JSON object for a serialized
63  /// work item
65  {
66  /// Serialize all fields -- no checkpointing is in progress
68 
69  /// Serialize the initial baseline fields for the work item
71 
72  /// Serialize only fields that can be updated dynamically, when the
73  /// work item cooks
74  eCheckpointUpdate
75  };
76 
77 public:
78  /// Constructs an empty, default-initialized serialization object
80 
81  /// Loads work item attributes from a JSON value
82  bool loadAttributes(
83  const UT_JSONValue& value,
84  PDG_AttributeSaveType save_type,
85  bool reset);
86 
87  /// Loads work item attributres from a buffer
88  bool loadAttributes(
89  const UT_WorkBuffer& buffer,
90  PDG_AttributeSaveType save_type,
91  bool reset);
92 
93  /// Converts the serialization object to a PDG_WorkItem, in the given
94  /// graph context
95  PDG_WorkItem* toWorkItem(
96  PDG_GraphContext* context,
97  PDG_WorkItemID base_id,
98  bool strict,
99  Map& compat_map,
100  UT_WorkBuffer& errors) const;
101 
102  /// Initializes this instance from JSON string data
103  bool initializeString(
104  const UT_StringHolder& data,
105  UT_WorkBuffer& errors);
106 
107  /// Initializes this instance from a stream of JSON data
108  bool initializeStream(
109  UT_IStream& istream,
110  UT_WorkBuffer& errors);
111 
112  /// Initializes this instance from a JSON file
113  bool initializeFile(
114  const UT_StringHolder& file_path,
115  UT_WorkBuffer& errors);
116 
117  /// Initializes this instance from a dictionary
118  bool initializeDict(
119  const UT_OptionsHolder& dict,
120  UT_WorkBuffer& errors);
121 
122  /// Returns the work item name. If the serialization object was created
123  /// from the old, name-based format this returns the work item's name
124  /// as listed in the json/python data. Otherwise, this method computes
125  /// the name from the work item's node name and unique ID.
126  UT_StringHolder workItemName() const;
127 
128 
129  /// Saves the specified work item to the buffer, as JSON
130  static bool saveJSON(
132  const PDG_WorkItem* work_item,
133  const PDG_SerializationOptions& options,
134  CheckpointType checkpoint=eCheckpointNone);
135 
136  /// Saves the specified work item to the stream, as JSON
137  static bool saveJSON(
138  std::ostream& ostream,
139  const PDG_WorkItem* work_item,
140  const PDG_SerializationOptions& options,
141  CheckpointType checkpoint=eCheckpointNone);
142 
143  /// Saves the specified work item to the file, as JSON
144  static bool saveJSON(
145  const UT_StringHolder& file_path,
146  const PDG_WorkItem* work_item,
147  const PDG_SerializationOptions& options,
148  CheckpointType checkpoint=eCheckpointNone);
149 
150  /// Saves the specified work item to the UT_Options as a dictionary
151  static bool saveDict(
152  UT_OptionsHolder& dict,
153  const PDG_WorkItem* work_item,
154  const PDG_SerializationOptions& options,
155  CheckpointType checkpoint=eCheckpointNone);
156 
157  /// Saves the specified work item array to the buffer, as JSON
158  static bool saveArrayJSON(
160  const PDG_WorkItemArray& work_items,
161  const PDG_SerializationOptions& options,
162  CheckpointType checkpoint=eCheckpointNone);
163 
164  /// Saves the specified work item array to the stream, as JSON
165  static bool saveArrayJSON(
166  std::ostream& ostream,
167  const PDG_WorkItemArray& work_items,
168  const PDG_SerializationOptions& options,
169  CheckpointType checkpoint=eCheckpointNone);
170 
171  /// Saves the specified work item array to the file, as JSON
172  static bool saveArrayJSON(
173  const UT_StringHolder& file_path,
174  const PDG_WorkItemArray& work_items,
175  const PDG_SerializationOptions& options,
176  CheckpointType checkpoint=eCheckpointNone);
177 
178  /// Saves the specified work item array to UT_Options
179  static bool saveArrayDict(
180  UT_OptionsHolder& dict,
181  const PDG_WorkItemArray& work_items,
182  const PDG_SerializationOptions& options,
183  CheckpointType checkpoint=eCheckpointNone);
184 
185 
186  /// Saves the specified work item dependency pair to the buffer, as JSON
187  static bool saveDependencyJSON(
189  const PDG_WorkItem* from,
190  const PDG_WorkItem* to,
191  bool required,
192  bool user_defined,
193  const PDG_SerializationOptions& options,
194  CheckpointType checkpoint=eCheckpointNone);
195 
196  /// Saves the specified work item dependency pair to the stream, as JSON
197  static bool saveDependencyJSON(
198  std::ostream& ostream,
199  const PDG_WorkItem* from,
200  const PDG_WorkItem* to,
201  bool required,
202  bool user_defined,
203  const PDG_SerializationOptions& options,
204  CheckpointType checkpoint=eCheckpointNone);
205 
206  /// Saves the specified work item dependency pair to the file, as JSON
207  static bool saveDependencyJSON(
208  const UT_StringHolder& file_path,
209  const PDG_WorkItem* from,
210  const PDG_WorkItem* to,
211  bool required,
212  bool user_defined,
213  const PDG_SerializationOptions& options,
214  CheckpointType checkpoint=eCheckpointNone);
215 
216 
217  /// Saves a map with the data version to the buffer, as JSON
218  static bool saveVersion(
220  const PDG_SerializationOptions& options);
221 
222  /// Saves a map with the data version to the stream, as JSON
223  static bool saveVersion(
224  std::ostream& ostream,
225  const PDG_SerializationOptions& options);
226 
227  /// Saves a map with the data version to the file, as JSON
228  static bool saveVersion(
229  const UT_StringHolder& file_path,
230  const PDG_SerializationOptions& options);
231 
232  /// Saves the specified work item to the buffer, as a Python script
233  static bool savePython(
235  const PDG_WorkItem* work_item,
236  const PDG_SerializationOptions& options);
237 
238 
239  /// Loads a work item from a buffer containing JSON string data
240  static PDG_WorkItem* fromJSON(
241  const UT_StringHolder& buffer,
242  PDG_GraphContext* context,
243  Map& compat_map,
244  bool make_active,
245  UT_WorkBuffer& errors);
246 
247  /// Loads a work item from a stream containing JSON data
248  static PDG_WorkItem* fromJSON(
249  UT_IStream& istream,
250  bool make_active,
251  UT_WorkBuffer& errors);
252 
253  /// Loads a work item from a file path
254  static PDG_WorkItem* fromJSON(
255  const UT_StringHolder& file_path,
256  bool make_active,
257  UT_WorkBuffer& errors);
258 
259  /// Loads a work item from a dict
260  static PDG_WorkItem* fromDict(
261  const UT_OptionsHolder& dict,
262  bool make_active,
263  UT_WorkBuffer& errors);
264 
265 
266  /// Loads a work item array from a stream containing JSON data
267  static bool fromArrayJSON(
268  PDG_WorkItemArray& work_items,
269  UT_IStream& istream,
270  PDG_GraphContext* context,
271  bool is_compressed,
272  UT_WorkBuffer& errors);
273 
274 
275  /// Loads a serialization object array from a buffer containing JSON data
276  static bool fromArrayJSON(
277  Array& serialization_array,
278  const UT_StringHolder& buffer,
279  UT_WorkBuffer& errors);
280 
281  /// Loads a serialization object array from a file containing JSON data
282  static bool fromArrayJSON(
283  Array& serialization_array,
284  const UT_StringHolder& file_path,
285  bool is_compressed,
286  UT_WorkBuffer& errors);
287 
288  /// Loads a serialization object array from a dictionary
289  static bool fromArrayDict(
290  Array& serialization_array,
291  const UT_OptionsHolder& dict,
292  UT_WorkBuffer& errors);
293 
294 
295  /// Stats the JSON data in the specified string
296  static bool statJSON(
297  Stat& json_stat,
298  const UT_StringHolder& buffer,
299  UT_WorkBuffer& errors);
300 
301  /// Stats the JSON data in the specified file
302  static bool statJSON(
303  Stat& json_stat,
304  const UT_StringHolder& file_path,
305  bool is_compressed,
306  UT_WorkBuffer& errors);
307 
308  /// Stats the JSON data in the stream
309  static bool statJSON(
310  Stat& json_stat,
311  UT_IStream& istream,
312  bool is_compressed,
313  UT_WorkBuffer& errors);
314 
315  /// Stats the dictionary data
316  static bool statDict(
317  Stat& dict_stat,
318  const UT_OptionsHolder& dict,
319  UT_WorkBuffer& errors);
320 
321  /// Creates a JSON patch from changed attributes and outputs on the work
322  /// item
323  static UT_StringHolder createJSONPatch(
324  const PDG_WorkItem* work_item,
325  bool pretty_print);
326 
327  /// Updates work item outputs and attributes from the patch stored in
328  /// the json map
329  static bool applyJSONPatch(
330  PDG_WorkItem* work_item,
331  const UT_JSONValueMap& patch_map,
332  UT_WorkBuffer& errors);
333 
334  /// Loads a checkpoint .json file
335  static bool loadCheckpoint(
336  PDG_GraphContext* context,
337  const UT_StringHolder& file_path,
338  UT_WorkBuffer& errors);
339 
340 private:
341  /// Fields for a dependency pair that was serialized to JSON
342  struct SerializedDependency
343  {
344  PDG_WorkItemID myTo;
345  PDG_WorkItemID myFrom;
346  bool myRequired;
347  bool myUserDefined;
348  };
349 
350  /// Populates this object from the json parser object
351  bool initializeJSONParser(
352  UT_AutoJSONParser& parser,
353  UT_WorkBuffer& errors);
354 
355  /// Populates this object from the json map, using the V1 format
356  bool initializeJSONV1(
357  const UT_JSONValueMap& item_map,
358  UT_WorkBuffer& errors);
359 
360  /// Populates this object from the json map, using the V2 format
361  bool initializeJSONV2(
362  const UT_JSONValueMap& item_map,
363  UT_WorkBuffer& errors);
364 
365  /// Sets the contents of the serialization object by loading the fields
366  /// from JSON
367  bool initializeJSONValue(
368  const UT_JSONValue& value,
369  bool validate,
370  UT_WorkBuffer& errors);
371 
372  /// Sets the contents of the serialization object by loading the fields
373  /// from a dict
374  bool initializeDict(
375  const UT_OptionsHolder& dict,
376  bool validate,
377  UT_WorkBuffer& errors);
378 
379  /// Initializes the attribute table in this serialization objeect based
380  /// on the attributes found in the JSON graph section
381  bool loadGraphJSONValue(
382  const UT_JSONValue& value,
383  UT_WorkBuffer& errors);
384 
385  /// Initializes the attribute table in this serialization object based
386  /// on the attributes found in the dict graph section
387  bool loadGraphDict(
388  const UT_OptionsHolder& value,
389  UT_WorkBuffer& errors);
390 
391  /// Internal wrapper that configures the JSON writer based on options,
392  /// and saves the specified work item
393  static bool saveJSON(
394  UT_JSONWriter& writer,
395  const PDG_WorkItem* work_item,
396  const PDG_SerializationOptions& options,
397  CheckpointType checkpoint);
398 
399  /// Internal wrapper that configures the JSON writer based on options,
400  /// save saves the entire list of work items to an array
401  static bool saveArrayJSON(
402  UT_JSONWriter& writer,
403  const PDG_WorkItemArray& work_items,
404  const PDG_SerializationOptions& options,
405  CheckpointType checkpoint);
406 
407  /// Internal wrapper that configures the JSON writer based on options,
408  /// and saves a dependency pair
409  static bool saveDependencyJSON(
410  UT_JSONWriter& writer,
411  const PDG_WorkItem* from,
412  const PDG_WorkItem* to,
413  bool required,
414  bool user_defined,
415  const PDG_SerializationOptions& options,
416  CheckpointType checkpoint);
417 
418  /// Internal wrapper that configures the JSON writer based on options,
419  /// and saves the data version
420  static bool saveVersion(
421  UT_JSONWriter& writer,
422  const PDG_SerializationOptions& options);
423 
424  /// Internal implementation of JSON saving. Writes a work item to the
425  /// specified writer, and writes batch sub items if the recurse flag is
426  /// set to true.
427  static bool saveJSONWorkItem(
428  UT_JSONWriter& writer,
429  const PDG_WorkItem* work_item,
430  const PDG_SerializationOptions& options,
431  CheckpointType checkpoint);
432 
433  /// Internal implementation of UT_Options saving. Writes a work item to the
434  /// specified options, and writes batch sub items if the recurse flag is
435  /// set to true.
436  static bool saveDictWorkItem(
437  UT_OptionsHolder& dict,
438  const PDG_WorkItem* work_item,
439  const PDG_SerializationOptions& options,
440  CheckpointType checkpoint);
441 
442  /// Saves attributes from a PDG_Graph to the specified writer
443  static bool saveJSONGraph(
444  UT_JSONWriter& writer,
445  const PDG_Graph* graph,
446  const PDG_SerializationOptions& options,
447  CheckpointType checkpoint);
448 
449  /// Saves attributes from a PDG_Graph to the specified UT_Options
450  static bool saveDictGraph(
451  UT_OptionsHolder& dict,
452  const PDG_Graph* graph,
453  const PDG_SerializationOptions& options,
454  CheckpointType checkpoint);
455 
456  /// Internal implementation of the fromJSON and fromDict public interface
457  static PDG_WorkItem* fromSerialization(
458  const PDG_WorkItemSerialization& serialization,
459  PDG_GraphContext* context,
460  Map& compat_map,
461  bool make_active,
462  UT_WorkBuffer& errors);
463 
464  /// Internal implementation of the array-based fromJSON method
465  static bool fromArrayJSONParser(
466  Array& serialization_array,
467  UT_AutoJSONParser& parser,
468  UT_WorkBuffer& errors);
469 
470  /// Internal implementation that interprets a JSONValue as an array
471  /// of work items
472  static bool fromArrayJSONValue(
473  Array& serialization_array,
474  const UT_JSONValue& value,
475  int version_num,
476  UT_WorkBuffer& errors);
477 
478  /// Internal implementation of the statJSON method that uses a JSON
479  /// parser to check for version, graph and item array fields.
480  static bool statJSONParser(
481  Stat& json_stat,
482  UT_AutoJSONParser& parser,
483  UT_WorkBuffer& errors);
484 
485  /// Loads a serialized dependency pair from a JSON value
486  static bool fromJSONDependency(
487  SerializedDependency& dependency,
488  const UT_JSONValue& value,
489  UT_WorkBuffer& errors);
490 
491  /// Loads checkpoint data from a stream
492  static bool loadCheckpoint(
493  PDG_GraphContext* context,
494  UT_IStream& istream,
495  UT_WorkBuffer& errors);
496 
497  /// Internal logic for creating a work item from this serialization
498  /// object
499  PDG_WorkItem* toWorkItem(
500  PDG_GraphContext* context,
501  PDG_WorkItemID base_id,
502  bool strict,
503  Map& compat_map,
504  bool make_active,
505  PDG_BatchWorkItem* batch_parent,
506  UT_WorkBuffer& errors) const;
507 
508 public:
509  // Full serialization keys
512  static const UT_StringHolder theIdKey;
547 
548  // Attribute patch serialization
552 
553  // Dependency serialization
557 
558  // Section keys
564 
565  // Deprecated json keys, no longer emitted during serialization
566  // but still used when loading olds format files.
581 
582  // Constants for detecting serialization format verison
583 
584  // The current verison
585  static const int theAttribVersionCurrent;
586 
587  // 1 -- The initial version of the PDG serialization format, added in H17.5
588  static const int theAttribV1;
589 
590  // 2 -- The new PDG attribute storage format, added in H18.0.
591  static const int theAttribV2;
592 
593  // 3 -- V2 attribute format, with IDs instead of string names. Compatible
594  // with V2.
595  static const int theAttribV2Ids;
596 
597  // 4 -- V2 attribute format, with work item command stored as an attribute
598  // instead of a separate field. Compatible with V2.
599  static const int theAttribV2Command;
600 
601  // 5 -- V2 attribute format, with the new loop storage format. Compatible
602  // with earlier V2 versions, but loop items will need to recook due to
603  // internal format changes
604  static const int theAttribV2LoopInfo;
605 
606  // 6 -- V2 attribute format, with the new PDG_WorkItemCookType to track
607  // how the work item should execute. Compatible from older version, but
608  // cook type has to be derived from a number of existing fields.
609  static const int theAttribV2CookType;
610 
611  // 7 -- V2 attribute format, with service block loops that can be run using
612  // services. This introduces a new field that locks a loop block to a
613  // particular service
614  static const int theAttribV2LoopLock;
615 
616 public:
619 
625 
627 
629 
639 
642 
645 
650 
652  int myIndex;
660 
664 
674 
680 };
681 
683 
684 #endif /* __PDG_WORKITEM_SERIALIZATION_H__ */
exint PDG_WorkItemID
Type defs for unique work item IDs.
static const UT_StringHolder theParentIdKey
static const UT_StringHolder theDependencySectionKey
PDG_BatchActivation
static const UT_StringHolder theLoopStackKey
static const UT_StringHolder thePatchOutputsKey
static const UT_StringHolder theIsNoGenerateKey
Definition: ImfArray.h:45
static const UT_StringHolder theCloneTargetIdKey
static const UT_StringHolder theGraphSectionKey
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
PDG_WorkItemExecutionType myExecutionType
static const UT_StringHolder thePatchAttributesKey
static const UT_StringHolder theBatchParentIdKey
static const UT_StringHolder theBatchParentKey
static const UT_StringHolder theWorkItemsSectionKey
#define PDG_API
Definition: PDG_API.h:23
Serialize the initial baseline fields for the work item.
static const UT_StringHolder theIsStaticKey
static const UT_StringHolder theIsPostCookKey
static const UT_StringHolder theIsClearEnvKey
static const UT_StringHolder theLoopStackIdsKey
static const UT_StringHolder theCloneTargetKey
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
static const UT_StringHolder theIdKey
static const UT_StringHolder theExpansionKey
static const UT_StringHolder theFrameStepKey
static const UT_StringHolder theDependencyFromKey
static const UT_StringHolder theFrameKey
static const UT_StringHolder theLoopStackItersKey
static const UT_StringHolder theTypeKey
static const UT_StringHolder theExecutionTypeKey
static const UT_StringHolder theIsCloneOutputFilesKey
static const UT_StringHolder theActivationCountKey
static const UT_StringHolder theAncestorsKey
static const UT_StringHolder theDependencyRequiredKey
PDG_WorkItemType
Enum of work item types.
static const UT_StringHolder theBatchNameKey
Definition: core.h:760
static const UT_StringHolder theBatchOffsetKey
GLboolean reset
Definition: glad.h:5138
static const UT_StringHolder theIsInProcessKey
static const UT_StringHolder theVersionSectionKey
PDG_WorkItemState
Enum of possible work item states.
static const UT_StringHolder theStateKey
static const UT_StringHolder theIsDynamicBatchKey
static const UT_StringHolder theIndexKey
static const UT_StringHolder theBatchCountKey
PDG_WorkItemCookType
Enumeration of work item cook types.
static const UT_StringHolder thePriorityKey
static const UT_StringHolder theHasFrameKey
SYS_DECLARE_LEGACY_TR(GU_Detail)
static const UT_StringHolder theLoopStackSizesKey
static const UT_StringHolder thePatchGraphKey
static const UT_StringHolder theCommandKey
Serialize all fields – no checkpointing is in progress.
PDG_AttributeSaveType
Enumeration of the different ways that attributes can be saved.
GT_API const UT_StringHolder version
A map of string to various well defined value types.
Definition: UT_Options.h:84
static const UT_StringHolder theActivationModeKey
static const UT_StringHolder theSubItemsKey
fpreal64 fpreal
Definition: SYS_Types.h:277
static const UT_StringHolder theNameKey
static const UT_StringHolder theLoopStackNumbersKey
static const UT_StringHolder theCustomDataTypeKey
static const UT_StringHolder theCustomDataKey
static const UT_StringHolder theEnvironmentKey
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
PDG_WorkItemExecutionType
Enum of work item runtime types.
static const UT_StringHolder theIsFrozenKey
Definition: core.h:1131
static const UT_StringHolder theSchedulerKey
static const UT_StringHolder theIsPartialCookKey
static const UT_StringHolder theParentKey
static const UT_StringHolder theIsControlKey
static const UT_StringHolder theDependencyToKey
static const UT_StringHolder theWorkItemSectionKey
static const UT_StringHolder theBatchStartKey
static const UT_StringHolder theCookTypeKey
static const UT_StringHolder theBaseNameKey
Definition: format.h:895
static const UT_StringHolder theNodeKey
static const UT_StringHolder theBatchIndexKey
static const UT_StringHolder theAttributesKey
static const UT_StringHolder theLoopStackLockIdsKey