file Script for exporting diagram to SCXML

1 week 21 hours ago #5279 by Greg
Hello,

I have been using Modelio for almost a month. Now I am working on the state machine diagrams. As I know Modelio does not provide generation C++ code from this kind of diagrams. So I tried to think about other solution. And I came up with exporting diagram as a SCXML (state chart XML).

I tried using jython scripts but I achieved only this (I rewrote example and changed code from here: forge.modelio.org/projects/modelio3-modu...ervices#Code-example ). This is my code. I added some comments to clarify my way of thinking.
def dumpDiagram(diagram):
	
	dh = Modelio.getInstance().getDiagramService().getDiagramHandle(diagram)
	diagramNode = dh.getDiagramNode()
	# change for StateMachine interface?
	
	# for now constant InitialPseudoState - will change later
	state = InitialPseudoState.MNAME
	# I want to take actual name of initial "pseudo" state ( something like getName() )

	# header for each scxml file - only initial property need to be added
	print '<?xml version="1.0" encoding="UTF-8"?>'
	print '<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initial="' + state + '">'
	
	print
	print 'This is not that necessary but I leave it for now'
	print '<diagram name="' + diagramNode.getName() +  '" class="' + diagramNode.getClass().getSimpleName()  + '">'
	# this shows class="StateDiagramDG" I need simple StateMachineModel
	print
	
	# for each over all nodes in diagram (I want to iterate over states - something like - for state in StateMachine.getStates():
	for topLevelNode in diagramNode.getNodes():
		dumpNode(topLevelNode, "  ")
		
	#just add last line of the file
	print
	print '</scxml>'
	dh.close()
	
	# function takes a node and print state section, calls dumpTransition
def dumpNode(node, indent):
	if node != None:
		# something like State.getName() but I couldnt find this in javadoc
		print indent, '<state id="' +  node.getElement().getName() + '">'
		
		for trans in node.getFromLinks():
			dumpTransition(trans, indent+"   ")
		
		print indent, '</state>'
		print
		
		
def dumpTransition(trans, indent):
	# I want to get guard, target or/and event from transitions 
	print indent, '<transition ' + 'guard="" ' + 'target="' + trans.getTo().getName() + '" />'
	# I know something about Transition.getTarget() and Transition.getGuard() but I think I need pass state through arguments
		
	
# macro code starts here
selected = selectedElements.get(0)

if isinstance(selected, AbstractDiagram):
	dumpDiagram(selected)

I would like to know if it is good idea to try using this package org.modelio.metamodel.uml.behavior.stateMachineModel and interfaces/classes within it.
Firstly, I need to take name of states and transition properties. For next steps it would be substates but I should do first step.
I have read Modelio 3.7 API (the Javadoc documentation) and module developer’s APi guide (especially Diagram services section). It did not help that much.

If you could show me how to take diagram/state or if you could give me an advice, I will be so happy.

Thank you in advance and sorry for a long post.

Please Log in or Create an account to join the conversation.

19 hours 36 minutes ago #5284 by pan
Dear Greg,

First of all, your script is working.

# change for StateMachine interface?
It is not necessary. It depends if you want to obtain information about what you see in the state diagram or about the content of the state machine. It is possible that an element exists in the model but that it is masked in the diagram so you will not obtain the same information in the two contexts.
If you want to get the StateMachine model element use this code
diagram.getOrigin()
Documentation for AbstractDiagram

state = InitialPseudoState.MNAME
# I want to take actual name of initial "pseudo" state ( something like getName() )

You have to parse the nodes and find the ones who are "pseudo" states.
if isinstance(node.getElement(), InitialPseudoState):
			print 'Initial Pseudo State: ' + node.getElement().getName()

For next steps it would be substates

In order to process substates, you have to make the dumpNode method recursive.
def dumpNode(node, indent):
	if node != None:
		# something like State.getName() but I couldnt find this in javadoc
		print indent, '<state id="' +  node.getElement().getName() + '" >'
		
		for trans in node.getFromLinks():
			dumpTransition(trans, indent+"   ")
                #recursion
		for subnode in node.getNodes():
			dumpNode(subnode,  indent+"  ")
		print indent, '</state>'

# I know something about Transition.getTarget() and Transition.getGuard() but I think I need pass state through arguments

You could modify the dumpTransition method like this:
def dumpTransition(trans, indent):
	# I want to get guard, target or/and event from transitions 
	if isinstance(trans.getElement(), Transition):
		print indent, '<transition ' + 'guard="' + trans.getElement().getGuard() + '" ' + 'target="' + trans.getTo().getName() + '" />'
	# I know something about Transition.getTarget() and Transition.getGuard() but I think I need pass state through arguments
In this case, you can use "trans.getTo().getName()" or "trans.getElement().getTarget().getName()" for getting the target. It is equivalent.

Let me know if it helps.

Please Log in or Create an account to join the conversation.

Moderators: chmcma
Time to create page: 0.026 seconds
^ Back to Top