nodes()

Read(2422) Label: child node,

Here’s how to use nodes() function.

P.nodes()

Description:

Find records in a recursive way where the foreign key values reference a specified record.

Syntax:

P.nodes(F,r,n)

Note:

r. The function finds all records from record sequence P where foreigh key F references record r, the root node, in a recursive way. The resulting records are record r’s child nodes. If these child nodes don’t have any child nodes, that is, their foreign key values don’t reference them, they are called the root node’s leaf nodes; otherwise, they are called the subtrees.

 

If the specified record r can’t be found, the function will return NULL. Parameter n is the maximum recursion depth, whose default is 1,000.

Parameter:

P

A record sequence.

F

Field name.

r

A record.

n

A number.

Option:

@d

Make the function return leaf-level nodes.

@p

Make the function return all child nodes and their level relative to the root node.

Return value:

Sequence

Example:

 

A

 

1

=file("D://emp.txt").import@t().keys(empid)

 

2

>A1.switch(mgrid,A1:empid)

 

3

=A1.nodes(mgrid,A1.select@1(name=="Diana"),5)

According to mgrid field, the foreign key references, search for all records of Diana’s subordinates and those of the subordinates of Diana’s subordinates.

4

=A1.nodes@d(mgrid,A1.select@1(name=="Diana"),5)

Search for the records of Diana’s subordinates who are not a manager.

5

=A1.nodes@p(mgrid,A1.select@1(name=="Diana"),5)

 

Display a relationship structure with Diana as the root, from which you see that Diana is the manager of empid=7, who is the manager of empid=14.

x.nodes@r(Fi,...)

Description:

Expand the referencing field(s) through multilevel of recursion.

Syntax:

x.nodes@r(Fi,...)

Note:

The function expands the referencing fields in parameter x through multilevel of recursion.

 

When parameter x is a single value, return an empty sequence;

 

When parameter x is a record and if the specified Fi,… fields all have single values, return a sequence consisting of x; otherwise return x.Fi.nodes@r(Fi,... )|...;

 

When parameter x is a sequence or a record sequence, return x.conj(~.nodes@r(Fi,...)).

 

When parameter Fi is absent, traverse all fields of x.

Parameter:

x

A single value/a sequence/a record.

Fi

The field name.

Return value:

Sequence

Example:

When parameter x is a single value:

 

A

 

1

=rand(10)

Return random values within 10

2

=A1.nodes@r()

Return [].

 

When parameter x is a single record or a sequence:

 

A

 

1

=demo.query("select  * from DEPARTMENT").keys(DEPT)

Return a table sequence:

2

=demo.query("select NAME,ABBR from STATES").keys(NAME)

Return a table sequence:

3

=demo.query("select top 10 EID,NAME,DEPT,STATE from EMPLOYEE")

Return a table sequence:

4

>A3.switch(DEPT,A1:DEPT)

Convert A3’s DEPT field into the referencing field according to the corresponding value of A1’s key field DEPT.

5

>A3.switch(STATE,A2:NAME)

Convert A3’s STATE field into the referencing field according to the corresponding value of A2’s key field NAME; After A4 and A5 are successfully executed, A3’s sequence becomes this:

6

=A3.select@1(NAME=="Ashley")

Get the first record where NAME value is Ashley from A3:

7

=A6.nodes@r(EID,NAME)

Get records where both EID field and NAME field are single values and return them as a sequence.

8

=A6.nodes@r(DEPT,STATE)

Get records where both DEPT field and STATE field are single values and return them as a sequence:

9

=A6.nodes@r(DEPT)

Get the record where DEPT field is the single value and return it as a sequence:

10

=A3.nodes@r()

As parameter Fi is absent, traverse all fields of A3 to get records where all fields are single values and return them as a sequence: