Wiki

Clone wiki

org.openmarkov / Developing_code / Edits

Creating an edit

An edit is the lego brick of the modifications commanded by the user. Adding a link, removing it, creating a node are some examples of edits. Edits can be combined into more complex modifications like removing a node (which includes removing the node and removing every link it had). The main advantage of the edit structure is the easy implementation of undo/redo history.

To create an edit, write a class with name <WhatYourEditDoes>Edit in the action folder of org.openmarkov.core. Search for AddLinkEdit or AddNodeEdit if you have trouble finding the folder.

Looking at one of the edits in the folder will give you insights of how your edit should be written.

The class should extend SimplePNEdit or CompoundPNEdit if your edit is a complex action that runs several other edits (like the remove node case). This inheritance will force you to write this three methods:

  1. doEdit(): here you program what the edit does.

  2. undo(): this code should undo doEdit(), remember to save (in doEdit()) everything necessary to undo it cleanly.

  3. redo(): redo doEdit(), save everything necessary to redo the edit from memory, saving the computations. The redo method should begin with:

    setTypicalRedo(false) // Tells OpenMarkov to run your code instead of `doEdit()` again
    super.redo() // General test to see if a `redo()` can be applied
    
    Note that edits that extend CompoundPNEdit manage undo() and redo() by themselves using those of its components. You don't need to write them.

Once created, you can run the edit using the function probnet.doEdit(WhatYourEditDoesEdit), for instance, when a menu item is clicked.

Updated