add FLower, Order and PriorityQueue classes

This commit is contained in:
FilipKudlacz 2019-06-01 19:54:44 +02:00
parent 9f651765a3
commit 5369e2c57c
16 changed files with 136 additions and 6 deletions

View File

@ -1,3 +1,3 @@
{
"python.pythonPath": "/usr/bin/python3.6"
"python.pythonPath": "/usr/local/bin/python3"
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,25 @@
class Flower {
constructor(type) {
this.type = type;
this.maturity = 0;
this.maxMaturity;
this.grown = false;
}
grow() {
if(!this.isReady()) {
this.maturity++;
if(this.grown == false && this.maturity == this.maxMaturity) {
this.grown == true;
}
}
}
isReady() {
return this.grown;
}
setMaxMaturity(maxMaturity) {
this.maxMaturity = maxMaturity;
}
}

View File

@ -9,6 +9,8 @@ class Forklift {
this.direction = createVector(0, 0);
this.end = false;
this.currentSection = 0;
this.cargo = null;
this.queue = new PriorityQueue();
}
draw() {
@ -28,8 +30,12 @@ class Forklift {
if (this.targetId < this.path.length) {
this.currentTarget = this.path[this.targetId];
} else {
// Final destination reached
magazineState[this.currentTarget].push(this.cargo);
this.removeCargo();
this.end = true;
this.targetId = 0;
}
}
@ -56,6 +62,14 @@ class Forklift {
}
}
setCargo(cargo) {
this.cargo = cargo;
}
removeCargo() {
this.cargo = null;
}
sub(target, pos) {
return createVector(target.x - pos.x, target.y - pos.y);
}

View File

@ -0,0 +1,25 @@
class Order {
constructor(from, what, to, message){
this.from = from;
this.what = what;
this.to = to;
this.message = message;
}
getFrom() {
return this.from;
}
getWhat() {
return this.what;
}
getTo() {
return this.to;
}
getMessage() {
console.log(this.message);
}
}

View File

@ -0,0 +1,37 @@
class PriorityQueue {
constructor() {
this.collection = [];
}
enqueue(element) {
if(this.collection.isEmpty()) {
this.collection.push(element);
}else {
let added = false;
for(var i = 0; i < this.collection.length; i++) {
if(element[1] < this.collection[i][1]) {
this.collection.splice(i,0,element);
added = true;
break;
}
}
if(!added) {
this.collection.push(element);
}
}
}
dequeue() {
let value = this.collection.shift();
return value[1];
}
size() {
return this.collection.length;
}
isEmpty() {
return (this.collection.length == 0);
}
}

View File

@ -4,6 +4,7 @@ let roads;
let packageClaim;
let going = false;
let forklift;
let magazineState = { 1: [], 2: [], 3: [], 4: [], 5: [], 6: [] };
// This runs once at start
function setup() {
@ -18,8 +19,10 @@ function setup() {
sepalLength = select('#sepalLength');
petalWidth = select('#petalWidth');
petalLength = select('#petalLength');
// Create a forklift instance
// Create a forklift instance
forklift = new Forklift(sections[0].x, sections[0].y);
setInterval(ageFlowers, 5000)
}
// This runs every frame
@ -77,6 +80,7 @@ function getIrisType() {
petalLength: pl,
};
httpPost(serverUrl + '/classify', data, response => {
forklift.setCargo(new Flower(response))
deliver(response);
});
}
@ -147,3 +151,26 @@ function magazineToGraph() {
}
return graph;
}
function ageFlowers() {
for (let key of Object.keys(magazineState)) {
for (let flower of magazineState[key]) {
flower.grow();
if(flower.isReady()) {
let order;
let priority;
if(int(key) == 0) {
order = new Order(0, flower, int(flower), "Delivering new flower");
priority = 1;
} else if(int(key) > 3) {
order = new Order(int(key), flower, 0, "Fully grown flower ready");
priority = 3;
} else {
order = new Order(int(key), flower, (int(key) + 3), "Moving grown flower seedling");
priority = 2;
}
forklift.queue.enqueue([order, priority]);
}
}
}
}

View File

@ -4,7 +4,9 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/addons/p5.dom.min.js"></script>
<script src="{% static 'magazine/forklift.js' %}"></script>
<script src="{% static 'magazine/flower.js' %}"></script>
<script src="{% static 'magazine/sketch.js' %}"></script>
<script src="{% static 'magazine/priorityQueue.js' %}"></script>
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<style>
body {
@ -41,13 +43,13 @@
<div class="container">
<div class="package">
<h3 style="margin-top: 0;">Package description</h1>
<label for="width">Sepal Width</label>
<label >Sepal Width</label>
<input type="number" id="sepalWidth">
<label for="topWidth">Sepal Length</label>
<label >Sepal Length</label>
<input type="number" id="sepalLength">
<label for="botWidth">Petal Width</label>
<label >Petal Width</label>
<input type="number" id="petalWidth">
<label for="height">Petal Length</label>
<label >Petal Length</label>
<input type="number" id="petalLength">
<button id="button">Send Package</button>
</div>