% ==========================================
\documentclass[11pt,final,a4paper]{article}
\input{config.tex}
\begin{document}
\title{The Black Box Toolkit\\Package Developers' Guide}
\date{\today}
\author{Laurent Guigues}
\maketitle
% ==========================================
\tableofcontents
% ==========================================


% ==========================================
%\section*{Abstract}
% ==========================================
\newpage
% ==========================================
% ==========================================
\vspace{0.5cm}\hrule
%\section{Creating your own black boxes}
%\label{bbp}
% ==========================================

% ==========================================
\section{Steps in the creation of new black boxes}
% ==========================================
Any black box must be included in a \bbtk package, 
that is in a particular shared library which can be loaded 
dynamically by \bbtk (hence applications which use \bbtk, 
such as the interpreter \bbi). 

\begin{enumerate}
\item \textbf{Create a new package. }
Before defining any black box you  
have to create a package, or more precisely  
the files which will allow you to generate the package 
(compile and link the shared library) and may be install it. 
The utility \texttt{bbCreatePackage} does it for you. 

Two cases occur :
\begin{itemize}
\item The black boxes you want to create are based on 
a processing code (\CPP classes or \C functions) which 
is in an existing project handled by \cmake
and you want the new package to be part of your existing project. 
You will have to create your new package into the source tree of your 
project. 
\item You do not have an already existing project (you want 
to create the new boxes from scratch) or you want/are imposed  
that the existing project remain external to the package project.
You will have to create your new package in a new location and 
may be include/link against existing libraries. 
\end{itemize}

\item \textbf{Describe your new box. }
You can do it either :
\begin{itemize}
\item In \CPP code. You will have to write the class for 
your box, mostly using \bbtk macros.  
\item In \xml code. 
When configuring your project with \cmake, 
the utility \bbfy will then generate the corresponding \CPP code. 
\end{itemize}

\end{enumerate}

% ==========================================
\section{Creating a new black box package}
% ==========================================

The utility \texttt{bbCreatePackage} allows to 
create a new void package. It is a command script 
(a \texttt{bash} script on linux,
a \texttt{dos} script on windows) 
which creates the directory structure and the \texttt{cmake} 
files necessary to build the project.

Its usage is as follows :
\begin{verbatim}
> bbCreatePackage <package-folder-with-complete-path>
\end{verbatim}

You must then decide the name of your new package. 
It will be the name used to load the package in \texttt{bbi}. 

For example, on linux, turn to an empty folder and type :

\begin{verbatim}
> bbCreatePackage pack1
\end{verbatim}

you get :

\begin{verbatim}
----- Creating new black box package in 'pack1' -----
-> Creating directory 'pack1'
-> Creating file 'pack1/CMakeLists.txt'
-> Creating directory 'pack1/cmake'
-> Copying files in 'pack1/cmake'
Done !
Edit the file 'pack1/CMakeLists.txt' to customize your package
\end{verbatim}

the file tree obtained is :

\begin{verbatim}
> tree pack1
pack1/
|-- CMakeLists.txt
`-- cmake
    |-- Configure.cmake
    |-- ConfigurePackage.cmake
    |-- FindGTK2.cmake
    |-- FindLibrary.cmake.in
    |-- FindwxW.cmake
    |-- GenerateLibraryConfig.cmake
    |-- InstallLibrary.cmake
    |-- LibraryConfig.cmake.in
    |-- UseLibrary.cmake.in
    |-- bbPackage.cxx.in
    `-- bbPackage.h.in

1 directory, 12 files
\end{verbatim}

The directory \texttt{pack1} is the directory of your new package, 
in which you will create the files describing your black boxes.
But first, you have to customize your new package, by editing the file 
\texttt{CMakeLists.txt} in the \texttt{pack1} directory.
This file contains :

\begin{file}{pack1/CMakeLists.txt}
\small
\begin{verbatim}
#...........................................................................
# CMake configuration file for a bbtk package. 
# Automatically generated by bbCreatePackage.
# - The lines which are between dotted comments (#...) must be left unchanged 
# - The lines which are between dashed comments (#===) can be edited to customize the package
#...........................................................................

#...........................................................................
INCLUDE(cmake/Configure.cmake)
#...........................................................................


#===========================================================================
# THE NAME OF THE PACKAGE 
SET(PACKAGE_NAME Example)
#===========================================================================


#...........................................................................
# DOES THE USER WANT TO BUILD THE PACKAGE ?
OPTION(BUILD_PACKAGE_${PACKAGE_NAME} 
  "Build the bbtk package ${PACKAGE_NAME}" ON)
#...........................................................................

#...........................................................................
# IF THE USER HAS CHOSEN TO BUILD THE PACKAGE
IF(BUILD_PACKAGE_${PACKAGE_NAME})
  #...........................................................................


  #===========================================================================
  # UNCOMMENT NEXT LINES IF THE PACKAGE USES THE VTK LIBRARY, THE ITK LIB, ETC.
  # SET(${PACKAGE_NAME}_USE_VTK  ON)
  # SET(${PACKAGE_NAME}_USE_ITK  ON)
  # SET(${PACKAGE_NAME}_USE_GDCM ON)
  # SET(${PACKAGE_NAME}_USE_GSMIS ON)
  # SET(${PACKAGE_NAME}_USE_WXWIDGETS ON)
  #===========================================================================
  
  #===========================================================================
  # PACKAGE AUTHOR : PREFERABLY PROVIDE YOUR EMAIL ADDRESS
  SET(PACKAGE_AUTHOR "foo.bar@creatis.insa-lyon.fr")
  #===========================================================================

  #===========================================================================
  # PACKAGE DESCRIPTION
  SET(PACKAGE_DESCRIPTION "My marvelous black box package")
  #===========================================================================

  #===========================================================================
  # PACKAGE VERSION NUMBER 
  SET(PACKAGE_MAJOR_VERSION 1)
  SET(PACKAGE_MINOR_VERSION 0)
  SET(PACKAGE_BUILD_VERSION 0)
  #===========================================================================
 
  #===========================================================================
  # THE xml SOURCES OF THE PACKAGE
  # EITHER UNCOMMENT NEXT LINE TO bbfy ALL .xml OF THE DIRECTORY :
  SET(COMPILE_ALL_XML ON)
  # ... OR LIST THE FILES TO COMPILE MANUALLY :
  #SET(PACKAGE_XML_SOURCES
    # LIST HERE THE FILES TO bbfy TO BUILD THE LIB
    # E.G. TO bbfy "toto.xml" ADD "toto" (NO EXTENSION)
  #    )
  #===========================================================================

  #===========================================================================
  # THE C++ SOURCES OF THE PACKAGE
  # EITHER UNCOMMENT NEXT LINE TO COMPILE ALL .cxx OF THE DIRECTORY :
  SET(COMPILE_ALL_CXX ON)
  # ... OR LIST THE FILES TO COMPILE MANUALLY :
  #SET(PACKAGE_CXX_SOURCES
    # LIST HERE THE FILES TO COMPILE TO BUILD THE LIB
    # E.G. TO COMPILE "toto.cxx" ADD "toto" (NO EXTENSION)
  #    )
  #===========================================================================

  #===========================================================================
  INCLUDE_DIRECTORIES(
    # LIST HERE YOUR ADDITIONAL INCLUDE DIRECTORIES (EXCEPT BBTK'S)
    )
  #===========================================================================
 
  #===========================================================================
  SET(PACKAGE_LIBS 
    # LIST HERE THE ADDITIONAL LIBS TO LINK AGAINST (EXCEPT BBTK'S)   
    )
  #===========================================================================

  
  #...........................................................................
  INCLUDE(cmake/ConfigurePackage.cmake)
  #...........................................................................
  
ENDIF(BUILD_PACKAGE_\${PACKAGE_NAME})
#...........................................................................
# EOF
#...........................................................................
\end{verbatim}
\end{file}

The comments in the file should be easily understandable !
You have to customize the lines which are enclosed 
between dashed comment lines.
In these sections, you can set :
\begin{itemize}
\item The \textbf{name} of your package. This will be the name used to load it in \bbi. The shared library however will be called \texttt{bb}name hence on 
\lin the object file will be called \texttt{libbb}name\texttt{.so} 
and on \win it  will be called \texttt{bb}name\texttt{.dll}.
\item The \textbf{libraries used} by the package : \vtk, \itk, \gdcm, \gsmis, \wx. The mecanisms to find these libraries, their sources and to link against them are automatically handled by the \cmake files installed by \bbCreatePackage. You just have to uncomment a line to use one of these libraries.
\item The \textbf{author(s)} of the package. Preferably provide e-mail adresses.
\item A \textbf{description} of the package, which will appear in the help of your package or in its html documentation automatically generated by \bbdoc.
\item The \textbf{version} of the package.
\item The \textbf{\xml sources} of the package : you can list each input \xml file explicitly or tell \cmake to include in the project all the \xml files of the directory. 
\item The \textbf{\CPP sources} of the package :  you can list each input \CPP file explicitly or tell \cmake to include in the project all the \CPP files of the directory.
\item \textbf{Additional include directories}. Set it if your package needs to include source files which are not in the package directory, typically if it depends on another library which is not one the libraries automatically handled (\vtk, \itk...).
\item \textbf{Additional libraries} to link against. Set it if your package needs to link against another library which is not one the libraries automatically handled (\vtk, \itk...).
\end{itemize}

Of course, this is only a framework and you can add any other \cmake commands
in the file.

% ==========================================
\section{Creating a new box}
% ==========================================

% ==========================================
\subsection{Principles}
% ==========================================

\subsubsection{\texttt{C++} or \texttt{XML} ?}
There are two ways to create a new black box in an existing package :
\begin{itemize}
\item Write an \xml description file which will be automatically 
translated in \CPP by the \bbfy application (recommanded).
\item Write the \CPP code of the box using \bbtk macros.
\end{itemize}

\subsubsection{From which \bbtk class inherit ?}

Apart from this choice of the description langage to use, 
there is an important choice to do concerning the implementation of the box. 
In \CPP, a black box is nothing but a class which has the standard 
interface of all black boxes : what's its name ? inputs ? outputs ? and so on. 
The abstract description of this interface is done in the class 
\texttt{bbtk::BlackBox} and is implemented in its child classes : 
\texttt{bbtk::UserBlackBox} and \texttt{bbtk::WxBlackBox} 
\footnote{all the classes of the \bbtk library are in a \emph{namespace} 
called \texttt{bbtk} 
and the \CPP header of a class called \texttt{NameOfAClass} is 
in the file called \texttt{bbtkNameOfAClass.h}}.
To create a new black box, you have to inherit one of these two  
concrete classes in order to inherit the black box interface and a 
particular implementation of this interface. 
If your black box is a \emph{Widget} black box, 
that is a black box which has (or is) 
a piece of a graphical interface based on the \wx library,
then it must inherit the class \texttt{bbtk::WxBlackBox}. 
Concretely, a \texttt{bbtk::WxBlackBox} is associated 
a \texttt{wxWindow} and must be able to return a pointer to it. 
If your black box is not a widget black box, it must inherit 
\texttt{bbtk::UserBlackBox}.

\subsubsection{Inherit or encapsulate ?}

Now, your black box will do something (hopefully !). 
When you decide to write a new black box, 
you should be in one of these three cases :
\begin{enumerate}
\item You already have a \texttt{C}-like function which 
does the processing that you wish to 'blackboxify'
\item You already have a \CPP class which 
does the processing that you wish to 'blackboxify'
\item You start from scratch without any existing code
\end{enumerate}

The idea of \BBTK is to embed processing codes into 
\CPP objects which have a standard and generic interface - 
namely black boxes - to be able to chain arbitrary 
processes afterwards. 

In \CPP, in order to embed an existing processing \emph{class}  
into a standard interface you only have two possibilities :
\begin{enumerate}
\item {\bf Inherit} the existing processing class 
\emph{and} the interface class (e.g. \texttt{bbtk::UserBlackBox}).
In this case you have to :
\begin{enumerate}
\item make the link between the inputs and outputs of the black box 
and the interface of the inherited class
\item call the processing 
method of the inherited class in the processing method of the black box.
\end{enumerate}
\item {\bf Encapsulate} the existing processing class 
in a class inherited from 
the interface class (e.g. \texttt{bbtk::UserBlackBox}).
In this case you have to :
\begin{enumerate}
\item declare it as a member of the black box, 
\item instantiate it at the right time 
(either in the constructor or in the processing method of the black box)
\item in the processing method of the black box : 
\begin{enumerate}
\item set the inputs of the member procesing class with the inputs of the black box, 
\item call the processing method of the encapsulated class  
\item set the ouputs of the black box with the outputs of the encapsulated 
class.
\end{enumerate}
\end{enumerate}
\end{enumerate}

If you wish to 'blackboxify' a C-like \emph{function}, 
you do not have the choice, you can only use the second mechanism, 
namely encapsulation.

Obviously, the inheritance mechanism is more powerfull 
and - when it is possible to use it - it demands less effort 
because, as we will see, in \bbtk you can directly 
link the accessors to the input and output data of the box 
to the accessors of the inherited processing class, 
as well as the procesing method of the black box 
to the processing method of the inherited processing class, 
very much like a callback mechanism.

\subsubsection{Informations to provide}

Finally, to create a new black box, you will have to give :
\begin{enumerate}
\item The {\bf name} of the box 
\item The {\bf author} of the box
\item A {\bf description} of the box
\item The {\bf package} to which the box belongs (can we do it automatically ? LG : think about it)
\item Its {\bf parent black box}, either \texttt{bbtk::UserBlackBox} or \texttt{bbtk::WxBlackBox} 
\item $[$Optional$]$ The additional {\bf include files} which are necessary for the code to compile (classes or functions declarations ...)  
\item $[$Optional$]$ The other {\bf parent(s)} of the box (which must be known hence their header included)
\item $[$Optional$]$ The {\bf namespace} to which the box belongs
\item The box {\bf inputs} and {\bf outputs}, and for each one :
\begin{enumerate}
\item Its {\bf name} : the string which will identify the input or output
\item Its {\bf type} : any \CPP type, either a basic type or a user defined type (class ...) but which must be known, hence the necessary files must be included.  
\item Its {\bf help} : a string describing the input / output 
\end{enumerate}
\item Its {\bf processing} code, which can be a simple callback or an arbitrary complex code
\end{enumerate}

\subsubsection{Input and output accessors}

When you encapsulate a processing class or a C function 
or when you write down a black box from scratch, 
you must access the inputs and outputs of the black box, 
in order to interface it manually with your processing method 
or simply write your processing code 
(there are other cases in which you also need to access the 
inputs and outputs, we will talk about them later). 

The only thing you must know about the \CPP code generated 
from your \xml or your \CPP macro-based description 
is that when you declare an input 
or an output of a black box then 
two \emph{accessors} for this input or output are generated : 
one to \emph{get} the value of the input or output and 
one to \emph{set} it. 
These accessors have normalized names : 

\begin{itemize} 
\item The declaration of an {\bf input} called \texttt{NAME} and 
of type \texttt{TYPE} generates the two accessors
\footnote{For the sake of simplicity, the parameters and return value are 
shown here as if they were all passed by value. 
However the actual code can use references. 
The same way, the issue of const or non const methods is eluded here. 
Different cases occur in practice.}: 
\begin{itemize} 
\item \texttt{void bbSetInput<NAME>(<TYPE>);} 
\item \texttt{<TYPE> bbGetInput<NAME>();} 
\end{itemize}
\item The declaration of an {\bf output} called \texttt{NAME} and  
of type \texttt{TYPE} generates the two accessors:
\begin{itemize} 
\item \texttt{void bbSetOutput<NAME>(<TYPE>);} 
\item \texttt{<TYPE> bbGetOutput<NAME>();} 
\end{itemize}
\end{itemize}

For example, declaring an input called \texttt{Image} 
would generate the two accessors \texttt{bbSetInputImage} and 
\texttt{bbGetInputImage}. 

Note that 
\begin{itemize}
\item All \bbtk methods are prefixed by \texttt{bb} 
to avoid conflicts with potential inherited methods. 
\item An input and an output can have the same name (e.g. 'Image').
No conflict between accessors occur (e.g. 
four distinct accessors are created : 
\texttt{bbSetInputImage}, 
\texttt{bbGetInputImage}, 
\texttt{bbSetOutputImage} and 
\texttt{bbGetOutputImage}).
\end{itemize}

% ==========================================
\subsection{\texttt{XML} description of a box}
% ==========================================

% ==========================================
\subsubsection{General \texttt{xml} tags}
% ==========================================

Let us examine the \texttt{xml} file 
describing the \texttt{Add} box of the \texttt{std} package :

\begin{file}{\texttt{src/packages/std/bbAdd.xml}}
\small
\begin{verbatim}
<?xml version="1.0" encoding="iso-8859-1"?>

<blackbox name="Add">

  <description> Adds its inputs                      </description>
  <author>      laurent.guigues@creatis.insa-lyon.fr </author>
  <package>     std                                  </package>

  <parentblackbox> bbtk::UserBlackBox                </parentblackbox>
  <namespace>      bbstd                             </namespace>

  <input  name="In1" type="double">  First number to add   </input>
  <input  name="In2" type="double">  Second number to add  </input>
  <output name="Out" type="double">  Result                </output>

  <process><PRE>
    bbSetOutputOut( bbGetInputIn1() + bbGetInputIn2() );
  </PRE></process>

</blackbox>
\end{verbatim}
\end{file}

The tags and their role are easily understandable.

As the box is not a widget, we inherit from 
\texttt{bbtk::UserBlackBox} (\texttt{parentblackbox} tag).

Note that we decided to include the generated class 
into the namespace \texttt{bbstd}.

The only part of the file which demand a bit of explaination is 
the body of the \texttt{process} tag, which describes the 
actual code to execute in the box. 
This code must be enclosed in a \texttt{<PRE></PRE>} tag 
to tell the \xml parser not to interpret it as \xml instructions. 
This is necessary to be able to use any symbol, 
like the \texttt{<} and \texttt{>} which have a 
special meaning in \xml. 
In the case of the \texttt{Add} box, the process code 
is very simple : remember that 
\texttt{bbGetInputIn1} is the 
accessor to the input \texttt{In1} declared above and 
\texttt{bbGetInputIn2} is the 
accessor to the input \texttt{In2};  
the code simply adds the values of the two inputs 
and sets the output \texttt{Out} with the resulting value.

To describe your own black boxes in \xml code, 
you can use the template \texttt{xml} file 
\texttt{examples/TEMPLATE\_bbPackagenameBoxname.xml}.

If \texttt{MyPack} is the name of your package and 
\texttt{MyBox} the name of your box, then :
\begin{enumerate}
\item Copy this file in your package folder with 
the normalised name \texttt{bbMyPackMyBox.xml} 
\item Replace each occurrence of \$PACKAGENAME\$ by MyPack 
and each occurrence of \$BOXNAME\$ by MyBox.
\item Fill in the description and author tags
\item Create your inputs and outputs
\item Fill in the process tag 
\end{enumerate} 


% ==========================================
\subsubsection{Specific \texttt{xml} tags for \texttt{itk::ImageToImageFilter} classes bbfication}
% ==========================================

% ==========================================
\subsubsection{Specific \texttt{xml} tags for \texttt{vtkImageAlgorithm} classes bbfication}
% ==========================================

% ==========================================
\subsubsection{Specific \texttt{xml} tags for \texttt{vtkPolyDataAlgorithm} classes bbfication}
% ==========================================

% ==========================================
\subsubsection{\bbfy \texttt{xml} tags reference}
% ==========================================

% ==========================================
\begin{table}[!ht]
\caption{\label{xml_tags}
\bbfy \texttt{xml} tags reference}
\small
\begin{tabular}{|lcllm{6cm}|}
\hline
Tag & Attributes & Condition & Multiplicity & Description
 \\ \hline

\texttt{<blackbox>} & \texttt{name} & - & 1 & The name of the box \\ \hline
		& \texttt{type} & - & 1 & The type of the box. In: 
	\{\texttt{standard} (default), 
\texttt{itkImageToImageFilter},
\texttt{vtkImageAlgorithm},
\texttt{vtkPolyDataAlgorithm}\} \\\hline
& \texttt{generic} & a) & 0-1 &
Generate the generic filter (see text)\\ \hline 

