Skip to content

Instantly share code, notes, and snippets.

@jbsarrodie
Last active May 23, 2022 12:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbsarrodie/45a312cff559d23cf55ed102422e8af7 to your computer and use it in GitHub Desktop.
Save jbsarrodie/45a312cff559d23cf55ed102422e8af7 to your computer and use it in GitHub Desktop.
#jArchi script to change concepts' type (and optionally convert no more valid relationships to association)
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();
}
@IT-Cleric
Copy link

This is awesome! Thank you!

@Phillipus
Copy link

Phillipus commented Oct 29, 2020

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
Copy link
Author

jbsarrodie commented Oct 29, 2020

@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'.

@Phillipus
Copy link

Phillipus commented Oct 29, 2020

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...

@Phillipus
Copy link

Phillipus commented Oct 30, 2020

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__); 

@Phillipus
Copy link

Phillipus commented Oct 30, 2020

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.

@rchevallier
Copy link

rchevallier commented Jan 7, 2021

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();
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment