-
-
Save jbsarrodie/45a312cff559d23cf55ed102422e8af7 to your computer and use it in GitHub Desktop.
function convert(selection, convertToType) { | |
var relaxed = window.confirm('By default, selected concepts are converted, and relationships involving them that would no more be valid are converted to associations. Click Ok for this behavior or Cancel if you want a "strict" mode where relationships are not changed.'); | |
$(selection).each(function(o) { | |
$(concept(o)).outRels().each(function(r) { | |
if (! $.model.isAllowedRelationship(r.type, convertToType, r.target.type)) { | |
checkAndConvertRelationship(r, relaxed); | |
} | |
}); | |
$(concept(o)).inRels().each(function(r) { | |
if (! $.model.isAllowedRelationship(r.type, r.source.type, convertToType)) { | |
checkAndConvertRelationship(r, relaxed); | |
} | |
}); | |
concept(o).concept.type = convertToType; | |
}); | |
} | |
function checkAndConvertRelationship(r, relaxed) { | |
if (relaxed) { | |
r.documentation = 'This relationship has been converted from "'+r.type.replace(/-relationship$/, '')+'" to "association"\n'+r.documentation; | |
r.type = "association-relationship"; | |
} else { | |
window.alert('Relationship "'+r.name+'" from "'+r.source.name+'" to "'+r.target.name+'" will no more be valid after convertion and "strict" mode is on. Convertion aborted.'); | |
exit(); | |
} | |
} | |
function concept(o) { | |
return o.concept ? o.concept : o; | |
} |
load(__DIR__+"/../ConvertConcept.lib.js"); | |
convert(selection, getTypeFromFilename()); | |
function getTypeFromFilename() { | |
return __FILE__.replace(/^.*\//, '').replace(/.ajs$/, '').replace(/%20|\s/g, '-').toLowerCase(); | |
} |
load(__DIR__+"/../ConvertConcept.lib.js"); | |
convert(selection, getTypeFromFilename()); | |
function getTypeFromFilename() { | |
return __FILE__.replace(/^.*\//, '').replace(/.ajs$/, '').replace(/%20|\s/g, '-').toLowerCase(); | |
} |
Thank you for so cute solution!
Just a little fix to ensure all '%20' replacement correctly (Course of Action):
function getTypeFromFilename() {
return FILE.replace(/^.*//, '').replace(/.ajs$/, '').replace(new RegExp('%20','g'),'-').toLowerCase();
}
Excellent. So useful it shall be integrated in Archi by default. Thanks a lot
This is awesome! Thank you!
Here's a problem.
Windows uses the "%20" character while Mac uses the " " (space) character in the __FILE__
variable for file paths.
So this should fix it:
function getTypeFromFilename() {
return __FILE__.replace(/^.*\//, '').replace(/.ajs$/, '').replace(/%20|\s/g, '-').toLowerCase();
}
@jbsarrodie - Can you confirm? I can't edit or push to this gist. :-)
@jbsarrodie - Can you confirm? I can't edit or push to this gist. :-)
This should work, I've seen it tested on Archi's Forum so I'm updating the gist.
Thank you!
Edit: Done, but in fact it would be better to redesign a bit the main ConvertConcept.lib.js
script to manage this in only one place. I should maybe create a real GitHub repository to store this and other scripts which are part of my 'toolbox'.
but in fact it would be better to redesign a bit the main ConvertConcept.lib.js script to manage this in only one place
Yeah, I thought so too and I was going to have a go at it, but then I opened a beer and picked up my guitar...
but in fact it would be better to redesign a bit the main ConvertConcept.lib.js script to manage this in only one place
Try this:
ConvertConcept.lib.js
function convert(filename) {
convertToType = getTypeFromFilename(filename);
var relaxed = window.confirm('By default, selected concepts are converted, and relationships involving them that would no more be valid are converted to associations. Click OK for this behavior or Cancel if you want a "strict" mode where relationships are not changed.');
$(selection).each(function (o) {
$(concept(o)).outRels().each(function (r) {
if (!$.model.isAllowedRelationship(r.type, convertToType, r.target.type)) {
checkAndConvertRelationship(r, relaxed);
}
});
$(concept(o)).inRels().each(function (r) {
if (!$.model.isAllowedRelationship(r.type, r.source.type, convertToType)) {
checkAndConvertRelationship(r, relaxed);
}
});
concept(o).concept.type = convertToType;
});
}
function checkAndConvertRelationship(r, relaxed) {
if (relaxed) {
r.documentation = 'This relationship has been converted from "' + r.type.replace(/-relationship$/, '') + '" to "association"\n' + r.documentation;
r.type = "association-relationship";
} else {
window.alert('Relationship "' + r.name + '" from "' + r.source.name + '" to "' + r.target.name + '" will not be valid after conversion and "strict" mode is on. Conversion aborted.');
exit();
}
}
function concept(o) {
return o.concept ? o.concept : o;
}
function getTypeFromFilename(fileName) {
return fileName.replace(/^.*\//, '').replace(/.ajs$/, '').replace(/%20|\s/g, '-').toLowerCase();
}
Capability.ajs
load(__DIR__ + "/../ConvertConcept.lib.js");
convert(__FILE__);
Also, we may be able to remove selection
as an argument:
function convert(filename) {
...
load(__DIR__ + "/../ConvertConcept.lib.js");
convert(__FILE__);
Edit - that works, so updated the code above.
With the latest jArchi v1.0.0 and support GraalVM, because of __FILE__
change in path format, to support both ES5, ES6 and Graal:
function getTypeFromFilename() {
return __FILE__.replace(/^.*[\/\\]/, '').replace(/\.ajs$/, '').replace(/(%20|\s)/g, '-').toLowerCase();
}
Very helpful! I have felt a need for such capability and I was wondering how this could be implemented. You have done that. Thanks.