Subject: [netmod] false() vs 'false' in 'when' statements

From: "Sterne, Jason (Nokia - CA/Ottawa)" <[email-masked]>
Date: Tue, 7 Aug 2018 17:36:59 +0000

Hi all,

When comparing the value of a boolean leaf in a 'when' statement (or a 'must' statement), is an equality comparison to 'true' or 'false' the same as a check against true() or false() ?

For example:

leaf a {
    type boolean;
}
leaf b {
    type uint32;
    when "../a = 'true'";
}
leaf c {
    type uint32;
    when "../a = 'false'";
}
(note that neither b nor c can be configured if leaf a isn't explicitly configured with a value since there is no default)

If I replace the two 'when' statements above with the following instead, is the behavior the same ?
    ...
    when "../a = true()";
    ...
    when "../a = false()";
    ...

Or does "../a = true()" behave as an existence check on leaf a and would be enabled whether a is configured as true or false ?

Regards,
Jason

---

From: "Ivory, William" <[email-masked]>
Date: Wed, 8 Aug 2018 07:27:25 +0000

On 07/08/18 18:37, Sterne, Jason (Nokia - CA/Ottawa) wrote:
Hi all,

For example:

leaf a {
    type boolean;
}
leaf b {
    type uint32;
    when ".../a = 'true'";
}
leaf c {
    type uint32;
    when ".../a = 'false'";
}

    …
    when ".../a = true()";
    …
    when ".../a = false()";
    …

Regards,
Jason

See https://www.w3.org/TR/1999/REC-xpath-19991116/#booleans

(a) "../a = 'true'"

Compares 'string-value' (same as value of node if a leaf, rather more interesting if a list / container!) of each node in nodeset generated by '../a' and if any node in the nodeset matches 'true', expression is true.

(b) "../a = true()"

Similar to above but after getting the string-value, this is converted to a boolean before comparing with the boolean true.

(c) '../a'

True if the nodeset generated by this expression is not empty.

---

Caveats alluded to above are that if '../a' matches multiple nodes, then only one of these nodes needs to pass the test for the test to be true.  Separately, if any of the nodes are not leaves, the 'string-value' is a concatenated string of the values of the node's child / grandchild leaves.

Hope that helps.  You wouldn't believe the number of unit tests I have to make sure we get this logic right!  Using '!=' when comparing a nodeset with >1 node with other values is very likely not to give the answer you expect (-:

Regards,

William

---

From: "Sterne, Jason (Nokia - CA/Ottawa)" <[email-masked]>
Date: Wed, 8 Aug 2018 13:36:45 +0000

Thanks William.
Please see below.
Regards,
Jason

> -----Original Message-----
> From: Ivory, William <[email-masked]>
> Sent: Wednesday, August 8, 2018 3:27 AM
> To: Sterne, Jason (Nokia - CA/Ottawa) <[email-masked]>;
> [email-masked]
> Subject: Re: [netmod] false() vs 'false' in 'when' statements
> 
> When comparing the value of a boolean leaf in a 'when' statement (or a 'must'
> statement), is an equality comparison to 'true' or 'false' the same as a check
> against true() or false() ?
> 
> For example:
> 
> leaf a {
>     type boolean;
> }
> leaf b {
>     type uint32;
>     when ".../a = 'true'";
> }
> leaf c {
>     type uint32;
>     when ".../a = 'false'";
> }
> Or does "../a = true()" behave as an existence check on leaf a and would be
> enabled whether a is configured as true or false ?
> 
> Regards,
> Jason
> 
> 
> 
> Compares 'string-value' (same as value of node if a leaf, rather more
> interesting if a list / container!) of each node in nodeset generated by '../a' and
> if any node in the nodeset matches 'true', expression is true.

[>>JTS: ] In my specific example, leaf a is a single Boolean leaf.  In that case, are these equivalent ?
    when ".../a = 'true'";
    when ".../a = true()";
and are these also equivalent ?
    when ".../a = 'false'";
    when ".../a = false()";
(sorry if I'm being dense here, but I'm not 100% positive if I'm interpreting your explanations correctly and getting the right conclusion.  I've never seen must or when statements that use = false() or = true()).

[>>JTS: ] Are the strings 'true' and 'false' the "string-value" (that you mention above) for a Boolean leaf ?
'a' is a single Boolean leaf so the nodeset is that single leaf and the value is 'true' or 'false'.

> 
> (b) "../a = true()"
> 
> Similar to above but after getting the string-value, this is converted to a
> boolean before comparing with the boolean true.

[>>JTS: ] I'm a bit confused here.  Are you saying that the string-value of leaf a is converted to a Boolean ?
Do the string-values of a convert to something that is equal to true() or false() ?

> Caveats alluded to above are that if '../a' matches multiple nodes, then only
> one of these nodes needs to pass the test for the test to be true.  Separately, if
> any of the nodes are not leaves, the 'string-value' is a concatenated string of
> the values of the node's child / grandchild leaves.
> 
> Hope that helps.  You wouldn't believe the number of unit tests I have to make
> sure we get this logic right!  Using '!=' when comparing a nodeset with >1 node
> with other values is very likely not to give the answer you expect (-:
> 
> Regards,
> 
> William
>

---

From: "Sterne, Jason (Nokia - CA/Ottawa)" <[email-masked]>
Date: Wed, 8 Aug 2018 13:45:42 +0000

What I'm also a little worried about is whether
    when "../a = true()" 
is actually checking the *existence* of leaf a.  

In other words, could that expression be evaluated as enabling the when whether leaf a is set to 'true' or if it is set to 'false' ?  In either case it has a value and exists.

Jason

> From: Sterne, Jason (Nokia - CA/Ottawa)
> Sent: Wednesday, August 8, 2018 9:37 AM
> To: 'Ivory, William' <[email-masked]>; [email-masked]
> Subject: RE: [netmod] false() vs 'false' in 'when' statements
> 
> Thanks William.
> Please see below.
> Regards,
> Jason
> 
> (sorry if I'm being dense here, but I'm not 100% positive if I'm interpreting your
> explanations correctly and getting the right conclusion.  I've never seen must or
> when statements that use = false() or = true()).
>

---

From: Martin Bjorklund <[email-masked]>
Date: Wed, 08 Aug 2018 15:50:48 +0200 (CEST)

Hi,

"Sterne, Jason (Nokia - CA/Ottawa)" <[email-masked]> wrote:
> > (note that neither b nor c can be configured if leaf a isn't
> > explicitly configured
> > with a value since there is no default)
> > 
> > If I replace the two 'when' statements above with the following
> > instead, is the
> > behavior the same ?
> >     ...
> >     when ".../a = true()";
> >     ...
> >     when ".../a = false()";
> >     ...
> > 

No...

> and are these also equivalent ?

... and no.

What you probably want is ".../a = 'true'" (or 'false').

The expression:

    ".../a = true()";

will first take the string value of ".../a", which e.g. is "false".
This string value is then converted to a boolean, resulting in the
boolean value True (b/c the string is non-empty).

This means that if "a" is set at all (to true or false), the
expression ".../a = true()" returns true.  The expression ".../a =
false()" will only return true if "a" is unset.

> (sorry if I'm being dense here, but I'm not 100% positive if I'm
> interpreting your explanations correctly and getting the right
> conclusion.  I've never seen must or when statements that use =
> false() or = true()).
> 

Yes, according to 9.5.1 in RFC 7950.

> [>>JTS: ] I'm a bit confused here.  Are you saying that the
> string-value of leaf a is converted to a Boolean ?

See above.

/martin

> _______________________________________________
> netmod mailing list
> [email-masked]
> https://www.ietf.org/mailman/listinfo/netmod
>

---

From: Andy Bierman <[email-masked]>
Date: Wed, 8 Aug 2018 08:13:32 -0700

On Wed, Aug 8, 2018 at 6:50 AM, Martin Bjorklund <[email-masked]> wrote:

> .... and no.

I suggest reading the XPath definition of boolean comparisons:
https://www.w3.org/TR/1999/REC-xpath-19991116/#booleans

It is a bit complicated, but generally, a node-set is converted
to a boolean (==false for empty, ==true for not empty) when
compared to a boolean for equality/inequality.

 false is a boolean
 'false' is a string
 false() is a function that return a boolean

Andy

---

From: Martin Bjorklund <[email-masked]>
Date: Wed, 08 Aug 2018 17:44:03 +0200 (CEST)

> compared to a boolean for equality/inequality.

You're right.   But the result is the same in the example above; i.e.,
"../a = true()" is true iff a exists.

>  false is a boolean

There are no boolean literals in XPath 1.0.  So false would refer to a
node with the name "false".

/martin

---

From: Andy Bierman <[email-masked]>
Date: Wed, 8 Aug 2018 08:57:02 -0700

On Wed, Aug 8, 2018 at 8:44 AM, Martin Bjorklund <[email-masked]> wrote:

> > > > > When comparing the value of a boolean leaf in a 'when' statement
> (or a
> > > > > 'must'
> > > > > statement), is an equality comparison to 'true' or 'false' the
> same as
> > > > > a check
> > > > > interesting if a list / container!) of each node in nodeset
> generated
> > > > > by '../a' and
> > > > [>>JTS: ] In my specific example, leaf a is a single Boolean leaf.
> In
> > > > that case, are these equivalent ?
> > > >     when ".../a = 'true'";
> > > >     when ".../a = true()";
> > >
> > > No...
> > >

But there is the string() function is 4.2:

   -

   The boolean false value is converted to the string false. The boolean
   true value is converted to the string true.

> >  false() is a function that return a boolean
compare node-set to boolean will not work correctly. It tests existence.

    ../a = false

If one object to be compared is a node-set and the other is a boolean, then
the comparison will be true if and only if the result of performing the
comparison on the boolean and on the result of converting the node-set to a
boolean using the *boolean
<https://www.w3.org/TR/1999/REC-xpath-19991116/#function-boolean>* function
is true.

compare node-set to string will work as expected

  ../a = 'false'

If one object to be compared is a node-set and the other is a string, then
the comparison will be true if and only if there is a node in the node-set
such that the result of performing the comparison on the string-value
<https://www.w3.org/TR/1999/REC-xpath-19991116/#dt-string-value> of the
node and the other string is true.

Andy

> > > > > Similar to above but after getting the string-value, this is
> converted
> > > > > to a
> > > > Do the string-values of a convert to something that is equal to
> true()
> > > > or false() ?
> > >
> > > See above.
> > >
> > >
> > > /martin
> > >
> > >
> > > >
> > > > >
> > > > > (c) '../a'
> > > > >
> > > > > any of the nodes are not leaves, the 'string-value' is a
> concatenated
> > > > > string of
> > > > > Hope that helps.  You wouldn't believe the number of unit tests I
> have
> > > > > to make

---

From: Martin Bjorklund <[email-masked]>
Date: Wed, 08 Aug 2018 18:09:09 +0200 (CEST)

>    true value is converted to the string true.

Right, so:

   string(false()) --> "false"

> compare node-set to boolean will not work correctly. It tests existence.

Right.

>     ../a = false

This compares two node sets.

/martin