\texttt{<description>} & - & - & 0-n &  The description of the box. Multiple occurrence are concatenated \\\hline 
\texttt{<author>} & - & - & 0-n &  The author of the box. Multiple occurrence are concatenated \\\hline 
\texttt{<parentblackbox>} & - & - & 1 &  The parent black box of the box.
In: \{\texttt{bbtk::BlackBox, bbtk::WxBlackBox, bbtk::WxContainerBlackBox}\}\\\hline 
\texttt{<package>} & - & - & 1 &  The package of the box \\\hline 
\texttt{<namespace>} & - & - & 0-1 &  The namespace of the box. 
Use \texttt{bbPACKAGE}, where \texttt{PACKAGE} is the name of the package\\\hline 
\texttt{<include>} & - & - & 0-n & Additionnal file to include 
(generates : \texttt{\#include 'value'})\\\hline 

\texttt{<template>} & - & - & 0-n &  Template parameter of the box. The template parameter list is generated in the order of appearance of the tag. \\\hline 

\texttt{<itkparent>} &  - & a) &  1 & The parent itk class (with namespace) \\\hline

\texttt{<vtkparent>} &  - & b) &  1 & The parent vtk class \\\hline

\texttt{<input>} & \texttt{name} & - & 1 &  The name of the input \\\hline 
	 & \texttt{type} & - & 1 &  The type of the input \\\hline 
	 & \texttt{special} & - & 0-1 & In: \{\texttt{``itk input'', 
``vtk input'', ``itk parameter'', ``vtk parameter''}\} (see below).\\\hline 
	 & \texttt{generic\_type} & c) & 0-1 & The ``generic'' type of the input (see text). \\\hline 
