% \iffalse meta-comment
%
% Copyright (C) 2020 by S. Vandevelde, F. Pantigny
% ------------------------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in:
%
% http://www.latex-project.org/lppl.txt
%
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% \fi
% \iffalse
%
\def\dmnfileversion{0.0.3}
\def\dmnfiledate{2020/12/08}
%
%<*batchfile>
\begingroup
\input l3docstrip.tex
\keepsilent
\usedir{tex/latex/cascade}
\preamble

Copyright (C) 2020 by S.Vandevelde, F. Pantigny
-----------------------------------

This file may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either version 1.3
of this license or (at your option) any later version.
The latest version of this license is in:

http://www.latex-project.org/lppl.txt

and version 1.3c or later is part of all distributions of LaTeX
version 2005/12/01 or later.

\endpreamble
\askforoverwritefalse
\endgroup
%</batchfile>
%
%<@@=cascade>
%<*driver>
\documentclass[dvipsnames]{l3doc}% option dvipsnames is for xcolor (loaded by tikz)
\usepackage{xltxtra}
\usepackage{geometry}
\geometry{left=2.8cm,right=2.8cm,top=2.5cm,bottom=2.5cm,papersize={21cm,29.7cm}}
\setlength{\fboxsep}{0pt}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{float}
\usetikzlibrary{calc}
\usepackage{decision-table}
\hfuzz=5cm
\def\interitem{\vskip 7mm plus 2 mm minus 3mm}          
\def\emphase{\bgroup\color{RoyalPurple}\let\next=}
\fvset{commandchars=\~\#\@,formatcom={\color{gray}}}
\parindent 0pt
\skip\footins = 2\bigskipamount



\begin{document}
\DocInput{decision-table.dtx}
\end{document}
%</driver>
% \fi
% \title{The package \pkg{decision-table}\thanks{This document corresponds to the
% version~\dmnfileversion\space of \pkg{decision-table}, at the date of~\dmnfiledate.}} 
% \author{S. Vandevelde, F. Pantigny \\ \texttt{s.vandevelde@kuleuven.be} \\ \texttt{fpantigny@wanadoo.fr}}
%
% \maketitle
%
% \begin{abstract}
% The LaTeX package \pkg{decision-table} provides a command |\dmntable|, which allows for an easy way to generate decision tables in the Decision Model and Notation (DMN) format. (See Fig. \ref{ex1})
% This package ensures consistency in the tables (i.e. fontsize), and is thus a better alternative to inserting tables via images.
% \end{abstract}
% 
%
%
% \vspace{1cm}
% \section{Description}
% 
% The \texttt{decision-table} package allows for an easy way to generate decision tables in the Decision Model and Notation (DMN) format. (See Fig. \ref{ex1})
% This package ensures consistency in the tables (i.e. fontsize), and is thus a better alternative to inserting tables via images.
% 
% \begin{figure}[H]
%     \centering
%     \dmntable{Calculate BMI}{U}{Weight(kgs),Length(m)}{BMI}{ %               ---,---,weight/length*length}
%     \caption{Example of a DMN table}
%     \label{ex1}
% \end{figure}
% \section{How to use}
% 
% The \texttt{decision-table} package adds the \texttt{dmntable} command, with which tables can be created.
% This command expands into a \texttt{tabular}, so it can be used within a \texttt{table} or \texttt{figure} environment.
% Furthermore, this allows labels and captions to be added seamlessly. 
% It is also possible to place multiple DMN tables in one \texttt{table}/\texttt{figure} environment.
% 
% The \texttt{dmntable} command has the following inputs:
% \begin{itemize}
%     \item title
%     \item hit policy
%     \item input column headers
%     \item output column headers
%     \item the table values
% \end{itemize}
% The command is used as follows:
% \begin{lstlisting}[language=tex, frame=single]
% \dmntable{title}{hitpolicy}{input}{output}{values}
% \end{lstlisting}
% 
% The input, output and cell values are split by a comma.
% It is not necessary to include the row numbers for the cell values.
% For example, \ref{ex1} is generated by the following code:
% \begin{lstlisting}[language=tex, frame=single, basicstyle=\small]
% \begin{figure}[H]
%     \centering
%     \dmntable{Calculate BMI}{U}             % Title and HP
%              {Weight(kgs),Length(m)}{BMI}   % Input, output
%              {---,---,weight/length*length} % Cell values
%     \caption{Example of a DMN table}
%     \label{ex1}
% \end{figure}
% \end{lstlisting}
% 
% If a cell value contains multiple values (e.g. multiple string values), then accolades should be written around them.
% See the example \ref{ex2}.
% 
% 
% \section{Examples}
% This section contains some example code and their resulting tables.
% 
% \begin{lstlisting}[language=tex, frame=single, basicstyle=\small]
% \begin{figure}[H]
%     \centering
%     \dmntable{Calculate BMI}{U}             % Title and HP
%              {Weight(kgs),Length(m)}{BMI}   % Input, output
%              {---,---,weight/length*length} % Cell values
%     \caption{Example of a DMN table}
% \end{figure}
% \end{lstlisting}
% \begin{figure}[H]
%     \centering
%     \dmntable{Calculate BMI}{U}
%              {Weight(kgs),Length(m)}{BMI}
%              {---,---,weight/length*length}
%     \caption{Example of a DMN table}
% \end{figure}
% 
% 
% \begin{lstlisting}[language=tex, frame=single, basicstyle=\small]
% \begin{ligure}[H]
%     \centering
%     \dmntable{Decide BMI Level}{U}
%              {BMI}{BMI Level, Risk Level}
%              {$< 18.5$, Underweight, Increased,
%               $[18.5..24.9]$, Normal, Low,
%               $[25..29.9]$, overweight, Increased,
%               $[30..34.9]$, Obese I, High,
%               $[35..39.9]$, Obese II, Very High,
%               $> 39.9$, Extreme Obesity, Extremely High}
%     \caption{Example of a DMN table}
% \end{figure}
% \end{lstlisting}
% \begin{figure}[H]
%     \centering
%     \dmntable{Decide BMI Level}{U}
%              {BMI}{BMI Level, Risk Level}
%              {$< 18.5$, Underweight, Increased,
%               $[18.5..24.9]$, Normal, Low,
%               $[25..29.9]$, overweight, Increased,
%               $[30..34.9]$, Obese I, High,
%               $[35..39.9]$, Obese II, Very High,
%               $> 39.9$, Extreme Obesity, Extremely High}
%     \caption{Example of a larger DMN table}
% \end{figure}
% 
% \begin{lstlisting}[language=tex, frame=single, basicstyle=\tiny]
% \begin{figure}[H]
%     \centering
%     \dmntable{Decide Risk Level}{U}
%              {BMI level, Waist(cm)}{Risk Level}
%              {Overweight, $<=88$, Increased,
%               Overweight, $ > 88$, High,
%               Obese I,$ <= 88$, High,
%               Obese II, ---, Very High,
%               Extreme Obesity, ---, Extremely High,
%               {not(Overweight, Obese I, Obese II, Extreme Obesity)}, ---, Low
%              }
%     \caption{Example of cell with multiple values}
%     \label{ex2}
% \end{figure}
% \end{lstlisting}
% 
% \begin{figure}[H]
%     \centering
%     \dmntable{Decide Risk Level}{U}
%              {BMI level, Waist(cm)}{Risk Level}
%              {Overweight, $<=88$, Increased,
%               Overweight, $ > 88$, High,
%               Obese I,$ <= 88$, High,
%               Obese II, ---, Very High,
%               Extreme Obesity, ---, Extremely High,
%               {not(Overweight, Obese I, Obese II, Extreme Obesity)}, ---, Low
%              }
%     \caption{Example of cell with multiple values}
%     \label{ex2}
% \end{figure}
% 
% \begin{lstlisting}[language=tex, frame=single, basicstyle=\tiny]
% \begin{figure}[H]
%     \centering
%     \dmntable{Rule 1}{U}
%              {Age, Service Years}{Eligible1}
%              {$\leq18$, ---, Yes,
%               $\geq60$, ---, Yes,
%               {$[18, 60]$}, $\geq 30$, Yes
%              }
%     \dmntable{Rule 2}{U}
%              {Age, Service Years}{Eligible2}
%              {---,$\geq30$, Yes,
%               $\geq60$,$<30$, Yes
%              }
%     \dmntable{Rule 3}{U}
%              {Age, Service Years}{Eligible3}
%              {$< 45$, {$[15, 30)$}, Yes,
%               $> 45$, ---, Yes
%              }
%     \dmntable{Vacation days}{C+}
%              {Eligible1, Eligible2, Eligble3}{Vacation Days}
%              {---, ---, ---, 22,
%               Yes, ---, ---, 5,
%               ---, Yes, ---, 3,
%               No, ---, Yes, 2
%              }
%     \caption{dmn table example for the Vacation Days}
% \end{figure}
% \end{lstlisting}
% \begin{figure}[H]
%     \centering
%     \dmntable{Rule 1}{U}
%              {Age, Service Years}{Eligible1}
%              {$\leq18$, ---, Yes,
%               $\geq60$, ---, Yes,
%               {$[18, 60]$}, $\geq 30$, Yes
%              }
%     \dmntable{Rule 2}{U}
%              {Age, Service Years}{Eligible2}
%              {---,$\geq30$, Yes,
%               $\geq60$,$<30$, Yes
%              }
%     \dmntable{Rule 3}{U}
%              {Age, Service Years}{Eligible3}
%              {$< 45$, {$[15, 30)$}, Yes,
%               $> 45$, ---, Yes
%              }
%     \dmntable{Vacation days}{C+}
%              {Eligible1, Eligible2, Eligble3}{Vacation Days}
%              {---, ---, ---, 22,
%               Yes, ---, ---, 5,
%               ---, Yes, ---, 3,
%               No, ---, Yes, 2
%              }
%     \caption{DMN table example for the Vacation Days}
% \end{figure}
% 
% 
% \section{Contributing}
% 
% Contributions are always welcome.
% The project is hosted at \url{https://gitlab.com/Vadevesi/dmn-tex}

%
% \newgeometry{left=4cm}
%
% \section{Implementation}
%
%<@@=dmn>
% 
% \bigskip
%
% 
% We give the traditional declaration of a package written with |expl3|:
%    \begin{macrocode}
\RequirePackage{l3keys2e}
\ProvidesExplPackage
  {decision-table}
  {\dmnfiledate}
  {\dmnfileversion}
  {Table of decision}
%    \end{macrocode}
%
%    \begin{macrocode}
\RequirePackage { nicematrix }
%    \end{macrocode}
%
% \bigskip
% We define the command |\dmntable| with the tools of \pkg{xparse} (in 2020
% October, \pkg{xparse} will be included in the LaTeX kernel).
%    \begin{macrocode}
\NewDocumentCommand \dmntable { m m m m m }
  {
%    \end{macrocode}
% The clist (\emph{commad separated list}) |\l_@@_input_clist| is for the list
% of the names of the input fields. 
%    \begin{macrocode}
    \clist_clear_new:N \l_@@_input_clist
    \clist_set:Nn \l_@@_input_clist { #3 }
%    \end{macrocode}
%
% \bigskip
% The clist |\l_@@_output_clist| is for the list of the names of the output fields.
%    \begin{macrocode}
    \clist_clear_new:N \l_@@_output_clist
    \clist_set:Nn \l_@@_output_clist { #4 }
%    \end{macrocode}
%
% \bigskip
% The integer |\l_@@_input_int| is the number of the input fields.
%    \begin{macrocode}
    \int_zero_new:N \l_@@_input_int
    \int_set:Nn \l_@@_input_int { \clist_count:N \l_@@_input_clist }
%    \end{macrocode}
%
% \bigskip
% The integer |\l_@@_output_int| is the number of the output fields.
%    \begin{macrocode}
    \int_zero_new:N \l_@@_output_int
    \int_set:Nn \l_@@_output_int { \clist_count:N \l_@@_output_clist }
%    \end{macrocode}
% 
%
% \bigskip
% The sequence |\l_@@_cells_seq| is the sequence of all the cells of the
% ``body'' of the tabular. 
%    \begin{macrocode}
    \seq_clear_new:N \l_@@_cells_seq
    \seq_set_split:Nnn \l_@@_cells_seq { , } { #5 }
%    \end{macrocode}
% 
% 
% \vspace{1cm}
% Now, we will begin the construction of the tabular (a |{NiceTabular}| of
% \pkg{nicematrix}). 
% 
% \bigskip
% The command |\use:x| will expand its argument. Indeed, the preamble of the
% |{NiceTabular}| (which has the same format as a preamble of |{tabular}| must
% be computed before the execution of the |\begin{NiceTabular}|.
%    \begin{macrocode}
    \use:x
      {
        \exp_not:N \begin { NiceTabular } 
%    \end{macrocode}
% Here is the preamble of the tabular. The command |\prg_replicate:nn| is
% expandable and hence will be expanded by the |use:x|.
%    \begin{macrocode}
        { r \prg_replicate:nn { \l_@@_input_int + \l_@@_output_int } l }
%    \end{macrocode}
% Here is the list of options of the |{NiceTabular}| (a standard tabular of
% |{array}| don't have such list of options. Once again, we have to compute some
% quantities in this list of options before the execution of |\begin{NiceTabular}|.
%    \begin{macrocode}
        [ 
%    \end{macrocode}
% The key |hvlines-except-corners| will draw all the rules of the tabular,
% excepted in the (upper right) corner.
%    \begin{macrocode}
          hvlines-except-corners ,
%    \end{macrocode}
% The key |code-before| of |{NiceTabular}| contains instructions to color the
% cells \emph{before} the rules (doing so, the resulting \textsc{pdf} gives
% better results in the \textsc{pdf} viewers).
%    \begin{macrocode}
          code-before = 
%    \end{macrocode}
% First, a |\rectanglecolor| for the labels of the ``input'' fields. The command
% |\int_eval:n| is expandable and, hence, will be expanded by the |\use:x|. On
% the other side, we have to prevent the expansion of |\rectanglecolor| which,
% in fact, at that point is not defined (it will be defined by \pkg{nicematrix}
% after the construction of the array).
%    \begin{macrocode}
           \exp_not:N \rectanglecolor 
             { blue!10!green!60!black!30 }
             { 2 - 2 }
             { 2 - \int_eval:n { \l_@@_input_int + 1 } }
%    \end{macrocode}
% A |\rectanglecolor| for the labels of the ``output'' fields.
%    \begin{macrocode}
           \exp_not:N \rectanglecolor 
             { green!30!blue!15 }
             { 2 - \int_eval:n { \l_@@_input_int + 2 } }
             { 2 - \int_eval:n { \l_@@_input_int + \l_@@_output_int + 1 } }
        ]
      }
%    \end{macrocode}
% Now, we begin the body of the tabular (the environment |{NiceTabular}|).
% 
% \medskip
% The body begins by a |\multicolumn| for the title. However, we have to compute
% the number of cells of that |\multicolumn|. That's why we have to expand the
% first argument of the |\multicolumn| before executing the |\multicolumn|.
% However, we have to do that in an expandable way in order to prevent the
% functionnality of the |\multicolumn| (which internally give a |\omit| of TeX).
% That's why we have to use |\exp_args:Ne| (|\exp_args:Nx| would not do the job).
%    \begin{macrocode}
    \exp_args:Ne \multicolumn 
      { \int_eval:n { \l_@@_input_int + 1 } } 
      { l } 
      { #1 } \\
%    \end{macrocode}
% |#2| is the \emph{hit policy}.
%    \begin{macrocode}
    #2 & 
%    \end{macrocode}
% Now, the fields (``input fields'' and ``output fields''). By using
% |\clist_use:Nn|, we replace the commas by ampersands (|&|).
%    \begin{macrocode}
    \clist_use:Nn \l_@@_input_clist { & } & 
    \clist_use:Nn \l_@@_output_clist { & } \\
%    \end{macrocode}
% Now, all the rows corresponding to the rules. We begin a loop over all the
% cells with |\seq_map_inline:Nn|. 
%    \begin{macrocode}
    1 & 
    \seq_map_inline:Nn \l_@@_cells_seq
      {
%    \end{macrocode}
% |\c@jCol| and |\c@iRow| are counters provided by |{NiceTabular}| for the
% current column and the current row. If you are in the first column, we insert
% the number of rule.
%    \begin{macrocode}
        \int_compare:nT { \c@jCol = 0 } { \int_eval:n { \c@iRow - 1 } & } 
%    \end{macrocode}
% Now, we add one composante of |\l_@@_cells_seq|.
%    \begin{macrocode}
        ##1
%    \end{macrocode}
% Before the following cell, we have, of course, to add |\\| (if we are at the
% end of the row) or |&| (elsewhere).
%    \begin{macrocode}
        \int_compare:nTF { \c@jCol = \l_@@_input_int + \l_@@_output_int + 1 } 
          { \\ } 
          { & }
      }
    \end { NiceTabular }
  }
%    \end{macrocode}
% 
%
%
% \tableofcontents
%
% \end{document}
% 
% Local Variables:
% TeX-fold-mode: t
% TeX-fold-preserve-comments: nil
% flyspell-mode: nil
% fill-column: 80
% End:






