Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 849dfe05a2eb8ed7bfe24b73c782bd9a5ebf3d4d (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
 * All rights reserved. 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
 *******************************************************************************/

package org.eclipse.etrice.runtime.java.modelbase;

import org.eclipse.etrice.runtime.java.config.IVariableService;
import org.eclipse.etrice.runtime.java.messaging.Address;
import org.eclipse.etrice.runtime.java.messaging.IMessageReceiver;
import org.eclipse.etrice.runtime.java.messaging.IRTObject;
import org.eclipse.etrice.runtime.java.messaging.Message;
import org.eclipse.etrice.runtime.java.modelbase.RTSystemProtocol.RTSystemPort;


/**
 * The base class for model actor classes.
 * 
 * @author Thomas Schuetz
 *
 */
public abstract class ActorClassBase extends SystemPortOwner implements IMessageReceiver {

	protected static final int EVT_SHIFT = 1000;	// TODOHRR: use 256 or shift operation later

	protected static final int NO_STATE = 0;
	protected static final int STATE_TOP = 1;

	protected static final int NOT_CAUGHT = 0;

	protected static final int IFITEM_RTSystemPort = 0;
	
	private String className = "noname";

	/**
	 * the current state
	 */
	protected int state = NO_STATE;

	protected RTSystemPort rtSystemPort = null;
	
	public ActorClassBase(IRTObject parent, String name) {
		super(parent, name);
		
		// own ports
		rtSystemPort = new RTSystemPort(this, IFITEM_RTSystemPort);
	}

	public String toString(){
		return "ActorClass(className="+className+", instancePath="+getInstancePath()+")";
	}
	
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}

	@Override
	public Address getAddress() {
		// TODO: Actor should have its own address for services and debugging
		return null;
	}
	
	public SubSystemClassBase getSubSystem() {
		// the sub system could be cached
		// but it is rarely used so we just compute it every time
		IRTObject p = getParent();
		while (p!=null) {
			if (p instanceof SubSystemClassBase)
				return (SubSystemClassBase) p;
			p = p.getParent();
		}
		return null;
	}
	
	public IVariableService getVariableService() {
		// the variable service could be cached
		// but variable service operations are costly so it doesn't hurt if we compute it every time
		SubSystemClassBase ssc = getSubSystem();
		if (ssc==null)
			return null;
		
		return ssc.getVariableService();
	}
	
	//--------------------- life cycle functions
	public void init() {
		for (IRTObject child : getChildren()) {
			if (child instanceof ActorClassBase)
				((ActorClassBase) child).init();
		}
		
		initUser();
	}

	public void start() {
		for (IRTObject child : getChildren()) {
			if (child instanceof ActorClassBase)
				((ActorClassBase) child).start();
		}

		startUser();
	}
	
	public void stop() {
		stopUser();

		for (IRTObject child : getChildren()) {
			if (child instanceof ActorClassBase)
				((ActorClassBase) child).stop();
		}
	}
	
	public void destroy() {
		super.destroy();
	}
	
	public abstract void executeInitTransition();

	// not automatically generated life cycle functions
	// are called, but with empty implementation -> can be overridden by user
	public void initUser(){}
	public void startUser(){}
	public void stopUser(){}

	@Override
	public void receive(Message msg) {
	}
	
	public int getState() {
		return state;
	}
	
	protected boolean handleSystemEvent(InterfaceItemBase ifitem, int evt, Object generic_data){
		if (ifitem == null || ifitem.getLocalId()!=0){
			return false;
		}
		
		switch (evt){
		case RTSystemServicesProtocol.IN_executeInitialTransition :
			if (state==NO_STATE)
				executeInitTransition();
			break;
		case RTSystemServicesProtocol.IN_startDebugging :
			break;
		case RTSystemServicesProtocol.IN_stopDebugging :
			break;		
		}
		return true;
	}
}

Back to the top