Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 61eede315c046cc3326fa4b6a9df7906102b0b54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************************************
 * Copyright (c) 2012 Rohit Agrawal
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Rohit Agrawal (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.abstractexec.behavior;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Platform;
import org.eclipse.etrice.core.genmodel.etricegen.ActiveTrigger;
import org.eclipse.etrice.core.genmodel.etricegen.ExpandedActorClass;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.MessageFromIf;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.RoomFactory;
import org.eclipse.etrice.core.room.SemanticsRule;
import org.eclipse.etrice.core.room.State;
import org.eclipse.etrice.core.room.util.RoomHelpers;

public class ProposalGenerator {
	private ExpandedActorClass xpac;
	private SemanticsCheck checker;
	private List<MessageFromIf> outgoingProposal = new LinkedList<MessageFromIf>();
	private List<MessageFromIf> incomingProposal = new LinkedList<MessageFromIf>();
	private static boolean traceProposals = false;
	static {
		if (Activator.getDefault().isDebugging()) {
			String value = Platform
					.getDebugOption("org.eclipse.etrice.abstractexec.behavior/trace/proposals");
			if (value != null && value.equalsIgnoreCase(Boolean.toString(true))) {
				traceProposals = true;
			}
		}
	}

	public ProposalGenerator(ExpandedActorClass xp, SemanticsCheck chk) {
		xpac = xp;
		checker = chk;
	}

	public List<MessageFromIf> getIncomingProposals() {
		return incomingProposal;
	}

	public List<MessageFromIf> getOutgoingProposals() {
		return outgoingProposal;
	}

	
	public void createProposals(State st) {
		ActiveRules rules = checker.getActiveRules(st);

		// in case the state is disconnected component of the graph
		if (rules == null)
			return;

		outgoingProposal.clear();
		incomingProposal.clear();

		Set<SemanticsRule> rulesToIgnore = new HashSet<SemanticsRule>();
		
			for (ActiveTrigger trigger : xpac.getActiveTriggers(st)) {
				SemanticsRule match = null;
				Port port = (Port) trigger.getIfitem();
				if (rules.getPortList().contains(port)) {
					List<SemanticsRule> ruleList = rules.getRulesForPort(port);
					for (SemanticsRule curRule : ruleList) {
						if (curRule.getMsg() == trigger.getMsg()) {
							match = curRule;
							break;
						}
					}
				}
				if (match != null) {
					// mark this rule for ignoring while generating proposals
					// as they have already been taken care of
					rulesToIgnore.add(match);
				} 
			}

		// now start generating proposals by listing all the rules and ignoring
		// the ones
		// marked above
		for (InterfaceItem item : rules.getPortList()) {
			for (SemanticsRule ruleToCheck : rules.getRulesForPort(item)) {
				if (!rulesToIgnore.contains(ruleToCheck)) {
					MessageFromIf mif = RoomFactory.eINSTANCE
							.createMessageFromIf();
					mif.setFrom(item);
					mif.setMessage(ruleToCheck.getMsg());
					boolean isOutgoing = RoomHelpers.getMessageListDeep(item, true)
							.contains(ruleToCheck.getMsg());
					if (isOutgoing) {
						outgoingProposal.add(mif);
					} else {
						incomingProposal.add(mif);
					}

				}
			}
		}

		if (traceProposals) {
			System.out.println("  Proposals for : " + st.getName());

			for (MessageFromIf msg : outgoingProposal) {
				System.out.println("    Outgoing msg proposal : "
						+ msg.getFrom().getName() + "."
						+ msg.getMessage().getName() + "()");
			}
			for (MessageFromIf msg : incomingProposal) {
				System.out.println("    Incoming msg proposal : "
						+ msg.getMessage().getName() + " from "
						+ msg.getFrom().getName());
			}
		}
	}

}

Back to the top