Skip to main content

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

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


Back to the top