def new_place(self, candidates=None, terrains=None, types=None): places = (place for place in self._kb.filter(facts.Place) if place.uid not in self._reserved) if types is not None: places = (place for place in places if place.type in types) if candidates is not None: places = (place for place in places if place.uid in candidates) if terrains: terrains = set(terrains) places = (place for place in places if set(place.terrains) & terrains) places = list(places) if not places: raise exceptions.NoFactSelectedError(method='new_place', arguments={ 'terrains': terrains, 'types': types, 'candidates': candidates }, reserved=self._reserved) place = random.choice(places) self._reserved.add(place.uid) return place
def preferences_friend(self): try: return self._kb.filter(facts.PreferenceFriend).next() except StopIteration: raise exceptions.NoFactSelectedError(method='preferences_friend', arguments={}, reserved=self._reserved)
def preferences_enemy(self): try: return next(self._kb.filter(facts.PreferenceEnemy)) except StopIteration: raise exceptions.NoFactSelectedError(method='preferences_enemy', arguments={}, reserved=self._reserved)
def upgrade_equipment_cost(self): try: return self._kb.filter(facts.UpgradeEquipmentCost).next() except StopIteration: raise exceptions.NoFactSelectedError( method='upgrade_equipment_cost', arguments={}, reserved=self._reserved)
def new_person(self, first_initiator, candidates=None, professions=None, places=None, restrict_places=True, restrict_persons=True): locations = self._locations(places=places, restrict_places=restrict_places, restrict_objects=restrict_persons) persons = (self._kb[location.object] for location in locations if isinstance(self._kb[location.object], facts.Person)) if professions is not None: persons = (person for person in persons if person.profession in professions) if candidates is not None: persons = (person for person in persons if person.uid in candidates) if first_initiator: not_initiators = set( restriction.person for restriction in self._kb.filter(facts.NotFirstInitiator)) persons = (person for person in persons if person.uid not in not_initiators) persons = list(persons) if not persons: raise exceptions.NoFactSelectedError(method='new_person', arguments={ 'first_initiator': first_initiator, 'candidates': candidates, 'professions': professions, 'places': places, 'restrict_places': restrict_places, 'restrict_persons': restrict_persons }, reserved=self._reserved) person = random.choice(persons) self._reserved.add(person.uid) return person
def place_for(self, objects): locations = list( self._locations(objects=objects, restrict_places=False, restrict_objects=False)) if not locations: raise exceptions.NoFactSelectedError( method='place', arguments={'objects': objects}, reserved=self._reserved) place = self._kb[random.choice(locations).place] self._reserved.add(place.uid) return place
def new_person(self, first_initiator=False, candidates=None, professions=None, places=None, restrict_places=True, restrict_persons=True, restrict_social_connections=(), social_connections=()): locations = self._locations(places=places, restrict_places=restrict_places, restrict_objects=restrict_persons) persons = (self._kb[location.object] for location in locations if isinstance(self._kb[location.object], facts.Person)) for connected_person_uid, social_connection_type in restrict_social_connections: persons = ( person for person in persons if not self.check_social_connections( person, connected_person_uid, social_connection_type)) social_filter_applied = False if social_connections: probability = self._social_connection_probability * len( set(connected_person_uid for connected_person_uid, social_connection_type in social_connections)) if random.random() < probability: social_filter_applied = True persons = (person for person in persons if any( self.check_social_connections(person, connected_person_uid, social_connection_type) for connected_person_uid, social_connection_type in social_connections)) if professions is not None: persons = (person for person in persons if person.profession in professions) if candidates is not None: persons = (person for person in persons if person.uid in candidates) if first_initiator: not_initiators = set( restriction.person for restriction in self._kb.filter(facts.NotFirstInitiator)) persons = (person for person in persons if person.uid not in not_initiators) persons = list(persons) if not persons: if social_filter_applied: return self.new_person( first_initiator=first_initiator, candidates=candidates, professions=professions, places=places, restrict_places=restrict_places, restrict_persons=restrict_persons, restrict_social_connections=restrict_social_connections, social_connections=( )) # <- reset social connections requirements else: raise exceptions.NoFactSelectedError(method='new_person', arguments={ 'first_initiator': first_initiator, 'candidates': candidates, 'professions': professions, 'places': places, 'restrict_places': restrict_places, 'restrict_persons': restrict_persons }, reserved=self._reserved) person = random.choice(persons) self._reserved.add(person.uid) return person