Skip to content

Instantly share code, notes, and snippets.

@hudsonsferreira
Created September 12, 2012 21:26
Show Gist options
  • Save hudsonsferreira/3710053 to your computer and use it in GitHub Desktop.
Save hudsonsferreira/3710053 to your computer and use it in GitHub Desktop.
modelo de patch para inserir no Yakindu
//deve ser inserido no metodo createStatechartModel da classe FactoryUtils
//path: org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/factories/FactoryUtils.java
public static void createStatechartModel(Resource resource,
PreferencesHint preferencesHint) {
// Create a statechart
Statechart statechart = SGraphFactory.eINSTANCE.createStatechart();
statechart.setSpecification("interface light:\nvar on:boolean\nvar off:boolean\n\ninterface thermostat:\nvar minimum:boolean\nvar maximum:boolean\n\ninterface:\nin event open\nin event close");
String lastSegment = resource.getURI().lastSegment();
String statechartName = lastSegment.substring(0,
lastSegment.indexOf('.'));
statechart.setName(statechartName);
Diagram diagram = ViewService.createDiagram(statechart,
StatechartDiagramEditor.ID, preferencesHint);
diagram.setElement(statechart);
// Add to resource
resource.getContents().add(statechart);
resource.getContents().add(diagram);
// Create an initial region
Region region = SGraphFactory.eINSTANCE.createRegion();
region.setName(INITIAL_REGION_NAME);
statechart.getRegions().add(region);
Node regionView = ViewService.createNode(diagram, region,
SemanticHints.REGION, preferencesHint);
setRegionViewLayoutConstraint(regionView);
// // Create an initial state
Entry initialState = SGraphFactory.eINSTANCE.createEntry();
initialState.setKind(EntryKind.INITIAL);
region.getVertices().add(initialState);
Node initialStateView = ViewService.createNode(
getRegionCompartmentView(regionView), initialState,
SemanticHints.ENTRY, preferencesHint);
setInitialStateViewLayoutConstraint(initialStateView);
// Create the first state
State closedDoor = SGraphFactory.eINSTANCE.createState();
closedDoor.setName("Closed Door");
closedDoor.setSpecification("entry/\nlight.off = true;\nthermostat.minimum = true;\nlight.on = false;\nthermostat.maximum = false");
region.getVertices().add(closedDoor);
Node closedDoorNode = ViewService.createNode(
getRegionCompartmentView(regionView), closedDoor,
SemanticHints.STATE, preferencesHint);
setStateViewLayoutConstraint(closedDoorNode);
State openedDoor = SGraphFactory.eINSTANCE.createState();
openedDoor.setName("Opened Door");
openedDoor.setSpecification("entry/\nlight.off = false;\nthermostat.minimum = false;\nlight.on = true;\nthermostat.maximum = true");
region.getVertices().add(openedDoor);
Node openedDoorNode = ViewService.createNode(
getRegionCompartmentView(regionView), openedDoor,
SemanticHints.STATE, preferencesHint);
setStateViewLayoutConstraint(openedDoorNode);
Transition open = SGraphFactory.eINSTANCE.createTransition();
open.setSpecification("open");
open.setSource(closedDoor);
open.setTarget(openedDoor);
Transition close = SGraphFactory.eINSTANCE.createTransition();
close.setSpecification("close");
close.setSource(openedDoor);
close.setTarget(closedDoor);
// Create the transition from Initial State to State
Transition transition = SGraphFactory.eINSTANCE.createTransition();
transition.setSource(initialState);
transition.setTarget(closedDoor);
initialState.getOutgoingTransitions().add(transition);
ViewService.createEdge(initialStateView, closedDoorNode, transition,
SemanticHints.TRANSITION, preferencesHint);
// Create the textcompartment for events / variables
Node textCompartment = ViewService.createNode(diagram, statechart,
SemanticHints.STATECHART_TEXT, preferencesHint);
setTextCompartmentLayoutConstraint(textCompartment);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment