def invoke_user_allocate(usernm, aid, q): string = 'User Responded and Allocated : ' + str(usernm) + ' For Aid : ' + str(aid) + 'Posses Asset About : ' + str(q) facts.append(string) member = User.objects.get(username=str(usernm)) if member.category == "volunteer": mem = Volunteer.objects.get(user=member) if member.category == 'hospital': mem = Hospital.objects.get(user=member) if member.category == 'accommodation': mem = Accomadation.objects.get(user=member) if member.category == 'pronearea': mem = Pronearea.objects.get(user=member) if mem.status == 0: mem.status = 1 mem.loyalto = eng_var[4] mem.save() try: a = clips.Fact(eng_var[3]) a.Slots['username'] = usernm a.Slots['foraid'] = aid a.Slots['action'] = "Allocated" a.Slots['quantity'] = q a.Assert() except clips.ClipsError: print ("Already Asserted Fact") for tmap in list_map: if tmap['title'] == str(usernm) and tmap['aid']==str(aid): tmap['status']="Allocated"
def user_status_checking(): dealers = Messagebox.objects.filter(fromaddr=eng_var[4], topic="Request for Service", ack=1, nack=0) a = clips.Fact(eng_var[3]) print (clips.PrintFacts()) for dealer in dealers: try: a.Slots['username'] = dealer.toaddr a.Slots['foraid'] = dealer.message a.Slots['action'] = "Allocate" a.Slots['quantity'] = dealer.parameter a.Assert() dealer.nack = 1 dealer.save() except clips.ClipsError: print ("Already Asserted Fact")
def prone_area_checking(lat, longt, obj1, dist=500): print('Prone area checking invoked') try: prone_area = Pronearea.objects.all() for area in prone_area: if distance((lat, longt), (area.user.latitude, area.user.longitude)) <= dist: try: a = clips.Fact(obj1) a.Slots['username'] = area.user.username a.Slots['foraid'] = "ProneArea" a.Slots['action'] = "AlertArea" a.Slots['quantity'] = area.population a.Assert() except clips.ClipsError: print('Clips Error') except Pronearea.DoesNotExist: pass
def message_dealer(usernm, lat3, long3, obj): dealers = Messagebox.objects.filter(toaddr=usernm, topic="Request", ack=1, nack=0) e = clips.Fact(obj) print(clips.PrintFacts()) for dealer in dealers: try: e.Slots['aid'] = dealer.message e.Slots['latitude'] = lat3 e.Slots['longitude'] = long3 e.Slots['quantity'] = dealer.parameter e.Assert() dealer.nack = 1 dealer.save() except clips.ClipsError: print("Already Asserted Fact")
def invoke_alert_area(pname,p): string = 'Prone Area Found : ' + str(pname) + ' Alerted the Area ' facts.append(string) msg = Messagebox() msg.fromaddr = eng_var[4] msg.toaddr = str(pname) msg.topic = "Alert" msg.message = "NearBy Hazard Need To Displace To safer Location" msg.parameter = 0 msg.save() try: member = User.objects.get(username=str(pname)) d = clips.Fact(eng_var[5]) d.Slots['hazard'] ="displace" d.Slots['latitude'] = member.latitude d.Slots['longitude'] = member.longitude d.Slots['span'] = int(p) d.Assert() except clips.ClipsError: print("Clips Error")
import clips clips.Reset() t0 = clips.BuildTemplate("person", """ (slot name (type STRING)) (slot age (type INTEGER)) """, "template for a person") print t0.PPForm() f1 = clips.Fact(t0) f1_slotkeys = f1.Slots.keys() print f1_slotkeys f1.Slots['name'] = "Grace" f1.Slots['age'] = 24 print f1.PPForm() clips.PrintFacts() f1.Assert() clips.PrintFacts() f1.Retract() clips.PrintFacts()
def system_run(input_ set): if 'hazard' in input_set: clips.Clear() clips.Reset() eng_var[:]=[] eng_var.append(input_set['lat']) eng_var.append(input_set['long']) eng_var.append(clips.BuildTemplate("entity", """ (slot aid (type STRING)) (slot latitude (type NUMBER)) (slot longitude (type NUMBER)) (slot quantity (type NUMBER)) """, "template for a entity")) eng_var.append(clips.BuildTemplate("allies", """ (slot username (type STRING)) (slot foraid (type STRING)) (slot action (type STRING)) (slot quantity (type NUMBER)) """, "template for a allies")) eng_var.append(input_set['control']) eng_var.append(clips.BuildTemplate("disaster", """ (slot hazard (type STRING)) (slot latitude (type NUMBER)) (slot longitude (type NUMBER)) (slot span (type NUMBER)) """, "template for a disaster")) d=clips.Fact(eng_var[5]) d.Slots['hazard'] = input_set['hazard'] d.Slots['latitude'] = input_set['lat'] d.Slots['longitude'] = input_set['long'] d.Slots['span'] = input_set['span'] d.Assert() facts[:] = [] fields[:] = [] list_map[:] = [] clips.RegisterPythonFunction(invoke_pronearea) clips.RegisterPythonFunction(display_disaster) clips.RegisterPythonFunction(invoke_entity) clips.RegisterPythonFunction(invoke_useralert) clips.RegisterPythonFunction(invoke_user_allocate ) clips.RegisterPythonFunction(invoke_user_deallocate) clips.RegisterPythonFunction(invoke_message) clips.RegisterPythonFunction(invoke_alert_area) clips.BatchStar("/home/jishnu/PycharmProjects/dmis/controlunit/expertsystem/inference.clp") clips.Run() if 'refresh' in input_set: if len(eng_var)==0: return {'fact': facts, 'field': fields,'map_data':list_map} message_dealer(input_set['control'],eng_var[0],eng_var[1],eng_var[2]) user_status_checking() clips.Run() if 'send' in input_set: if len(eng_var)==0: return {'fact': facts, 'field': fields,'map_data':list_map} message_handler(input_set['control'],input_set['selectaid'],input_set['parameter']) if 'deallocate' in input_set: try: clips.Assert("(DeallocateAll)") except clips.ClipsError: print("Error in Deallocation") clips.Run() facts[:] = [] fields[:] = [] list_map[:] = [] clips.SendCommand("run") print (clips.PrintFacts()) list_temp=list(facts) list_temp.reverse() return {'fact':list_temp,'field':list(set(fields)),'map_data':list_map}
def resource_handler(req_aid, lat, longt, quantity, allies): if quantity == 0: return gathered_quantity = 0 rangedistance = 50 if req_aid == "Food": try: food = Food.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in food: print( str(lat) + " " + str(longt) + "acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude) + " " + str(rangedistance) + " " + str(quantity) + " gr" + str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.packet a.Assert() gathered_quantity = gathered_quantity + aid.packet except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Food.DoesNotExist: pass if req_aid == "Volunteer": try: vol = Volunteer.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in vol: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.humanaid a.Assert() gathered_quantity = gathered_quantity + aid.humanaid except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Volunteer.DoesNotExist: pass if req_aid == "Hospital": try: hos = Hospital.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in hos: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.canoccupy a.Assert() gathered_quantity = gathered_quantity + aid.canoccupy except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Hospital.DoesNotExist: pass if req_aid == "Accommodation": try: acc = Accomadation.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in acc: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.capacity a.Assert() gathered_quantity = gathered_quantity + aid.capacity except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Accomadation.DoesNotExist: pass if req_aid == "Water": try: wat = Water.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in wat: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.quantity a.Assert() gathered_quantity = gathered_quantity + aid.quantity except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Water.DoesNotExist: pass if req_aid == "RescueTool": try: restool = Rescuetool.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in restool: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.box a.Assert() gathered_quantity = gathered_quantity + aid.box except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Rescuetool.DoesNotExist: pass if req_aid == "FirstAid": try: firstaid = Firstaid.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in firstaid: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.kit a.Assert() gathered_quantity = gathered_quantity + aid.kit except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Firstaid.DoesNotExist: pass if req_aid == "Transport-Human": try: thuman = TransportHuman.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in thuman: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.capacity a.Assert() gathered_quantity = gathered_quantity + aid.capacity except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except TransportHuman.DoesNotExist: pass if req_aid == "Transport-Goods": try: tgoods = TransportGoods.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in tgoods: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.capacity a.Assert() gathered_quantity = gathered_quantity + aid.capacity except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except TransportGoods.DoesNotExist: pass if req_aid == "JCB": try: mach1 = Machine.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in mach1: if aid.jcb != 0: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.jcb a.Assert() gathered_quantity = gathered_quantity + aid.jcb except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Machine.DoesNotExist: pass if req_aid == "FireEngine": try: mach2 = Food.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in mach2: if aid.fireengine != 0: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.fireengine a.Assert() gathered_quantity = gathered_quantity + aid.fireengine except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Machine.DoesNotExist: pass if req_aid == "Ambulance": try: mach3 = Machine.objects.all() while (gathered_quantity <= quantity) and (rangedistance <= 1500): for aid in mach3: if aid.ambulance != 0: #print (str(lat)+" "+str(long)+"acquired " + str(aid.user.latitude) + " " + str(aid.user.longitude)+" "+str(rangedistance)+" "+str(quantity)+" gr"+str(gathered_quantity)) if distance((lat, longt), (aid.user.latitude, aid.user.longitude)) <= rangedistance: try: print("got " + aid.user.username) a = clips.Fact(allies) a.Slots['username'] = aid.user.username a.Slots['foraid'] = req_aid a.Slots['action'] = "Alert" a.Slots['quantity'] = aid.ambulance a.Assert() gathered_quantity = gathered_quantity + aid.ambulance except clips.ClipsError: print("Already Asserted Fact") rangedistance = rangedistance + 50 except Machine.DoesNotExist: pass if gathered_quantity < quantity: try: clips.Assert( "(Message \"Searched Aid : " + req_aid + " # Quantity is Unavailable in Perimeter , Gathered Quantity :" + str(gathered_quantity) + "\")") except clips.ClipsError: print("Error") return