@yanqirenshi/d3.ter 0.1.26 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/js/Port.js CHANGED
@@ -26,7 +26,25 @@ var Port = exports["default"] = /*#__PURE__*/function (_Atman) {
26
26
  _classCallCheck(this, Port);
27
27
  _this = _callSuper(this, Port, [type === 'from' ? 'PORT-FROM' : 'PORT-TO', relatihonship_data]);
28
28
  _this._rectum = rectum;
29
- _this._id = relatihonship_data.id + '_' + entity._id; // Idenrifier-Instance
29
+
30
+ // 端の種別(from / to)を混ぜて、両端が同じエンティティの自己結線(再帰)でも
31
+ // ポート id が一意になるようにする。
32
+ //
33
+ // 旧: relationship.id + '_' + entity._id
34
+ // 自己結線だと両端が同じ id になり、(1) entity.ports.items.ht で一方が
35
+ // もう一方を上書きする (2) ポート記号の d3 data join のキー
36
+ // (Painters/PortCardinalities.js・PortOptionalities.js)が重複する。
37
+ //
38
+ // 実測では (2) で記号が落ちることは無かった(重複キーの2件目は enter に
39
+ // なるので両方描かれ、再描画でも 2 個のまま維持される)。
40
+ //
41
+ // それでも一意にするのは、この重複が「自己結線(再帰)を入れたことで
42
+ // 初めて生まれる状態」だから。自己結線が無かったこれまでは from/to が
43
+ // 必ず別エンティティで id は常に一意だった。重複キーに依存した状態を
44
+ // 新機能として持ち込まない。ht も両端を保持できるようになる
45
+ // (現状 ht は読まれていないが、次に参照する人が静かに片方だけ
46
+ // 受け取る罠になる)。
47
+ _this._id = relatihonship_data.id + '_' + type + '_' + entity._id; // Idenrifier-Instance
30
48
 
31
49
  _this._owner = entity;
32
50
  _this._type = type;
@@ -160,11 +178,11 @@ var Port = exports["default"] = /*#__PURE__*/function (_Atman) {
160
178
  if (arguments.length === 1) this._optionality = v;
161
179
  return this._optionality;
162
180
  }
163
- /* **************************************************************** *
164
- * Data manegement
181
+ /* **************************************************************** *
182
+ * Data manegement
165
183
  * **************************************************************** */
166
- /**
167
- * entity と port
184
+ /**
185
+ * entity と port
168
186
  */
169
187
  }, {
170
188
  key: "calLinePoints",
@@ -190,8 +208,8 @@ var Port = exports["default"] = /*#__PURE__*/function (_Atman) {
190
208
  margin: margin
191
209
  });
192
210
  }
193
- /* **************************************************************** *
194
- *
211
+ /* **************************************************************** *
212
+ *
195
213
  * **************************************************************** */
196
214
  }, {
197
215
  key: "addPort2Entity",
package/dist/js/Rectum.js CHANGED
@@ -92,10 +92,10 @@ var Rectum = exports["default"] = /*#__PURE__*/function (_Colon) {
92
92
  var entities = this.entities();
93
93
  return entities.ht[id] || null;
94
94
  }
95
- /**
96
- * クラス(CLASS)を端点に持つリレーションシップの検証。
97
- * クラスは切り出し元(親)としか結線できない。親かどうかは
98
- * 「クラスが持つ識別子(親の個体指定子 R)のマスタを相手も持つか」で判定する。
95
+ /**
96
+ * クラス(CLASS)を端点に持つリレーションシップの検証。
97
+ * クラスは切り出し元(親)としか結線できない。親かどうかは
98
+ * 「クラスが持つ識別子(親の個体指定子 R)のマスタを相手も持つか」で判定する。
99
99
  */
100
100
  }, {
101
101
  key: "assertClassLink",
@@ -115,11 +115,15 @@ var Rectum = exports["default"] = /*#__PURE__*/function (_Colon) {
115
115
  });
116
116
  if (!is_parent) throw new Error('クラス(CLASS)は切り出し元(親)としか結線できません。' + 'relationship id=' + r.id + ', entity id=' + other._id);
117
117
  }
118
- /**
119
- * 対応表/対照表(mapping)の解決と検証。
120
- * relationship.mapping = { entity: id } は、この結線の中点(○)から
121
- * 垂下線でぶら下げる対応表(CORRESPONDENCE)/対照表(COMPARATIVE)を指す
122
- * (参照 tm3-1 §2「対応表(onto-mapping)」の書き方)。
118
+ /**
119
+ * 対応表/対照表/再帰表(mapping)の解決と検証。
120
+ * relationship.mapping = { entity: id } は、この結線の中点(○)から
121
+ * 垂下線でぶら下げる表を指す
122
+ * (参照 tm3-1 §2「対応表(onto-mapping)」の書き方)。
123
+ *
124
+ * 再帰表(RECURSION)は自己結線(from と to が同じモノ)のループから
125
+ * 垂下するものなので、通常の結線に指定された場合は modeling のミスとして
126
+ * 例外にする(assertClassLink と同じく、描画が壊れる前に検出する)。
123
127
  */
124
128
  }, {
125
129
  key: "resolveMapping",
@@ -128,7 +132,8 @@ var Rectum = exports["default"] = /*#__PURE__*/function (_Colon) {
128
132
  var entity = this.getEntity(r.mapping.entity);
129
133
  if (!entity) throw new Error('mapping に指定されたエンティティが見つかりません。' + 'relationship id=' + r.id + ', entity id=' + r.mapping.entity);
130
134
  var type = entity._core.type;
131
- if (type !== 'CORRESPONDENCE' && type !== 'COMPARATIVE') throw new Error('mapping に指定できるのは対応表(CORRESPONDENCE)か' + '対照表(COMPARATIVE)だけです。' + 'relationship id=' + r.id + ', entity id=' + entity._id + ', type=' + type);
135
+ if (type !== 'CORRESPONDENCE' && type !== 'COMPARATIVE' && type !== 'RECURSION') throw new Error('mapping に指定できるのは対応表(CORRESPONDENCE)・' + '対照表(COMPARATIVE)・再帰表(RECURSION)だけです。' + 'relationship id=' + r.id + ', entity id=' + entity._id + ', type=' + type);
136
+ if (type === 'RECURSION' && r.from.entity !== r.to.entity) throw new Error('再帰表(RECURSION)は自己結線(from と to が同じエンティティ)にだけ' + '指定できます。relationship id=' + r.id + ', from=' + r.from.entity + ', to=' + r.to.entity);
132
137
  return entity;
133
138
  }
134
139
  }, {
@@ -234,7 +239,13 @@ var Rectum = exports["default"] = /*#__PURE__*/function (_Colon) {
234
239
  distance: 11
235
240
  }
236
241
  },
237
- relationship: {}
242
+ relationship: {
243
+ // 自己結線(再帰)のループが箱の外へ膨らむ距離。
244
+ // 制御点を各ポートの外向きにこの距離だけ伸ばす。
245
+ loop: {
246
+ distance: 40
247
+ }
248
+ }
238
249
  };
239
250
  }
240
251
  }]);
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@yanqirenshi/d3.ter",
3
- "version": "0.1.26",
3
+ "version": "0.3.0",
4
4
  "author": "yanqirenshi@gmail.com",
5
- "license": "GPL-3.0-or-later",
6
- "description": "See `src/index.html`",
5
+ "license": "MIT",
6
+ "description": "TM (T-shaped ER method) diagram renderer — 10 entity kinds, cardinality/optionality markers, and correspondence / comparative / recursion tables hung from a connector",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
9
9
  "directories": {
@@ -11,18 +11,19 @@
11
11
  "example": "example"
12
12
  },
13
13
  "scripts": {
14
- "transpile": "babel src -d dist --copy-files",
14
+ "transpile": "babel src -d dist --copy-files --delete-dir-on-start",
15
15
  "prepublishOnly": "npm run transpile",
16
16
  "test": "jest"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",
20
- "url": "git+https://github.com/yanqirenshi/D3.TER.git"
20
+ "url": "git+https://github.com/Foolsgolds/Assholes.git",
21
+ "directory": "packages/d3.ter"
21
22
  },
22
23
  "bugs": {
23
- "url": "https://github.com/yanqirenshi/D3.TER/issues"
24
+ "url": "https://github.com/Foolsgolds/Assholes/issues"
24
25
  },
25
- "homepage": "https://github.com/yanqirenshi/D3.TER#readme",
26
+ "homepage": "https://github.com/Foolsgolds/Assholes/tree/main/packages/d3.ter#readme",
26
27
  "peerDependencies": {
27
28
  "react": "^18.0.0 || ^19.0.0",
28
29
  "react-dom": "^18.0.0 || ^19.0.0"
@@ -46,6 +46,15 @@ const DATA = (relationships) => ({
46
46
  ],
47
47
  attributes: [],
48
48
  },
49
+ {
50
+ id: 82000, type: 'RECURSION', name: 'Entity.Entity.再帰表', description: '',
51
+ position: { x: 0, y: 700, z: 0 }, size: { w: 0, h: 0 },
52
+ identifiers: [
53
+ { id: 8200001, identifier: 10000 },
54
+ { id: 8200002, identifier: 10000 },
55
+ ],
56
+ attributes: [],
57
+ },
49
58
  ],
50
59
  relationships,
51
60
  });
@@ -148,8 +157,8 @@ test('mapping resolves the correspondence entity hung from the line', () => {
148
157
  expect(rels[2].mapping()).toBeNull(); // mapping 無し → 垂下なし
149
158
  });
150
159
 
151
- test('mapping to a non-correspondence/comparative entity throws', () => {
152
- // RESOURCE(30001) は対応表/対照表ではない
160
+ test('mapping to a non-correspondence/comparative/recursion entity throws', () => {
161
+ // RESOURCE(30001) は対応表/対照表/再帰表ではない
153
162
  const bad = [{
154
163
  id: 1, type: null,
155
164
  from: { entity: 30000, cardinality: 1, optionality: 0, position: 270 },
@@ -159,7 +168,107 @@ test('mapping to a non-correspondence/comparative entity throws', () => {
159
168
  }];
160
169
 
161
170
  expect(() => new Rectum({}).data(DATA(bad)))
162
- .toThrow(/対応表\(CORRESPONDENCE\)か対照表\(COMPARATIVE\)だけ/);
171
+ .toThrow(/対応表\(CORRESPONDENCE\)・対照表\(COMPARATIVE\)・再帰表\(RECURSION\)だけ/);
172
+ });
173
+
174
+ // issue #73: 再帰(自己結線 + 再帰表の垂下)
175
+ // 本(佐藤正美)の書き方では、モノから出た線がそのモノ自身に戻るループを描き、
176
+ // ループの途中の○から再帰表をぶら下げる。
177
+ const SELF_LINK = (extra={}) => ([{
178
+ id: 10, type: null,
179
+ from: { entity: 30000, cardinality: 3, optionality: 0, position: 270 },
180
+ to: { entity: 30000, cardinality: 3, optionality: 0, position: 0 },
181
+ details: [{ from: { id: 3000001 }, to: { id: 3000001 } }],
182
+ ...extra,
183
+ }]);
184
+
185
+ test('自己結線の両端のポートは id が別になり、ht も両端を保持する', () => {
186
+ const rectum = new Rectum({});
187
+ rectum.data(DATA(SELF_LINK()));
188
+
189
+ const entity = rectum.entities().ht[30000];
190
+ const ports = entity.ports.items.list;
191
+
192
+ // 両端が同じエンティティなので、同じ箱に2つのポートが付く
193
+ expect(ports).toHaveLength(2);
194
+
195
+ // id は端の種別(from / to)で分かれる。
196
+ // 旧実装は relationship.id + entity._id だったため自己結線で重複し、
197
+ // ht では一方がもう一方を上書きしていた。
198
+ const ids = ports.map((p)=> p.id());
199
+ expect(new Set(ids).size).toBe(2);
200
+ expect(ids[0]).toContain('_from_');
201
+ expect(ids[1]).toContain('_to_');
202
+
203
+ expect(Object.keys(entity.ports.items.ht)).toHaveLength(2);
204
+ });
205
+
206
+ test('通常の結線でもポート id は端ごとに別になる', () => {
207
+ const rectum = new Rectum({});
208
+ rectum.data(DATA([{
209
+ id: 1, type: null,
210
+ from: { entity: 30000, cardinality: 1, optionality: 0, position: 270 },
211
+ to: { entity: 30001, cardinality: 1, optionality: 0, position: 90 },
212
+ details: [{ from: { id: 3000001 }, to: { id: 3000101 } }],
213
+ }]));
214
+
215
+ const from_port = rectum.entities().ht[30000].ports.items.list[0];
216
+ const to_port = rectum.entities().ht[30001].ports.items.list[0];
217
+
218
+ expect(from_port.id()).not.toBe(to_port.id());
219
+ });
220
+
221
+ test('mapping に再帰表(RECURSION)を指定できる(自己結線)', () => {
222
+ const rectum = new Rectum({});
223
+ rectum.data(DATA(SELF_LINK({ mapping: { entity: 82000 } })));
224
+
225
+ const rel = rectum._relationships.ht[10];
226
+ expect(rel.mapping()).toBe(rectum.entities().ht[82000]);
227
+ });
228
+
229
+ test('再帰表(RECURSION)を通常の結線に指定すると例外になる', () => {
230
+ // 再帰表は自己結線から垂下するもの。別のモノどうしの結線に付けるのは
231
+ // modeling のミスなので、描画が壊れる前に検出する。
232
+ const bad = [{
233
+ id: 10, type: null,
234
+ from: { entity: 30000, cardinality: 3, optionality: 0, position: 270 },
235
+ to: { entity: 30001, cardinality: 3, optionality: 0, position: 90 },
236
+ details: [{ from: { id: 3000001 }, to: { id: 3000101 } }],
237
+ mapping: { entity: 82000 },
238
+ }];
239
+
240
+ expect(() => new Rectum({}).data(DATA(bad)))
241
+ .toThrow(/再帰表\(RECURSION\)は自己結線/);
242
+ });
243
+
244
+ test('自己結線に対応表(CORRESPONDENCE)を指定するのは従来どおり許可する', () => {
245
+ const rectum = new Rectum({});
246
+
247
+ expect(() => rectum.data(DATA(SELF_LINK({ mapping: { entity: 80000 } }))))
248
+ .not.toThrow();
249
+ });
250
+
251
+ test('同じモノに複数のループ(角度違い)を作れる', () => {
252
+ const rectum = new Rectum({});
253
+ rectum.data(DATA([
254
+ {
255
+ id: 10, type: null,
256
+ from: { entity: 30000, cardinality: 3, optionality: 0, position: 270 },
257
+ to: { entity: 30000, cardinality: 3, optionality: 0, position: 0 },
258
+ details: [{ from: { id: 3000001 }, to: { id: 3000001 } }],
259
+ },
260
+ {
261
+ id: 11, type: null,
262
+ from: { entity: 30000, cardinality: 3, optionality: 0, position: 90 },
263
+ to: { entity: 30000, cardinality: 3, optionality: 0, position: 180 },
264
+ details: [{ from: { id: 3000001 }, to: { id: 3000001 } }],
265
+ },
266
+ ]));
267
+
268
+ const ports = rectum.entities().ht[30000].ports.items.list;
269
+
270
+ expect(ports).toHaveLength(4);
271
+ expect(new Set(ports.map((p)=> p.id())).size).toBe(4);
163
272
  });
164
273
 
165
274
  test('mapping to a missing entity throws', () => {
@@ -43,3 +43,87 @@ describe('mappingGeometry: 垂下線は最寄りの辺の中央に取り付く',
43
43
  expect(g.my).toBe(-250);
44
44
  });
45
45
  });
46
+
47
+ // issue #73: 自己結線(再帰)のループ。
48
+ // 本(佐藤正美)の書き方では、モノから出た線がそのモノ自身に戻るループを描き、
49
+ // ループの途中の○から再帰表をぶら下げる。
50
+ describe('自己結線(再帰)はループとして描く', () => {
51
+ // style() は rectum 経由で引くので、テスト用の parent を噛ませる
52
+ const painter = new Relationships({
53
+ rectum: ()=> ({ style: ()=> ({ relationship: { loop: { distance: 40 } } }) }),
54
+ });
55
+
56
+ // 箱(100,100)-(300,200)。中心は (200,150)
57
+ const owner = { position: { x: 100, y: 100 }, size: { w: 200, h: 100 } };
58
+ const other = { position: { x: 600, y: 100 }, size: { w: 200, h: 100 } };
59
+
60
+ // port の座標は owner 相対。上辺中央(100,-33)と右辺中央(233,50)に付ける
61
+ const selfLink = (mappingEntity=null) => ({
62
+ _id: 'r-self',
63
+ from: { position: ()=> ({ x: 100, y: -33 }), _owner: owner },
64
+ to: { position: ()=> ({ x: 233, y: 50 }), _owner: owner },
65
+ mapping: ()=> mappingEntity,
66
+ });
67
+
68
+ const plainLink = () => ({
69
+ _id: 'r-plain',
70
+ from: { position: ()=> ({ x: 100, y: -33 }), _owner: owner },
71
+ to: { position: ()=> ({ x: -33, y: 50 }), _owner: other },
72
+ mapping: ()=> null,
73
+ });
74
+
75
+ it('isSelfLink は from と to の owner が同じときだけ真', () => {
76
+ expect(painter.isSelfLink(selfLink())).toBe(true);
77
+ expect(painter.isSelfLink(plainLink())).toBe(false);
78
+ });
79
+
80
+ it('ループは3次ベジエで、制御点が箱の外へ伸びる', () => {
81
+ const g = painter.loopGeometry(selfLink());
82
+
83
+ // 端点は port の絶対座標
84
+ expect(g.p0).toEqual({ x: 200, y: 67 }); // (100,100)+(100,-33)
85
+ expect(g.p3).toEqual({ x: 333, y: 150 }); // (100,100)+(233,50)
86
+
87
+ // 制御点は中心(200,150)から見て外向き。上辺の端は上へ、右辺の端は右へ
88
+ expect(g.p1.y).toBeLessThan(g.p0.y);
89
+ expect(g.p1.x).toBe(g.p0.x); // 真上なので x は動かない
90
+ expect(g.p2.x).toBeGreaterThan(g.p3.x);
91
+ expect(g.p2.y).toBe(g.p3.y); // 真右なので y は動かない
92
+
93
+ // 伸ばす距離は style の loop.distance
94
+ expect(g.p0.y - g.p1.y).toBeCloseTo(40, 6);
95
+ expect(g.p2.x - g.p3.x).toBeCloseTo(40, 6);
96
+
97
+ expect(painter.loopPath(selfLink())).toMatch(/^M 200 67 C /);
98
+ });
99
+
100
+ it('junctionPoint は直線なら中点、自己結線ならループの膨らんだ点', () => {
101
+ // 直線: from(200,67) と to(567,150) の中点
102
+ expect(painter.junctionPoint(plainLink())).toEqual({ x: 383.5, y: 108.5 });
103
+
104
+ // 自己結線: 箱の外(右上)へ膨らむので、端点の単純な中点より
105
+ // 右かつ上に寄る
106
+ const j = painter.junctionPoint(selfLink());
107
+ const naive = { x: (200 + 333) / 2, y: (67 + 150) / 2 };
108
+
109
+ expect(j.x).toBeGreaterThan(naive.x);
110
+ expect(j.y).toBeLessThan(naive.y);
111
+
112
+ // ○とラベルが箱の中に入らないこと(箱は x:100-300, y:100-200)。
113
+ // このケースは上辺の外へ抜ける(j = 281.5, 93.5)
114
+ const inside = j.x >= 100 && j.x <= 300 && j.y >= 100 && j.y <= 200;
115
+ expect(inside).toBe(false);
116
+ });
117
+
118
+ it('垂下の○とラベルは同じ点(junctionPoint)に乗る', () => {
119
+ const table = { position: { x: 100, y: 700 }, size: { w: 200, h: 100 } };
120
+ const d = selfLink(table);
121
+
122
+ const j = painter.junctionPoint(d);
123
+ const g = painter.mappingGeometry(d);
124
+
125
+ // mappingGeometry の junction が junctionPoint と一致する
126
+ // (ここがズレると○は正しいのにラベルだけ箱に重なる)
127
+ expect({ x: g.jx, y: g.jy }).toEqual(j);
128
+ });
129
+ });