\texttt{<output>} & \texttt{name} & - & 1 &  The name of the output \\\hline 
	 & \texttt{type} & - & 1 &  The type of the output \\\hline 
	 & \texttt{special} & - & 0-1 & In: \{\texttt{``itk output'', 
``vtk output''}\} (see below).\\\hline 
	 & \texttt{generic\_type} & c) & 0-1 & The ``generic'' type of the output (see text).\\\hline 

\texttt{<process>} & - & - & 0-1 & The code of the processing method of the box. Must be put between clear tags : \texttt{<PRE></PRE>} \\\hline 

\end{tabular}
\end{table}

% ==========================================
\begin{table}[!ht]
\caption{\label{xml_tags}
\bbfy \texttt{xml} tags conditions}
\small
\begin{tabular}{|ll|}
\hline
a) & \texttt{<blackbox type == ''itkImageToImageFilter''>} \\ \hline
b) & \texttt{<blackbox type == ''vtkImageAlgorithm'' or ''vtkPolyDataAlgorithm''>} \\ \hline
c) & \texttt{<blackbox type == ''itkImageToImageFilter''>} and 
 	\texttt{<blackbox generic>} is present. \\ \hline
\end{tabular}
\end{table}


% ==========================================
\subsection{\CPP description of a box}
% ==========================================

TO DO...

Look at the files \texttt{examples/TEMPLATE\_bbPackagenameBoxname.h/cxx}
and \texttt{examples/TEMPLATE\_bbPackagenameWXBoxname.h/cxx}

%\bibliography{all}



%\section{Conclusion}
\end{document}

