aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/generator/CpsGenerator.xtend
blob: e8d29949e476edfdd004eeefe37a5d4ee3c5fca6 (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
package hu.bme.mit.inf.dslreasoner.domains.cps.generator

import hu.bme.mit.inf.dslreasoner.domains.cps.CpsFactory
import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem
import hu.bme.mit.inf.dslreasoner.domains.cps.HostType
import java.util.Collection
import java.util.Random
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl

class CpsGenerator {
	extension val CpsFactory = CpsFactory.eINSTANCE

	static val MIN_MEMORY = 1
	static val MAX_MEMORY = 6
	static val MIN_HDD = 1
	static val MAX_HDD = 30
	static val HIGH_CPU_FRACTION = 4
	static val MIN_REPLICAS = 1
	static val MAX_REPLICAS = 4

	val Random random
	val int applicationTypeCount
	val int demandFactor
	val boolean populateAppInstances

	new(long randomSeed, int applicationTypeCount, int demandFactor) {
		this(randomSeed, applicationTypeCount, demandFactor, false)
	}

	new(long randomSeed, int applicationTypeCount, int demandFactor, boolean populateAppInstances) {
		this.random = new Random(randomSeed)
		this.applicationTypeCount = applicationTypeCount
		this.demandFactor = demandFactor
		this.populateAppInstances = populateAppInstances
	}

	def generateCpsProblem() {
		val resourceSet = new ResourceSetImpl
		val resource = resourceSet.createResource(URI.createFileURI("dummy.dummyext"))
		createCyberPhysicalSystem => [
			val cps = it
			resource.contents += cps
			createLowCpuHostTypes
			val highCpuHostTypes = createHighCpuHostTypes
			for (var int i = 0; i < applicationTypeCount; i++) {
				if (i % HIGH_CPU_FRACTION == 0) {
					createRandomApplicationType(highCpuHostTypes)
				} else {
					createRandomApplicationType(hostTypes)
				}
			}
			for (var int i = 0; i < demandFactor; i++) {
				requests += createRequest => [
					for (appType : cps.applicationTypes) {
						requirements += createRequirement => [
							count = nextInt(CpsGenerator.MIN_REPLICAS, CpsGenerator.MAX_REPLICAS)
							type = appType
							if (populateAppInstances) {
								for (j : 0 ..< count) {
									val app = createApplicationInstance
									app.type = appType
									appType.instances += app
									instances += app
								}
							}
						]
					}
				]
			}
		]
	}

	private def void createRandomApplicationType(CyberPhysicalSystem it, Collection<HostType> allowedHostTypes) {
		val appType = createApplicationType
		val memory = nextInt(MIN_MEMORY, MAX_MEMORY)
		val hdd = nextInt(MIN_HDD, MAX_HDD)
		for (hostType : allowedHostTypes) {
			appType.requirements += createResourceRequirement => [
				it.hostType = hostType
				requiredMemory = memory
				requiredHdd = hdd
			]
		}
		applicationTypes += appType
	}

	private def createLowCpuHostTypes(CyberPhysicalSystem it) {
		#[
			createHostType(2, 8, 75), // m5d.large
			createHostType(4, 16, 150), // m5d.xlarge
			createHostType(3, 16, 75), // r5d.large
			createHostType(6, 32, 150) // r5d.xlarge
		]
	}

	private def createHighCpuHostTypes(CyberPhysicalSystem it) {
		#[
			createHostType(2, 4, 50), // c5d.large
			createHostType(4, 8, 100) // c5d.xlarge
		]
	}

	private def createHostType(CyberPhysicalSystem it, int cost, int memory, int hdd) {
		val hostType = createHostType => [
			it.cost = cost
			defaultMemory = memory
			defaultHdd = hdd
		]
		hostTypes += hostType
		hostType
	}

	private def nextInt(int lower, int upper) {
		lower + random.nextInt(upper - lower + 1)
	}
}