Previous Page | Next Page

The OPTMODEL Procedure

SLICE Expression

SLICE( < element-1, ...element-n >, set-expression ) ;

The SLICE expression produces a new set by selecting members in the operand set that match a pattern tuple. The pattern tuple is specified by the element list in angle brackets. Each element in the pattern tuple must specify a numeric or string expression. The expressions are used to match the values of the corresponding elements in the operand set member tuples. You can also specify an element by using an asterisk (*). The sequence of element values that correspond to asterisk positions in each matching tuple is combined into a tuple of the result set. At least one asterisk element must be specified.

The following code demonstrates the SLICE expression:

proc optmodel;
   put (slice(<1,*>, {<1,3>, <1,0>, <3,1>}));
   put (slice(<*,2,*>, {<1,2,3>, <2,4,3>, <2,2,5>}));

This code produces the output in Figure 8.33.

Figure 8.33 SLICE Expression Output
{3,0}                                                                           
{<1,3>,<2,5>}                                                                   

For the first PUT statement, <1,*> matches set members <1,3> and <1,0> but not <3,1>. The second element of each matching set tuple, corresponding to the asterisk element, becomes the value of the resulting set member. In the second PUT statement, the values of the first and third elements of the operand set member tuple are combined into a two-position tuple in the result set.

The following code uses the SLICE expression to help compute the transitive closure of a set of tuples representing a relation by using Warshall’s algorithm. In this code the set parameter dep represents a direct dependency relation.

proc optmodel;
   set<str,str> dep = {<'B','A'>, <'C','B'>, <'D','C'>};
   set<str,str> cl;
   set<str> cn; 
   cl = dep;
   cn =  (setof{<i,j> in dep} i) inter (setof{<i,j> in dep} j);
   for {node in cn}
       cl = cl union (slice(<*,node>,cl) cross slice(<node,*>,cl));
   put cl;

The local dummy parameter node in the FOR statement iterates over the set cn of possible intermediate nodes that can connect relations transitively. At the end of each FOR iteration, the set parameter cl contains all tuples from the original set as well as all transitive tuples found in the current or previous iterations.

The output in Figure 8.34 includes the indirect as well as direct transitive dependencies from the set dep.

Figure 8.34 Warshall's Algorithm Output
{<'B','A'>,<'C','B'>,<'D','C'>,<'C','A'>,<'D','B'>,<'D','A'>}                   

A special form of index-set-item uses the SLICE expression implicitly. See the section More on Index Sets for details.

Previous Page | Next Page | Top of Page