wisemapping-frontend/packages/mindplot/test/playground/lib/SymmetricTestSuite.js

353 lines
15 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
* Copyright [2015] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-09-08 22:45:26 +02:00
const TestSuite = require('./TestSuite').default;
2021-10-03 03:37:42 +02:00
const Mindplot = require('../../../lib/mindplot');
2021-09-02 18:32:23 +02:00
const mindplot = Mindplot();
2021-07-16 16:41:58 +02:00
2021-09-08 22:45:26 +02:00
const SymmetricTestSuite = new Class({
Extends: TestSuite,
initialize: function () {
$('#symmetricTest').css('display', 'block');
2021-07-16 16:41:58 +02:00
this.testSymmetry();
this.testSymmetricPredict();
this.testSymmetricDragPredict();
},
2021-09-08 22:45:26 +02:00
testSymmetry: function () {
console.log('testSymmetry:');
var position = { x: 0, y: 0 };
var manager = new mindplot.layout.LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, TestSuite.NODE_SIZE, position);
manager.addNode(2, TestSuite.NODE_SIZE, position);
manager.addNode(3, TestSuite.NODE_SIZE, position);
manager.addNode(4, TestSuite.NODE_SIZE, position);
manager.addNode(5, TestSuite.NODE_SIZE, position);
manager.addNode(6, TestSuite.NODE_SIZE, position);
manager.addNode(7, TestSuite.NODE_SIZE, position);
manager.addNode(8, TestSuite.NODE_SIZE, position);
manager.addNode(9, TestSuite.NODE_SIZE, position);
manager.addNode(10, TestSuite.NODE_SIZE, position);
manager.addNode(11, TestSuite.NODE_SIZE, position);
manager.addNode(12, TestSuite.NODE_SIZE, position);
manager.addNode(13, TestSuite.NODE_SIZE, position);
manager.addNode(14, TestSuite.NODE_SIZE, position);
2021-07-16 16:41:58 +02:00
manager.connectNode(0, 14, 0);
manager.connectNode(14, 13, 0);
manager.connectNode(13, 1, 0);
manager.connectNode(13, 2, 1);
manager.connectNode(13, 3, 2);
manager.connectNode(13, 4, 3);
manager.connectNode(13, 5, 4);
manager.connectNode(1, 6, 0);
manager.connectNode(1, 7, 1);
manager.connectNode(7, 8, 0);
manager.connectNode(8, 9, 0);
manager.connectNode(5, 10, 0);
manager.connectNode(6, 11, 0);
manager.connectNode(6, 12, 1);
manager.layout();
2021-09-08 22:45:26 +02:00
manager.plot('testSymmetry', { width: 1600, height: 400 });
2021-07-16 16:41:58 +02:00
// All nodes should be positioned symmetrically with respect to their common ancestors
2021-09-08 22:45:26 +02:00
$assert(
manager.find(14).getPosition().y == manager.find(13).getPosition().y,
'Symmetry is not respected'
);
$assert(
manager.find(5).getPosition().y == manager.find(10).getPosition().y,
'Symmetry is not respected'
);
$assert(
manager.find(11).getPosition().y - manager.find(6).getPosition().y ==
-(manager.find(12).getPosition().y - manager.find(6).getPosition().y),
'Symmetry is not respected'
);
$assert(
manager.find(8).getPosition().y - manager.find(1).getPosition().y ==
-(manager.find(11).getPosition().y - manager.find(1).getPosition().y),
'Symmetry is not respected'
);
$assert(
manager.find(9).getPosition().y - manager.find(1).getPosition().y ==
-(manager.find(11).getPosition().y - manager.find(1).getPosition().y),
'Symmetry is not respected'
);
console.log('OK!\n\n');
2021-07-16 16:41:58 +02:00
},
2021-09-08 22:45:26 +02:00
testSymmetricPredict: function () {
console.log('testSymmetricPredict:');
var position = { x: 0, y: 0 };
var manager = new mindplot.layout.LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
2021-07-16 16:41:58 +02:00
// Prepare a sample graph ...
2021-09-08 22:45:26 +02:00
manager.addNode(1, TestSuite.NODE_SIZE, position);
manager.addNode(2, TestSuite.NODE_SIZE, position);
manager.addNode(3, TestSuite.NODE_SIZE, position);
manager.addNode(4, TestSuite.NODE_SIZE, position);
manager.addNode(5, TestSuite.NODE_SIZE, position);
manager.addNode(6, TestSuite.NODE_SIZE, position);
manager.addNode(7, TestSuite.NODE_SIZE, position);
manager.addNode(8, TestSuite.NODE_SIZE, position);
manager.addNode(9, TestSuite.NODE_SIZE, position);
manager.addNode(10, TestSuite.NODE_SIZE, position);
manager.addNode(11, TestSuite.NODE_SIZE, position);
2021-07-16 16:41:58 +02:00
manager.connectNode(0, 1, 0);
manager.connectNode(0, 2, 1);
manager.connectNode(0, 3, 2);
manager.connectNode(3, 4, 0);
manager.connectNode(3, 5, 1);
manager.connectNode(3, 6, 2);
manager.connectNode(5, 7, 0);
manager.connectNode(5, 8, 1);
manager.connectNode(5, 11, 2);
manager.connectNode(2, 9, 0);
manager.connectNode(2, 10, 1);
manager.layout();
// Graph
2021-09-08 22:45:26 +02:00
var graph1 = manager.plot('testSymmetricPredict1', { width: 1000, height: 400 });
2021-07-16 16:41:58 +02:00
2021-09-08 22:45:26 +02:00
console.log('\tAdded as child of node 9 and dropped at (-280, 45):');
var prediction1a = manager.predict(9, null, { x: -280, y: 45 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph1, prediction1a);
2021-09-08 22:45:26 +02:00
$assert(
prediction1a.position.x < manager.find(9).getPosition().x &&
prediction1a.position.y == manager.find(9).getPosition().y,
'Prediction incorrectly positioned'
);
$assert(prediction1a.order == 0, 'Prediction order should be 0');
console.log('\tAdded as child of node 1 and dropped at (155, -90):');
var prediction1b = manager.predict(1, null, { x: -155, y: -90 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph1, prediction1b);
2021-09-08 22:45:26 +02:00
$assert(
prediction1b.position.x > manager.find(1).getPosition().x &&
prediction1b.position.y == manager.find(1).getPosition().y,
'Prediction is incorrectly positioned'
);
$assert(prediction1b.order == 0, 'Prediction order should be 0');
2021-07-16 16:41:58 +02:00
// Graph 2
2021-09-08 22:45:26 +02:00
var graph2 = manager.plot('testSymmetricPredict2', { width: 1000, height: 400 });
2021-07-16 16:41:58 +02:00
2021-09-08 22:45:26 +02:00
console.log('\tAdded as child of node 5 and dropped at (380, -30):');
var prediction2d = manager.predict(5, null, { x: 380, y: -30 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph2, prediction2d);
2021-09-02 18:32:23 +02:00
// Prediction calculator error
2021-09-08 22:45:26 +02:00
$assert(
prediction2d.position.y < manager.find(7).getPosition().y &&
prediction2d.position.x == manager.find(7).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction2d.order == 0, 'Prediction order should be 0');
console.log('\tAdded as child of node 5 and dropped at (375, 15):');
var prediction2a = manager.predict(5, null, { x: 375, y: 15 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph2, prediction2a);
2021-09-02 18:32:23 +02:00
2021-09-08 22:45:26 +02:00
$assert(
prediction2a.position.y > manager.find(7).getPosition().y &&
prediction2a.position.y < manager.find(8).getPosition().y &&
prediction2a.position.x == manager.find(7).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction2a.order == 1, 'Prediction order should be 1');
console.log('\tAdded as child of node 5 and dropped at (375, 45):');
var prediction2b = manager.predict(5, null, { x: 375, y: 45 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph2, prediction2b);
2021-09-08 22:45:26 +02:00
$assert(
prediction2b.position.y > manager.find(8).getPosition().y &&
prediction2b.position.y < manager.find(11).getPosition().y &&
prediction2b.position.x == manager.find(7).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction2b.order == 2, 'Prediction order should be 2');
console.log('\tAdded as child of node 5 and dropped at (375, 45):');
var prediction2c = manager.predict(5, null, { x: 375, y: 65 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph2, prediction2c);
2021-09-08 22:45:26 +02:00
$assert(
prediction2c.position.y > manager.find(11).getPosition().y &&
prediction2c.position.x == manager.find(11).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction2c.order == 3, 'Prediction order should be 3');
2021-07-16 16:41:58 +02:00
// Graph 3
2021-09-08 22:45:26 +02:00
var graph3 = manager.plot('testSymmetricPredict3', { width: 1000, height: 400 });
2021-07-16 16:41:58 +02:00
2021-09-08 22:45:26 +02:00
console.log('\tAdded as child of node 3 and dropped at (280, 45):');
var prediction3a = manager.predict(3, null, { x: 280, y: 45 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph3, prediction3a);
2021-09-08 22:45:26 +02:00
$assert(
prediction3a.position.y > manager.find(5).getPosition().y &&
prediction3a.position.y < manager.find(6).getPosition().y &&
prediction3a.position.x == manager.find(5).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction3a.order == 2, 'Prediction order should be 2');
console.log('\tAdded as child of node 3 and dropped at (255, 110):');
var prediction3b = manager.predict(3, null, { x: 255, y: 110 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph3, prediction3b);
2021-09-08 22:45:26 +02:00
$assert(
prediction3b.position.y > manager.find(6).getPosition().y &&
prediction3b.position.x == manager.find(6).getPosition().x,
'Prediction incorrectly positioned'
);
$assert(prediction3b.order == 3, 'Prediction order should be 3');
2021-07-16 16:41:58 +02:00
// Graph 4
2021-09-08 22:45:26 +02:00
console.log('\tAdded as child of node 2 and dropped at (-260, 0):');
var graph4 = manager.plot('testSymmetricPredict4', { width: 1000, height: 400 });
var prediction4 = manager.predict(2, null, { x: -260, y: 0 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph4, prediction4);
2021-09-08 22:45:26 +02:00
$assert(
prediction4.position.y > manager.find(9).getPosition().y &&
prediction4.position.y < manager.find(10).getPosition().y &&
prediction4.position.x == manager.find(9).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction4.order == 1, 'Prediction order should be 1');
2021-07-16 16:41:58 +02:00
// Graph 5
2021-09-08 22:45:26 +02:00
console.log('\tPredict nodes added with no position:');
var graph5 = manager.plot('testSymmetricPredict5', { width: 1000, height: 400 });
2021-07-16 16:41:58 +02:00
var prediction5a = manager.predict(1, null, null);
this._plotPrediction(graph5, prediction5a);
2021-09-08 22:45:26 +02:00
$assert(
prediction5a.position.y == manager.find(1).getPosition().y &&
prediction5a.position.x > manager.find(1).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction5a.order == 0, 'Prediction order should be 0');
2021-07-16 16:41:58 +02:00
var prediction5b = manager.predict(2, null, null);
this._plotPrediction(graph5, prediction5b);
2021-09-08 22:45:26 +02:00
$assert(
prediction5b.position.y > manager.find(10).getPosition().y &&
prediction5b.position.x < manager.find(2).getPosition().x &&
prediction5b.position.x == manager.find(10).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction5b.order == 2, 'Prediction order should be 2');
2021-07-16 16:41:58 +02:00
var prediction5c = manager.predict(3, null, null);
this._plotPrediction(graph5, prediction5c);
2021-09-08 22:45:26 +02:00
$assert(
prediction5c.position.y > manager.find(6).getPosition().y &&
prediction5c.position.x > manager.find(3).getPosition().x &&
prediction5c.position.x == manager.find(6).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction5c.order == 3, 'Prediction order should be 3');
2021-07-16 16:41:58 +02:00
var prediction5d = manager.predict(10, null, null);
this._plotPrediction(graph5, prediction5d);
2021-09-08 22:45:26 +02:00
$assert(
prediction5d.position.y == manager.find(10).getPosition().y &&
prediction5d.position.x < manager.find(10).getPosition().x,
'Prediction is incorrectly positioned'
);
$assert(prediction5d.order == 0, 'Prediction order should be 0');
console.log('OK!\n\n');
2021-07-16 16:41:58 +02:00
},
2021-09-08 22:45:26 +02:00
testSymmetricDragPredict: function () {
console.log('testSymmetricDragPredict:');
var position = { x: 0, y: 0 };
var manager = new mindplot.layout.LayoutManager(0, TestSuite.ROOT_NODE_SIZE);
2021-09-02 18:32:23 +02:00
2021-09-08 22:45:26 +02:00
manager.addNode(1, TestSuite.NODE_SIZE, position).connectNode(0, 1, 1);
manager.addNode(2, TestSuite.NODE_SIZE, position).connectNode(1, 2, 0);
2021-07-16 16:41:58 +02:00
manager.layout();
// Graph 1
2021-09-08 22:45:26 +02:00
var graph1 = manager.plot('testSymmetricDragPredict1', { width: 1000, height: 400 });
2021-07-16 16:41:58 +02:00
2021-09-08 22:45:26 +02:00
var prediction1a = manager.predict(1, 2, { x: -250, y: -20 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph1, prediction1a);
2021-09-08 22:45:26 +02:00
$assert(
prediction1a.position.x == manager.find(2).getPosition().x &&
prediction1a.position.y == manager.find(2).getPosition().y,
'Prediction position should be the same as node 2'
);
$assert(
prediction1a.order == manager.find(2).getOrder(),
'Predicition order should be the same as node 2'
);
var prediction1b = manager.predict(1, 2, { x: -250, y: 20 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph1, prediction1b);
2021-09-08 22:45:26 +02:00
$assert(
prediction1b.position.x == manager.find(2).getPosition().x &&
prediction1b.position.y == manager.find(2).getPosition().y,
'Prediction position should be the same as node 2'
);
$assert(
prediction1b.order == manager.find(2).getOrder(),
'Predicition order should be the same as node 2'
);
var prediction1c = manager.predict(0, 2, { x: -100, y: -20 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph1, prediction1c);
2021-09-08 22:45:26 +02:00
$assert(
prediction1c.position.x == manager.find(1).getPosition().x &&
prediction1c.position.y < manager.find(1).getPosition().y,
'Prediction is incorrectly positioned'
);
$assert(prediction1c.order == 1, 'Prediction order should be 1');
var prediction1d = manager.predict(0, 2, { x: -100, y: 20 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph1, prediction1d);
2021-09-08 22:45:26 +02:00
$assert(
prediction1d.position.x == manager.find(1).getPosition().x &&
prediction1d.position.y > manager.find(1).getPosition().y,
'Prediction is incorrectly positioned'
);
$assert(prediction1d.order == 3, 'Prediction order should be 3');
var prediction1e = manager.predict(1, 2, { x: -250, y: 0 });
2021-07-16 16:41:58 +02:00
this._plotPrediction(graph1, prediction1e);
2021-09-08 22:45:26 +02:00
$assert(
prediction1e.position.x == manager.find(2).getPosition().x &&
prediction1e.position.y == manager.find(2).getPosition().y,
'Prediction position should be the same as node 2'
);
$assert(
prediction1e.order == manager.find(2).getOrder(),
'Predicition order should be the same as node 2'
);
console.log('OK!\n\n');
},
});
2021-07-16 16:41:58 +02:00
2021-09-08 22:45:26 +02:00
export default SymmetricTestSuite;