Exemple #1
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
Exemple #2
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
Exemple #3
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 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)
 def reason(self, policy, state):
     intellect = Intellect()
     intellect.learn(policy)
     intellect.learn(state)
     intellect.reason()
Exemple #6
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)
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)