0

I am using streambase, and have created a map to convert incoming integers (0-255) into character codes. Right now, I'm using manual if/else statements. For example:

    if (message_code == '106') then 
        message_code = 'J'
    else if (message_code == '110') then
         message_code= 'N'

However, I'd like to just use this more generally. Searching Streambase Studio help didn't really provide anything as far as I could tell. I know this can be done in Java, but would probably require calling a java external function. I'm not so competent at this, so am a bit lost.

Is there a better way to do this in StreamBase?

Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
  • Adam - can you clarify your question a bit? It looks you have strings that look like numbers coming in and then you want to convert them into a single character string that corresponds into a capital ASCII character, even though the number in the incoming string actually corresponds to a lower case ASCII character? Is there some general algorithm for the conversion you can express? Because the answer of how to better do it in the StreamBase EventFlow expression language will depend a lot on what you actually want to do. – sbarber2 Oct 06 '15 at 14:43
  • Sorry. Essentially, I have ASCII character codes streaming in (http://ascii.cl/) so for example, the integer 89 corresponds to the letter Y. I'm not worried about the case, just in general, want to convert streams of integers into corresponding ASCII characters. Is there a builtin utility for this, or would I need to define my own mapping? – Adam Hughes Oct 06 '15 at 17:13

1 Answers1

1

Initial investigations confirm that the StreamBase Expression Language or built-in Functions don't have an obvious way to convert ints representing ASCII character values into a one-character string corresponding to the ASCII value. If there's some non-obvious trick I discover later, I'll come back and edit this answer.

I definitely wouldn't use a 255-clause if/then/else expression!

I would probably use a Java function and here is an example I've lightly tested:

package example;

public class ASCIIIntToString {

    public static String ASCIIToString(Integer a) {
        if (a == null || a < 0 || a > 255) {
            return null;
        } else {
            return Character.toString((char) (int) a);
        }
    }
}

And the custom-function declaration to drop into your sbd.sbconf would look like this:

<custom-functions>
    <custom-function alias="ASCIIToString"
                     args="auto"
                     class="example.ASCIIIntToString"
                     language="java"
                     name="ASCIIToString"
                     type="simple"/>
</custom-functions>

But if you would rather not drop into Java and stay in EventFlow then two methods come to mind:

  1. Load a CSV file with int -> string mappings in them into a Query Table and dto a lookup with Query Read operator, perhaps using the Initial Contents tab to reference the CSV file since ASCII isn't going to change anytime soon.
  2. Create a list constant called, say, ASCII, that has all the strings you want to convert to and index the list by the int. For example, in a Map operator do Add c ASCII[i], and add some bounds checking logic (perhaps in a user function).

Disclosure/Disclaimer: I am an employee of TIBCO Software, Inc. Opinions expressed here are my own and not TIBCO's.

sbarber2
  • 105
  • 9
  • Thanks, I appreciate it! I'll try to put the java function in (something I need to learn anyway). PS, do you have any examples of shape plots using SVG paths that I could borrow to tinker with? – Adam Hughes Oct 08 '15 at 13:14
  • Adam and I took the SVG question offline. – sbarber2 Oct 08 '15 at 22:21
  • What is the "example" package referring to? In other words, why is it necessary to declare a package? – Adam Hughes Oct 09 '15 at 13:37
  • See [Is the use of Java's default package a bad practice?](http://stackoverflow.com/questions/7849421/is-the-use-of-javas-default-package-a-bad-practice). People usually use a package name starting with a domain name: com.mycompany.streambasestuff.stringutilities.blahblahblah, for example. – sbarber2 Oct 09 '15 at 14:11
  • Thanks (sorry I'm very new to Java). – Adam Hughes Oct 09 '15 at 14:16