question

Marco Baccalaro avatar image
3 Likes"
Marco Baccalaro asked Matthew Gillespie commented

How to do objectexist in 2017

I read that the objectexists is deprecated and to use treenode.rank instead.

How do I have to do it correctly? I'm not sure I'm doing it in the correct way.

If I do like this I get an exception when object does not exist:

Object box = item.first;
if (box.rank)
	box.color = Color(1,0,0);
FlexSim 17.0.0
flexscriptdot syntaxrankobjectexists
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Matthew Gillespie avatar image
3 Likes"
Matthew Gillespie answered Matthew Gillespie commented

The treenode.rank line is a copy-paste mistake. If the object doesn't exist the variable should be null and so you just put the variable into an if statement.

Object box = model().find("Queue1").first;
if(box)
	box.color = Color.red;
· 19
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Sebastian Hemmann avatar image Sebastian Hemmann commented ·

And how does this work, if I want to know if a Label exists?

1 Like 1 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Sebastian Hemmann commented ·

You would need to get a reference to the label node:

if(box.labels["myLabel"])
	box.color = Color.red;
0 Likes 0 ·
Cameron Pluim avatar image Cameron Pluim Sebastian Hemmann commented ·

Similar to @Matthew Gillespie's comment, you could also use:

if(box.myLabel?)
        box.color = Color.red;
0 Likes 0 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Cameron Pluim commented ·

Using the ? in this case would give you slightly different behavior depending on what the value of the label is:

//myLabel = 0

if(box.labels["myLabel"])	//true
if(box.myLabel?)		//false

//myLabel = 1

if(box.labels["myLabel"])	//true
if(box.myLabel?)		//true

//myLabel doesn't exist

if(box.labels["myLabel"])	//false
if(box.myLabel?)		//false
3 Likes 3 ·
Show more comments
Ralf Gruber avatar image Ralf Gruber ♦ commented ·

Would this be ok to use, if looking for a label "MyLabel" on Queue1 in the model? Or is there a better way?

treenode thelabel = model().find("Queue1>labels/MyLabel");
if(thelabel)
	return thelabel.value;
1 Like 1 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Ralf Gruber ♦ commented ·

If you just want to get the value of the label if the label exists there is an easier way to do this using the ? operator:

return model().find("Queue1").MyLabel?;

Your code works, but the preferred way to get a label node is using the labels property:

treenode thelabel = model().find("Queue1").labels["MyLabel"];
0 Likes 0 ·
Ralf Gruber avatar image Ralf Gruber ♦ Matthew Gillespie ♦♦ commented ·

@Matthew Gillespie,

seems I am too stupid to figure this out by myself: Attached is a model as a sample. OnEntry of the Queue, I create a label MyLabel with the value of "1" on 50% of all items. On sendtport of that Queue, I want to send the item through the label value's port number ("1"), if the label exists and through port 2, if it does not exist.

Everything I tried does not work or throws an exception, because the label does not exist.

Can you please help me (and @christoph gruber) here?

Thanks

iflabeldoesnotexist.fsm

0 Likes 0 ·
Show more comments
christoph gruber avatar image christoph gruber commented ·

Sorry, but I do not understand exectly:

//myLabel doesn't exist
 if(item.labels["myLabel"] == 0){
     msg ("Info", "Label myLabel doesen´t exist!");
}

This code does not work in 17.03 - it only works in 17.1!

But why doesn´t work the code below in 17.1?

@Ralf Gruber ; @Matthew Gillespie

//myLabel exist
if (item.labels["CControl"] == 1){
    msg ("Info", "Label myLabel exist!");
}
0 Likes 0 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ christoph gruber commented ·

You shouldn't be comparing the label node to 0 or 1. If the label does exist it will return a valid treenode (which doesn't equal 1 or 0) and if it doesn't exist it will return nullvar (which also doesn't equal 1 or 0). So your code examples should look like this:

//myLabel doesn't exist
 if(!item.labels["myLabel"]){
     msg ("Info", "Label myLabel does not exist!");
}

//myLabel exist
if (item.labels["myLabel"]){
    msg ("Info", "Label myLabel exists!");
}
0 Likes 0 ·
Arun Kr avatar image Arun Kr commented ·

I am getting invalid exception error for the following code.

Object Box = current.subnodes[2];
if(Box)
{
		 current.color = Color.black;
}

Also see the attached model. The code is written in the process finish trigger of the processor.

objectexists-error.fsm

0 Likes 0 ·
Mischa Spelt avatar image Mischa Spelt Arun Kr commented ·

Have you checked that the current node actually has two subnodes?

if(current.subnodes.length >= 2)
{
	Object box = current.subnodes[2];
	current.color = Color.black; // Did you mean: box.color = .. ?
}
0 Likes 0 ·
Arun Kr avatar image Arun Kr Mischa Spelt commented ·

Hi Misha,

Thanks for the reply, your code will help.

The current doesn't have two sub nodes, only one. Conceptually, objectexists condition using

 if(current.subnodes[2])
{
		 current.color = Color.black;//I meant current.color only
}

shouldn't throw an exception right?

Regards,

Arun KR

0 Likes 0 ·
Show more comments