Пример #1
0
class RuleEngine():
    """Rule engine that are used to manage all the rules associated with a
       cloud manager"""

    def __init__(self, resources, cmd_queue):
        self.cmd_queue = cmd_queue
        self.resource_info = resources
        self.resources = {}
        self.my_intellect = Intellect()
        self.agenda_groups = []

    def init_resources(self):
        '''
        creates a new instance of Resource for each
        resource in self.resources
        '''
        for resource in self.resource_info:
            self.resources[resource['id']] = Resource(resource_id=resource["id"],
                                                      name=resource["name"],
                                                      command_queue=self.cmd_queue)
            print("[RuleEngine] Add resource {0} as fact".format(resource["name"]))
            self.my_intellect.learn(self.resources[resource['id']])

    def add_instance(self, resource):
        self.resources[resource['id']] = Resource(resource_id=resource["id"],
                                                  name=resource["name"],
                                                  command_queue=self.cmd_queue)
        print("[RuleEngine] Add resource {0} as fact".format(resource["name"]))
        self.my_intellect.learn(self.resources[resource['id']])


    def read_policies(self):
        policy_a = self.my_intellect.learn(Intellect.local_file_uri("intellect/policies/openstack.policy"))
        #print("[RuleEngine] Openstack policy loaded")
        # TEST
        # TODO gestione agenda groups
        self.agenda_groups.append("cpu")
        self.agenda_groups.append("network")

    def run(self, meters_queue):
        self.init_resources()
        self.read_policies()
        while True:
            try:
                element = meters_queue.get()
                print("[RuleEngine] Value received for resource {0}"
                       .format(str(element)))

                self.resources[element["resource_id"]].add_sample(meter=element["meter"],
                                                                  value=element["value"],
                                                                  timestamp=element["timestamp"])

                self.check_policies()

            except Exception, e:
                print("[RuleEngine] Error %s" % e.args[0])
                print(sys.exc_info()[0].__name__)
                print(os.path.basename(sys.exc_info()[2].tb_frame.f_code.co_filename))
                print(sys.exc_info()[2].tb_lineno)
 def __init__(self, registry=None, logger=None):
     self.active_agreements = {}
     self.active_policies = {}
     self.subscribed_devices = {}
     self.logger = logger or logging.getLogger(__name__)
     Intellect.__init__(self)
     if registry:
         RulesEngine._registry = registry
         DB.remove({})
Пример #3
0
 def __init__(self, registry=None, logger=None):
     self.active_agreements = {}
     self.active_policies = {}
     self.subscribed_devices = {}
     self.logger = logger or logging.getLogger(__name__)
     Intellect.__init__(self)
     if registry:
         RulesEngine._registry = registry
         DB.remove({})
Пример #4
0
def execute_balance_calculation_rule(ws_id, user_id):

    leaves = DayOff.query(). \
        filter(DayOff.user_id == user_id). \
        filter(DayOff.workspace_id == ws_id). \
        all()

    ws_user = WorkspaceUser.find(user_id=user_id, ws_id=ws_id)

    rule_payload = BalanceCalculationRulePayload(start_date=ws_user.start_date)

    node = _get_rule_node(ws_id=ws_id,
                          type=WorkspaceRuleTypes.BALANCE_CALCULATION)

    intellect = Intellect()
    intellect.policy.append_child(node)

    for leave_day in _leaves_to_leave_days(leaves):
        intellect.learn_fact(leave_day)

    intellect.learn_fact(rule_payload)

    intellect.reason()

    return rule_payload
Пример #5
0
def execute_day_off_validation_rule(day_off):

    balance = execute_balance_calculation_rule(ws_id=day_off.workspace_id,
                                               user_id=day_off.user_id)

    ws_user = WorkspaceUser.find(user_id=day_off.user_id,
                                 ws_id=day_off.workspace_id)

    if not ws_user:
        raise Exception('User is not added to the specified workspace')

    ws_roles = WorkspaceUserRole.find_all(ws_id=day_off.workspace_id,
                                          user_id=day_off.user_id)

    rule_payload = DayOffValidationPayload(user_start_date=ws_user.start_date,
                                           leave=day_off,
                                           balance=balance,
                                           user_roles=list(
                                               map(lambda r: r.role,
                                                   ws_roles)))

    intellect = Intellect()
    intellect.policy.append_child(
        _get_rule_node(ws_id=day_off.workspace_id,
                       type=WorkspaceRuleTypes.DAY_OFF_VALIDATION))

    for leave_day in _leaves_to_leave_days([day_off]):
        intellect.learn_fact(leave_day)

    intellect.learn_fact(rule_payload)

    intellect.reason()

    return rule_payload
