Beispiel #1
0
        public static RulesetFrame getRulesetFrame(string key)
        {
            RulesetFrame frame = (RulesetFrame)getInstance().rulesetFrames[key];

            if (frame == null)
            {
                throw new FrameIsNotFound("The ruleset " + key + " is not found");
            }
            return((RulesetFrame)getInstance().rulesetFrames[key]);
        }
Beispiel #2
0
        public static bool forwardChaining(string rulesetName, string goal)
        {
            IList        candidates1 = new ArrayList();      // candidates that have high proirity (first lane)
            IList        candidates2 = new ArrayList();      // candidates that have low priority (second lane)
            RulesetFrame ruleset     = CSharpExpertAbstract.getRulesetFrame(rulesetName);

            if (!ruleset.isContextInitialized)
            {
                throw new KnowledgeException("Context of ruleset \"" + rulesetName + "\" must be initialized before consulting");
            }
            if (!ruleset.isParamsInitialized)
            {
                throw new KnowledgeException("Parameters of ruleset \"" + rulesetName + "\" must be set before consulting");
            }

            foreach (Rule rule in ruleset.getRules().Values)
            {
                if (rule.condition() == true &&
                    !((IList)statesByRule[rule.getName()]).Contains(ruleset.getParams()))
                {
                    if (firedRules.Contains(rule))
                    {
                        candidates2.Add(rule);
                    }
                    else
                    {
                        candidates1.Add(rule);
                    }
                }
            }

            if (candidates1.Count == 0 && candidates2.Count == 0)
            {
                ruleset.isContextInitialized = false;
                ruleset.isParamsInitialized  = false;
                CSharpExpertAbstract.getOutputStream().WriteLine("Goal = \"{0}\" can't be calculated by using the set of rules \"{1}\"", goal, ruleset.getName());
                return(false);
            }

            // method of conflict-resolution is very simple, just call first rule
            ComparableDict parameters  = ruleset.getParams();
            Rule           choosenRule = null;

            if (candidates1.Count > 0)
            {
                choosenRule = ((Rule)candidates1[0]);
            }
            else if (candidates2.Count > 0)
            {
                choosenRule = ((Rule)candidates2[0]);
            }

            ((IList)statesByRule[choosenRule.getName()]).Add(parameters);
            object old_value_of_goal = parameters[goal];

            choosenRule.if_statement();              // "if" block of chosen rule is fired
            firedRules.Add(choosenRule);
            if (showComments)
            {
                CSharpExpertAbstract.getOutputStream().WriteLine("Rule \"{0}\" has been fired:\n {1}", choosenRule.getName(), choosenRule.getComment());
            }

            foreach (Rule rule in ruleset.getRules().Values)             // "else" blocks of all other rules are fired
            {
                if (rule != choosenRule)
                {
                    rule.else_statement();
                }
            }

            if (!ruleset.getParams()[goal].Equals(old_value_of_goal))
            {
                ruleset.isContextInitialized = false;
                ruleset.isParamsInitialized  = false;
                CSharpExpertAbstract.getOutputStream().WriteLine("Goal is calculated: \"{0}\"={1}", goal, ruleset.getParams()[goal]);
                return(true);
            }

            return(forwardChaining(rulesetName, goal));
        }
Beispiel #3
0
        public static void consult(string ruleset)
        {
            RulesetFrame frame = CSharpExpertAbstract.getRulesetFrame(ruleset);

            consult(ruleset, frame.Goal);
        }