Ejemplo n.º 1
0
        public virtual ComparableDict getParams()
        {
            ComparableDict dict = new ComparableDict();

            foreach (DictionaryEntry entry in slots)
            {
                object slotValue = ((Slot)entry.Value).slotValue;
                if (slotValue is ICloneable)
                {
                    slotValue = ((ICloneable)slotValue).Clone();
                }
                dict.Add(entry.Key, slotValue);
            }
            return(dict);
        }
Ejemplo n.º 2
0
        public static bool consult(string ruleset, string goal, ChainingMethod method)
        {
            statesByRule = new ComparableDict();
            firedRules   = new ArrayList();
            foreach (string ruleName in CSharpExpertAbstract.getRulesetFrame(ruleset).getRules().Keys)
            {
                statesByRule.Add(ruleName, new ArrayList());
            }

            if (method.Equals(ChainingMethod.backward))
            {
                return(backwardChaining(ruleset, goal));
            }
            else if (method.Equals(ChainingMethod.forward))
            {
                return(forwardChaining(ruleset, goal));
            }

            return(false);
        }
Ejemplo n.º 3
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));
        }