Пример #6
0
 def read_policies(self):
     policy_a = self.my_intellect.learn(Intellect.local_file_uri("intellect/policies/openstack.policy"))
     #print("[RuleEngine] Openstack policy loaded")
     # TEST
     # TODO gestione agenda groups
     self.agenda_groups.append("cpu")
     self.agenda_groups.append("network")
Пример #7
0
    def read_policies(self):
        def is_policy(_file):
            return _file.endswith('.policy')

        _path = policies.__path__[0]
        all_policies = filter(is_policy, os.listdir(_path))
        for policy in all_policies:
            self.my_intellect.learn(Intellect.local_file_uri( policies.__path__[0] + '/' + policy))

        logging.info("All policies loaded")

        _File = open(_path + '/' + 'agenda-groups','r')
        for _line in _File:
            self.agenda_groups.append(_line.rstrip())

        logging.info("All agenda-groups loaded")

        def queue_get_all(self,q):
            items = []
            maxItemsToRetreive = 3
            for numOfItemsRetrieved in range(0, maxItemsToRetreive):
                try:
                    if numOfItemsRetrieved == maxItemsToRetreive:
                        break
                        items.append(q.get_nowait())
                except Empty, e:
                    break
                return items
Пример #8
0
 def read_policies(self):
     self.my_intellect.learn(Intellect.local_file_uri("intellect/policies/openstack.policy"))
     logging.info("OpenStack policy loaded")
     # TEST
     # TODO gestione agenda groups
     self.agenda_groups.append("cpu")
     self.agenda_groups.append("network")
Пример #9
0
 def __init__(self, resources, cmd_queue):
     self.cmd_queue = cmd_queue
     self.resource_info = resources
     self.resources = {}
     self.my_intellect = Intellect()
     self.agenda_groups = []
     self.signal = True
Пример #10
0
def _get_rule_node(ws_id, type):
    ws_rule = WorkspaceRule.find(ws_id=ws_id, type=type)

    if not ws_rule:
        raise Exception(f'{type} rule has\'t been defined for workspace.')

    if not ws_rule.node:
        return Intellect().learn_policy(ws_rule.rule)

    return pickle.loads(ws_rule.node)
Пример #11
0
class RuleEngine():
    """Rule engine that are used to manage all the rules associated with a
       cloud manager"""

    def __init__(self, resources, cmd_queue):
        self.cmd_queue = cmd_queue
        self.resource_info = resources
        self.resources = {}
        self.my_intellect = Intellect()
        self.agenda_groups = []
        self.signal = True

    def init_resources(self):
        '''
        creates a new instance of Resource for each
        resource in self.resources
        '''
        logging.debug("Initializing rule engine resources")
        for resource in self.resource_info:
            self.resources[resource['id']] = Resource(resource_id=resource["id"],
                                                      name=resource["name"],
                                                      command_queue=self.cmd_queue)
            logging.info("Add resource {0} as fact".format(resource["name"]))
            self.my_intellect.learn(self.resources[resource['id']])

    def add_instance(self, resource):
        self.resources[resource['id']] = Resource(resource_id=resource["id"],
                                                  name=resource["name"],
                                                  command_queue=self.cmd_queue)
        logging.info("Add resource {0} as fact".format(resource["name"]))
        self.my_intellect.learn(self.resources[resource['id']])


    def read_policies(self):
        self.my_intellect.learn(Intellect.local_file_uri("intellect/policies/openstack.policy"))
        logging.info("OpenStack policy loaded")
        # TEST
        # TODO gestione agenda groups
        self.agenda_groups.append("cpu")
        self.agenda_groups.append("network")

    def run(self, meters_queue):
        self.init_resources()
        self.read_policies()
        logging.info("Rule engine initialization completed")

        while self.signal:
            try:
                element = meters_queue.get()
                logging.info("[RuleEngine] Value received for resource {0}".format(str(element)))
                logging.debug("Add sample: {0}".format(element))

                self.resources[element["resource_id"]].add_sample(meter=element["meter"],
                                                                  value=element["value"],
                                                                  timestamp=element["timestamp"])

                self.check_policies()

            except Exception, e:
                logging.error("An error occured: %s" % e.args[0])
        username = '******'
        projectname = 'training'
        event_type = 'course_complete'
        event_data = {}
        event_data[event_type] = training_event_data = {}
    
        course_ids = ['008031', '008189', '008582', '009446', '013413', '013567', '016003', '016094', '017724', '020146', '023416']

        for cid in course_ids:
            print ('Adding course {0}'.format(cid))
            training_event_data[cid] = event_dtg
            
            class Object(object):
                pass
            user = Object()
            user.username = username
            project = Object()
            project.name = projectname
            
            self.reason(policy, DemoState(user, project, event_data))

