提交 2a1694f5 编写于 作者: P pah100

update dependcy graph

上级 5e3e9867
......@@ -17,11 +17,6 @@ define(function(require) {
* @private
*/
this._state = {};
/**
* @type {Array}
* @private
*/
this._stack = [];
this.mergeOption();
},
......
......@@ -4,12 +4,6 @@
define(function (require) {
require('../echarts').registerProcessor(function (ecModel) {
var legendModel = ecModel.getComponent('legend');
if (legendModel) {
ecModel.filterSeries(function (series) {
return legendModel.isSelected(series.name);
});
}
});
});
\ No newline at end of file
......@@ -16,16 +16,6 @@ define(function(require) {
*/
var componentModelClasses = {};
/**
* key: conponentType,
* value: {
* predecessor: [conponentTypes...]
* successor: [conponentTypes...]
* }
* @type {Object}
*/
var dependencyGraph = {};
/**
* @alias module:echarts/model/Component
* @constructor
......@@ -63,8 +53,6 @@ define(function(require) {
throw new Error('Component model "' + componentType + '" exists.');
}
componentModelClasses[componentType] = SubComponentModel;
initDepndency(opts);
}
return SubComponentModel;
};
......@@ -86,16 +74,17 @@ define(function(require) {
* @param {Array.<string>} componentTypeList Target Component type list.
* @param {Function} callback Params: componentType, depends.
*/
ComponentModel.topologicalTavel = function (componentTypeList, callback) {
ComponentModel.topologicalTavel = function (componentTypeList, callback, scope) {
if (!componentTypeList.length) {
return;
}
var dependencyGraph = makeDepndencyGraph(componentTypeList);
var stack = [];
var enterCount = [];
var entryCount = [];
zrUtil.each(dependencyGraph, function (vertex, componentType) {
enterCount[componentType] = vertex.predecessor.length;
if (enterCount[componentType] === 0) {
zrUtil.each(componentTypeList, function (componentType) {
entryCount[componentType] = dependencyGraph[componentType].predecessor.length;
if (entryCount[componentType] === 0) {
stack.push(componentType);
}
});
......@@ -107,34 +96,49 @@ define(function(require) {
while (stack.length) {
var currComponentType = stack.pop();
var currVertex = dependencyGraph[currComponentType];
callback(currComponentType, currVertex.predecessor.slice());
callback.call(scope, currComponentType, currVertex.predecessor.slice());
zrUtil.each(currVertex.successor, removeEdge);
}
function removeEdge(succComponentType) {
enterCount[succComponentType]--;
if (enterCount[succComponentType] === 0) {
entryCount[succComponentType]--;
if (entryCount[succComponentType] === 0) {
stack.push(succComponentType);
}
}
};
function initDepndency(opts) {
var currComponentType = opts.type;
var thisItem = createDependencyMapItem(currComponentType);
zrUtil.each(opts.depends || [], function (depComponentType) {
if (zrUtil.indexOf(thisItem.predecessor, depComponentType) < 0) {
thisItem.predecessor.push(depComponentType);
}
var thatItem = createDependencyMapItem(depComponentType);
if (zrUtil.indexOf(thatItem.successor, depComponentType) < 0) {
thatItem.successor.push(currComponentType);
}
/**
* DepndencyGraph: {Object}
* key: conponentType,
* value: {
* predecessor: [conponentTypes...]
* successor: [conponentTypes...]
* }
*/
function makeDepndencyGraph(componentTypeList) {
var dependencyGraph = {};
zrUtil.each(componentTypeList, function (componentType) {
var thisItem = createDependencyGraphItem(dependencyGraph, componentType);
var ModelClass = componentModelClasses[componentType];
var depends = ModelClass.prototype.depends || [];
zrUtil.each(depends, function (depComponentType) {
if (zrUtil.indexOf(thisItem.predecessor, depComponentType) < 0) {
thisItem.predecessor.push(depComponentType);
}
var thatItem = createDependencyGraphItem(dependencyGraph, depComponentType);
if (zrUtil.indexOf(thatItem.successor, depComponentType) < 0) {
thatItem.successor.push(componentType);
}
});
});
return dependencyGraph;
}
function createDependencyMapItem(componentType) {
function createDependencyGraphItem(dependencyGraph, componentType) {
if (!dependencyGraph[componentType]) {
dependencyGraph[componentType] = {predecessor: [], successor: []};
}
......
......@@ -179,7 +179,7 @@ define(function (require) {
}
}
}
});
}, this);
},
/**
......
......@@ -11,5 +11,5 @@ require.config({
name: 'zrender'
}
],
urlArgs: '_v_=' + Math.random()
urlArgs: '_v_=' + +new Date()
});
\ No newline at end of file
......@@ -18,17 +18,21 @@ describe('Component', function() {
}
function xtestCase() {} // jshint ignore:line
testCase('topologicalTavel1', function (ComponentModel) {
testCase('topologicalTavel_base', function (ComponentModel) {
ComponentModel.extend({type: 'm1', depends: ['a1', 'a2']});
ComponentModel.extend({type: 'a1'});
ComponentModel.extend({type: 'a2'});
var result = [];
ComponentModel.topologicalTavel(['m1'], function (componentType, depends) {
ComponentModel.topologicalTavel(['m1', 'a1', 'a2'], function (componentType, depends) {
result.push([componentType, depends]);
});
expect(result).toEqual([['a2', []], ['a1', []], ['m1', ['a1', 'a2']]]);
});
testCase('topologicalTavel2', function (ComponentModel) {
testCase('topologicalTavel_empty', function (ComponentModel) {
ComponentModel.extend({type: 'm1', depends: ['a1', 'a2']});
ComponentModel.extend({type: 'a1'});
ComponentModel.extend({type: 'a2'});
var result = [];
ComponentModel.topologicalTavel([], function (componentType, depends) {
result.push([componentType, depends]);
......@@ -36,15 +40,55 @@ describe('Component', function() {
expect(result).toEqual([]);
});
xtestCase('topologicalTavel3_loop', function (ComponentModel) {
testCase('topologicalTavel_isolate', function (ComponentModel) {
ComponentModel.extend({type: 'a2'});
ComponentModel.extend({type: 'a1'});
ComponentModel.extend({type: 'm1', depends: ['a2']});
var result = [];
ComponentModel.topologicalTavel(['a1', 'a2', 'm1'], function (componentType, depends) {
result.push([componentType, depends]);
});
expect(result).toEqual([['a2', []], ['m1', ['a2']], ['a1', []]]);
});
testCase('topologicalTavel_diamond', function (ComponentModel) {
ComponentModel.extend({type: 'a1', depends: []});
ComponentModel.extend({type: 'a2', depends: ['a1']});
ComponentModel.extend({type: 'a3', depends: ['a1']});
ComponentModel.extend({type: 'm1', depends: ['a2', 'a3']});
var result = [];
ComponentModel.topologicalTavel(['m1', 'a1', 'a2', 'a3'], function (componentType, depends) {
result.push([componentType, depends]);
});
expect(result).toEqual([['a1', []], ['a3', ['a1']], ['a2', ['a1']], ['m1', ['a2', 'a3']]]);
});
testCase('topologicalTavel_loop', function (ComponentModel) {
ComponentModel.extend({type: 'm1', depends: ['a1', 'a2']});
ComponentModel.extend({type: 'm2', depends: ['m1', 'a2']});
ComponentModel.extend({type: 'a1', depends: ['m2', 'a2']});
ComponentModel.extend({type: 'a2'});
expect(function () {
ComponentModel.topologicalTavel(['m1', 'm2', 'a1']);
}).toThrowError(/Circl/);
});
testCase('topologicalTavel_re', function (ComponentModel) {
ComponentModel.extend({type: 'm1', depends: ['a1', 'a2']});
ComponentModel.extend({type: 'a1'});
ComponentModel.extend({type: 'a2'});
var result = [];
ComponentModel.topologicalTavel(['m1', 'm2', 'a1'], function (componentType, depends) {
ComponentModel.topologicalTavel(['m1', 'a1', 'a2'], function (componentType, depends) {
result.push([componentType, depends]);
});
expect(result).toEqual([]);
expect(result).toEqual([['a2', []], ['a1', []], ['m1', ['a1', 'a2']]]);
result = [];
ComponentModel.extend({type: 'm2', depends: ['a1', 'm1']});
ComponentModel.topologicalTavel(['m2', 'm1', 'a1', 'a2'], function (componentType, depends) {
result.push([componentType, depends]);
});
expect(result).toEqual([['a2', []], ['a1', []], ['m1', ['a1', 'a2']], ['m2', ['a1', 'm1']]]);
});
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册