示例#1
0
        private KnowledgeBuilder CreateKnowledgeBuilder(List <IRule> rules, TheiaClassLoader classLoader)
        {
            //Create configuration
            KnowledgeBuilderConfiguration kBuilderConfiguration = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, classLoader);
            //Create knowledgeBuilder
            KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kBuilderConfiguration);

            foreach (var rule in rules)
            {
                //Create my resource
                Resource myResource = ResourceFactory.newReaderResource(new java.io.StringReader(rule.Source));

                //Add resource to KnowledgeBuilder
                kBuilder.add(myResource, ResourceType.DRL);
                var ruleErrors = kBuilder.getErrors();

                if (ruleErrors.size() > 0)
                {
                    throw new TheiaException(
                              $"The rule contains an error and can not be compiled: {System.Environment.NewLine}{ruleErrors}");
                }
            }

            return(kBuilder);
        }
示例#2
0
        public void runRules(String[] rules, java.lang.Object[] facts)
        {
            KnowledgeBase    kbase    = KnowledgeBaseFactory.newKnowledgeBase();
            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

            for (int i = 0; i < rules.Length; i++)
            {
                String ruleFile = rules[i];
                Console.WriteLine("Loading file: " + ruleFile);
                ruleFile = Path.GetFullPath(Path.Combine("rules", ruleFile));
                kbuilder.add(ResourceFactory.newFileResource(ruleFile), ResourceType.DRL);
            }

            java.util.Collection pkgs = kbuilder.getKnowledgePackages();
            kbase.addKnowledgePackages(pkgs);
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

            for (int i = 0; i < facts.Length; i++)
            {
                java.lang.Object fact = facts[i];
                //Console.WriteLine("Inserting fact: " + fact);
                ksession.insert(fact);
            }



            ksession.fireAllRules();
        }