Esempio n. 1
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
Esempio n. 2
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")
Esempio n. 3
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")
Esempio n. 4
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)
        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
Esempio n. 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 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
Esempio n. 7
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)
Esempio n. 8
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
Esempio n. 9
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)