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_Types.h"
17 
18 #include <UT/UT_Array.h>
19 #include <UT/UT_StringArray.h>
20 #include <UT/UT_StringMap.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_WorkBuffer;
37 
38 /**
39  * Helper class for loading and storing work items to serialization
40  * formats
41  */
43 {
44 public:
47 
48  /// Returned by the statJSON methods, to provide information about
49  /// the type and number of work items in a .json file or string
50  struct Stat
51  {
52  int myVersion;
54 
55  bool myIsArray;
57  };
58 
59  /// Enumeration of checkpoint save types, which are used to determine
60  /// which set of fields to write to the JSON object for a serialized
61  /// work item
63  {
64  /// Serialize all fields -- no checkpointing is in progress
66 
67  /// Serialize the initial baseline fields for the work item
69 
70  /// Serialize only fields that can be updated dynamically, when the
71  /// work item cooks
72  eCheckpointUpdate
73  };
74 
75 public:
76  /// Constructs an empty, default-initialized serialization object
78 
79  /// Loads work item attributes from a JSON value
80  bool loadAttributes(
81  const UT_JSONValue& value,
83  bool reset);
84 
85  /// Loads work item attributres from a buffer
86  bool loadAttributes(
87  const UT_WorkBuffer& buffer,
89  bool reset);
90 
91  /// Converts the serialization object to a PDG_WorkItem, in the given
92  /// graph context
93  PDG_WorkItem* toWorkItem(
94  PDG_GraphContext* context,
95  PDG_WorkItemID base_id,
96  bool strict,
97  Map& compat_map,
98  UT_WorkBuffer& errors) const;
99 
100  /// Initializes this instance from JSON string data
101  bool initializeString(
102  const UT_StringHolder& data,
103  UT_WorkBuffer& errors);
104 
105  /// Initializes this instance from a stream of JSON data
106  bool initializeStream(
107  UT_IStream& istream,
108  UT_WorkBuffer& errors);
109 
110  /// Initializes this instance from a JSON file
111  bool initializeFile(
112  const UT_StringHolder& file_path,
113  UT_WorkBuffer& errors);
114 
115  /// Returns the work item name. If the serialization object was created
116  /// from the old, name-based format this returns the work item's name
117  /// as listed in the json/python data. Otherwise, this method computes
118  /// the name from the work item's node name and unique ID.
119  UT_StringHolder workItemName() const;
120 
121 
122  /// Saves the specified work item to the buffer, as JSON
123  static bool saveJSON(
125  const PDG_WorkItem* work_item,
126  const PDG_SerializationOptions& options,
127  CheckpointType checkpoint=eCheckpointNone);
128 
129  /// Saves the specified work item to the stream, as JSON
130  static bool saveJSON(
131  std::ostream& ostream,
132  const PDG_WorkItem* work_item,
133  const PDG_SerializationOptions& options,
134  CheckpointType checkpoint=eCheckpointNone);
135 
136  /// Saves the specified work item to the file, as JSON
137  static bool saveJSON(
138  const UT_StringHolder& file_path,
139  const PDG_WorkItem* work_item,
140  const PDG_SerializationOptions& options,
141  CheckpointType checkpoint=eCheckpointNone);
142 
143 
144  /// Saves the specified work item array to the buffer, as JSON
145  static bool saveArrayJSON(
147  const PDG_WorkItemArray& work_items,
148  const PDG_SerializationOptions& options,
149  CheckpointType checkpoint=eCheckpointNone);
150 
151  /// Saves the specified work item array to the stream, as JSON
152  static bool saveArrayJSON(
153  std::ostream& ostream,
154  const PDG_WorkItemArray& work_items,
155  const PDG_SerializationOptions& options,
156  CheckpointType checkpoint=eCheckpointNone);
157 
158  /// Saves the specified work item array to the file, as JSON
159  static bool saveArrayJSON(
160  const UT_StringHolder& file_path,
161  const PDG_WorkItemArray& work_items,
162  const PDG_SerializationOptions& options,
163  CheckpointType checkpoint=eCheckpointNone);
164 
165 
166  /// Saves the specified work item dependency pair to the buffer, as JSON
167  static bool saveDependencyJSON(
169  const PDG_WorkItem* from,
170  const PDG_WorkItem* to,
171  bool required,
172  bool user_defined,
173  const PDG_SerializationOptions& options,
174  CheckpointType checkpoint=eCheckpointNone);
175 
176  /// Saves the specified work item dependency pair to the stream, as JSON
177  static bool saveDependencyJSON(
178  std::ostream& ostream,
179  const PDG_WorkItem* from,
180  const PDG_WorkItem* to,
181  bool required,
182  bool user_defined,
183  const PDG_SerializationOptions& options,
184  CheckpointType checkpoint=eCheckpointNone);
185 
186  /// Saves the specified work item dependency pair to the file, as JSON
187  static bool saveDependencyJSON(
188  const UT_StringHolder& file_path,
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 
197  /// Saves a map with the data version to the buffer, as JSON
198  static bool saveVersion(
200  const PDG_SerializationOptions& options);
201 
202  /// Saves a map with the data version to the stream, as JSON
203  static bool saveVersion(
204  std::ostream& ostream,
205  const PDG_SerializationOptions& options);
206 
207  /// Saves a map with the data version to the file, as JSON
208  static bool saveVersion(
209  const UT_StringHolder& file_path,
210  const PDG_SerializationOptions& options);
211 
212  /// Saves the specified work item to the buffer, as a Python script
213  static bool savePython(
215  const PDG_WorkItem* work_item,
216  const PDG_SerializationOptions& options);
217 
218 
219  /// Loads a work item from a buffer containing JSON string data
220  static PDG_WorkItem* fromJSON(
221  const UT_StringHolder& buffer,
222  PDG_GraphContext* context,
223  Map& compat_map,
224  bool make_active,
225  UT_WorkBuffer& errors);
226 
227  /// Loads a work item from a stream containing JSON data
228  static PDG_WorkItem* fromJSON(
229  UT_IStream& istream,
230  bool make_active,
231  UT_WorkBuffer& errors);
232 
233  /// Loads a work item from a file path
234  static PDG_WorkItem* fromJSON(
235  const UT_StringHolder& file_path,
236  bool make_active,
237  UT_WorkBuffer& errors);
238 
239 
240  /// Loads a work item array from a stream containing JSON data
241  static bool fromArrayJSON(
242  PDG_WorkItemArray& work_items,
243  UT_IStream& istream,
244  PDG_GraphContext* context,
245  bool is_compressed,
246  UT_WorkBuffer& errors);
247 
248 
249  /// Loads a serialization object array from a buffer containing JSON data
250  static bool fromArrayJSON(
251  Array& serialization_array,
252  const UT_StringHolder& buffer,
253  UT_WorkBuffer& errors);
254 
255  /// Loads a serialization object array from a file containing JSON data
256  static bool fromArrayJSON(
257  Array& serialization_array,
258  const UT_StringHolder& file_path,
259  bool is_compressed,
260  UT_WorkBuffer& errors);
261 
262 
263  /// Stats the JSON data in the specified string
264  static bool statJSON(
265  Stat& json_stat,
266  const UT_StringHolder& buffer,
267  UT_WorkBuffer& errors);
268 
269  /// Stats the JSON data in the specified file
270  static bool statJSON(
271  Stat& json_stat,
272  const UT_StringHolder& file_path,
273  bool is_compressed,
274  UT_WorkBuffer& errors);
275 
276  /// Stats the JSON data in the stream
277  static bool statJSON(
278  Stat& json_stat,
279  UT_IStream& istream,
280  bool is_compressed,
281  UT_WorkBuffer& errors);
282 
283 
284  /// Creates a JSON patch from changed attributes and outputs on the work
285  /// item
286  static UT_StringHolder createJSONPatch(
287  const PDG_WorkItem* work_item,
288  bool pretty_print);
289 
290  /// Updates work item outputs and attributes from the patch stored in
291  /// the json map
292  static bool applyJSONPatch(
293  PDG_WorkItem* work_item,
294  const UT_JSONValueMap& patch_map,
295  UT_WorkBuffer& errors);
296 
297  /// Loads a checkpoint .json file
298  static bool loadCheckpoint(
299  PDG_GraphContext* context,
300  const UT_StringHolder& file_path,
301  UT_WorkBuffer& errors);
302 
303 private:
304  /// Fields for a dependency pair that was serialized to JSON
305  struct SerializedDependency
306  {
307  PDG_WorkItemID myTo;
308  PDG_WorkItemID myFrom;
309  bool myRequired;
310  bool myUserDefined;
311  };
312 
313  /// Populates this object from the json parser object
314  bool initialize(
315  UT_AutoJSONParser& parser,
316  UT_WorkBuffer& errors);
317 
318  /// Populates this object from the json map, using the V1 format
319  bool initializeV1(
320  const UT_JSONValueMap& item_map,
321  UT_WorkBuffer& errors);
322 
323  /// Populates this object from the json map, using the V2 format
324  bool initializeV2(
325  const UT_JSONValueMap& item_map,
326  UT_WorkBuffer& errors);
327 
328  /// Sets the contents of the serialization object by loading the fields
329  /// from JSON
330  bool initializeJSONValue(
331  const UT_JSONValue& value,
332  bool validate,
333  UT_WorkBuffer& errors);
334 
335  /// Initializes the attribute table in this serialization objeect based
336  /// on the attributes found in the graph section
337  bool loadGraphJSONValue(
338  const UT_JSONValue& value,
339  UT_WorkBuffer& errors);
340 
341  /// Internal wrapper that configures the JSON writer based on options,
342  /// and saves the specified work item
343  static bool saveJSON(
344  UT_JSONWriter& writer,
345  const PDG_WorkItem* work_item,
346  const PDG_SerializationOptions& options,
347  CheckpointType checkpoint);
348 
349  /// Internal wrapper that configures the JSON writer based on options,
350  /// save saves the entire list of work items to an array
351  static bool saveArrayJSON(
352  UT_JSONWriter& writer,
353  const PDG_WorkItemArray& work_items,
354  const PDG_SerializationOptions& options,
355  CheckpointType checkpoint);
356 
357  /// Internal wrapper that configures the JSON writer based on options,
358  /// and saves a dependency pair
359  static bool saveDependencyJSON(
360  UT_JSONWriter& writer,
361  const PDG_WorkItem* from,
362  const PDG_WorkItem* to,
363  bool required,
364  bool user_defined,
365  const PDG_SerializationOptions& options,
366  CheckpointType checkpoint);
367 
368  /// Internal wrapper that configures the JSON writer based on options,
369  /// and saves the data version
370  static bool saveVersion(
371  UT_JSONWriter& writer,
372  const PDG_SerializationOptions& options);
373 
374  /// Internal implementation of JSON saving. Writes a work item to the
375  /// specified writer, and writes batch sub items if the recurse flag is
376  /// set to true.
377  static bool saveJSONWorkItem(
378  UT_JSONWriter& writer,
379  const PDG_WorkItem* work_item,
380  const PDG_SerializationOptions& options,
381  CheckpointType checkpoint);
382 
383  /// Saves attributes from a PDG_Graph to the specified writer
384  static bool saveJSONGraph(
385  UT_JSONWriter& writer,
386  const PDG_Graph* graph,
387  const PDG_SerializationOptions& options,
388  CheckpointType checkpoint);
389 
390  /// Internal implementation of the fromJSON public interface
391  static PDG_WorkItem* fromJSON(
392  const PDG_WorkItemSerialization& serialization,
393  PDG_GraphContext* context,
394  Map& compat_map,
395  bool make_active,
396  UT_WorkBuffer& errors);
397 
398  /// Internal implementation of the array-based fromJSON method
399  static bool fromArrayJSON(
400  Array& serialization_array,
401  UT_AutoJSONParser& parser,
402  UT_WorkBuffer& errors);
403 
404  /// Internal implementation that interprets a JSONValue as an array
405  /// of work items
406  static bool fromArrayValue(
407  Array& serialization_array,
408  const UT_JSONValue& value,
409  int version_num,
410  UT_WorkBuffer& errors);
411 
412  /// Internal implementation of the statJSON method
413  static bool statJSON(
414  Stat& json_stat,
415  UT_AutoJSONParser& parser,
416  UT_WorkBuffer& errors);
417 
418  /// Loads a serialized dependency pair from a JSON value
419  static bool fromJSONDependency(
420  SerializedDependency& dependency,
421  const UT_JSONValue& value,
422  UT_WorkBuffer& errors);
423 
424  /// Loads checkpoint data from a stream
425  static bool loadCheckpoint(
426  PDG_GraphContext* context,
427  UT_IStream& istream,
428  UT_WorkBuffer& errors);
429 
430  /// Internal logic for creating a work item from this serialization
431  /// object
432  PDG_WorkItem* toWorkItem(
433  PDG_GraphContext* context,
434  PDG_WorkItemID base_id,
435  bool strict,
436  Map& compat_map,
437  bool make_active,
438  PDG_BatchWorkItem* batch_parent,
439  UT_WorkBuffer& errors) const;
440 
441 public:
442  // Full serialization keys
445  static const UT_StringHolder theIdKey;
480 
481  // Attribute patch serialization
485 
486  // Dependency serialization
490 
491  // Section keys
497 
498  // Deprecated json keys, no longer emitted during serialization
499  // but still used when loading olds format files.
514 
515  // Constants for detecting serialization format verison
516 
517  // The current verison
518  static const int theAttribVersionCurrent;
519 
520  // 1 -- The initial version of the PDG serialization format, added in H17.5
521  static const int theAttribV1;
522 
523  // 2 -- The new PDG attribute storage format, added in H18.0.
524  static const int theAttribV2;
525 
526  // 3 -- V2 attribute format, with IDs instead of string names. Compatible
527  // with V2.
528  static const int theAttribV2Ids;
529 
530  // 4 -- V2 attribute format, with work item command stored as an attribute
531  // instead of a separate field. Compatible with V2.
532  static const int theAttribV2Command;
533 
534  // 5 -- V2 attribute format, with the new loop storage format. Compatible
535  // with earlier V2 versions, but loop items will need to recook due to
536  // internal format changes
537  static const int theAttribV2LoopInfo;
538 
539  // 6 -- V2 attribute format, with the new PDG_WorkItemCookType to track
540  // how the work item should execute. Compatible from older version, but
541  // cook type has to be derived from a number of existing fields.
542  static const int theAttribV2CookType;
543 
544  // 7 -- V2 attribute format, with service block loops that can be run using
545  // services. This introduces a new field that locks a loop block to a
546  // particular service
547  static const int theAttribV2LoopLock;
548 
549 public:
552 
558 
560 
562 
572 
575 
578 
583 
585  int myIndex;
593 
597 
607 
613 };
614 
616 
617 #endif /* __PDG_WORKITEM_SERIALIZATION_H__ */
static const UT_StringHolder theParentIdKey
static const UT_StringHolder theDependencySectionKey
static const UT_StringHolder theLoopStackKey
static const UT_StringHolder thePatchOutputsKey
static const UT_StringHolder theIsNoGenerateKey
Definition: ImfArray.h:47
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
PDG_WorkItemCookType
Enumeration of work item cook types.
Definition: PDG_Types.h:486
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
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
PDG_BatchActivation
Definition: PDG_Types.h:106
static const UT_StringHolder theVersionSectionKey
static const UT_StringHolder theStateKey
static const UT_StringHolder theIsDynamicBatchKey
static const UT_StringHolder theIndexKey
PDG_WorkItemExecutionType
Enum of work item runtime types.
Definition: PDG_Types.h:472
static const UT_StringHolder theBatchCountKey
static const UT_StringHolder thePriorityKey
static const UT_StringHolder theHasFrameKey
OPENVDB_API void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
Definition: logging.h:294
PDG_WorkItemState
Enum of possible work item states.
Definition: PDG_Types.h:413
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.
GT_API const UT_StringHolder version
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_WorkItemType
Enum of work item types.
Definition: PDG_Types.h:457
static const UT_StringHolder theIsFrozenKey
SaveType
Save operation to perform.
Definition: core.h:1131
static const UT_StringHolder theSchedulerKey
static const UT_StringHolder theIsPartialCookKey
static const UT_StringHolder theParentKey
static const UT_StringHolder theIsControlKey
exint PDG_WorkItemID
Type defs for unique work item IDs.
Definition: PDG_Types.h:48
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