Summary
In the VariableFactory interface it would be useful to have an option to forcibly overwrite any global variables that already exist in a program node’s data model.
What is it?
Currently you can create globals with a suggested name, and if that name is already taken the variable will be assigned a unique one automatically. It would be nice to have additional methods in the variable factory interface that give you the power to forcibly overwrite a variable (if it already exists), rather than assign a unique name and keep both.
Why is it needed?
My URCap takes input from users and, given an ‘apply’ command, writes global variables to the program’s data model. If that ‘apply’ command is used again, an overwrite method would be very helpful as it would be easy to use and ensure correct global variable naming.
Maybe I’m just doing things “wrong” and that’s why I’ve never run into this, but you don’t need to use the “Variable” datatype to do what you’re asking. Just store a string as the variable name, and a value as whatever data type. Then let your generateScript method do:
writer.appendLine("global " + getVarNameFromModel() + " = " + getVarValueFromModel());
You will now have a global URScript variable that you could rename on the fly if you wanted, and change its value of without making duplicates.
Because of the nature of generateScript() (i.e. it only compiles once, at the start of the program), this approach does not work if you want incrementation across program cycles, which I need for indexes in my use case. With your suggestion, the same value will be used at the start of each cycle, even your getVarValueFromModel() increments the value, too.
Possibly there is a work around, but I think it would be very neat to have a way to programmatically create globals and edit them before runtime, along with the added security of not creating a duplicate.
Can your CAP check first if that variable name exists and if it does, copy the value, delete the variable, make a new one (with the same name) and restore the value?
That’s another feature that would be nice to have: Deleting variables (or anything!) if they exist in the data model. I don’t think the API has a way to do this, just reading from the reference docs. In “Variable” you can only create globals, and in “DataModel” you can only get() and set(). set() will always assign a unique name, which is the exact issue I pointed out in my post.
if(model.isSet("yourStringKey") {
model.remove("yourStringKey")
}
These methods exist for the dataModel datatype. Not sure if that helps you, but you can absolutely delete from the dataModel if it exists.