Salome HOME
Minor source/doc coherency corrections for variable order and paths
[modules/adao.git] / src / daComposant / daCore / Aidsm.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2018 EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 """
24     Normalized interface for ADAO scripting (generic API)
25 """
26 __author__ = "Jean-Philippe ARGAUD"
27 __all__ = ["Aidsm"]
28
29 import os
30 import sys
31 #
32 from daCore.BasicObjects import State, Covariance, FullOperator, Operator
33 from daCore.BasicObjects import AlgorithmAndParameters, DataObserver
34 from daCore.BasicObjects import DiagnosticAndParameters, CaseLogger
35 from daCore import PlatformInfo
36 #
37 from daCore import ExtendedLogging ; ExtendedLogging.ExtendedLogging() # A importer en premier
38 import logging
39
40 # ==============================================================================
41 class Aidsm(object):
42     """ ADAO Internal Data Structure Model """
43     def __init__(self, name = "", addViewers=None):
44         self.__name = str(name)
45         self.__case = CaseLogger(self.__name, "case", addViewers)
46         #
47         self.__adaoObject   = {}
48         self.__StoredInputs = {}
49         #
50         self.__Concepts = [
51             "AlgorithmParameters",
52             "Background",
53             "CheckingPoint",
54             "ControlInput",
55             "Observation",
56             "Controls",
57             "BackgroundError",
58             "ObservationError",
59             "EvolutionError",
60             "ObservationOperator",
61             "EvolutionModel",
62             "ControlModel",
63             "Debug",
64             "NoDebug",
65             "Observer",
66             ]
67         #
68         for ename in self.__Concepts:
69             self.__adaoObject[ename] = None
70         for ename in ("ObservationOperator", "EvolutionModel", "ControlModel"):
71             self.__adaoObject[ename] = {}
72         for ename in ("Diagnostic", "Observer"):
73             self.__adaoObject[ename]   = []
74             self.__StoredInputs[ename] = []
75         #
76         # Récupère le chemin du répertoire parent et l'ajoute au path
77         # (Cela complète l'action de la classe PathManagement dans PlatformInfo,
78         # qui est activée dans Persistence)
79         self.__parent = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
80         sys.path.insert(0, self.__parent)
81         sys.path = PlatformInfo.uniq( sys.path ) # Conserve en unique exemplaire chaque chemin
82
83     def set(self,
84             Concept              = None, # Premier argument
85             Algorithm            = None,
86             AppliedInXb          = None,
87             AvoidRC              = True,
88             BaseType             = None,
89             Checked              = False,
90             Diagnostic           = None,
91             DiagonalSparseMatrix = None,
92             Identifier           = None,
93             Info                 = None,
94             Matrix               = None,
95             ObjectFunction       = None,
96             ObjectMatrix         = None,
97             OneFunction          = None,
98             Parameters           = None,
99             ScalarSparseMatrix   = None,
100             Scheduler            = None,
101             Script               = None,
102             Stored               = False,
103             String               = None,
104             Template             = None,
105             ThreeFunctions       = None,
106             Unit                 = None,
107             Variable             = None,
108             Vector               = None,
109             VectorSerie          = None,
110             ):
111         "Interface unique de definition de variables d'entrees par argument"
112         self.__case.register("set",dir(),locals(),None,True)
113         try:
114             if   Concept in ("Background", "CheckingPoint", "ControlInput", "Observation", "Controls"):
115                 commande = getattr(self,"set"+Concept)
116                 commande(Vector, VectorSerie, Script, Stored, Scheduler, Checked )
117             elif Concept in ("BackgroundError", "ObservationError", "EvolutionError"):
118                 commande = getattr(self,"set"+Concept)
119                 commande(Matrix, ScalarSparseMatrix, DiagonalSparseMatrix,
120                          Script, Stored, ObjectMatrix, Checked )
121             elif Concept == "AlgorithmParameters":
122                 self.setAlgorithmParameters( Algorithm, Parameters, Script )
123             elif Concept == "Debug":
124                 self.setDebug()
125             elif Concept == "NoDebug":
126                 self.setNoDebug()
127             elif Concept == "Observer":
128                 self.setObserver( Variable, Template, String, Script, Info, ObjectFunction, Scheduler )
129             elif Concept == "Diagnostic":
130                 self.setDiagnostic( Diagnostic, Identifier, Parameters, Script, Unit, BaseType )
131             elif Concept == "ObservationOperator":
132                 self.setObservationOperator(
133                     Matrix, OneFunction, ThreeFunctions, AppliedInXb,
134                     Parameters, Script, Stored, AvoidRC, Checked )
135             elif Concept in ("EvolutionModel", "ControlModel"):
136                 commande = getattr(self,"set"+Concept)
137                 commande(
138                     Matrix, OneFunction, ThreeFunctions,
139                     Parameters, Script, Scheduler, Stored, AvoidRC, Checked )
140
141             else:
142                 raise ValueError("the variable named '%s' is not allowed."%str(Concept))
143         except Exception as e:
144             if isinstance(e, SyntaxError): msg = "at %s: %s"%(e.offset, e.text)
145             else: msg = ""
146             raise ValueError("during settings, the following error occurs:\n"+\
147                              "\n%s %s\n\nSee also the potential messages, "+\
148                              "which can show the origin of the above error, "+\
149                              "in the launching terminal."%(str(e),msg))
150
151     # -----------------------------------------------------------
152
153     def setBackground(self,
154             Vector         = None,
155             VectorSerie    = None,
156             Script         = None,
157             Stored         = False,
158             Scheduler      = None,
159             Checked        = False):
160         "Definition d'un concept de calcul"
161         Concept = "Background"
162         self.__case.register("set"+Concept, dir(), locals())
163         self.__adaoObject[Concept] = State(
164             name               = Concept,
165             asVector           = Vector,
166             asPersistentVector = VectorSerie,
167             asScript           = Script,
168             scheduledBy        = Scheduler,
169             toBeChecked        = Checked,
170             )
171         if Stored:
172             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
173         return 0
174
175     def setCheckingPoint(self,
176             Vector         = None,
177             VectorSerie    = None,
178             Script         = None,
179             Stored         = False,
180             Scheduler      = None,
181             Checked        = False):
182         "Definition d'un concept de calcul"
183         Concept = "CheckingPoint"
184         self.__case.register("set"+Concept, dir(), locals())
185         self.__adaoObject[Concept] = State(
186             name               = Concept,
187             asVector           = Vector,
188             asPersistentVector = VectorSerie,
189             asScript           = Script,
190             scheduledBy        = Scheduler,
191             toBeChecked        = Checked,
192             )
193         if Stored:
194             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
195         return 0
196
197     def setControlInput(self,
198             Vector         = None,
199             VectorSerie    = None,
200             Script         = None,
201             Stored         = False,
202             Scheduler      = None,
203             Checked        = False):
204         "Definition d'un concept de calcul"
205         Concept = "ControlInput"
206         self.__case.register("set"+Concept, dir(), locals())
207         self.__adaoObject[Concept] = State(
208             name               = Concept,
209             asVector           = Vector,
210             asPersistentVector = VectorSerie,
211             asScript           = Script,
212             scheduledBy        = Scheduler,
213             toBeChecked        = Checked,
214             )
215         if Stored:
216             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
217         return 0
218
219     def setObservation(self,
220             Vector         = None,
221             VectorSerie    = None,
222             Script         = None,
223             Stored         = False,
224             Scheduler      = None,
225             Checked        = False):
226         "Definition d'un concept de calcul"
227         Concept = "Observation"
228         self.__case.register("set"+Concept, dir(), locals())
229         self.__adaoObject[Concept] = State(
230             name               = Concept,
231             asVector           = Vector,
232             asPersistentVector = VectorSerie,
233             asScript           = Script,
234             scheduledBy        = Scheduler,
235             toBeChecked        = Checked,
236             )
237         if Stored:
238             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
239         return 0
240
241     def setControls(self,
242             Vector         = (), # Valeur par defaut pour un vecteur vide
243             VectorSerie    = None,
244             Script         = None,
245             Stored         = False,
246             Scheduler      = None,
247             Checked        = False):
248         "Definition d'un concept de calcul"
249         Concept = "Controls"
250         self.__case.register("set"+Concept, dir(), locals())
251         self.__adaoObject[Concept] = State(
252             name               = Concept,
253             asVector           = Vector,
254             asPersistentVector = VectorSerie,
255             asScript           = Script,
256             scheduledBy        = Scheduler,
257             toBeChecked        = Checked,
258             )
259         if Stored:
260             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
261         return 0
262
263     def setBackgroundError(self,
264             Matrix               = None,
265             ScalarSparseMatrix   = None,
266             DiagonalSparseMatrix = None,
267             Script               = None,
268             Stored               = False,
269             ObjectMatrix         = None,
270             Checked              = False):
271         "Definition d'un concept de calcul"
272         Concept = "BackgroundError"
273         self.__case.register("set"+Concept, dir(), locals())
274         self.__adaoObject[Concept] = Covariance(
275             name          = Concept,
276             asCovariance  = Matrix,
277             asEyeByScalar = ScalarSparseMatrix,
278             asEyeByVector = DiagonalSparseMatrix,
279             asCovObject   = ObjectMatrix,
280             asScript      = Script,
281             toBeChecked   = Checked,
282             )
283         if Stored:
284             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
285         return 0
286
287     def setObservationError(self,
288             Matrix               = None,
289             ScalarSparseMatrix   = None,
290             DiagonalSparseMatrix = None,
291             Script               = None,
292             Stored               = False,
293             ObjectMatrix         = None,
294             Checked              = False):
295         "Definition d'un concept de calcul"
296         Concept = "ObservationError"
297         self.__case.register("set"+Concept, dir(), locals())
298         self.__adaoObject[Concept] = Covariance(
299             name          = Concept,
300             asCovariance  = Matrix,
301             asEyeByScalar = ScalarSparseMatrix,
302             asEyeByVector = DiagonalSparseMatrix,
303             asCovObject   = ObjectMatrix,
304             asScript      = Script,
305             toBeChecked   = Checked,
306             )
307         if Stored:
308             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
309         return 0
310
311     def setEvolutionError(self,
312             Matrix               = None,
313             ScalarSparseMatrix   = None,
314             DiagonalSparseMatrix = None,
315             Script               = None,
316             Stored               = False,
317             ObjectMatrix         = None,
318             Checked              = False):
319         "Definition d'un concept de calcul"
320         Concept = "EvolutionError"
321         self.__case.register("set"+Concept, dir(), locals())
322         self.__adaoObject[Concept] = Covariance(
323             name          = Concept,
324             asCovariance  = Matrix,
325             asEyeByScalar = ScalarSparseMatrix,
326             asEyeByVector = DiagonalSparseMatrix,
327             asCovObject   = ObjectMatrix,
328             asScript      = Script,
329             toBeChecked   = Checked,
330             )
331         if Stored:
332             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
333         return 0
334
335     def setObservationOperator(self,
336             Matrix         = None,
337             OneFunction    = None,
338             ThreeFunctions = None,
339             AppliedInXb    = None,
340             Parameters     = None,
341             Script         = None,
342             Stored         = False,
343             AvoidRC        = True,
344             Checked        = False):
345         "Definition d'un concept de calcul"
346         Concept = "ObservationOperator"
347         self.__case.register("set"+Concept, dir(), locals())
348         self.__adaoObject[Concept] = FullOperator(
349             name             = Concept,
350             asMatrix         = Matrix,
351             asOneFunction    = OneFunction,
352             asThreeFunctions = ThreeFunctions,
353             asScript         = Script,
354             asDict           = Parameters,
355             appliedInX       = AppliedInXb,
356             avoidRC          = AvoidRC,
357             scheduledBy      = None,
358             toBeChecked      = Checked,
359             )
360         if Stored:
361             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
362         return 0
363
364     def setEvolutionModel(self,
365             Matrix         = None,
366             OneFunction    = None,
367             ThreeFunctions = None,
368             Parameters     = None,
369             Script         = None,
370             Stored         = False,
371             Scheduler      = None,
372             AvoidRC        = True,
373             Checked        = False):
374         "Definition d'un concept de calcul"
375         Concept = "EvolutionModel"
376         self.__case.register("set"+Concept, dir(), locals())
377         self.__adaoObject[Concept] = FullOperator(
378             name             = Concept,
379             asMatrix         = Matrix,
380             asOneFunction    = OneFunction,
381             asThreeFunctions = ThreeFunctions,
382             asScript         = Script,
383             asDict           = Parameters,
384             appliedInX       = None,
385             avoidRC          = AvoidRC,
386             scheduledBy      = Scheduler,
387             toBeChecked      = Checked,
388             )
389         if Stored:
390             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
391         return 0
392
393     def setControlModel(self,
394             Matrix         = None,
395             OneFunction    = None,
396             ThreeFunctions = None,
397             Parameters     = None,
398             Script         = None,
399             Stored         = False,
400             Scheduler      = None,
401             AvoidRC        = True,
402             Checked        = False):
403         "Definition d'un concept de calcul"
404         Concept = "ControlModel"
405         self.__case.register("set"+Concept, dir(), locals())
406         self.__adaoObject[Concept] = FullOperator(
407             name             = Concept,
408             asMatrix         = Matrix,
409             asOneFunction    = OneFunction,
410             asThreeFunctions = ThreeFunctions,
411             asScript         = Script,
412             asDict           = Parameters,
413             appliedInX       = None,
414             avoidRC          = AvoidRC,
415             scheduledBy      = Scheduler,
416             toBeChecked      = Checked,
417             )
418         if Stored:
419             self.__StoredInputs[Concept] = self.__adaoObject[Concept].getO()
420         return 0
421
422     def setDebug(self, level = 10):
423         "NOTSET=0 < DEBUG=10 < INFO=20 < WARNING=30 < ERROR=40 < CRITICAL=50"
424         self.__case.register("setDebug",dir(),locals())
425         log = logging.getLogger()
426         log.setLevel( level )
427         self.__StoredInputs["Debug"]   = level
428         self.__StoredInputs["NoDebug"] = False
429         return 0
430
431     def setNoDebug(self):
432         "NOTSET=0 < DEBUG=10 < INFO=20 < WARNING=30 < ERROR=40 < CRITICAL=50"
433         self.__case.register("setNoDebug",dir(),locals())
434         log = logging.getLogger()
435         log.setLevel( logging.WARNING )
436         self.__StoredInputs["Debug"]   = logging.WARNING
437         self.__StoredInputs["NoDebug"] = True
438         return 0
439
440     def setAlgorithmParameters(self,
441             Algorithm  = None,
442             Parameters = None,
443             Script     = None):
444         "Definition d'un concept de calcul"
445         Concept = "AlgorithmParameters"
446         self.__case.register("set"+Concept, dir(), locals())
447         self.__adaoObject[Concept] = AlgorithmAndParameters(
448             name          = Concept,
449             asAlgorithm   = Algorithm,
450             asDict        = Parameters,
451             asScript      = Script,
452             )
453         return 0
454
455     def updateAlgorithmParameters(self,
456             Parameters = None,
457             Script     = None):
458         "Mise a jour d'un concept de calcul"
459         if "AlgorithmParameters" not in self.__adaoObject:
460             raise ValueError("No algorithm registred, ask for one before updating parameters")
461         self.__adaoObject["AlgorithmParameters"].updateParameters(
462             asDict        = Parameters,
463             asScript      = Script,
464             )
465         return 0
466
467     def setObserver(self,
468             Variable       = None,
469             Template       = None,
470             String         = None,
471             Script         = None,
472             Info           = None,
473             ObjectFunction = None,
474             Scheduler      = None):
475         "Definition d'un concept de calcul"
476         Concept = "Observer"
477         self.__case.register("set"+Concept, dir(), locals())
478         self.__adaoObject[Concept].append( DataObserver(
479             name        = Concept,
480             onVariable  = Variable,
481             asTemplate  = Template,
482             asString    = String,
483             asScript    = Script,
484             asObsObject = ObjectFunction,
485             withInfo    = Info,
486             scheduledBy = Scheduler,
487             withAlgo    = self.__adaoObject["AlgorithmParameters"]
488             ))
489         return 0
490
491     def removeObserver(self,
492             Variable       = None,
493             ObjectFunction = None,
494             ):
495         "Permet de retirer un observer à une ou des variable nommée"
496         if "AlgorithmParameters" not in self.__adaoObject:
497             raise ValueError("No algorithm registred, ask for one before removing observers")
498         #
499         # Vérification du nom de variable et typage
500         # -----------------------------------------
501         if isinstance(Variable, str):
502             VariableNames = (Variable,)
503         elif isinstance(Variable, list):
504             VariableNames = tuple(map( str, Variable ))
505         else:
506             raise ValueError("The observer requires a name or a list of names of variables.")
507         #
508         # Association interne de l'observer à la variable
509         # -----------------------------------------------
510         for ename in VariableNames:
511             if ename not in self.__adaoObject["AlgorithmParameters"]:
512                 raise ValueError("An observer requires to be removed on a variable named %s which does not exist."%ename)
513             else:
514                 return self.__adaoObject["AlgorithmParameters"].removeObserver( ename, ObjectFunction )
515
516     # -----------------------------------------------------------
517
518     def setDiagnostic(self,
519             Diagnostic = None,
520             Identifier = None,
521             Parameters = None,
522             Script     = None,
523             Unit       = None,
524             BaseType   = None):
525         "Definition d'un concept de calcul"
526         Concept = "Diagnostic"
527         self.__case.register("set"+Concept, dir(), locals())
528         self.__adaoObject[Concept].append( DiagnosticAndParameters(
529                  name               = Concept,
530                  asDiagnostic       = Diagnostic,
531                  asIdentifier       = Identifier,
532                  asDict             = Parameters,
533                  asScript           = Script,
534                  asUnit             = Unit,
535                  asBaseType         = BaseType,
536                  asExistingDiags    = self.__StoredInputs[Concept],
537                 ))
538         self.__StoredInputs[Concept].append(str(Identifier))
539         return 0
540
541     def get(self, Concept=None, noDetails=True ):
542         "Recuperation d'une sortie du calcul"
543         if Concept is not None:
544             try:
545                 self.__case.register("get", dir(), locals(), Concept) # Break pickle in Python 2
546             except Exception:
547                 pass
548             if Concept in self.__StoredInputs:
549                 return self.__StoredInputs[Concept]
550                 #
551             elif self.__adaoObject["AlgorithmParameters"] is not None and Concept == "AlgorithmParameters":
552                 return self.__adaoObject["AlgorithmParameters"].get()
553                 #
554             elif self.__adaoObject["AlgorithmParameters"] is not None and Concept in self.__adaoObject["AlgorithmParameters"]:
555                 return self.__adaoObject["AlgorithmParameters"].get( Concept )
556                 #
557             elif Concept == "AlgorithmRequiredParameters" and self.__adaoObject["AlgorithmParameters"] is not None:
558                 return self.__adaoObject["AlgorithmParameters"].getAlgorithmRequiredParameters(noDetails)
559                 #
560             elif Concept in self.__StoredInputs["Diagnostic"]:
561                 indice = self.__StoredInputs["Diagnostic"].index(Concept)
562                 return self.__adaoObject["Diagnostic"][indice].get()
563                 #
564             else:
565                 raise ValueError("The requested key \"%s\" does not exists as an input, a diagnostic or a stored variable."%Concept)
566         else:
567             allvariables = {}
568             allvariables.update( {"AlgorithmParameters":self.__adaoObject["AlgorithmParameters"].get()} )
569             # allvariables.update( self.__adaoObject["AlgorithmParameters"].get() )
570             allvariables.update( self.__StoredInputs )
571             allvariables.pop('Diagnostic', None)
572             allvariables.pop('Observer', None)
573             return allvariables
574
575     # -----------------------------------------------------------
576
577     def get_available_variables(self):
578         """
579         Renvoie les variables potentiellement utilisables pour l'étude,
580         initialement stockées comme données d'entrées ou dans les algorithmes,
581         identifiés par les chaînes de caractères. L'algorithme doit avoir été
582         préalablement choisi sinon la méthode renvoie "None".
583         """
584         if len(list(self.__adaoObject["AlgorithmParameters"].keys())) == 0 and \
585             len(list(self.__StoredInputs.keys())) == 0:
586             return None
587         else:
588             variables = []
589             if len(list(self.__adaoObject["AlgorithmParameters"].keys())) > 0:
590                 variables.extend(list(self.__adaoObject["AlgorithmParameters"].keys()))
591             if len(list(self.__StoredInputs.keys())) > 0:
592                 variables.extend( list(self.__StoredInputs.keys()) )
593             variables.remove('Diagnostic')
594             variables.remove('Observer')
595             variables.sort()
596             return variables
597
598     def get_available_algorithms(self):
599         """
600         Renvoie la liste des algorithmes potentiellement utilisables, identifiés
601         par les chaînes de caractères.
602         """
603         files = []
604         for directory in sys.path:
605             trypath = os.path.join(directory,"daAlgorithms")
606             if os.path.isdir(trypath):
607                 for fname in os.listdir(trypath):
608                     if os.path.isfile(os.path.join(trypath,fname)):
609                         fc = open(os.path.join(trypath,fname)).read()
610                         iselal = bool("class ElementaryAlgorithm" in fc)
611                         root, ext = os.path.splitext(fname)
612                         if iselal and ext == '.py' and root != '__init__':
613                             files.append(root)
614         files.sort()
615         return files
616
617     def get_available_diagnostics(self):
618         """
619         Renvoie la liste des diagnostics potentiellement utilisables, identifiés
620         par les chaînes de caractères.
621         """
622         files = []
623         for directory in sys.path:
624             if os.path.isdir(os.path.join(directory,"daDiagnostics")):
625                 for fname in os.listdir(os.path.join(directory,"daDiagnostics")):
626                     root, ext = os.path.splitext(fname)
627                     if ext == '.py' and root != '__init__':
628                         files.append(root)
629         files.sort()
630         return files
631
632     # -----------------------------------------------------------
633
634     def get_algorithms_main_path(self):
635         """
636         Renvoie le chemin pour le répertoire principal contenant les algorithmes
637         dans un sous-répertoire "daAlgorithms"
638         """
639         return self.__parent
640
641     def add_algorithms_path(self, Path=None):
642         """
643         Ajoute au chemin de recherche des algorithmes un répertoire dans lequel
644         se trouve un sous-répertoire "daAlgorithms"
645
646         Remarque : si le chemin a déjà été ajouté pour les diagnostics, il n'est
647         pas indispensable de le rajouter ici.
648         """
649         if not os.path.isdir(Path):
650             raise ValueError("The given "+Path+" argument must exist as a directory")
651         if not os.path.isdir(os.path.join(Path,"daAlgorithms")):
652             raise ValueError("The given \""+Path+"\" argument must contain a subdirectory named \"daAlgorithms\"")
653         if not os.path.isfile(os.path.join(Path,"daAlgorithms","__init__.py")):
654             raise ValueError("The given \""+Path+"/daAlgorithms\" path must contain a file named \"__init__.py\"")
655         sys.path.insert(0, os.path.abspath(Path))
656         sys.path = PlatformInfo.uniq( sys.path ) # Conserve en unique exemplaire chaque chemin
657         return 0
658
659     def get_diagnostics_main_path(self):
660         """
661         Renvoie le chemin pour le répertoire principal contenant les diagnostics
662         dans un sous-répertoire "daDiagnostics"
663         """
664         return self.__parent
665
666     def add_diagnostics_path(self, Path=None):
667         """
668         Ajoute au chemin de recherche des algorithmes un répertoire dans lequel
669         se trouve un sous-répertoire "daDiagnostics"
670
671         Remarque : si le chemin a déjà été ajouté pour les algorithmes, il n'est
672         pas indispensable de le rajouter ici.
673         """
674         if not os.path.isdir(Path):
675             raise ValueError("The given "+Path+" argument must exist as a directory")
676         if not os.path.isdir(os.path.join(Path,"daDiagnostics")):
677             raise ValueError("The given \""+Path+"\" argument must contain a subdirectory named \"daDiagnostics\"")
678         if not os.path.isfile(os.path.join(Path,"daDiagnostics","__init__.py")):
679             raise ValueError("The given \""+Path+"/daDiagnostics\" path must contain a file named \"__init__.py\"")
680         sys.path.insert(0, os.path.abspath(Path))
681         sys.path = PlatformInfo.uniq( sys.path ) # Conserve en unique exemplaire chaque chemin
682         return 0
683
684     # -----------------------------------------------------------
685
686     def execute(self, Executor=None, SaveCaseInFile=None):
687         "Lancement du calcul"
688         self.__case.register("execute",dir(),locals(),None,True)
689         Operator.CM.clearCache()
690         try:
691             if   Executor == "YACS": self.__executeYACSScheme( SaveCaseInFile )
692             else:                    self.__executePythonScheme( SaveCaseInFile )
693         except Exception as e:
694             if isinstance(e, SyntaxError): msg = "at %s: %s"%(e.offset, e.text)
695             else: msg = ""
696             raise ValueError(("during execution, the following error occurs:\n"+\
697                              "\n%s %s\n\nSee also the potential messages, "+\
698                              "which can show the origin of the above error, "+\
699                              "in the launching terminal.\n")%(str(e),msg))
700         return 0
701
702     def __executePythonScheme(self, FileName=None):
703         "Lancement du calcul"
704         self.__case.register("executePythonScheme", dir(), locals())
705         if FileName is not None:
706             self.dump( FileName, "TUI")
707         self.__adaoObject["AlgorithmParameters"].executePythonScheme( self.__adaoObject )
708         return 0
709
710     def __executeYACSScheme(self, FileName=None):
711         "Lancement du calcul"
712         self.__case.register("executeYACSScheme", dir(), locals())
713         self.dump( FileName, "YACS")
714         self.__adaoObject["AlgorithmParameters"].executeYACSScheme( FileName )
715         return 0
716
717     # -----------------------------------------------------------
718
719     def dump(self, FileName=None, Formater="TUI"):
720         "Restitution normalisée des commandes"
721         return self.__case.dump(FileName, Formater)
722
723     def load(self, FileName=None, Formater="TUI"):
724         "Chargement normalisé des commandes"
725         __commands = self.__case.load(FileName, Formater)
726         from numpy import array, matrix
727         for __command in __commands:
728             exec("self."+__command)
729         return self
730
731     def clear(self):
732         "Effacement du contenu du cas en cours"
733         self.__init__(self.__name)
734
735     # -----------------------------------------------------------
736
737     def __dir__(self):
738         "Clarifie la visibilité des méthodes"
739         return ['set', 'get', 'execute', '__doc__', '__init__', '__module__']
740
741     def prepare_to_pickle(self):
742         "Retire les variables non pickelisables, avec recopie efficace"
743         if self.__adaoObject['AlgorithmParameters'] is not None:
744             for k in self.__adaoObject['AlgorithmParameters'].keys():
745                 if k == "Algorithm": continue
746                 if k in self.__StoredInputs:
747                     raise ValueError("the key \"%s\s to be transfered for pickling will overwrite an existing one.")
748                 if self.__adaoObject['AlgorithmParameters'].hasObserver( k ):
749                     self.__adaoObject['AlgorithmParameters'].removeObserver( k, "", True )
750                 self.__StoredInputs[k] = self.__adaoObject['AlgorithmParameters'].pop(k, None)
751         if sys.version_info[0] == 2:
752             del self.__adaoObject # Because it breaks pickle in Python 2. Not required for Python 3
753             del self.__case       # Because it breaks pickle in Python 2. Not required for Python 3
754         return 0
755
756 # ==============================================================================
757 if __name__ == "__main__":
758     print('\n AUTODIAGNOSTIC \n')