def getAllBuildings(self):
		checkData(locals())

		buildings = Buildings()
		buildingList = []

		for building in buildings.getAllBuildings():
			buildingList.append(building.getDic())

		return {"buildings" : buildingList}
Example #2
0
	def test6(self):

		print "hi i'm here!!"

		from app.backend.model.buildings import Buildings		
		from app.backend.controller.rulesetChecker import RulesetChecker	
		import random	
		import time,datetime
		from app.backend.commons.console import flash

		buildings = Buildings()
		
		for building in buildings.getAllBuildings():
			buildingRules = building.getRules()


			for ruleNumber in range(0,81):		
				print "Testing with ruleNumber " + str(ruleNumber)
				for testNumber in range(0, 250):
					print "Testing with ruleNumber " + str(ruleNumber) + " #iteration: " + str(testNumber)
					tmpRuleSet = []
					tmpRuleIndex = []

					#Creating the temporary ruleset
					for i in range(0, ruleNumber):
						selectedRule = random.randint(0,len(buildingRules)-1)
						if selectedRule not in tmpRuleIndex:
							tmpRuleIndex.append(selectedRule)
							tmpRuleSet.append(buildingRules[selectedRule])

					rulesetChecker = RulesetChecker(tmpRuleSet)
		
					startTimeMilliseconds = long((time.time() + 0.5) * 1000)
					ruleCheckErrorList = rulesetChecker.check()
					endTimeMilliseconds = long((time.time() + 0.5) * 1000)
					
					opTimeMilliseconds = endTimeMilliseconds - startTimeMilliseconds

					if len(ruleCheckErrorList) == 0:
						flash("EXPERIMENT-TestRoomRuleVerification [SUCCESS]:" + "#rules=" + str(ruleNumber) + " - opTimeMilliseconds:" + str(opTimeMilliseconds))
					else:
						flash("EXPERIMENT-TestRoomRuleVerification [FAILED]:" + "#rules=" + str(ruleNumber) + " - opTimeMilliseconds:" + str(opTimeMilliseconds))

			print "DONE."