if __name__ == '__main__':
    policy1 = "from state import DemoState\nrule 'Rule 1':\n\twhen:\n\t\t$state := DemoState((project.name == 'training') and ('course_complete' in event_data) and ('008031' in event_data['course_complete']) and ('008189' in event_data['course_complete']) and ('008582' in event_data['course_complete']) and ('009446' in event_data['course_complete']) and ('013413' in event_data['course_complete']) and ('013567' in event_data['course_complete']) and ('016003' in event_data['course_complete']) and ('016094' in event_data['course_complete']) and ('017724' in event_data['course_complete']) and ('020146' in event_data['course_complete']) and ('023416' in event_data['course_complete']))\n\tthen:\n\t\t$state.award($state.user, $state.project, 'Gold')\n"
    utilIntellect = Intellect()
    policy2 = utilIntellect.local_file_uri('./mandatory_training_2.policy')
    engine = RulesEngine()
    engine.run_test(policy1, '') # User should get gold
    engine.run_test(policy2, datetime.now()) # User should get gold
    engine.run_test(policy2, datetime(2015, 1, 1)) # User should get silver
    engine.run_test(policy2, datetime(2015, 2, 1)) # User should get bronze
Пример #13
0
class RuleEngine():
    """Rule engine that are used to manage all the rules associated with a
       cloud manager"""

    def __init__(self, resources, cmd_queue):
        self.cmd_queue = cmd_queue
        self.resource_info = resources
        self.resources = {}
        self.my_intellect = Intellect()
        self.agenda_groups = []
        self.signal = True

    def init_resources(self):
        '''
        creates a new instance of Resource for each
        resource in self.resources
        '''
        logging.debug("Initializing rule engine resources")
        for resource in self.resource_info:
            self.resources[resource['id']] = Resource(resource_id=resource["id"],
                                                      name=resource["name"],
                                                      command_queue=self.cmd_queue)
            logging.info("Add resource {0} as fact".format(resource["name"]))
            self.my_intellect.learn(self.resources[resource['id']])

    def add_instance(self, resource):
        self.resources[resource['id']] = Resource(resource_id=resource["id"],
                                                  name=resource["name"],
                                                  command_queue=self.cmd_queue)
        logging.info("Add resource {0} as fact".format(resource["name"]))
        self.my_intellect.learn(self.resources[resource['id']])


    def read_policies(self):
        def is_policy(_file):
            return _file.endswith('.policy')

        _path = policies.__path__[0]
        all_policies = filter(is_policy, os.listdir(_path))
        for policy in all_policies:
            self.my_intellect.learn(Intellect.local_file_uri( policies.__path__[0] + '/' + policy))

        logging.info("All policies loaded")

        _File = open(_path + '/' + 'agenda-groups','r')
        for _line in _File:
            self.agenda_groups.append(_line.rstrip())

        logging.info("All agenda-groups loaded")

        def queue_get_all(self,q):
            items = []
            maxItemsToRetreive = 3
            for numOfItemsRetrieved in range(0, maxItemsToRetreive):
                try:
                    if numOfItemsRetrieved == maxItemsToRetreive:
                        break
                        items.append(q.get_nowait())
                except Empty, e:
                    break
                return items
