generator reads from backup and creates schedule for backup restore in portal

This commit is contained in:
Anja Reusch 2019-06-08 23:02:15 +02:00
parent 29da50b45b
commit d4da0ee959
2 changed files with 161 additions and 0 deletions

1
backup.json Normal file
View file

@ -0,0 +1 @@
{"posts":[],"rooms":[{"id":1,"name":"test","places":30,"projector":false,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"createdAt":1560014635207,"updateAt":1560018579608},{"id":2,"name":"test2","places":120,"projector":true,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"createdAt":1560018419580,"updateAt":1560018560539}],"schedules":[{"id":1,"workGroup":{"id":2,"name":"teset2","description":"","interested":115,"track":null,"projector":false,"resolution":false,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"length":50,"language":"GERMAN","leader":["dfsdf"],"constraints":[],"createdAt":1560018635244,"updateAt":1560027050746},"room":{"id":2,"name":"test2","places":120,"projector":true,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"createdAt":1560018419580,"updateAt":1560018560539},"day":1,"time":1000,"lockRoom":false,"lockTime":false,"createdAt":1560027295587,"updateAt":1560027295587},{"id":2,"workGroup":{"id":3,"name":"test","description":"","interested":20,"track":null,"projector":true,"resolution":false,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"length":110,"language":"GERMAN","leader":["asdasd"],"constraints":[],"createdAt":1560018657977,"updateAt":1560027055351},"room":{"id":2,"name":"test2","places":120,"projector":true,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"createdAt":1560018419580,"updateAt":1560018560539},"day":1,"time":1200,"lockRoom":false,"lockTime":false,"createdAt":1560027295597,"updateAt":1560027295597},{"id":3,"workGroup":{"id":4,"name":"testtest","description":"","interested":20,"track":null,"projector":false,"resolution":false,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"length":30,"language":"GERMAN","leader":["nulsd"],"constraints":[],"createdAt":1560026986183,"updateAt":1560027058387},"room":{"id":1,"name":"test","places":30,"projector":false,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"createdAt":1560014635207,"updateAt":1560018579608},"day":1,"time":1400,"lockRoom":false,"lockTime":false,"createdAt":1560027295600,"updateAt":1560027295600}],"tracks":[],"users":[{"id":1,"username":"admin","password":"$2a$10$TuwJnYxqb/G4Zu5st43FzeM6fYy9vBOcMPR0Fd0IeTePubjW837gW","permissions":["ADMIN"],"createdAt":1560014220841,"updateAt":1560014220841}],"workGroups":[{"id":2,"name":"teset2","description":"","interested":115,"track":null,"projector":false,"resolution":false,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"length":50,"language":"GERMAN","leader":["dfsdf"],"constraints":[],"createdAt":1560018635244,"updateAt":1560027050746},{"id":3,"name":"test","description":"","interested":20,"track":null,"projector":true,"resolution":false,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"length":110,"language":"GERMAN","leader":["asdasd"],"constraints":[],"createdAt":1560018657977,"updateAt":1560027055351},{"id":4,"name":"testtest","description":"","interested":20,"track":null,"projector":false,"resolution":false,"internet":false,"whiteboard":false,"blackboard":false,"accessible":false,"length":30,"language":"GERMAN","leader":["nulsd"],"constraints":[],"createdAt":1560026986183,"updateAt":1560027058387}]}

160
generate_rules.py Normal file
View file

