-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclormASPwDays
181 lines (152 loc) · 5.83 KB
/
clormASPwDays
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import functions_framework
from clorm import monkey
monkey.patch()
import clingo
from google.cloud import storage
import os
import json
from clorm import ConstantStr,FactBase, StringField, ContextBuilder, Predicate, ConstantField,ph1_
cb = ContextBuilder
ASP_PROGRAM= "tasksUsers.lp"
class DayVals(Predicate):
mon:bool
tue:bool
wed:bool
thu:bool
fri:bool
sat:bool
sun:bool
class User(Predicate):
name:ConstantStr
userID:ConstantStr
#avaliable:DayVals
class AssignDays(Predicate):
user:User
class Task(Predicate):
name:ConstantStr
duration:int
class Assignment(Predicate):
taskValue:ConstantStr
user:ConstantStr
duration:int
#avaliable:DayVals
time:int
days:int
# Triggered by a change in a storage bucket
@functions_framework.cloud_event
def clormTimetable(cloud_event):
def addToUserDict(name, userID):
updateDict={
"userID":userID,
"name":name
}
return updateDict
def readTasks():
storageClient= storage.Client()
bucketVal=storageClient.bucket("asp-react")
blob=bucketVal.blob("Task2.0.json")
taskData=blob.download_as_bytes().decode()
taskDictData=json.loads(taskData)
print(taskVals)
return taskVals
currentMembers=0
#Gain basic info on the file that was just added
data = cloud_event.data
event_id = cloud_event["id"]
event_type = cloud_event["type"]
bucket = data["bucket"]
name = data["name"]
metageneration = data["metageneration"]
timeCreated = data["timeCreated"]
updated = data["updated"]
#Initalize the bucket again to access USER files in the storage
storageClient=storage.Client()
bucketVal=storageClient.bucket(bucket)
blob=bucketVal.blob(name)
#Access the file contents in a way that the program can break down
userData=blob.download_as_bytes().decode()
userVals=json.loads(userData)
maxMembers=userVals['numMember']
allMembersDict ={"userSpecifications":[]}
#Create a somewhat async function that depends on all members being present READ THE USER DOCUMENTS
for blob in storageClient.list_blobs(bucket,prefix=userVals['hiveID']):
userData=blob.download_as_bytes().decode()
userDictData=json.loads(userData)
print(userDictData)
userDict=addToUserDict(userDictData['name'],userDictData['userID'])
allMembersDict["userSpecifications"].append(userDict)
print(allMembersDict)
currentMembers+=1
blobTask= bucketVal.blob("Task2.0.json")
taskData=blobTask.download_as_bytes().decode()
taskValuesJson=json.loads(taskData)
# for tasks in taskVals['TaskValues']:
# print(tasks['taskName'])
# print(allMembersDict)
# allTasksDict=readTasks()
# print(allTasksDict)
#If member is present call the ASP
if maxMembers==currentMembers:
ctrl=clingo.Control(unifier=[User,Task,Assignment])
ctrl.load(ASP_PROGRAM)
users=[User(name=userValues['name'],userID=userValues['userID']) for userValues in allMembersDict['userSpecifications']]
tasks=[Task(name=taskValues['taskName'],duration=taskValues['duration'])for taskValues in taskValuesJson["TaskValues"]]
instances=FactBase(users+tasks)
ctrl.add_facts(instances)
ctrl.ground([("base", [])])
solution= None
print(ctrl)
def on_model(model):
nonlocal solution
solution=model.facts(unifier=[User,Task,Assignment], atoms=True,raise_on_empty=True)
ctrl.solve(on_model=on_model)
if not solution:
raise ValueError("No solution Found")
query = solution.query(Assignment).where(Assignment.user == ph1_).order_by(Assignment.time)
results={}
for u in users:
print(u)
userValSet=set()
assignments = list(query.bind(u.name).all())
userID=u.userID
taskVals=[]
if not assignments:
print("User not assigned any tasks!".format(u.name))
else:
print("User {} assigned to: ".format(u.name))
for a in assignments:
print("\t chore {}, at time {}, on a date".format(a.taskValue,a.time,a.days))
taskVals.append({"TaskValue":a.taskValue, "time": a.time, "date": a.days})
results[str(userID)] = taskVals
bhiveStorage=storage.Client()
bhiveBucket= bhiveStorage.bucket("bhive-81306.appspot.com")
bhiveBlob= bhiveBucket.blob("results/"+userVals['hiveID'])
jsonResults= json.dumps(results,indent=4)
bhiveBlob.upload_from_string(jsonResults)
print("all Members present")
else:
print("Not all Members are present so timetable wont be made")
# ctrl=clingo.Control(unifier =[User, Task,Assignment])
# ctrl.load(ASP_PROGRAM)
# storageClient=storage.Client()
# print(userData[3])
# try:
# userJson= json.loads(userData)
# print(userJson)
# except e:
# print("PLEASEEE WHYYYY "+e)
# def readBucket(fileName, bucketName):
# storageClient=storage.Client()
# bucketBlob=storageClient.bucket(bucketName)
# blob=bucketBlob.blob(fileName)
# userData=blob.download_as_bytes().decode("utf8")
# userVals = json.loads(userData)
# numberHive=userVals['numMember']
# print(userVals)
# return numberHive
# jsonData=json.loads(contentString)
# for blobs in storageClient.list_blobs(bucket,prefix=userVals['hiveID']):
# # userVals[counter]={''}
# blobVal=bucketVal.blob(blobs.name)
# print(userDictData)
# userDictVals.update({'name':userDictData['name'], 'userID':userDictData['userID']})