def handle_event(request, *args, **kwargs):
    if request.method == 'POST':

        data = json.loads(request.body)

        # Get user and project
        user = get_object_or_404(User, username=kwargs['username'])
        project = get_object_or_404(Project, name=kwargs['projectname'])

        # Get event DTG
        try:
            event_dtg_str = data['event_dtg']
            try:
                event_dtg = parse(event_dtg_str)
            except ValueError:
                return HttpResponse('Invalid event_dtg', status=400)
        except MultiValueDictKeyError:
            event_dtg = timezone.now()
        #print('handle_event got event_dtg {0}').format(datetime.strftime(event_dtg, '%Y-%m-%dT%H:%M:%S%Z'))

        # Get event details
        try:
            details = data['details']
        except MultiValueDictKeyError:
            details = None
        #print('handle_event got details {0}').format(details)

        # Create Event object
        try:
            current_event = Event(user=user,
                                  project=project,
                                  event_dtg=event_dtg,
                                  details=details)
        except ValueError, ve:
            #print('handle_event failed create event: {0}').format(ve)
            return HttpResponse(
                'Invalid event',
                status=400)  # If, for example, 'details' JSON does not load

        # Save Event object
        current_event.save()

        #####################################################################
        # 'Training' demo policies. Assumes there is a 'training' project with badge id 1. Badge awarded when all courses finished.
        # Sample curl command for sending event for user 'admin':
        # curl -d "details={\"event_type\":\"course_complete\",\"course_id\":\"008031\"}" http://localhost:8000/users/admin/projects/training/event/
        #state_policy = "from gamification.events.models import Event\nrule 'Rule 1':\n\twhen:\n\t\t$event := Event(('event_type' in details_map) and ('course_complete' in details_map['event_type']) and ('course_id' in details_map))\n\tthen:\n\t\t$event.update_state('course_complete', $event.details_map['course_id'], $event.event_dtg)\n"
        #award_policy = "from gamification.events.state import State\nrule 'Rule 1':\n\twhen:\n\t\t$state := State((project.name == 'training') and ('course_complete' in event_data) and ('008031' in event_data['course_complete']) and ('008189' in event_data['course_complete']) and ('008582' in event_data['course_complete']) and ('009446' in event_data['course_complete']) and ('013413' in event_data['course_complete']) and ('013567' in event_data['course_complete']) and ('016003' in event_data['course_complete']) and ('016094' in event_data['course_complete']) and ('017724' in event_data['course_complete']) and ('020146' in event_data['course_complete']) and ('023416' in event_data['course_complete']))\n\tthen:\n\t\t$state.award($state.user, $state.project, 1)\n"
        #####################################################################

        #####################################################################
        # 'GeoQ AOI' demo policies. Assumes there is a 'geoq' project with badge id 4. Badge awarded when at least three AOIs are completed.
        # Sample curl command for sending event for user 'admin':
        # curl -d "details={\"event_type\":\"aoi_complete\",\"aoi_id\":\"2\"}" http://localhost:8000/users/admin/projects/geoq/event/
        #bronze_state_policy = "from gamification.events.models import Event\nrule 'Rule 1':\n\twhen:\n\t\t$event := Event((" + str(current_aoi) + " == details_map['aoi_id']) and ( details_map['feature_count'] > 0))\n\tthen:\n\t\t$event.update_state('aoi_complete', $event.details_map['aoi_id'], $event.event_dtg)\n"
        #bronze_award_policy = "from gamification.events.state import State\nrule 'Rule 1':\n\twhen:\n\t\t$state := State((project.name == 'django_geoq') and ('aoi_complete' in event_data) and (len(event_data['aoi_complete']) > 0))\n\tthen:\n\t\t$state.award($state.user, $state.project, 6)\n"
        #silver_state_policy = "from gamification.events.models import Event\nrule 'Rule 1':\n\twhen:\n\t\t$event := Event(('event_type' in details_map) and ('aoi_complete' in details_map['event_type']) and ('aoi_id' in details_map) and ( details_map['feature_count'] > 0))\n\tthen:\n\t\t$event.update_state('aoi_complete', $event.details_map['aoi_id'], $event.event_dtg)\n"
        #silver_award_policy = "from gamification.events.state import State\nrule 'Rule 1':\n\twhen:\n\t\t$state := State((project.name == 'django_geoq') and ('aoi_complete' in event_data) and (len(event_data['aoi_complete']) % 3 == 0) and (" + str(current_aoi) + " in event_data['aoi_complete']))\n\tthen:\n\t\t$state.award($state.user, $state.project, 5)\n"

        #policies = [[bronze_state_policy, bronze_award_policy], [silver_state_policy, silver_award_policy]]

        #####################################################################

        logger = logging.getLogger('intellect')
        logger.setLevel(logging.DEBUG)
        consoleHandler = logging.StreamHandler()
        consoleHandler.setFormatter(
            logging.Formatter(
                '%(asctime)s %(name)-12s %(levelname)-8s%(message)s'))
        logger.addHandler(consoleHandler)

        # get badges for this project, then for each badge run through its policies
        project_badges = ProjectBadge.objects.filter(project=project)

        for badge in project_badges:
            policies = Policy.objects.filter(project=project,
                                             projectbadge=badge)
            # make sure we have policies
            if policies.count() == 2:
                try:
                    state_policy = policies.get(type=Policy.STATE_POLICY)
                    intellect = Intellect()
                    intellect.learn(state_policy.rule)
                    events = Event.objects.filter(user=user, project=project)
                    event_data = {}
                    state = State(user, project, badge, event_data)
                    for e in events:
                        e.state = state
                        e.current_event = current_event.id
                        intellect.learn(e)
                    # import pdb; pdb.set_trace()
                    intellect.reason()
                except ObjectDoesNotExist:
                    print 'state policy not found'

                try:
                    award_policy = policies.get(type=Policy.AWARD_POLICY)
                    intellect = Intellect()
                    intellect.learn(award_policy.rule)
                    intellect.learn(state)
                    intellect.reason()
                except ObjectDoesNotExist:
                    print 'award policy not found'

        return HttpResponse(status=200)
Пример #15
0
        self.log("<<< called MyIntellect's bar method as it was decorated as Callable >>>")

if __name__ == "__main__":

    try:
        logger = logging.getLogger('intellect')
        #logger.setLevel(logging.DEBUG)
        logger.setLevel(logging.ERROR)
        consoleHandler = logging.StreamHandler(stream=sys.stdout)
        consoleHandler.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s%(message)s'))
        logger.addHandler(consoleHandler)

        myIntellect = MyIntellect()
        print("passo")

        policy_a = myIntellect.learn(Intellect.local_file_uri("intellect/policies/test_a.policy"))
        print("policy loaded")
        #print(policy_a)
        #myIntellect.reason(["test_a"])

        a = ClassA(property0="cpu", property1=10, property2={"a": 1, "b": 2})
        b = ClassA(property0="network", property1=11)
        c = ClassA(property0="cpu", property1=12)

        print("Learn instance a")
        myIntellect.learn(a)
        print("Learn instance b")
        myIntellect.learn(b)
        print("Learn instance c")
        myIntellect.learn(c)
Пример #16
0
        consoleHandler = logging.StreamHandler(stream=sys.stdout)
        consoleHandler.setFormatter(
            logging.Formatter(
                '%(asctime)s %(name)-12s %(levelname)-8s%(message)s'))
        logger.addHandler(consoleHandler)

        print "*" * 80
        print """create an instance of MyIntellect extending Intellect, create some facts, and exercise the MyIntellect's ability to learn and forget"""
        print "*" * 80

        myIntellect = MyIntellect()

        try:
            policy_bogus = myIntellect.learn(
                Intellect.local_file_uri("./rulesets/doesnt_exist.policy"))
        except IOError as e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type, exc_value, exc_traceback)

        policy_a = myIntellect.learn(
            Intellect.local_file_uri("./rulesets/test_a.policy"))
        policy_d = myIntellect.learn(
            Intellect.local_file_uri("./rulesets/test_d.policy"))

        myIntellect.reason(["test_d", "test_a"])

        myIntellect.forget_all()

        from intellect.examples.testing.subModule.ClassB import ClassB
Пример #17
0
 def __init__(self, ws_id, type, rule):
     self.ws_id = ws_id
     self.type = type
     self.rule = rule
     self.node = pickle.dumps(Intellect().learn_policy(rule))
