Node Class

The engine.
Post Reply
jdtoellner
Posts: 71
Joined: Mon Aug 01, 2016 9:45 pm

Node Class

Post by jdtoellner »

You showed me this code in a previous post:

Code: Select all

            DSL_node *nodeDefinitionPtr= theNet.GetNode(nodeHandle);
            DSL_nodeDefinition *nodeDefinitionPtr= parentNode->Definition();
            DSL_Dmatrix &cpt= *nodeDefinitionPtr->GetMatrix();
It enables me to simplify this:

Code: Select all

theNet.GetNode(nodeHandle)->GetNumberOfOutcomes();
To this:

Code: Select all

nodeDefinitionPtr->GetNumberOfOutcomes();
I can include these in a class structure:

Code: Select all

class NODE_nodeClass {
public:
	DSL_node * nodePtr;
	DSL_nodeDefinition * nodeDefinitionPtr;
	DSL_extraDefinition * nodeExtraDefinitionPtr;
}
I'd than set variables with this code:

Code: Select all

		nodePtr = theNet.GetNode(nodeHandle);
		nodeDefinitionPtr = theNet.GetNode(nodeHandle)->Definition();
		nodeExtraDefinitionPtr = theNet.GetNode(nodeHandle)->ExtraDefinition();
You also showed me this code:

Code: Select all

            DSL_idArray &outcomes = *nodeDefinitionPtr->GetOutcomesNames();
            int numberOfOutcomes = nodeDefinitionPtr->GetNumberOfOutcomes();
I'm able to add "numberOfOutcomes" to the class like this:

Code: Select all

class NODE_nodeClass {
public:
	DSL_node * nodePtr;
	DSL_nodeDefinition * nodeDefinitionPtr;
	DSL_extraDefinition * nodeExtraDefinitionPtr;
	int numberOfOutcomes;
}
I'd set the variable "numberOfOutcomes" this way:

Code: Select all

numberOfOutcomes = nodeDefinitionPtr->GetNumberOfOutcomes();
Can I add this to the class definition?

Code: Select all

            DSL_idArray &outcomes = *nodeDefinitionPtr ->GetOutcomesNames();
If so, how would I initialize it later?
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: Node Class

Post by shooltz[BayesFusion] »

If you want to initialize a class member after its creation, you'll need to replace the reference with the pointer.

Code: Select all

class NODE_nodeClass {
public:
   DSL_node * nodePtr;
   DSL_nodeDefinition * nodeDefinitionPtr;
   DSL_extraDefinition * nodeExtraDefinitionPtr;
   DSL_idArray * outcomes;
}

... 
// in a body of non-constructor method of NODE_nodeClass:
outcomes = nodeDefinitionPtr->GetOutcomesNames();
jdtoellner
Posts: 71
Joined: Mon Aug 01, 2016 9:45 pm

Re: Node Class

Post by jdtoellner »

Thanks for the reply; especially during a holiday!

I did what you suggested and ran into another problem. This original code you suggested works:

Code: Select all

		class NODE_nodeClass{
			DSL_idArray nodeStates;
			int numberOfStates = 0;
		}
... 
// in a body of non-constructor method of NODE_nodeClass:

		DSL_idArray &outcomes = *nodeDefinitionPtr->GetOutcomesNames();

		nodeStates.Add(outcomes[0]);
		nodeStates.Add(outcomes[1]);

		numberOfStates = nodeDefinitionPtr->GetNumberOfOutcomes();
This code, where I put a pointer to "outcomes" in "NODE_nodeClass" doesn't:

Code: Select all

		class NODE_nodeClass{
			DSL_idArray nodeStates;
			DSL_idArray * outcomes;
			int numberOfStates = 0;
		}
... 
// in a body of non-constructor method of NODE_nodeClass:

		outcomes = nodeDefinitionPtr->GetOutcomesNames();

		nodeStates.Add(outcomes[0]);
		nodeStates.Add(outcomes[1]);

		numberOfStates = nodeDefinitionPtr->GetNumberOfOutcomes();
I get a compile error with:

Code: Select all

		nodeStates.Add(outcomes[0]);
		nodeStates.Add(outcomes[1]);
"no suitable conversion function from "DSL_idArray" to "const char*"exists"

Let me tell you what I'm trying to do. I'm thinking that the method I'm using is probably not the best way.

I need to check the values of "outcomes". If "outcomes[0]" is equal to "State0" I'll rename it. Same goes if "outcomes[1]" is equal to "State1". Otherwise I use "AddOutcome" to add new outcomes. (I apparently cannot delete "State0" and "State1" which makes sense. If I could I'd use "AddOutcome" to add all the states I need to.)

I've moving values in "outcomes" into "nodeStates" and then I store those values in a string variable.

Code: Select all

		nodeStates.Add(outcomes[0]);
		nodeStates.Add(outcomes[1]);
		
	string state0 = nodeStates[0];
	string state1 = nodeStates[1];

	if (state0 == "State0") {...}
I'd love to use "outcomes" and interrogate values directly but can't figure out how to compare the element of an DSL_idArray to a string.
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: Node Class

Post by shooltz[BayesFusion] »

If the 'outcomes' variable is a pointer (not a reference) to DSL_idArray, you need to dereference it with * before applying operator[]:

Code: Select all

nodeStates.Add((*outcomes)[1]);
Alternatively, you can use the Subscript method and ->:

Code: Select all

nodeStates.Add(outcomes->Subscript(1));
jdtoellner
Posts: 71
Joined: Mon Aug 01, 2016 9:45 pm

Re: Node Class

Post by jdtoellner »

Thanks!!

You're my hero.
Post Reply