Example #3
0
	def start(self):

		import time,datetime
		startTimeMilliseconds = long((time.time() + 0.5) * 1000)

		analyzedRoomCounter = 0

		flash("Starting the actuation process...", "yellow")

		buildings = Buildings()
		buildingsManager = BuildingsManager()
		groupsManager = GroupsManager()
		rules = Rules()

		rules.resetActiveRules()
		
		for building in buildings.getAllBuildings():

			flash("Working on building '" + building.buildingName + "'...", "blue")

			triggeredRules = []
			triggeredRulesId = []
			
			# Getting all the triggered rules for the considered building	
			buildingRules = building.getRules()

			if len(buildingRules) == 0:
				flash("Nothing to do...")

			if len(buildingRules):
				for rule in buildingRules:

					if rule.roomName and not rule.groupId:

						if self.skipRuleOnRoomFilter(buildingName = building.buildingName, roomName = rule.roomName): continue
						analyzedRoomCounter += 1

						if self.checkRuleTrigger(rule):
							# If the antecedent of the rule is triggered, let us store the rule as triggered!
							triggeredRules.append(rule)
							triggeredRulesId.append(rule.id)
					
					elif rule.groupId and not rule.roomName:

						groupRoomList = groupsManager.getRooms(buildingName = building.buildingName, groupId = rule.groupId)["rooms"]
						for room in groupRoomList:

							if self.skipRuleOnRoomFilter(buildingName = building.buildingName, roomName = room.roomName): continue

							roomName = room["roomName"]
							newRule = copy.copy(rule)			# I need to copy the object to modify the room name
							newRule.roomName = roomName

							if self.checkRuleTrigger(newRule):
								# If the antecedent of the rule is triggered, let us store the rule as triggered!
								triggeredRules.append(newRule)
								triggeredRulesId.append(newRule.id)



				flash(building.buildingName + " - Total rules: " + str(len(buildingRules)), "gray")
				flash(building.buildingName + " - Triggered rules: " + str(len(triggeredRules)), "gray")


				# Now, let us partition rules among "Pure-Room-Rules" and "CRVG-Rules"
				roomScheduledRules = {}
				crvgScheduledRules = {}

				for rule in triggeredRules:



					if rule.roomName:

						# In this case we are selecting the rules spiecified for a specific room.
						# If the rule (for a specific category) is saved into a room belonging to a CRVG, I have to save it into the crvgScheduledRules set.
						# If the rule is not part of CRVG ruleset, then it is savet into the roomScheduledRules set.

						buildingName = building.buildingName
						roomName = rule.roomName
						validationCategories = [rule.category]
						crvgList = buildingsManager.getCrossRoomValidationGroups(buildingName = buildingName, roomName = roomName, validationCategories = validationCategories)

						if len(crvgList) == 0:


							if not roomName in roomScheduledRules.keys():
								roomScheduledRules[roomName] = []

							roomScheduledRules[roomName].append(rule)

						elif len(crvgList) == 1:

							
							if not crvgList[0].id in crvgScheduledRules.keys():
								crvgScheduledRules[crvgList[0].id] = []
							
							rule.gropuId = crvgList[0].id
							crvgScheduledRules[crvgList[0].id].append(rule)

						else:
							raise WrongBuildingGroupRipartitionError(roomName + " has been found to be part of two different Cross Room Validation Groups. This is not allowed.")

					elif rule.groupId and not rule.roomName:


						# Here we are selecting those rules that have been specified of a specific group.
						# Those groups can be standard groups or CRV GROUPS on a specific category. In the first case, i have to add a copy of the rule in each of the group rooms.
						# In the second case I have to add the rule to the corresponding CRVG dict (if the rule category is right).

						if groupsManager.isCrossRoomsValidationGroup(buildingName = building.buildingName, groupId = rule.groupId, crossRoomsValidationCategory = rule.category):

							if not rule.groupId in crvgScheduledRules.keys():
								crvgScheduledRules[rule.groupId] = []
							
							crvgScheduledRules[rule.groupId].append(rule)

						else:		
							raise UnknownError("Unexpected error into the database.")




					else:
						raise UnknownError("The rule with id " + rule.id + " has both the groupId and roomName field not null.")

				flash(building.buildingName + " - Number of rooms: " + str(len(roomScheduledRules.keys())), "gray")
				flash(building.buildingName + " - Number of CRV Groups: " + str(len(crvgScheduledRules.keys())), "gray")

				actuatedRulesCounter = 0

				flash("Executing actions for rooms...", "yellow")
				# Executing the rules per each room
				# In the case I have the same action category, I'll take the action with higher priority
				for roomName in roomScheduledRules.keys():
					flash("Room [" + building.buildingName + "." + roomName + "]..." , "blue")				
					ruleList = roomScheduledRules[roomName]
					#Let us order by rule priority
					ruleList = sorted(ruleList, key=lambda rule: rule.getPriority(), reverse=True)

					alreadyAppliedCategories = []
					for rule in ruleList:
							
						if rule.category not in alreadyAppliedCategories:

							alreadyAppliedCategories.append(rule.category)
							self.executeRule(rule)
							actuatedRulesCounter += 1
						else:
							flash(building.buildingName + " - Room " + roomName + ", ruleId " + str(rule.id) + " ignored.")

				flash("Executing actions for CRV Groups...", "yellow")
				for crvgId in crvgScheduledRules.keys():
					flash("Group " + building.buildingName + ".g[" + str(crvgId) + "]..." , "blue")				
					ruleList = crvgScheduledRules[crvgId]

					#Let us order by rule priority
					ruleList = sorted(ruleList, key=lambda rule: rule.getPriority(), reverse=True)

					alreadyAppliedCategories = []
					for rule in ruleList:
						if rule.category not in alreadyAppliedCategories:
							alreadyAppliedCategories.append(rule.category)
							self.executeRule(rule)
							actuatedRulesCounter += 1
						else:
							flash(building.buildingName + " - CRVGroup " + str(crvgId) + ", ruleId " + str(rule.id) + " ignored.")
							self.notifyIgnoredRule(rule)

		flash("The actuation process is ended.", "yellow")		
		endTimeMilliseconds = long((time.time() + 0.5) * 1000)
		opTimeMilliseconds = endTimeMilliseconds - startTimeMilliseconds
		flash("RunTimeRuleActuation:::RoomFilter=" + str(self.roomFilter) + "::Time=" + str(opTimeMilliseconds) + "::NumberOfRules:" + str(analyzedRoomCounter) + "::TriggeredRules:" + str(len(triggeredRules)) + "::ActuatedRules:" + str(actuatedRulesCounter) + "::IgnoredRules:" + str(len(triggeredRules)-actuatedRulesCounter))