#  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
#
#  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
#  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#

#==============================================================================
#  File      : supervisionexample.py
#  Created   : 23 nov 2001
#  Author    : Jean Rahuel
#  Project   : SALOME
#==============================================================================
#
from SuperV import *



# load this object with Nodes, Links and Datas stored in GraphEssai.xml
# (GraphEssai.xml was created with python in SuperVisionTest and GraphEssai.py)
myGraph = Graph("/work/a/ports/cad/salome-gui/work/src5.1.4/GUI_SRC_5.1.4/GUI_build/share/salome/resources/superv/GraphEssai.xml")

#myGraph = Graph("/home/data/jr_HEAD/build/share/salome/resources/superv/GraphEssai.xml")
# This DataFlow is "valid" : no loop, correct links between Nodes etc...

print myGraph.IsValid()

# Get Nodes
myGraph.PrintNodes()
Add,Sub,Mul,Div = myGraph.Nodes()

# Load Datas
Addx = Add.Input("x",3.)
Addy = Add.Input("y",4.5)
Subx = Sub.Input("x",1.5)

# Get Output Port
Addz = Add.Port('z')
Subz = Sub.Port('z')
Mulz = Mul.Port('z')
Divz = Div.Port('z')

# This DataFlow is "executable" : all pending Ports are defined with Datas
print myGraph.IsExecutable()

# Starts only execution of that DataFlow and gets control immediatly
print myGraph.Run()

# That DataFlow is running ==> 0 (false)
print myGraph.IsDone()

# Events of execution :
aStatus,aNode,anEvent,aState = myGraph.Event()
while aStatus :
    print aNode.Thread(),aNode.SubGraph(),aNode.Name(),anEvent,aState
    aStatus,aNode,anEvent,aState = myGraph.Event()
print myGraph.IsDone()

# Wait for Completion (but it is already done after event loop ...)
print "Done : ",myGraph.DoneW()

# Get result
print "Result : ",Divz.ToString()

# Intermediate results :
print "Intermediate Result Add\z : ",Addz.ToString()
print "Intermediate Result Sub\z : ",Subz.ToString()
print "Intermediate Result Mul\z : ",Mulz.ToString()

print " "
print "Type : print myGraph.IsDone()"
print "       If execution is finished ==> 1 (true)"
print " "
print "Type : print Divz.ToString()"
print "       You will get the result"
print " "
print "Type : myGraph.PrintPorts()"
print "       to see input and output values of the graph"
print " "
print "Type : Add.PrintPorts()"
print "Type : Sub.PrintPorts()"
print "Type : Mul.PrintPorts()"
print "Type : Div.PrintPorts()"
print "       to see input and output values of nodes"

# Export will create newsupervisionexample.xml and the corresponding .py file
print myGraph.Export("/work/a/ports/cad/salome-gui/work/src5.1.4/GUI_SRC_5.1.4/GUI_build/share/salome/resources/superv/newsupervisionexample.xml")

print " "
print "See file /work/a/ports/cad/salome-gui/work/src5.1.4/GUI_SRC_5.1.4/GUI_build/share/salome/resources/superv/newsupervisionexample.xml"
print "See file /work/a/ports/cad/salome-gui/work/src5.1.4/GUI_SRC_5.1.4/GUI_build/share/salome/resources/superv/newsupervisionexample.py"
