How to do objectexist in 2017

How to do objectexist in 2017

marco_baccalaro
Not applicable
55 Views
20 Replies
Message 1 of 21

How to do objectexist in 2017

marco_baccalaro
Not applicable

[ FlexSim 17.0.0 ]

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);
Accepted solutions (1)
56 Views
20 Replies
Replies (20)
Message 2 of 21

Matthew_Gillespie
Autodesk
Autodesk
Accepted solution

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;


Matthew Gillespie
FlexSim Software Developer

Message 3 of 21

sebastian_hemmannE3JAU
Collaborator
Collaborator

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

Message 4 of 21

Matthew_Gillespie
Autodesk
Autodesk

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

if(box.labels["myLabel"])
	box.color = Color.red;


Matthew Gillespie
FlexSim Software Developer

0 Likes
Message 5 of 21

ralf_gruber
Collaborator
Collaborator

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;
Message 6 of 21

Matthew_Gillespie
Autodesk
Autodesk

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"];


Matthew Gillespie
FlexSim Software Developer

0 Likes
Message 7 of 21

cameron_pluim
Not applicable

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

if(box.myLabel?)
        box.color = Color.red;
0 Likes
Message 8 of 21

Matthew_Gillespie
Autodesk
Autodesk

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


Matthew Gillespie
FlexSim Software Developer

Message 9 of 21

cameron_pluim
Not applicable

I didn't realize that. I thought it would always give true if the label exists. Thanks for the clarification

0 Likes
Message 10 of 21

Matthew_Gillespie
Autodesk
Autodesk

box.myLabel returns the value of myLabel, not the node.

box.myLabel? does the same thing, but returns nullvar if the label doesn't exist

So either statement just gets you the value of the label. The if statement then gets given whatever that value is and it interprets certain things like 0's and null values as false.



Matthew Gillespie
FlexSim Software Developer

0 Likes
Message 11 of 21

christoph_gruber
Not applicable

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
Message 12 of 21

ralf_gruber
Collaborator
Collaborator

@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
Message 13 of 21

cameron_pluim
Not applicable

@Ralf Gruber, I'm seeing the errors as well, however if I replace the send to port to the following code:

if(item.MyLabel?)
	return item.MyLabel;
else
	return 2;

Then it correctly checks for the label, and doesn't throw the error.

But if you change the OnEntry to create a label with a value of "0" (so it can go to any output) then the above if statements returns false, and will return 2 instead of 0.

I'm not sure why your code throws an error, especially since the error is saying that the label MyLabel does not exist when that is what we are checking.

0 Likes
Message 14 of 21

ralf_gruber
Collaborator
Collaborator

Cam,

that makes totally sense to me: Your expression checks the value of the label, while I want to check for the bare existence of that label. If you change the label value to "0" the condition never becomes true and all items go to port 2.

It throws the error for the 50% of items, where I do not create the label...I want my "objectexists()" back!

Thanks

Ralf

0 Likes
Message 15 of 21

Matthew_Gillespie
Autodesk
Autodesk

You just need to be on 17.0.4 (or 17.1). The second line in the What's New for 17.0.4 says:

  • Fixed an issue with the [] operator throwing an exception when the node doesn't exist.

This isn't an issue that objectexists would solve, the issue was that the [] operator on the labels property was being too strict.



Matthew Gillespie
FlexSim Software Developer

0 Likes
Message 16 of 21

Matthew_Gillespie
Autodesk
Autodesk

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!");
}


Matthew Gillespie
FlexSim Software Developer

0 Likes
Message 17 of 21

ralf_gruber
Collaborator
Collaborator

Matt,

thanks for clarification, it does work in the newer release. @christoph gruber

0 Likes
Message 18 of 21

arunTTT2P
Explorer
Explorer

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
Message 19 of 21

mischa_spelt
Advisor
Advisor

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
Message 20 of 21

arunTTT2P
Explorer
Explorer

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