@ -0,0 +1,160 @@
import json
from math import floor
import subprocess
data = json.load(open('backup.json'))
SLOT_LEN = 120
DAYS = 3
START = 800
END = 1800
class WorkGroup:
def __init__(self, id, interest, track, projector, reso, length, leaders):
self.id = id # must be int
self.interest = interest
self.track = track if track is not None else 'none' # haben alle aks einen track?
self.projector = 'true' if projector else 'false'
self.reso = 'true' if reso else 'false'
self.length = floor(length/SLOT_LEN) + 1
self.leaders = leaders
def __str__(self):
return f'ak({self.id}, {self.interest}, "{self.track}", {self.projector}, {self.reso}, {self.length}).' # TODO Leader
class Room:
def __init__(self, id, n_places, projector): # TODO internet, whiteboard & co.
self.id = id # must be int
self.n_places = n_places
self.projector = 'true' if projector else 'false'
def __str__(self):
return f'room({self.id}, {self.n_places}, {self.projector}).'
class Timeslot:
def __init__(self, order, timeslot):
self.begin, self.end, self.day = timeslot
self.id = f'{self.day}#{self.begin}#{self.end}'
self.order = order
# self.begin = begin
# self.end = end
# self.day = day
def __str__(self):
return f'timeslot("{self.id}", {self.order}, {self.begin}, {self.end}, {self.day}).'
class Schedule:
def __init__(self, id, workgroup, day, time, room):
self.id = id
self.workgroup = workgroup
self.room = room
self.day = day
self.time = time
def to_dict(self):
return {
'id': self.id,
'workGroup': self.workgroup,
'room': self.room,
'time': self.time,
'day': self.day
}
workgroups = []
rooms = []
id_to_workgroup = {}
id_to_room = {}
for i, workgroup in enumerate(data['workGroups']):
workgroups.append(WorkGroup(workgroup['id'], workgroup['interested'], workgroup['track'], workgroup['projector'],
workgroup['resolution'], workgroup['length'], '')) # TODO add leaders
id_to_workgroup[workgroup['id']] = i
for i, room in enumerate(data['rooms']):
rooms.append(Room(room['id'], room['places'], room['projector'])) # TODO internet, whiteboard & co.
id_to_room[room['id']] = i
rules = """
LENGTH = { schedule(AK,TIMESLOT,ROOM): timeslot(TIMESLOT,_,_,_,_), room(ROOM,_,_) } :- ak(AK,_,_,_,_,LENGTH).
% forbid 2AKS in the same room at the same time
:- schedule(AK1,TIMESLOT,ROOM), schedule(AK2,TIMESLOT,ROOM), AK1 != AK2.
% forbid ak with beamer request in room without beamer
:- schedule(AK,_,ROOM), ak(AK,_,_,true,_,_), room(ROOM,_,false).
% forbid ak in room that is too small
:- schedule(AK,_,ROOM), ak(AK,INTEREST,_,_,_,_), room(ROOM,PLACES,_), PLACES<INTEREST.
% forbid two AKs with same leader at same timestep
:- schedule(AK1,TIMESTEP,_), schedule(AK2,TIMESTEP,_), leader(AK1,LEADER), leader(AK2,LEADER), AK1 != AK2.
"""
for workgroup in workgroups:
rules += str(workgroup) + '\n'
for room in rooms:
rules += str(room) + '\n'
timeslots = [(start, start+200, day) for day in range(DAYS) for start in range(START, END, 200)]
for order, timeslot in enumerate(timeslots):
rules += str(Timeslot(order, timeslot)) + '\n'
rules += "#show schedule/3."
output_file_name = "generated_rules.pl"
open(output_file_name, 'w').write(rules)
# create subprocess definition
process = ['clingo', output_file_name]
# run subprocess, not sure, if busy-waiting..
completed_process = subprocess.run(process, universal_newlines = True, stdout = subprocess.PIPE, stderr = subprocess.DEVNULL)
output = completed_process.stdout
if 'SATISFIABLE' not in output:
print('UNSATISFIABLE')
else:
import re
match = re.match('(.|\n)*Answer: (.+)\n(.*)\nSATISFIABLE', output)
schedules_string = match.group(3)
schedules = []
for i, schedule in enumerate(schedules_string.split('schedule')[1:]):
match = re.match('\((.+),"(.+)#(.+)#.+",(.+)\)', schedule)
workgroup_id = id_to_workgroup[int(match.group(1))]
room_id = id_to_room[int(match.group(4))]
schedules.append(
{
'id': i,
'workGroup': data['workGroups'][workgroup_id],
'room': data['rooms'][room_id],
'time': match.group(3),
'day': match.group(2),
'lockRoom': False,
'lockTime': False,
})
s = {
'id': i,
'workGroup': data['workGroups'][workgroup_id],
'room': data['rooms'][room_id],
'time': match.group(3),
'day': match.group(2),
'lockRoom': False,
'lockTime': False,
}
#json.dumps(s)
data = {}
data['schedules'] = schedules
json.dump(data, open('output.json', 'w'))