message.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Experimental
  2. var message = message || {};
  3. message.ModelFactory = class {
  4. match(context) {
  5. const obj = context.open('json');
  6. if (obj && obj.signature && obj.signature.startsWith('netron:')) {
  7. return obj.signature;
  8. }
  9. return '';
  10. }
  11. open(context) {
  12. return Promise.resolve().then(() => {
  13. const obj = context.open('json');
  14. return new message.Model(obj);
  15. });
  16. }
  17. };
  18. message.Model = class {
  19. constructor(data) {
  20. this._format = data.format || '';
  21. this._producer = data.producer || '';
  22. this._version = data.version || '';
  23. this._description = data.description || '';
  24. this._metadata = (data.metadata || []).map((entry) => { return { name: entry.name, value: entry.value }; });
  25. this._graphs = (data.graphs || []).map((graph) => new message.Graph(graph));
  26. }
  27. get format() {
  28. return this._format;
  29. }
  30. get producer() {
  31. return this._producer;
  32. }
  33. get version() {
  34. return this._version;
  35. }
  36. get description() {
  37. return this._description;
  38. }
  39. get metadata() {
  40. return this._metadata;
  41. }
  42. get graphs() {
  43. return this._graphs;
  44. }
  45. };
  46. message.Graph = class {
  47. constructor(data) {
  48. this._nodes = (data.nodes || []).map((node) => new message.Node(node));
  49. this._inputs = (data.inputs || []).map((input) => new message.Parameter(input));
  50. this._outputs = (data.outputs || []).map((output) => new message.Parameter(output));
  51. }
  52. get inputs() {
  53. return this._inputs;
  54. }
  55. get outputs() {
  56. return this._outputs;
  57. }
  58. get nodes() {
  59. return this._nodes;
  60. }
  61. };
  62. message.Parameter = class {
  63. constructor(data) {
  64. this._name = data.name;
  65. this._arguments = (data.arguments || []).map((argument) => new message.Argument(argument));
  66. }
  67. get name() {
  68. return this._name;
  69. }
  70. get arguments() {
  71. return this._arguments;
  72. }
  73. get visible() {
  74. return true;
  75. }
  76. };
  77. message.Argument = class {
  78. constructor(data) {
  79. this._name= data.name || '';
  80. this._type = data.type ? new message.TensorType(data.type) : null;
  81. this._initializer = data.initializer ? new message.Tensor(data.initializer) : null;
  82. }
  83. get name() {
  84. return this._name;
  85. }
  86. get type() {
  87. if (this._initializer) {
  88. return this._initializer.type;
  89. }
  90. return this._type;
  91. }
  92. set type(value) {
  93. this._type = value;
  94. }
  95. get initializer() {
  96. return this._initializer;
  97. }
  98. };
  99. message.Node = class {
  100. constructor(data) {
  101. this._type = { name: data.type.name, category: data.type.category };
  102. this._name = data.name;
  103. this._inputs = (data.inputs || []).map((input) => new message.Parameter(input));
  104. this._outputs = (data.outputs || []).map((output) => new message.Parameter(output));
  105. this._attributes = (data.attributes || []).map((attribute) => new message.Attribute(attribute));
  106. }
  107. get type() {
  108. return this._type;
  109. }
  110. get name() {
  111. return this._name;
  112. }
  113. get inputs() {
  114. return this._inputs;
  115. }
  116. get outputs() {
  117. return this._outputs;
  118. }
  119. get attributes() {
  120. return this._attributes;
  121. }
  122. };
  123. message.Attribute = class {
  124. constructor(attribute) {
  125. this._name = attribute.name;
  126. this._value = attribute.value;
  127. }
  128. get name() {
  129. return this._name;
  130. }
  131. get value() {
  132. return this._value;
  133. }
  134. get type() {
  135. return this._type;
  136. }
  137. };
  138. message.Error = class extends Error {
  139. constructor(message) {
  140. super(message);
  141. this.name = 'Message Error';
  142. }
  143. };
  144. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  145. module.exports.ModelFactory = message.ModelFactory;
  146. }