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  static bool saveArrayJSON(
165  const PDG_WorkItemPtrArray& work_items,
166  const PDG_SerializationOptions& options,
167  CheckpointType checkpoint=eCheckpointNone);
168 
169  /// Saves the specified work item array to the stream, as JSON
170  static bool saveArrayJSON(
171  std::ostream& ostream,
172  const PDG_WorkItemArray& work_items,
173  const PDG_SerializationOptions& options,
174  CheckpointType checkpoint=eCheckpointNone);
175  static bool saveArrayJSON(
176  std::ostream& ostream,
177  const PDG_WorkItemPtrArray& work_items,
178  const PDG_SerializationOptions& options,
179  CheckpointType checkpoint=eCheckpointNone);
180 
181  /// Saves the specified work item array to the file, as JSON
182  static bool saveArrayJSON(
183  const UT_StringHolder& file_path,
184  const PDG_WorkItemArray& work_items,
185  const PDG_SerializationOptions& options,
186  CheckpointType checkpoint=eCheckpointNone);
187  static bool saveArrayJSON(
188  const UT_StringHolder& file_path,
189  const PDG_WorkItemPtrArray& work_items,
190  const PDG_SerializationOptions& options,
191  CheckpointType checkpoint=eCheckpointNone);
192 
193  /// Saves the specified work item array to UT_Options
194  static bool saveArrayDict(
195  UT_OptionsHolder& dict,
196  const PDG_WorkItemArray& work_items,
197  const PDG_SerializationOptions& options,
198  CheckpointType checkpoint=eCheckpointNone);
199 
200 
201  /// Saves the specified work item dependency pair to the buffer, as JSON
202  static bool saveDependencyJSON(
204  const PDG_WorkItem* from,
205  const PDG_WorkItem* to,
206  bool required,
207  bool user_defined,
208  const PDG_SerializationOptions& options,
209  CheckpointType checkpoint=eCheckpointNone);
210 
211  /// Saves the specified work item dependency pair to the stream, as JSON
212  static bool saveDependencyJSON(
213  std::ostream& ostream,
214  const PDG_WorkItem* from,
215  const PDG_WorkItem* to,
216  bool required,
217  bool user_defined,
218  const PDG_SerializationOptions& options,
219  CheckpointType checkpoint=eCheckpointNone);
220 
221  /// Saves the specified work item dependency pair to the file, as JSON
222  static bool saveDependencyJSON(
223  const UT_StringHolder& file_path,
224  const PDG_WorkItem* from,
225  const PDG_WorkItem* to,
226  bool required,
227  bool user_defined,
228  const PDG_SerializationOptions& options,
229  CheckpointType checkpoint=eCheckpointNone);
230 
231 
232  /// Saves a map with the data version to the buffer, as JSON
233  static bool saveVersion(
235  const PDG_SerializationOptions& options);
236 
237  /// Saves a map with the data version to the stream, as JSON
238  static bool saveVersion(
239  std::ostream& ostream,
240  const PDG_SerializationOptions& options);
241 
242  /// Saves a map with the data version to the file, as JSON
243  static bool saveVersion(
244  const UT_StringHolder& file_path,
245  const PDG_SerializationOptions& options);
246 
247  /// Saves the specified work item to the buffer, as a Python script
248  static bool savePython(
250  const PDG_WorkItem* work_item,
251  const PDG_SerializationOptions& options);
252 
253 
254  /// Loads a work item from a buffer containing JSON string data
255  static PDG_WorkItem* fromJSON(
256  const UT_StringHolder& buffer,
257  PDG_GraphContext* context,
258  Map& compat_map,
259  bool make_active,
260  UT_WorkBuffer& errors);
261 
262  /// Loads a work item from a stream containing JSON data
263  static PDG_WorkItem* fromJSON(
264  UT_IStream& istream,
265  bool make_active,
266  UT_WorkBuffer& errors);
267 
268  /// Loads a work item from a file path
269  static PDG_WorkItem* fromJSON(
270  const UT_StringHolder& file_path,
271  bool make_active,
272  UT_WorkBuffer& errors);
273 
274  /// Loads a work item from a dict
275  static PDG_WorkItem* fromDict(
276  const UT_OptionsHolder& dict,
277  bool make_active,
278  UT_WorkBuffer& errors);
279 
280 
281  /// Loads a work item array from a stream containing JSON data
282  static bool fromArrayJSON(
283  PDG_WorkItemArray& work_items,
284  UT_IStream& istream,
285  PDG_GraphContext* context,
286  bool is_compressed,
287  UT_WorkBuffer& errors);
288 
289 
290  /// Loads a serialization object array from a buffer containing JSON data
291  static bool fromArrayJSON(
292  Array& serialization_array,
293  const UT_StringHolder& buffer,
294  UT_WorkBuffer& errors);
295 
296  /// Loads a serialization object array from a file containing JSON data
297  static bool fromArrayJSON(
298  Array& serialization_array,
299  const UT_StringHolder& file_path,
300  bool is_compressed,
301  UT_WorkBuffer& errors);
302 
303  /// Loads a serialization object array from a dictionary
304  static bool fromArrayDict(
305  Array& serialization_array,
306  const UT_OptionsHolder& dict,
307  UT_WorkBuffer& errors);
308 
309 
310  /// Stats the JSON data in the specified string
311  static bool statJSON(
312  Stat& json_stat,
313  const UT_StringHolder& buffer,
314  UT_WorkBuffer& errors);
315 
316  /// Stats the JSON data in the specified file
317  static bool statJSON(
318  Stat& json_stat,
319  const UT_StringHolder& file_path,
320  bool is_compressed,
321  UT_WorkBuffer& errors);
322 
323  /// Stats the JSON data in the stream
324  static bool statJSON(
325  Stat& json_stat,
326  UT_IStream& istream,
327  bool is_compressed,
328  UT_WorkBuffer& errors);
329 
330  /// Stats the dictionary data
331  static bool statDict(
332  Stat& dict_stat,
333  const UT_OptionsHolder& dict,
334  UT_WorkBuffer& errors);
335 
336  /// Creates a JSON patch from changed attributes and outputs on the work
337  /// item
338  static UT_StringHolder createJSONPatch(
339  const PDG_WorkItem* work_item,
340  bool pretty_print);
341 
342  /// Updates work item outputs and attributes from the patch stored in
343  /// the json map
344  static bool applyJSONPatch(
345  PDG_WorkItem* work_item,
346  const UT_JSONValueMap& patch_map,
347  UT_WorkBuffer& errors);
348 
349  /// Loads a checkpoint .json file
350  static bool loadCheckpoint(
351  PDG_GraphContext* context,
352  const UT_StringHolder& file_path,
353  UT_WorkBuffer& errors);
354 
355 private:
356  /// Fields for a dependency pair that was serialized to JSON
357  struct SerializedDependency
358  {
359  PDG_WorkItemID myTo;
360  PDG_WorkItemID myFrom;
361  bool myRequired;
362  bool myUserDefined;
363  };
364 
365  /// Populates this object from the json parser object
366  bool initializeJSONParser(
367  UT_AutoJSONParser& parser,
368  UT_WorkBuffer& errors);
369 
370  /// Populates this object from the json map, using the V1 format
371  bool initializeJSONV1(
372  const UT_JSONValueMap& item_map,
373  UT_WorkBuffer& errors);
374 
375  /// Populates this object from the json map, using the V2 format
376  bool initializeJSONV2(
377  const UT_JSONValueMap& item_map,
378  UT_WorkBuffer& errors);
379 
380  /// Sets the contents of the serialization object by loading the fields
381  /// from JSON
382  bool initializeJSONValue(
383  const UT_JSONValue& value,
384  bool validate,
385  UT_WorkBuffer& errors);
386 
387  /// Sets the contents of the serialization object by loading the fields
388  /// from a dict
389  bool initializeDict(
390  const UT_OptionsHolder& dict,
391  bool validate,
392  UT_WorkBuffer& errors);
393 
394  /// Initializes the attribute table in this serialization objeect based
395  /// on the attributes found in the JSON graph section
396  bool loadGraphJSONValue(
397  const UT_JSONValue& value,
398  UT_WorkBuffer& errors);
399 
400  /// Initializes the attribute table in this serialization object based
401  /// on the attributes found in the dict graph section
402  bool loadGraphDict(
403  const UT_OptionsHolder& value,
404  UT_WorkBuffer& errors);
405 
406  /// Internal wrapper that configures the JSON writer based on options,
407  /// and saves the specified work item
408  static bool saveJSON(
409  UT_JSONWriter& writer,
410  const PDG_WorkItem* work_item,
411  const PDG_SerializationOptions& options,
412  CheckpointType checkpoint);
413 
414  /// Internal wrapper that configures the JSON writer based on options,
415  /// save saves the entire list of work items to an array
416  static bool saveArrayJSON(
417  UT_JSONWriter& writer,
418  const PDG_WorkItemArray& work_items,
419  const PDG_SerializationOptions& options,
420  CheckpointType checkpoint);
421  static bool saveArrayJSON(
422  UT_JSONWriter& writer,
423  const PDG_WorkItemPtrArray& work_items,
424  const PDG_SerializationOptions& options,
425  CheckpointType checkpoint);
426 
427  /// Internal wrapper that configures the JSON writer based on options,
428  /// and saves a dependency pair
429  static bool saveDependencyJSON(
430  UT_JSONWriter& writer,
431  const PDG_WorkItem* from,
432  const PDG_WorkItem* to,
433  bool required,
434  bool user_defined,
435  const PDG_SerializationOptions& options,
436  CheckpointType checkpoint);
437 
438  /// Internal wrapper that configures the JSON writer based on options,
439  /// and saves the data version
440  static bool saveVersion(
441  UT_JSONWriter& writer,
442  const PDG_SerializationOptions& options);
443 
444  /// Internal implementation of JSON saving. Writes a work item to the
445  /// specified writer, and writes batch sub items if the recurse flag is
446  /// set to true.
447  static bool saveJSONWorkItem(
448  UT_JSONWriter& writer,
449  const PDG_WorkItem* work_item,
450  const PDG_SerializationOptions& options,
451  CheckpointType checkpoint);
452 
453  /// Internal implementation of UT_Options saving. Writes a work item to the
454  /// specified options, and writes batch sub items if the recurse flag is
455  /// set to true.
456  static bool saveDictWorkItem(
457  UT_OptionsHolder& dict,
458  const PDG_WorkItem* work_item,
459  const PDG_SerializationOptions& options,
460  CheckpointType checkpoint);
461 
462  /// Saves attributes from a PDG_Graph to the specified writer
463  static bool saveJSONGraph(
464  UT_JSONWriter& writer,
465  const PDG_Graph* graph,
466  const PDG_SerializationOptions& options,
467  CheckpointType checkpoint);
468 
469  /// Saves attributes from a PDG_Graph to the specified UT_Options
470  static bool saveDictGraph(
471  UT_OptionsHolder& dict,
472  const PDG_Graph* graph,
473  const PDG_SerializationOptions& options,
474  CheckpointType checkpoint);
475 
476  /// Internal implementation of the fromJSON and fromDict public interface
477  static PDG_WorkItem* fromSerialization(
478  const PDG_WorkItemSerialization& serialization,
479  PDG_GraphContext* context,
480  Map& compat_map,
481  bool make_active,
482  UT_WorkBuffer& errors);
483 
484  /// Internal implementation of the array-based fromJSON method
485  static bool fromArrayJSONParser(
486  Array& serialization_array,
487  UT_AutoJSONParser& parser,
488  UT_WorkBuffer& errors);
489 
490  /// Internal implementation that interprets a JSONValue as an array
491  /// of work items
492  static bool fromArrayJSONValue(
493  Array& serialization_array,
494  const UT_JSONValue& value,
495  int version_num,
496  UT_WorkBuffer& errors);
497 
498  /// Internal implementation of the statJSON method that uses a JSON
499  /// parser to check for version, graph and item array fields.
500  static bool statJSONParser(
501  Stat& json_stat,
502  UT_AutoJSONParser& parser,
503  UT_WorkBuffer& errors);
504 
505  /// Loads a serialized dependency pair from a JSON value
506  static bool fromJSONDependency(
507  SerializedDependency& dependency,
508  const UT_JSONValue& value,
509  UT_WorkBuffer& errors);
510 
511  /// Loads checkpoint data from a stream
512  static bool loadCheckpoint(
513  PDG_GraphContext* context,
514  UT_IStream& istream,
515  UT_WorkBuffer& errors);
516 
517  /// Internal logic for creating a work item from this serialization
518  /// object
519  PDG_WorkItem* toWorkItem(
520  PDG_GraphContext* context,
521  PDG_WorkItemID base_id,
522  bool strict,
523  Map& compat_map,
524  bool make_active,
525  PDG_BatchWorkItem* batch_parent,
526  UT_WorkBuffer& errors) const;
527 
528 public:
529  // Full serialization keys
532  static const UT_StringHolder theIdKey;
567 
568  // Attribute patch serialization
572 
573  // Dependency serialization
577 
578  // Section keys
584 
585  // Deprecated json keys, no longer emitted during serialization
586  // but still used when loading olds format files.
601 
602  // Constants for detecting serialization format verison
603 
604  // The current verison
605  static const int theAttribVersionCurrent;
606 
607  // 1 -- The initial version of the PDG serialization format, added in H17.5
608  static const int theAttribV1;
609 
610  // 2 -- The new PDG attribute storage format, added in H18.0.
611  static const int theAttribV2;
612 
613  // 3 -- V2 attribute format, with IDs instead of string names. Compatible
614  // with V2.
615  static const int theAttribV2Ids;
616 
617  // 4 -- V2 attribute format, with work item command stored as an attribute
618  // instead of a separate field. Compatible with V2.
619  static const int theAttribV2Command;
620 
621  // 5 -- V2 attribute format, with the new loop storage format. Compatible
622  // with earlier V2 versions, but loop items will need to recook due to
623  // internal format changes
624  static const int theAttribV2LoopInfo;
625 
626  // 6 -- V2 attribute format, with the new PDG_WorkItemCookType to track
627  // how the work item should execute. Compatible from older version, but
628  // cook type has to be derived from a number of existing fields.
629  static const int theAttribV2CookType;
630 
631  // 7 -- V2 attribute format, with service block loops that can be run using
632  // services. This introduces a new field that locks a loop block to a
633  // particular service
634  static const int theAttribV2LoopLock;
635 
636 public:
639 
645 
647 
649 
659 
662 
665 
670 
672  int myIndex;
680 
684 
694 
700 };
701 
703 
704 #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
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#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:39
static const UT_StringHolder theIdKey
static const UT_StringHolder theExpansionKey
GLuint buffer
Definition: glcorearb.h:660
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
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:87
static const UT_StringHolder theActivationModeKey
static const UT_StringHolder theSubItemsKey
fpreal64 fpreal
Definition: SYS_Types.h:283
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
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:1821
static const UT_StringHolder theNodeKey
static const UT_StringHolder theBatchIndexKey
static const UT_StringHolder theAttributesKey
static const UT_StringHolder theLoopStackLockIdsKey