Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8868056a40ac8b26307095c3511a50ca89452d8d (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
/**
 ********************************************************************************
 * Copyright (c) 2017 Dortmund University of Applied Sciences and Arts and others.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 * 
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *    Dortmund University of Applied Sciences and Arts - initial API and implementation
 ********************************************************************************
 */

package org.eclipse.app4mc.multicore.execution.logic.executionmodel;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.eclipse.app4mc.multicore.execution.logic.executionmodel.misc.EMTimeType;
import org.eclipse.app4mc.multicore.execution.logic.executionmodel.types.EMCore;
import org.eclipse.app4mc.multicore.execution.logic.executionmodel.types.EMTask;
import org.eclipse.app4mc.multicore.execution.logic.executionmodel.types.EMTaskHistState;
import org.eclipse.app4mc.multicore.execution.logic.executionmodel.types.EMTask.DeadlineEntry;

public class ExecutionModel{
	
	private final Map<String,EMCore> cores = new HashMap<>();
	
	private final List<Consumer<EMCore>> coreAddedListener = new LinkedList<>();
	
	private final Map<String,EMTask> tasks = new HashMap<>();
	
	private final List<Consumer<EMTask>> taskAddedListener = new LinkedList<>();
	
	private List<BiConsumer<EMTask, EMTaskHistState>> histEntryListener = new LinkedList<>();

	private List<BiConsumer<EMTask, DeadlineEntry>> deadlineListener=new LinkedList<>();
	
	private EMTimeType timeScale=EMTimeType.NONE;
	
	public Map<String, EMCore> getCores() {
		return cores;
	}
	
	/**
	 * Tasks in of after execution.
	 * @see EMTask 
	 * @return
	 */
	public Map<String, EMTask> getTasks() {
		return tasks;
	}
	
	public void setTimeScale(EMTimeType t){
		this.timeScale=t;
	}
	
	public EMTimeType getTimeScale(){
		return timeScale;
	}
	
	public void addTaskDeadlineMissedEntry(String taskName,String core,Long time){
		final EMTask t =tasks.get(taskName);
		DeadlineEntry d= new DeadlineEntry(time, cores.get(core));
		t.getMissedDeadlines().add(d);
		notifyTaskDeadlineMissed(t, d);
	}
	
	/**
	 * Add a history entry to a task (e.g state running from time point 1000 to 1223).
	 * By adding the entry with this method observer will be notified about completed task hist entries.
	 * @param taskName
	 * @param e
	 */
	public void addTaskHistEntry(String taskName,EMTaskHistState e){
		final EMTask t =tasks.get(taskName);
		t.getStateHistory().add(e);
		notifyTaskHistEntryListener(t,e);
	}
	
	/**
	 * Add a task to the model. Only this method will notify observers about added tasks.
	 * @param t
	 */
	public synchronized void addTask(EMTask t){
		tasks.put(t.getName(), t);
		notifyTaskAddedListener(t);
	}
	
	/**
	 * Add a core to the model. Only this method will notify observers about added cores.
	 * @param c
	 */
	public void addCore(EMCore c){
		cores.put(c.getName(), c);
		notifyAllCoreAddedObserver(c);
	}
	
	private void notifyAllCoreAddedObserver(EMCore c) {
		coreAddedListener.forEach(x->x.accept(c));
	}

	private void notifyTaskAddedListener(EMTask t){
		taskAddedListener.forEach(x->x.accept(t));
	}
	
	private void notifyTaskHistEntryListener(EMTask t, EMTaskHistState e){
		histEntryListener.forEach(x->x.accept(t, e));
	}
	
	public void addTaskHistEntryAddedListener(BiConsumer<EMTask, EMTaskHistState> e ){
		histEntryListener.add(e);
	}
	
	public void addTaskAddedListener(Consumer<EMTask> t){
		taskAddedListener.add(t);
	}
	
	public void addCoreAddedListener(Consumer<EMCore> c){
		coreAddedListener.add(c);
	}

	public void addTaskDeadlineListener(BiConsumer<EMTask, DeadlineEntry> e) {
		deadlineListener.add(e);
	}
	
	private void notifyTaskDeadlineMissed(EMTask t, DeadlineEntry e){
		deadlineListener.forEach(x->x.accept(t, e));
	}

}

Back to the top