Type of Variables

Post Reply
User avatar
Hippogriff
Posts: 48
Joined: Thu Nov 19, 2015 4:02 pm
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2012

Type of Variables

Post by Hippogriff »

Hi all,

My first ever post here; I'm hoping this forum will be a really useful resource and maybe I can contribute something over time.

I am kinda brand new to this product set and I'm shadowing a colleague for the time-being. I've got a development background and, for sure, some of this is all going over my head, but I hope to get there. To get to the crux of my question today I'd like to explain what I want to do... I actually want to use the ExecuteProcess function in a TI Process to potentially call a different Process depending upon some logic, and those different Processes might have a different parameter Type - having a different Process being called is fine as that's just a String. I'm finding that my development background might be giving me some misconceptions as to how this stuff works. :oops:

Am I correct in my thinking that a Variable you use in a TI Process is not explicitly declared, it is effectively declared through the action of you just using it in code?

Am I also correct that the Type of the variable gets defined automatically?

My issue is that I reckon this code should work, but it doesn't pass the validation that's run on it by the code editor in Architect or Performance Modeler.

Code: Select all

IF (logical expression);
  myVariable = 1;
ELSE;
  myVariable = '1';
ENDIF;
It seems that myVariable has a Type assigned to it of Numeric, regardless of whether that line of code would actually be executed. So the 'second' assignment of a String value to myVariable causes an error in the editor. That's kinda OK in my mind if I've explicitly declared myVariable as a Numeric... but I don't think I can do that and, therefore, I'm curious if myVariable can either be a Numeric or a String or whether there is a compilation process happening when I hit "Save" that is concluding myVariable must be a Numeric and cannot be a String... but that's seemingly just because myVariable = 1; comes first, if myVariable = '1'; comes first in the code then it's the assignment of a Numeric that it complains about (still the second assignment in that case).

Does that even make sense? :?:
--
Cheers, Hippo
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: Type of Variables

Post by Alan Kirk »

Hippogriff wrote: I am kinda brand new to this product set and I'm shadowing a colleague for the time-being. I've got a development background and, for sure, some of this is all going over my head, but I hope to get there. To get to the crux of my question today I'd like to explain what I want to do... I actually want to use the ExecuteProcess function in a TI Process to potentially call a different Process depending upon some logic, and those different Processes might have a different parameter Type - having a different Process being called is fine as that's just a String. I'm finding that my development background might be giving me some misconceptions as to how this stuff works. :oops:

