Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

How to do an outer join of tuplesets in OPL (or how to test for nullity and join efficiently)

  • 1.  How to do an outer join of tuplesets in OPL (or how to test for nullity and join efficiently)

    Posted Thu April 06, 2023 05:26 PM

    I want to do something similar to a database outer join in OPL. Below, I found a way to do it. I test for nullity by using card function. Is there a better way to test for nullity in OPL?

    //Tupleset definitions
    tuple rvPerson {
      key string Name;
      string StateCode;
    }
     
    tuple rvState {
      key string StateCode;
      string StateName;
     
    tuple rvPersonState {
      key string PersonName;
      string StateName;
    }
     
    //Tupleset data
    setof(rvPerson) relPersons = {<"Mark", "BC">, <"Joe", "VT">, <"Bill", "VT">};
     
    setof(rvState) relStates = {<"BC", "Britsh-Columbia">, <"VT", "Vermont">};
     
    setof(rvState) relStatesBis = {<"BC", "Britsh-Columbia">, <"QC", "Québec">};
     
     
    //Inner Join
    {rvPersonState} relPersonsStates ={<p.Name, s.StateName> | p in relPersons, s in relStates : p.StateCode==s.StateCode};
     
    //Outer Join
    {rvPersonState} relPersonsStatesBis ={<p.Name,  (card({s | s in relStatesBis : p.StateCode==s.StateCode})==1?item(relStatesBis,<p.StateCode>).StateName:"null") > | p in relPersons};
     
    /*Results
    relPersonsStates=
    {
    PersonName,StateName
    Mark,Britsh-Columbia
    Joe,Vermont
    Bill,Vermont}
     
    relStatesBis=
    {
    PersonName,StateName
    Mark,Britsh-Columbia
    Joe,null
    Bill,null}



    ------------------------------
    Marko BLAIS
    ------------------------------


  • 2.  RE: How to do an outer join of tuplesets in OPL (or how to test for nullity and join efficiently)
    Best Answer

    Posted Tue April 11, 2023 06:34 AM

    Hi,

    you can do that join with the SQL request. But if you do that in OPL , I tend to use sets instead of what you did.

    //Tupleset definitions
    tuple rvPerson {
      key string Name;
      string StateCode;
    }
     
    tuple rvState {
      key string StateCode;
      string StateName;
    } 
     
    tuple rvPersonState {
      key string PersonName;
      string StateName;
    }
     
    //Tupleset data
    setof(rvPerson) relPersons = {<"Mark", "BC">, <"Joe", "VT">, <"Bill", "VT">};
     
    setof(rvState) relStates = {<"BC", "Britsh-Columbia">, <"VT", "Vermont">};
     
    setof(rvState) relStatesBis = {<"BC", "Britsh-Columbia">, <"QC", "Québec">};
     
     
    //Inner Join
    {rvPersonState} relPersonsStates ={<p.Name, s.StateName> | p in relPersons, s in relStates : p.StateCode==s.StateCode};
     
    //Outer Join
    {rvPersonState} relPersonsStatesBis ={<p.Name,  (card({s | s in relStatesBis : p.StateCode==s.StateCode})==1?item(relStatesBis,<p.StateCode>).StateName:"null") > | p in relPersons};
     
    /*Results
    relPersonsStates=
    {
    PersonName,StateName
    Mark,Britsh-Columbia
    Joe,Vermont
    Bill,Vermont}
     
    relStatesBis=
    {
    PersonName,StateName
    Mark,Britsh-Columbia
    Joe,null
    Bill,null}
    
    */
    
    execute
    {
      writeln(relPersonsStatesBis );
    }
    
    {string} pStates={p.StateCode | p in relPersons};
    {string} states={s.StateCode | s in relStatesBis};
    {string} interStates=pStates inter states;
    {string} diffStates=pStates diff states;
    
    {rvPersonState} relPersonsStatesTer =
    {<p.Name, s.StateName> | p in relPersons, s in relStatesBis : p.StateCode==s.StateCode && p.StateCode in interStates}
    union
    {<p.Name, "null"> | p in relPersons: p.StateCode in diffStates};
    
    execute
    {
      writeln(relPersonsStatesTer );
    }


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: How to do an outer join of tuplesets in OPL (or how to test for nullity and join efficiently)

    Posted Tue April 11, 2023 10:25 AM

    Thank you for this answer.



    ------------------------------
    Marko BLAIS
    ------------------------------