1

I am trying to create a function in Groovy that inputs a string and returns a modified string. The problem I believe is within an addon, which is a specific software environment I am working with i.e. logic.script.Microblock. The error message I am receiving is:

No signature of method: com.controlj.addonsupport.logic.script.Microblock.capAbbreviate() is applicable for argument types: (java.lang.String) values: [OAT Dewpoint bavo]. 

I have tried dispName = capAbbreviate(dispName.toString()), dispName = capAbbreviate(dispName), and capAbbreviate(dispName).

The software environment is using some sort of addon. I am still fairly new to Groovy/Java so this seems like it could be something simple but it's not clicking in my head just yet.

The code simplified below is:

def exceptions = ['Ac':'AC','Oat':'OAT','Vav':'VAV']
def exceptionNonAlpha = '(?=[^a-zA-Z])'
def dispName

def capAbbreviate(String mbText)
{
    // Iterates through 'exceptions' map and finds abbreviations and recapitalizes them
    for (hvacName in exceptions.keySet()) {
        mbText = mbText.replaceAll(hvacName + exceptionNonAlpha, exceptions[hvacName])
    }
    return mbText
}

logic.microblocks
{
    dispName = prop.'display name'
    dispName = capAbbreviate(dispName.toString()) // also tried capAbbreviate(dispName)
    println dispName
}
Screamcheese
  • 105
  • 1
  • 10
  • Your code is not really correct and not enough to reproduce the problem. 1. You have problems with using `exceptions` and `exceptionNonAlpha` script variables inside the function. 2. How do you define `logic` variable? 3. What is `com.controlj.addonsupport.logic.script.Microblock`? Is it your class or a class from some library? 4. The code above - is it script or is it a part of some class? Could you please update your question considering these points? – Andrej Istomin Jul 27 '22 at 22:24
  • As a half-blind guess I may say that you just change `def capAbbreviate(String mbText)` definition to `static def capAbbreviate(String mbText)`, but I'm not sure it'll help much. Would be nice if you could update your question with additional information. I see that something is wrong with the scope of your method that you call from the closure. – Andrej Istomin Jul 27 '22 at 22:29
  • @AndrejIstomin I updated the original problem but the `com.controlj.addonsupport.logic.script.Microblock` is the software environment I am working in that automatically loads that addon. I did try adding in `static def` but came back with this error: `org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 158: Apparent variable 'exceptions' was found in a static scope but doesn't refer to a local variable, static field or class.` – Screamcheese Jul 27 '22 at 23:51
  • 1
    try to declare `exceptions` variable as `exceptions = ['Ac':'AC','Oat':'OAT','Vav':'VAV']` instead of `exceptions = ['Ac':'AC','Oat':'OAT','Vav':'VAV']` (the same for `exceptionNonAlpha`), it will make those variables global. – Andrej Istomin Jul 28 '22 at 05:26
  • @AndrejIstomin I'm guessing you mean `**def** exceptions = ['Ac':'AC','Oat':'OAT','Vav':'VAV']` to `exceptions = ['Ac':'AC','Oat':'OAT','Vav':'VAV']`. I'm trying to determine how to make `dispName` global as well because that might be part of the problem. – Screamcheese Jul 28 '22 at 16:29
  • Yes, I meant just delete `def`, that's how you make the variable "global". See the explanation: https://stackoverflow.com/questions/6305910/how-do-i-create-and-access-the-global-variables-in-groovy – Andrej Istomin Jul 28 '22 at 16:35
  • Thanks @AndrejIstomin, that solved part of the issue. I was able to do trial and error and found that `this.` before the method resolved the issue. I can finally rest in peace. – Screamcheese Aug 06 '22 at 00:50

1 Answers1

0

The solution has two parts:

Similar to what @AndrejIstomin mentioned, removing the def to make a list or variable global resolved one part of the issue

The second part to the solution is that this. needed to be used to call the method. i.e. this.capAbbreviate(dispName)

Screamcheese
  • 105
  • 1
  • 10