diff --git a/AlgebraicDataflowArchitectureModel/src/application/simulator/InputEventCellEditor.java b/AlgebraicDataflowArchitectureModel/src/application/simulator/InputEventCellEditor.java index 46bc39a..6f94f02 100644 --- a/AlgebraicDataflowArchitectureModel/src/application/simulator/InputEventCellEditor.java +++ b/AlgebraicDataflowArchitectureModel/src/application/simulator/InputEventCellEditor.java @@ -75,6 +75,7 @@ protected int minimumWidth = DEFAULT_MIN_WIDTH; protected int minimumHeight = DEFAULT_MIN_HEIGHT; + private SimulatorWindow window; private Object editingCell; private EventObject trigger; private JComboBox comboBox; @@ -84,8 +85,8 @@ private Editor editor; private boolean bReflectingArchitectureModel = false; - public InputEventCellEditor(mxGraphComponent graphComponent, Simulator simulator, Editor editor) { - this.graphComponent = graphComponent; + public InputEventCellEditor(SimulatorWindow window, mxGraphComponent graphComponent, Simulator simulator, Editor editor) { this.graphComponent = graphComponent; + this.window = window; this.simulator = simulator; this.editor = editor; } @@ -109,26 +110,8 @@ ArrayList eventMessages = new ArrayList<>();//messageList ResourcePath eventResPath = null; - for(Channel ch : simulator.getModel().getInputChannels()) {//all channel - if(((DataTransferChannel)ch).getInputResources().size()== 0) {//ioch Or normalch - for(ChannelMember out: ((DataTransferChannel)ch).getOutputChannelMembers()) { - ResourcePath resPath = out.getResource(); - if(resId.isInstanceOf(resPath)) {//account.uid == acounts.123 - eventResPath = resPath; - eventChs.add(((DataTransferChannel)ch)); - String message = null; - Expression mesExp = out.getStateTransition().getMessageExpression();//sync(a,b) - eventMessages.add(mesExp); - if(mesExp instanceof Term) { - message = ((Term) mesExp).getSymbol().toString();//sync - }else if(mesExp instanceof Variable) { - message = ((Variable) mesExp).getName();//x,y,z.. - } - messages.add(message);//pulldown - - } - } - } + for (Channel ch: simulator.getModel().getInputChannels()) {//all channel + eventResPath = getSelectableMessages(ch, resId, eventChs, messages, eventMessages, eventResPath); } if(messages.isEmpty()) { @@ -140,7 +123,7 @@ JPanel eventChoice = new JPanel(); eventChoice.add(event);//FirstEventChoice - int ret = JOptionPane.showConfirmDialog(null, eventChoice, "Event Choice", JOptionPane.OK_CANCEL_OPTION); + int ret = JOptionPane.showConfirmDialog(window, eventChoice, "Event Choice", JOptionPane.OK_CANCEL_OPTION); if (ret == JOptionPane.OK_OPTION) { JPanel inputEvent = new JPanel(); @@ -157,22 +140,22 @@ JTextArea textArea = new JTextArea(eventMessages.get(eventNum).toString(), 10, 30);//EventInput inputEvent.add(textArea); - int approve = JOptionPane.showConfirmDialog(null, inputEvent, "Event Code", JOptionPane.OK_CANCEL_OPTION); + int approve = JOptionPane.showConfirmDialog(window, inputEvent, "Event Code", JOptionPane.OK_CANCEL_OPTION); if (approve == JOptionPane.OK_OPTION) { try { - TokenStream stream = new Parser.TokenStream(); - Parser parser = new Parser(stream); - stream.addLine(textArea.getText()); - Expression eventMessage = parser.parseTerm(stream,simulator.getModel()); - - Event newEvent = new Event(eventChs.get(eventNum), eventMessage, eventResPath, simulator.getCurState().getResource(resId)); - simulator.transition(newEvent); - -// SimulationLayout layout = new SimulationLayout(simulator.getCurState()); -// layout.constructSimulateGraph(graph, simulator); - - graphComponent.setCellEditor(new InputEventCellEditor(graphComponent, simulator, this.editor)); + TokenStream stream = new Parser.TokenStream(); + Parser parser = new Parser(stream); + stream.addLine(textArea.getText()); + Expression eventMessage = parser.parseTerm(stream,simulator.getModel()); + + Event newEvent = new Event(eventChs.get(eventNum), eventMessage, eventResPath, simulator.getCurState().getResource(resId)); + simulator.transition(newEvent); + +// SimulationLayout layout = new SimulationLayout(simulator.getCurState()); +// layout.constructSimulateGraph(graph, simulator); + + graphComponent.setCellEditor(new InputEventCellEditor(window, graphComponent, simulator, this.editor)); } catch (ParameterizedIdentifierIsFutureWork | ResolvingMultipleDefinitionIsFutureWork | InvalidMessage | UnificationFailed | ValueUndefined | ExpectedRightBracket | WrongJsonExpression | ExpectedColon | ExpectedDoubleQuotation e) { @@ -180,8 +163,6 @@ } } } - //resource - return; } mxCellState state = graphComponent.getGraph().getView().getState(cell); @@ -203,7 +184,34 @@ } } } - + + private ResourcePath getSelectableMessages(Channel ch, ResourceIdentifier resId, + ArrayList eventChs, ArrayList messages, ArrayList eventMessages, + ResourcePath eventResPath) { + if (((DataTransferChannel)ch).getInputResources().size() == 0) {//ioch Or normalch + for (ChannelMember out: ((DataTransferChannel)ch).getOutputChannelMembers()) { + ResourcePath resPath = out.getResource(); + if (resId.isInstanceOf(resPath)) {//account.uid == acounts.123 + eventResPath = resPath; + eventChs.add(((DataTransferChannel)ch)); + String message = null; + Expression mesExp = out.getStateTransition().getMessageExpression();//sync(a,b) + eventMessages.add(mesExp); + if (mesExp instanceof Term) { + message = ((Term) mesExp).getSymbol().toString();//sync + } else if(mesExp instanceof Variable) { + message = ((Variable) mesExp).getName();//x,y,z.. + } + messages.add(message);//pulldown + } + } + for (Channel childCh: ch.getChildren()) { + eventResPath = getSelectableMessages(childCh, resId, eventChs, messages, eventMessages, eventResPath); + } + } + return eventResPath; + } + // private mxGraph constructNextSimulateGraph(Set simulateRes, DataTransferModel model,DataFlowGraph dataFlowGraph, mxGraph nextGraph) { // bReflectingArchitectureModel = true; // ((mxGraphModel) nextGraph.getModel()).clear(); diff --git a/AlgebraicDataflowArchitectureModel/src/application/simulator/SimulatorWindow.java b/AlgebraicDataflowArchitectureModel/src/application/simulator/SimulatorWindow.java index 6dfb078..adfd10f 100644 --- a/AlgebraicDataflowArchitectureModel/src/application/simulator/SimulatorWindow.java +++ b/AlgebraicDataflowArchitectureModel/src/application/simulator/SimulatorWindow.java @@ -131,8 +131,8 @@ simulator = new Simulator(model); SimulationLayout layout = new SimulationLayout(simulator.getCurState()); layout.constructSimulateGraph(graph, simulator); - graphComponent.setCellEditor(new InputEventCellEditor(graphComponent, simulator, this.editor)); - simulator.addSystemReceiver(this); + graphComponent.setCellEditor(new InputEventCellEditor(this, graphComponent, simulator, this.editor)); + simulator.addSystemReceiver(this); menuBar = new SimulatorMenuBar(this); setJMenuBar(menuBar); diff --git a/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java b/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java index ab82982..13875a6 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java @@ -59,6 +59,7 @@ List> constructorParams = new ArrayList<>(); List> constructorStatements = new ArrayList<>(); Map> updateStatements = new HashMap<>(); + Map> childGetters = new HashMap<>(); // For each components (1st pass). for (Node componentNode: components) { @@ -105,9 +106,10 @@ // Declare the accessor method in the main component to call the getter method. declareAccessorInMainComponent(mainComponent, resourceNode, stateGetter, langSpec); - + } + if (component != null) { // Declare the getter methods in this resource to obtain the children resources. - declareChildGetterMethod(resourceNode, component, langSpec); + declareChildGetterMethod(resourceNode, component, childGetters, langSpec); } } } @@ -236,7 +238,7 @@ while (termEntItr.hasNext()) { Entry termEnt = termEntItr.next(); Term jsonTerm = termEnt.getValue(); - if (jsonTerm.getType().equals(replacedJsonType)) { + if (jsonTerm.getType() != null && jsonTerm.getType().equals(replacedJsonType)) { MethodDeclaration childConstructor = getConstructor(childComponent); List params = new ArrayList<>(); for (VariableDeclaration var: childConstructor.getParameters()) { @@ -714,9 +716,13 @@ } } - private void declareChildGetterMethod(ResourceNode resourceNode, TypeDeclaration component, ILanguageSpecific langSpec) { + private void declareChildGetterMethod(ResourceNode resourceNode, TypeDeclaration component, Map> childGetters, ILanguageSpecific langSpec) { // Declare the getter methods in this resource to obtain the children resources. - Set children = new HashSet<>(); + Set children = childGetters.get(resourceNode.getResourceHierarchy()); + if (children == null) { + children = new HashSet<>(); + childGetters.put(resourceNode.getResourceHierarchy(), children); + } for (ResourceNode child: resourceNode.getChildren()) { if (generatesComponent(child.getResourceHierarchy())) { // A component for the child is generated. diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JavaCodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/generators/JavaCodeGenerator.java index e86cc63..e85b349 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/JavaCodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/JavaCodeGenerator.java @@ -133,6 +133,7 @@ Map> updates = new HashMap<>(); Map> inputs = new HashMap<>(); List> fields = new ArrayList<>(); + Map> childGetters = new HashMap<>(); List> constructorParams = new ArrayList<>(); Map> dependedRootComponentGraph = getDependedRootComponentGraph(model); @@ -272,9 +273,14 @@ // Declare the accessor method in the main type to call the getter method. declareAccessorMethodInMainComponent(resourceNode, mainComponent); - + } + if (component != null) { // Declare the getter methods to obtain the children resources. - Set children = new HashSet<>(); + Set children = childGetters.get(resourceNode.getResourceHierarchy()); + if (children == null) { + children = new HashSet<>(); + childGetters.put(resourceNode.getResourceHierarchy(), children); + } for (ResourceNode child: resourceNode.getChildren()) { if (generatesComponent(child.getResourceHierarchy())) { // A component for the child is generated. diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JavaMethodBodyGenerator.java b/AlgebraicDataflowArchitectureModel/src/generators/JavaMethodBodyGenerator.java index 08bc6ec..4ab9dd3 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/JavaMethodBodyGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/JavaMethodBodyGenerator.java @@ -832,7 +832,7 @@ while (termEntItr.hasNext()) { Entry termEnt = termEntItr.next(); Term jsonTerm = termEnt.getValue(); - if (jsonTerm.getType().equals(replacedJsonType)) { + if (jsonTerm.getType() != null && jsonTerm.getType().equals(replacedJsonType)) { String constructorInvocation = "new " + replacingClassName + "("; MethodDeclaration childConstructor = getConstructor(childComponent); String delimiter = ""; diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JerseyCodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/generators/JerseyCodeGenerator.java index 78a04d2..a85d094 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/JerseyCodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/JerseyCodeGenerator.java @@ -136,6 +136,7 @@ Map> updates = new HashMap<>(); Map> inputs = new HashMap<>(); List> fields = new ArrayList<>(); + Map> childGetters = new HashMap<>(); Map getterAccessors = new HashMap<>(); Map inputAccessors = new HashMap<>(); Map> constructorParams = new HashMap<>(); @@ -218,9 +219,14 @@ stateGetter.addAnnotation(new Annotation("GET")); } component.addMethod(stateGetter); - + } + if (component != null) { // Declare the getter methods to obtain the children resources. - Set children = new HashSet<>(); + Set children = childGetters.get(resourceNode.getResourceHierarchy()); + if (children == null) { + children = new HashSet<>(); + childGetters.put(resourceNode.getResourceHierarchy(), children); + } for (ResourceNode child: resourceNode.getChildren()) { if (generatesComponent(child.getResourceHierarchy())) { // The child generates a component. diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JerseyMethodBodyGenerator.java b/AlgebraicDataflowArchitectureModel/src/generators/JerseyMethodBodyGenerator.java index 4efb2dd..d695463 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/JerseyMethodBodyGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/JerseyMethodBodyGenerator.java @@ -952,7 +952,7 @@ while (termEntItr.hasNext()) { Entry termEnt = termEntItr.next(); Term jsonTerm = termEnt.getValue(); - if (jsonTerm.getType().equals(replacedJsonType)) { + if (jsonTerm.getType() != null && jsonTerm.getType().equals(replacedJsonType)) { String constructorInvocation = "new " + replacingClassName + "("; MethodDeclaration childConstructor = getConstructor(childComponent); String delimiter = ""; diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java index cfef320..95c2b47 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java @@ -75,8 +75,13 @@ public HashMap getSubTerms(Class clazz) { HashMap subTerms = new HashMap<>(); - if (clazz == this.getClass()) { - subTerms.put(new Position(), (T) this); + Class thisClass = this.getClass(); + while (thisClass != null) { + if (clazz == thisClass) { + subTerms.put(new Position(), (T) this); + break; + } + thisClass = thisClass.getSuperclass(); } for (int i = 0; i < children.size(); i++) { HashMap terms = children.get(i).getSubTerms(clazz); diff --git a/AlgebraicDataflowArchitectureModel/src/parser/Parser.java b/AlgebraicDataflowArchitectureModel/src/parser/Parser.java index 3362f4f..abec1de 100644 --- a/AlgebraicDataflowArchitectureModel/src/parser/Parser.java +++ b/AlgebraicDataflowArchitectureModel/src/parser/Parser.java @@ -166,11 +166,14 @@ } String leftBracket = stream.next(); if (leftBracket.equals(LEFT_BRACKET)) { - // has a selector - String selector = stream.next(); - Variable var = parseVariable(stream, model, selector); - channel.addSelector(var); - String rightBracket = stream.next(); + // has selectors + String rightBracket = null; + do { + String selector = stream.next(); + Variable var = parseVariable(stream, model, selector); + channel.addSelector(var); + rightBracket = stream.next(); + } while (rightBracket.equals(COMMA)); if (!rightBracket.equals(RIGHT_BRACKET)) throw new ExpectedRightBracket(stream.getLine()); leftBracket = stream.next(); } diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/ResourceIdentifier.java b/AlgebraicDataflowArchitectureModel/src/simulator/ResourceIdentifier.java index d454391..d0463cf 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/ResourceIdentifier.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/ResourceIdentifier.java @@ -24,7 +24,12 @@ } public void setPathParam(int paramIdx, Constant param) { - pathParams.set(paramIdx, param); + if (paramIdx < pathParams.size()) { + pathParams.set(paramIdx, param); + if (parent != null) { + ((ResourceIdentifier) parent).setPathParam(paramIdx, param); + } + } } public boolean equals(ResourceIdentifier another) { diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/SystemState.java b/AlgebraicDataflowArchitectureModel/src/simulator/SystemState.java index 645ed14..cac52a4 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/SystemState.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/SystemState.java @@ -275,12 +275,11 @@ } public Map.Entry createResourceState(ResourceIdentifier resourceIdentifier, Expression resStateValue) { -// Type resType = null; -// if (resStateValue instanceof Term) { -// resType = ((Term) resStateValue).getType(); -// resStateValue = ((Term) resStateValue).reduce(); -// } Type resType = resourceIdentifier.getResourceStateType(); + if (resType == null && resStateValue instanceof Term) { + resType = ((Term) resStateValue).getType(); + resStateValue = ((Term) resStateValue).reduce(); + } if (resType != null) { if (DataConstraintModel.typeList.isAncestorOf(resType)) { if (resStateValue instanceof Constant) {