Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [henshin-dev] Creating a rule programmatically

Hi Christian,

thank you very much for your help, now I can create the attributes in the right hand side too.
Also thank you very much for your remark about my method, now I'm using the correct one.

About the second problem, by an "empty diagram" I meant a diagram which is representing the module, but no other element is added to / shown in the diagram.

I compared two henshin files as you said, and the very big difference that they have is, that no xmi id is assigned to the elements that I create programmatically. But I don't see from where they should come from.

About the EPackage imports, I do as follows:

        // create module
        module = HenshinFactory.eINSTANCE.createModule();

        // register the package
        EPackage pack = myPackage .eINSTANCE;

        // import the package to the module
        module.getImports().add(pack);

The imported package IS shown in the properties of the module (in the model file) but not in the diagram.

Thanks for your help,
Best regards,
Faezeh



2014-04-14 19:59 GMT+02:00 Christian Krause <henshin.ck@xxxxxxxxx>:
Hi Faezeh,

here are my comments:

1) The <<delete>> action has actually no useful meaning for attributes. The interpreter should do no changes to such an attribute. Anyway, to set it to <<preserve>> you can add a copy of this attribute to the corresponding node in the right-hand side, e.g. like this:

Node rhsNode = rule.getMappings().getImage(stateNode, rule.getRhs());
HenshinFactory.eINSTANCE.createAttribute(rhsNode, isInitialType, isInitialValue);

In principle, this should also work (but I haven't tried it out for attributes):

attribute.setAction(new Action(Action.Type.PRESERVE)));

Regarding your method getTypeByName(EClass eclass, String name): you are using "==" for strings. You should rather use name.equals(...) instead. Also, you can simply use eclass.getEStructuralFeature(name).

2) What do you mean by "empty diagram"? You should make sure that in the module, you specify the EPackage-Imports correctly. What you could do for debugging is to create your Henshin file first using the graphical editor, then programmatically, and then compare the two saved Henshin files (e.g. in a text editor).

I hope this helps.

Cheers,
Christian


2014-04-14 17:04 GMT+02:00 Faezeh Ghassemi <faezeh.ghassemi@xxxxxxxxx>:
Dear Henshin Developers,

I am trying to create a rule out of a model file programmatically and for this, I encounter 2 problems:

1. My model file represents a finite state machine, so it contains some "States". For each State, I create a Node in the Henshin rule, using the method rule.createNode(..), which adds a node of type State to both "left hand side" and "right hand side". I also want to add an attribute to the node which is created, but when I add the attribute using the method createAttribute(..), the Attribute is created only in the "left hand side", and therefore, the Action of it, is set to "delete". I wanted to know how I can add an attribute to a node (programmatically) in a way that I can "preserve" it. For this, I do the following steps:

        // create module
        module = HenshinFactoryImpl.eINSTANCE.createModule();

        // create the rule
        rule = HenshinFactoryImpl.eINSTANCE.createRule();

        // add the rule to the module
        module.getUnits().add(rule);

        // create a node for each state
        for (State state : myModel.getStates()) {
         
            Node stateNode = rule.createNode(state.eClass());

            // create isInitial attribute
            EAttribute isInitialType = (EAttribute) getTypeByName(state.eClass(), "isInitial");
            String isInitialValue = state.isInitial() ? "true" : "false";

            HenshinFactoryImpl.eINSTANCE.createAttribute(stateNode, isInitialType, isInitialValue);
        }

protected EStructuralFeature getTypeByName(EClass eclass, String typeName) {
        for (EStructuralFeature sf : eclass.getEAllStructuralFeatures()) {
            if (sf.getName() == typeName) {
                return sf;
            }
        }
        return null;
    }

2. My second problem is that when I save the rule, I can only generate an "Empty diagram" from it, and when I try to import a package to this diagram file from the registry, I receive a NullPointerException, complaining about module.eResource().getResourceSet().
For saving the file, I create a ResourceSet, then a Resource, I add the module to the resource, and then I save the resource.

I would be very grateful if you can help me,
Best regards,
Faezeh


_______________________________________________
henshin-dev mailing list
henshin-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/henshin-dev



_______________________________________________
henshin-dev mailing list
henshin-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/henshin-dev



Back to the top