Пример #18
0
def handle_event(request, *args, **kwargs):
    if request.method == "POST":

        data = json.loads(request.body)

        # Get user and project
        user = get_object_or_404(User, username=kwargs["username"])
        project = get_object_or_404(Project, name=kwargs["projectname"])

        # Get event DTG
        try:
            event_dtg_str = data["event_dtg"]
            try:
                event_dtg = parse(event_dtg_str)
            except ValueError:
                return HttpResponse("Invalid event_dtg", status=400)
        except MultiValueDictKeyError:
            event_dtg = timezone.now()
        # print('handle_event got event_dtg {0}').format(datetime.strftime(event_dtg, '%Y-%m-%dT%H:%M:%S%Z'))

        # Get event details
        try:
            details = data["details"]
        except MultiValueDictKeyError:
            details = None
        # print('handle_event got details {0}').format(details)

        # Create Event object
        try:
            current_event = Event(user=user, project=project, event_dtg=event_dtg, details=details)
        except ValueError, ve:
            # print('handle_event failed create event: {0}').format(ve)
            return HttpResponse("Invalid event", status=400)  # If, for example, 'details' JSON does not load

        # Save Event object
        current_event.save()

        #####################################################################
        # 'Training' demo policies. Assumes there is a 'training' project with badge id 1. Badge awarded when all courses finished.
        # Sample curl command for sending event for user 'admin':
        # curl -d "details={\"event_type\":\"course_complete\",\"course_id\":\"008031\"}" http://localhost:8000/users/admin/projects/training/event/
        # state_policy = "from gamification.events.models import Event\nrule 'Rule 1':\n\twhen:\n\t\t$event := Event(('event_type' in details_map) and ('course_complete' in details_map['event_type']) and ('course_id' in details_map))\n\tthen:\n\t\t$event.update_state('course_complete', $event.details_map['course_id'], $event.event_dtg)\n"
        # award_policy = "from gamification.events.state import State\nrule 'Rule 1':\n\twhen:\n\t\t$state := State((project.name == 'training') and ('course_complete' in event_data) and ('008031' in event_data['course_complete']) and ('008189' in event_data['course_complete']) and ('008582' in event_data['course_complete']) and ('009446' in event_data['course_complete']) and ('013413' in event_data['course_complete']) and ('013567' in event_data['course_complete']) and ('016003' in event_data['course_complete']) and ('016094' in event_data['course_complete']) and ('017724' in event_data['course_complete']) and ('020146' in event_data['course_complete']) and ('023416' in event_data['course_complete']))\n\tthen:\n\t\t$state.award($state.user, $state.project, 1)\n"
        #####################################################################

        #####################################################################
        # 'GeoQ AOI' demo policies. Assumes there is a 'geoq' project with badge id 4. Badge awarded when at least three AOIs are completed.
        # Sample curl command for sending event for user 'admin':
        # curl -d "details={\"event_type\":\"aoi_complete\",\"aoi_id\":\"2\"}" http://localhost:8000/users/admin/projects/geoq/event/
        # bronze_state_policy = "from gamification.events.models import Event\nrule 'Rule 1':\n\twhen:\n\t\t$event := Event((" + str(current_aoi) + " == details_map['aoi_id']) and ( details_map['feature_count'] > 0))\n\tthen:\n\t\t$event.update_state('aoi_complete', $event.details_map['aoi_id'], $event.event_dtg)\n"
        # bronze_award_policy = "from gamification.events.state import State\nrule 'Rule 1':\n\twhen:\n\t\t$state := State((project.name == 'django_geoq') and ('aoi_complete' in event_data) and (len(event_data['aoi_complete']) > 0))\n\tthen:\n\t\t$state.award($state.user, $state.project, 6)\n"
        # silver_state_policy = "from gamification.events.models import Event\nrule 'Rule 1':\n\twhen:\n\t\t$event := Event(('event_type' in details_map) and ('aoi_complete' in details_map['event_type']) and ('aoi_id' in details_map) and ( details_map['feature_count'] > 0))\n\tthen:\n\t\t$event.update_state('aoi_complete', $event.details_map['aoi_id'], $event.event_dtg)\n"
        # silver_award_policy = "from gamification.events.state import State\nrule 'Rule 1':\n\twhen:\n\t\t$state := State((project.name == 'django_geoq') and ('aoi_complete' in event_data) and (len(event_data['aoi_complete']) % 3 == 0) and (" + str(current_aoi) + " in event_data['aoi_complete']))\n\tthen:\n\t\t$state.award($state.user, $state.project, 5)\n"

        # policies = [[bronze_state_policy, bronze_award_policy], [silver_state_policy, silver_award_policy]]

        #####################################################################

        logger = logging.getLogger("intellect")
        logger.setLevel(logging.DEBUG)
        consoleHandler = logging.StreamHandler()
        consoleHandler.setFormatter(logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s%(message)s"))
        logger.addHandler(consoleHandler)

        # get badges for this project, then for each badge run through its policies
        project_badges = ProjectBadge.objects.filter(project=project)

        for badge in project_badges:
            policies = Policy.objects.filter(project=project, projectbadge=badge)
            # make sure we have policies
            if policies.count() == 2:
                try:
                    state_policy = policies.get(type=Policy.STATE_POLICY)
                    intellect = Intellect()
                    intellect.learn(state_policy.rule)
                    events = Event.objects.filter(user=user, project=project)
                    event_data = {}
                    state = State(user, project, badge, event_data)
                    for e in events:
                        e.state = state
                        e.current_event = current_event.id
                        intellect.learn(e)
                    # import pdb; pdb.set_trace()
                    intellect.reason()
                except ObjectDoesNotExist:
                    print "state policy not found"

                try:
                    award_policy = policies.get(type=Policy.AWARD_POLICY)
                    intellect = Intellect()
                    intellect.learn(award_policy.rule)
                    intellect.learn(state)
                    intellect.reason()
                except ObjectDoesNotExist:
                    print "award policy not found"

        return HttpResponse(status=200)
Пример #19
0
        # set up logging for the example
        logger = logging.getLogger('example')
        logger.setLevel(logging.DEBUG)

        consoleHandler = logging.StreamHandler(stream=sys.stdout)
        consoleHandler.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s%(message)s'))
        logger.addHandler(consoleHandler)

        print "*"*80
        print """create an instance of MyIntellect extending Intellect, create some facts, and exercise the MyIntellect's ability to learn and forget"""
        print "*"*80

        myIntellect = MyIntellect()

        try:
            policy_bogus = myIntellect.learn(Intellect.local_file_uri("./rulesets/doesnt_exist.policy"))
        except IOError as e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type, exc_value, exc_traceback)

        policy_a = myIntellect.learn(Intellect.local_file_uri("./rulesets/test_a.policy"))
        policy_d = myIntellect.learn(Intellect.local_file_uri("./rulesets/test_d.policy"))

        myIntellect.reason(["test_d", "test_a"])

        myIntellect.forget_all()

        from intellect.examples.testing.subModule.ClassB import ClassB

        # keep an identifier (b1) around to a ClassB, to demonstrate that a rule
        # can modify the object and the change is reflected in b1
