EGL Development User Group

EGL Development User Group

EGL Development User Group

The EGL Development User Group is dedicated to sharing news, knowledge, and insights regarding the EGL language and Business Developer product. Consisting of IBMers, HCL, and users, this community collaborates to advance the EGL ecosystem.

 View Only
  • 1.  Comparing type any

    Posted 12/16/15 05:46 AM

    Is there any :) way to compare two variables of type any, derrived from identical records:

    record RandomRecord type BasicRecord    a int;     b String; endrecA RandomRecord{    a = 1,    b = "text"}; recB RandomRecord{    a = 1,     b = "text"};anyA any = recA; anyB any = recB; if (anyA == anyB)    // hooray, success!else    // booooh, failure...end

     

    This alwas seems to go into the failure code.

    Is there any way to compare the two records without being aware of their structure?

     

    Thanks for the help!

    Bram_Callewaert


  • 2.  Re: Comparing type any

    Posted 12/17/15 02:58 AM

    Hi Bram, 

    I guess this is what your are looking for:

    handler bram type RUIhandler {initialUI = [ ui ], onConstructionFunction = start }                ui Div{ children = [] };                function start()                recA RandomRecord{ a = 1, b = "In a galaxy far, far away" };                 recB RandomRecord{ a = 1, b = "In a galaxy far, far away" };                if (equal(recA as any, recB as any))                    SysLib.writeStdout("equal" );                else                    SysLib.writeStdout("not equal" );                end                recA = new RandomRecord{ a = 1, b = "May the force be with you" };                 recB = new RandomRecord{ a = 1, b = "May the force be with me?" };                if (equal(recA as any, recB as any))                    SysLib.writeStdout("equal" );                else                    SysLib.writeStdout("not equal" );                end        end                     function equal(record1 any in, record2 any in) returns(boolean)                isEqual boolean = true;                dicRecord1 Dictionary{};                dicRecord2 Dictionary{};                ServiceLib.convertFromJSON(ServiceLib.convertToJSON(record1), dicRecord1);                ServiceLib.convertFromJSON(ServiceLib.convertToJSON(record2), dicRecord2);                fieldsRecord1 string[] = dicRecord1.getKeys();                for (i int from 1 to fieldsRecord1.getSize())                        if (dicRecord2.containsKey(fieldsRecord1[i]) and dicRecord1[fieldsRecord1[i]] != dicRecord2[fieldsRecord1[i]])                                isEqual = false;                                exit for;                        end                end                return (isEqual);        end        endrecord RandomRecord type BasicRecord    a int;     b String; end

    With kind regards,

    Guus

    gweis


  • 3.  Re: Comparing type any

    Posted 12/17/15 06:47 AM

    Based on the answer from Guus, here is a simplified compare function that also handles values that are not records. Identical record values should result in identical JSON-code, so converting back to Dictionary for further comparing is not necessary. This also handles the case of nested records.

    function areEqual(value1 any in, value2 any in) returns(boolean)  // try to compare regular values  if(value1 == value2)    return(yes);  end  try    return(ServiceLib.convertToJSON(value1) == ServiceLib.convertToJSON(value2));  onException(exc AnyException)    // only records can be converted to JSON    return(no);  endend
    Tommy Carlier


  • 4.  Re: Comparing type any

    Posted 12/17/15 05:35 PM

    Thanks Guus and Tommy. This was exactly what I was looking for.

     

    Kind regards,

     

    Bram

    Bram_Callewaert


  • 5.  Re: Comparing type any

    Posted 12/18/15 09:10 AM

    The code above does not seem to work for decimal types.

            a decimal(1) = 5;        b decimal(1) = 5;        aa any = a as any;        ba any = b as any;        SysLib.writeStdout("" + (aa == ba));  

    Always returns false.

    I've adapted the code to the following:

            function areEqual(value1 any in, value2 any in) returns(boolean)                if(value1 == value2)                        return(yes);                end                                try                        if (("" + value1) == ("" + value2))                                return (yes);                         end                onException(exception AnyException)                end                                try                        return(ServiceLib.convertToJSON(value1) == ServiceLib.convertToJSON(value2));                onException(exc AnyException)                        // enkel records kunnen naar JSON geconverteerd worden                        return(no);                end        end

     

     

    Kind regards,

    Bram

     

    Bram_Callewaert