2019-09-12 12:58:32 -07:00
|
|
|
import { logger } from '../../logger';
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
let relations = [];
|
|
|
|
let classes = {};
|
2015-11-15 15:06:24 +01:00
|
|
|
|
2019-12-06 20:35:22 -08:00
|
|
|
const splitClassNameAndType = function(id) {
|
2019-12-05 12:55:46 -08:00
|
|
|
let genericType = '';
|
|
|
|
let className = id;
|
|
|
|
|
2019-12-06 20:35:22 -08:00
|
|
|
if(id.indexOf('~') > 0) {
|
2019-12-05 12:55:46 -08:00
|
|
|
let split = id.split('~');
|
|
|
|
className = split[0];
|
|
|
|
genericType = split[1];
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:35:22 -08:00
|
|
|
return { className: className, type: genericType };
|
|
|
|
};
|
2019-12-05 12:55:46 -08:00
|
|
|
|
2015-10-26 08:03:30 +01:00
|
|
|
/**
|
2015-12-27 14:18:21 +01:00
|
|
|
* Function called by parser when a node definition has been found.
|
2015-10-26 08:03:30 +01:00
|
|
|
* @param id
|
2019-10-05 12:38:29 +02:00
|
|
|
* @public
|
2015-10-26 08:03:30 +01:00
|
|
|
*/
|
2019-09-12 12:58:32 -07:00
|
|
|
export const addClass = function(id) {
|
2019-12-05 12:55:46 -08:00
|
|
|
let classId = splitClassNameAndType(id);
|
2019-10-05 12:38:29 +02:00
|
|
|
// Only add class if not exists
|
2019-12-05 12:55:46 -08:00
|
|
|
if (typeof classes[classId.className] !== 'undefined') return;
|
2019-10-05 12:38:29 +02:00
|
|
|
|
2019-12-05 12:55:46 -08:00
|
|
|
classes[classId.className] = {
|
|
|
|
id: classId.className,
|
|
|
|
type: classId.type,
|
2019-10-05 12:38:29 +02:00
|
|
|
methods: [],
|
|
|
|
members: [],
|
|
|
|
annotations: []
|
|
|
|
};
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const clear = function() {
|
|
|
|
relations = [];
|
|
|
|
classes = {};
|
|
|
|
};
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const getClass = function(id) {
|
|
|
|
return classes[id];
|
|
|
|
};
|
|
|
|
export const getClasses = function() {
|
|
|
|
return classes;
|
|
|
|
};
|
2015-10-29 07:49:08 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const getRelations = function() {
|
|
|
|
return relations;
|
|
|
|
};
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const addRelation = function(relation) {
|
|
|
|
logger.debug('Adding relation: ' + JSON.stringify(relation));
|
|
|
|
addClass(relation.id1);
|
|
|
|
addClass(relation.id2);
|
2019-12-05 12:55:46 -08:00
|
|
|
|
|
|
|
relation.id1 = splitClassNameAndType(relation.id1).className;
|
|
|
|
relation.id2 = splitClassNameAndType(relation.id2).className;
|
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
relations.push(relation);
|
|
|
|
};
|
2015-10-28 08:12:47 +01:00
|
|
|
|
2019-10-05 12:38:29 +02:00
|
|
|
/**
|
|
|
|
* Adds an annotation to the specified class
|
|
|
|
* Annotations mark special properties of the given type (like 'interface' or 'service')
|
|
|
|
* @param className The class name
|
|
|
|
* @param annotation The name of the annotation without any brackets
|
|
|
|
* @public
|
|
|
|
*/
|
2019-10-04 21:49:02 +02:00
|
|
|
export const addAnnotation = function(className, annotation) {
|
2019-12-05 12:55:46 -08:00
|
|
|
const validatedClassName = splitClassNameAndType(className).className;
|
|
|
|
classes[validatedClassName].annotations.push(annotation);
|
2019-10-04 21:49:02 +02:00
|
|
|
};
|
|
|
|
|
2019-10-05 12:38:29 +02:00
|
|
|
/**
|
|
|
|
* Adds a member to the specified class
|
|
|
|
* @param className The class name
|
|
|
|
* @param member The full name of the member.
|
|
|
|
* If the member is enclosed in <<brackets>> it is treated as an annotation
|
|
|
|
* If the member is ending with a closing bracket ) it is treated as a method
|
|
|
|
* Otherwise the member will be treated as a normal property
|
|
|
|
* @public
|
|
|
|
*/
|
2019-09-12 12:58:32 -07:00
|
|
|
export const addMember = function(className, member) {
|
2019-12-05 12:55:46 -08:00
|
|
|
const validatedClassName = splitClassNameAndType(className).className;
|
|
|
|
const theClass = classes[validatedClassName];
|
|
|
|
|
2018-10-25 21:30:28 +02:00
|
|
|
if (typeof member === 'string') {
|
2019-10-05 12:38:29 +02:00
|
|
|
// Member can contain white spaces, we trim them out
|
2019-10-04 23:49:58 +02:00
|
|
|
const memberString = member.trim();
|
2019-10-05 12:38:29 +02:00
|
|
|
|
2019-10-04 23:49:58 +02:00
|
|
|
if (memberString.startsWith('<<') && memberString.endsWith('>>')) {
|
2019-10-05 12:38:29 +02:00
|
|
|
// Remove leading and trailing brackets
|
2019-10-04 23:49:58 +02:00
|
|
|
theClass.annotations.push(memberString.substring(2, memberString.length - 2));
|
|
|
|
} else if (memberString.endsWith(')')) {
|
|
|
|
theClass.methods.push(memberString);
|
2019-10-09 21:15:17 +02:00
|
|
|
} else if (memberString) {
|
2019-10-04 23:49:58 +02:00
|
|
|
theClass.members.push(memberString);
|
2015-11-01 19:00:14 +01:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-10-28 08:12:47 +01:00
|
|
|
|
2019-10-08 22:57:40 +02:00
|
|
|
export const addMembers = function(className, members) {
|
|
|
|
if (Array.isArray(members)) {
|
|
|
|
members.reverse();
|
|
|
|
members.forEach(member => addMember(className, member));
|
2018-10-25 21:30:28 +02:00
|
|
|
}
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2018-10-25 21:30:28 +02:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const cleanupLabel = function(label) {
|
2017-04-11 22:14:25 +08:00
|
|
|
if (label.substring(0, 1) === ':') {
|
2019-09-12 12:58:32 -07:00
|
|
|
return label.substr(2).trim();
|
2017-04-11 22:14:25 +08:00
|
|
|
} else {
|
2019-09-12 12:58:32 -07:00
|
|
|
return label.trim();
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-11-11 13:16:38 +01:00
|
|
|
|
2017-09-10 21:23:04 +08:00
|
|
|
export const lineType = {
|
2017-04-11 22:14:25 +08:00
|
|
|
LINE: 0,
|
|
|
|
DOTTED_LINE: 1
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-10-26 08:03:30 +01:00
|
|
|
|
2017-09-10 21:23:04 +08:00
|
|
|
export const relationType = {
|
2017-04-11 22:14:25 +08:00
|
|
|
AGGREGATION: 0,
|
|
|
|
EXTENSION: 1,
|
|
|
|
COMPOSITION: 2,
|
|
|
|
DEPENDENCY: 3
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2017-09-10 21:51:48 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
addClass,
|
|
|
|
clear,
|
|
|
|
getClass,
|
|
|
|
getClasses,
|
2019-10-04 21:49:02 +02:00
|
|
|
addAnnotation,
|
2017-09-10 21:51:48 +08:00
|
|
|
getRelations,
|
|
|
|
addRelation,
|
2018-10-25 21:30:28 +02:00
|
|
|
addMember,
|
2017-09-10 21:51:48 +08:00
|
|
|
addMembers,
|
|
|
|
cleanupLabel,
|
|
|
|
lineType,
|
|
|
|
relationType
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|