Пример #20
0
    # set up logging for the example
    logger = logging.getLogger('example')
    logger.setLevel(logging.DEBUG)

    consoleHandler = logging.StreamHandler(stream=sys.stdout)
    consoleHandler.setFormatter(
        logging.Formatter(
            '%(asctime)s %(name)-12s %(levelname)-8s%(message)s'))
    logger.addHandler(consoleHandler)

    print "*" * 80
    print """create an instance of MyIntellect extending Intellect, create some facts, and exercise the grammar"""
    print "*" * 80

    try:
        myIntellect = Intellect()

        policy_a = myIntellect.learn(
            Intellect.local_file_uri("./rulesets/test_f.policy"))

        myIntellect.learn(ClassA(property1="apple"))
        #myIntellect.learn(ClassA( property1="pear"))
        #myIntellect.learn(ClassA( property1="grape"))

        #logger.debug("reasoning over policy w/ objects in memory")

        myIntellect.reason()
    except Exception as e:
        traceback.print_exc(limit=sys.getrecursionlimit(), file=sys.stdout)
Пример #21
0
    consoleHandler = logging.StreamHandler(stream=sys.stdout)
    consoleHandler.setFormatter(logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s%(message)s"))
    logger.addHandler(consoleHandler)

    # set up logging for the example
    logger = logging.getLogger("example")
    logger.setLevel(logging.DEBUG)

    consoleHandler = logging.StreamHandler(stream=sys.stdout)
    consoleHandler.setFormatter(logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s%(message)s"))
    logger.addHandler(consoleHandler)

    print "*" * 80
    print """create an instance of MyIntellect extending Intellect, create some facts, and exercise the grammar"""
    print "*" * 80

    try:
        myIntellect = Intellect()

        policy_a = myIntellect.learn(Intellect.local_file_uri("./rulesets/test_f.policy"))

        myIntellect.learn(ClassA(property1="apple"))
        # myIntellect.learn(ClassA( property1="pear"))
        # myIntellect.learn(ClassA( property1="grape"))

        # logger.debug("reasoning over policy w/ objects in memory")

        myIntellect.reason()
    except Exception as e:
        traceback.print_exc(limit=sys.getrecursionlimit(), file=sys.stdout)
 def reason(self, policy, state):
     intellect = Intellect()
     intellect.learn(policy)
     intellect.learn(state)
     intellect.reason()