Am I correct in my thinking that a Variable you use in a TI Process is not explicitly declared, it is effectively declared through the action of you just using it in code?
Yes and no. There are two types of variables. There are data source variables, which come from the data source of a text file, cube view or what have you. You need to define whether those are string or numeric in the Variables tab. The one exception is when the data source is a cube view where the value (the last field / variable in the data source) could be either an S or an N value. You can use the implicitly defined variables NValue and SValue to extract the appropriate one, and use the Value_Is_String variable to tell which it is. See the Reference Guide for details. (There's a link in the FAQ thread.)

Parameters, which are passed to the process at runtime either through the execute process command or through the GUI, fall into the same basket, declaration-wise.

The other type is the type that you define yourself in the Prolog / Metadata / Data and/or Epilog tabs.
Hippogriff wrote:Am I also correct that the Type of the variable gets defined automatically?
For the second class of variables, yes. There is no formal declaration of data type. Rather it's implied by the type of value first assigned to it.
Hippogriff wrote:My issue is that I reckon this code should work, but it doesn't pass the validation that's run on it by the code editor in Architect or Performance Modeler.

Code: Select all

IF (logical expression);
  myVariable = 1;
ELSE;
  myVariable = '1';
ENDIF;
It seems that myVariable has a Type assigned to it of Numeric, regardless of whether that line of code would actually be executed. So the 'second' assignment of a String value to myVariable causes an error in the editor.
Please take a look at the Request For Assistance Guidelines; you should be posting the full actual code and the full error message. "An error" doesn't give us anything to work with, and without the full code we have no way of knowing whether this is really an error relating to the variable or to the omitted logical condition.

However it does appear that this will be a problem, no matter which type of variable it is. I could have sworn that this wasn't always the case and that at one time a user defined variable would automatically redefine depending on what was assigned to it. (It may have been as long ago as version 7; it's not something that I've looked at recently, I just remember being annoyed about it as I'm pretty fastidious about variable declaration, even in VBA. However it's certainly the case that the first type assigned seems to "stick" in 10.2.2.)
Hippogriff wrote:That's kinda OK in my mind if I've explicitly declared myVariable as a Numeric... but I don't think I can do that and, therefore, I'm curious if myVariable can either be a Numeric or a String or whether there is a compilation process happening when I hit "Save" that is concluding myVariable must be a Numeric and cannot be a String... but that's seemingly just because myVariable = 1; comes first, if myVariable = '1'; comes first in the code then it's the assignment of a Numeric that it complains about (still the second assignment in that case).
That seems to be so in 10.2 (and 9.5.2, actually) at least. You may want to reconsider whether you really want to use the same name for the parameters of the processes that you're calling.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
User avatar
Hippogriff
Posts: 48
Joined: Thu Nov 19, 2015 4:02 pm
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2012

Re: Type of Variables

Post by Hippogriff »

My apologies Alan... believe it or not, the actual Process (at the moment) simply consists of this (it's a stub that I'm using to test my assumptions and educate myself, it's nothing more at this moment in time):

Code: Select all

IF (1 = 1);
  myVariable = 1;
ELSE;
  myVariable = '1';
ENDIF;
With the above, the error is "Syntax error on or before: '1'; invalid numeric expression". If I swap the 1 and '1' around so the code looks like this:

Code: Select all

IF (1 = 1);
  myVariable = '1';
ELSE;
  myVariable = 1;
ENDIF;
Then the error remains on the same line, but now refers to the different Type, "Syntax error on or before: 1; invalid string expression"... which makes sense to me if it is as I originally assumed - the Type of a variable is determined and immutable and based on the first time it is written in code, not necessarily executed in code or actually the first assignment (which in this case, could be unknown).

So, if I change the code to:

Code: Select all

IF (1 = 0);
  myVariable = 1;
ELSE;
  myVariable = '1';
ENDIF;
Then it's the same error on the same (second) line assignment line that arises, so in the case above the error is "Syntax error on or before: '1'; invalid numeric expression" but in that case it is that line which would end up being executed, not the first line, due to the logical expression, right? And it's this bit that I got a bit bamboozled by.
--
Cheers, Hippo
lotsaram
MVP
Posts: 3704
Joined: Fri Mar 13, 2009 11:14 am
OLAP Product: TableManager1
Version: PA 2.0.x
Excel Version: Office 365
Location: Switzerland

Re: Type of Variables

Post by lotsaram »

Hippogriff wrote:I originally assumed - the Type of a variable is determined and immutable and based on the first time it is written in code, not necessarily executed in code or actually the first assignment (which in this case, could be unknown).
...
Then it's the same error on the same (second) line assignment line that arises, so in the case above the error is "Syntax error on or before: '1'; invalid numeric expression" but in that case it is that line which would end up being executed, not the first line, due to the logical expression, right? And it's this bit that I got a bit bamboozled by.
As far as I know your original assumption is correct. The first time a variable is declared it's type is immutable, regardless whether at run time that line of code is going to be executed or now. I think the issue is just that the error parser wasn't built to take practical application of thought experiments into account. I wouldn't worry about it, there are enough real problems to deal with.
Please place all requests for help in a public thread. I will not answer PMs requesting assistance.
User avatar
qml
MVP
Posts: 1097
Joined: Mon Feb 01, 2010 1:01 pm
OLAP Product: TM1 / Planning Analytics
Version: 2.0.9 and all previous
Excel Version: 2007 - 2016
Location: London, UK, Europe

Re: Type of Variables

Post by qml »

In TM1 you cannot overload variables. The first use of a variable determines its type until the end of its existence. Also, interestingly, it's not just the parser executed on process save that prevents conflicting type assignments. If you save the following TI through the GUI (which should parse just fine):

Code: Select all

IF (1 = 1);
  myVariable = '1';
ELSE;
  myVariable = '1';
ENDIF;
and then edit the *.pro file manually to change the code to:

Code: Select all

IF (1 = 1);
  myVariable = '1';
ELSE;
  myVariable = 1;
ENDIF;
then the code will still not execute on runtime. You will get a slightly different error message: "Error on line 8 Syntax error on or before: 1; invalid string expression: Could not initialize process". So although the offending portion of the code would never be executed you cannot run a process with conflicting variable type assignments, even if you can get past the TI editor parser.

In practice I advise you adopt a naming convention that indicates the type of every variable (e.g. sVar, nVar). That will help you avoid issues like that in your code and avoided they must be.
Kamil Arendt
User avatar
Hippogriff
Posts: 48
Joined: Thu Nov 19, 2015 4:02 pm
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2012

Re: Type of Variables

Post by Hippogriff »

qml wrote:...then the code will still not execute on runtime. You will get a slightly different error message: "Error on line 8 Syntax error on or before: 1; invalid string expression: Could not initialize process". So although the offending portion of the code would never be executed you cannot run a process with conflicting variable type assignments, even if you can get past the TI editor parser.
That is interesting.

So it seems there would never be a way to do something like (excuse my pseudo-code, just typing as I go):

Code: Select all

sParameterName = 'ParameterName';

IF (1 = 1);
  sProcessName = 'ProcessThatTakesAString';
  snParameterValue = '1';
ELSE;
  sProcessName = 'ProcessThatTakesANumeric;
  snParameterValue = 1;
END IF;

ExecuteProcess (sProcessName,sParameterName,snParameterValue);
Which is what I wanted to do (but in a more complex scenario - more parameters). I would, instead, need to bring the ExecuteProcess call into the IF and then have numerous calls instead of collating everything and having the single call... which I think could get messy. Maybe I abandon the idea.
--
Cheers, Hippo
User avatar
qml
MVP
Posts: 1097
Joined: Mon Feb 01, 2010 1:01 pm
OLAP Product: TM1 / Planning Analytics
Version: 2.0.9 and all previous
Excel Version: 2007 - 2016
Location: London, UK, Europe

Re: Type of Variables

Post by qml »

Hippogriff wrote:Maybe I abandon the idea.
You don't need to abandon the idea entirely, you only need to tweak it slightly. You could change all the parameter types to String and then just convert the parameters that require conversion to numbers in the process being called itself. Converting a number to a string (and vice versa) in TM1 is a piece of cake and you can choose from a few functions (STR, NumberToString, NumberToStringEx, NUMBR, StringToNumber, StringToNumberEx).
Kamil Arendt
User avatar
Hippogriff
Posts: 48
Joined: Thu Nov 19, 2015 4:02 pm
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2012

Re: Type of Variables

Post by Hippogriff »

Nice.
--
Cheers, Hippo
lotsaram
MVP
Posts: 3704
Joined: Fri Mar 13, 2009 11:14 am
OLAP Product: TableManager1
Version: PA 2.0.x
Excel Version: Office 365
Location: Switzerland

Re: Type of Variables

Post by lotsaram »

Hippogriff wrote:
qml wrote:...then the code will still not execute on runtime. You will get a slightly different error message: "Error on line 8 Syntax error on or before: 1; invalid string expression: Could not initialize process". So although the offending portion of the code would never be executed you cannot run a process with conflicting variable type assignments, even if you can get past the TI editor parser.
That is interesting.

So it seems there would never be a way to do something like (excuse my pseudo-code, just typing as I go):

Code: Select all

sParameterName = 'ParameterName';

IF (1 = 1);
  sProcessName = 'ProcessThatTakesAString';
  snParameterValue = '1';
ELSE;
  sProcessName = 'ProcessThatTakesANumeric;
  snParameterValue = 1;
END IF;

ExecuteProcess (sProcessName,sParameterName,snParameterValue);
Which is what I wanted to do (but in a more complex scenario - more parameters). I would, instead, need to bring the ExecuteProcess call into the IF and then have numerous calls instead of collating everything and having the single call... which I think could get messy. Maybe I abandon the idea.
Interesting this point. I was wondering what Hippo was on about with the original post but this is a bit of a conundrum when trying to extend this to a practical application of calling a process with parameters. E.g. it would be really nice to be able to write this ..

Code: Select all

ExecuteProcess( sProc, P1_name, IF( P1_isString = 1, P1_value, StringToNumber( P1_value ), ... Pn_name, IF( Pn_isString = 1, Pn_value, StringToNumber( Pn_value ) );
... except of course that you can't because the compiler doesn't care what will be executed at run time only that a parameter can't possibly be string and/or numeric. Rather a shame because it prevents being able to write a generalized wrapper to call any TI process unless you go the tm1runti route instead. (Or if you were to enforce a blanket rule of parameters can only be of type string might be another option.)
Please place all requests for help in a public thread. I will not answer PMs requesting assistance.
Post Reply