]> Creatis software - bbtk.git/blob - samples/SampleInterpreter/bbtkSampleInterpreter.cxx
Feature #1774
[bbtk.git] / samples / SampleInterpreter / bbtkSampleInterpreter.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 //=========================================================================
29 // How to create and use a script defined black box
30 //=========================================================================
31
32 //=========================================================================
33 #include <bbtkInterpreter.h>
34 //=========================================================================
35
36 //=========================================================================
37 int main(int argv, char* argc[])
38 {
39   // To track all ...
40   //bbtk::MessageManager::SetMessageLevel("all",9);
41   
42   try
43     {
44       // Create an interpreter
45       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
46       
47       // We tell the interpreter to throw exceptions on error
48       I->SetThrow(true);
49
50       // Interpret the file supposed to define a box called 'Processing'
51       I->InterpretFile("bbProcessing.bbs");
52       
53       // Create an instance 'p' of the 'Processing' box
54       bbtk::BlackBox::Pointer p 
55         = I->GetExecuter()->GetFactory()->NewBlackBox("Processing","p");
56       
57       // Prompt the user
58       std::cout << "Enter a number : ";
59       double num = 0;
60       std::cin >> num;
61
62       // Set its input 'In' to num
63       p->bbSetInput("In",num);
64
65       // Execute it
66       p->bbExecute();
67       
68       // Print out the output 'Out':
69       // 1) We get the output with the generic bbGetOutput method.
70       // 2) It returns a bbtk::Data which can store any data type
71       // 3) We get the value of the data as a double using get<TYPE>()
72       // (because we know it's a double !)
73       double v = p->bbGetOutput("Out").get<double>();
74       
75       std::cout << num << "+1 = "<<v<<std::endl;
76         
77       //============================================================
78       // Second example : imagine the bbProcessing.bbs script has created
79       // the box "a" of type Processing
80       // Here we simulate it using:
81       I->InterpretLine("new Processing a");
82       // We get a the black box "a" in the workspace:
83       bbtk::BlackBox::Pointer a 
84         = I->GetExecuter()->GetWorkspace()->GetPrototype()->bbGetBlackBox("a");
85       // Then the same as previously...
86       a->bbSetInput("In",num);
87       a->bbExecute();
88       double w = p->bbGetOutput("Out").get<double>();
89       std::cout << "In case you did not understand:"<<std::endl<<num << "+1 = "<<w<<std::endl;
90
91       // That's all !
92    }
93   catch (bbtk::Exception e)
94     {
95       std::cout << "* ERROR : "<<e.GetErrorMessage()<<std::endl;
96       return 1;
97     }
98   return 0; 
99
100
101   // To get the list of bbtk object still allocated after main ends
102   // bbtk::StaticInitTime::PrintObjectListInfo = true;
103   // bbtk::MessageManager::SetMessageLevel("object",1);
104 }
105 //=========================================================================