Ejemplo n.º 1
0
        public static CSharpExpertAbstract loadKnoweldgeBase(string fname)
        {
            Stream          stream    = File.Open(fname, FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();

            instance = (CSharpExpertAbstract)formatter.Deserialize(stream);
            stream.Close();

            //
            setOutputStream(Console.Out);
            getInstance().createFrames();
            return(instance);
        }
Ejemplo n.º 2
0
        public virtual bool isInherited(string origin)
        {
            foreach (string frameName in isA)
            {
                if (frameName == origin)
                {
                    return(true);
                }
                else if (CSharpExpertAbstract.getDataFrame(frameName).isInherited(origin))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns own slot of frame by the name of own slot
        /// </summary>
        /// <param name="iden">short or full-qualified name of own slot</param>
        /// <exception cref="SlotIsNotFound">throw this exception in case of the slot is not found</exception>
        /// <returns>instance of the class Slot</returns>
        public virtual Slot getSlot(string iden)
        {
            string[] idens  = iden.Split('.');
            Slot     result = null;

            if (idens.Length > 2 || idens.Length == 0)
            {
                throw new SlotIsNotFound();
            }

            if (idens.Length == 1)
            {
                if (!CSharpExpertAbstract.isSlot(getName() + "." + iden))
                {
                    foreach (string frame in isA)
                    {
                        try
                        {
                            result = CSharpExpertAbstract.getDataFrame(frame).getSlot(iden);
                        }
                        catch (SlotIsNotFound) {}

                        if (result != null)
                        {
                            break;
                        }
                    }

                    if (result == null)
                    {
                        throw new SlotIsNotFound();
                    }
                }
                else
                {
                    result = CSharpExpertAbstract.getSlot(getName() + "." + iden);
                }
            }
            else if (idens.Length == 2)
            {
                result = CSharpExpertAbstract.getDataFrame(idens[0]).getSlot(idens[1]);
            }

            return(result);
        }
Ejemplo n.º 4
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.º 5
0
 protected void _initContext()
 {
     this.slots           = new SortedList();
     isContextInitialized = true;
     foreach (ContextParam param in context.Values)
     {
         if (!param.isInstance)
         {
             IDictionary slots = CSharpExpertAbstract.getDataFrame(param.name).getSlots();
             foreach (DictionaryEntry entry in slots)
             {
                 if (this.slots.Contains(entry.Key))
                 {
                     throw new KnowledgeException("Not unique names of slot in the context of the frame \"" + getName() + "\"");
                 }
                 this.slots.Add(entry.Key, entry.Value);
             }
         }
     }
 }
Ejemplo n.º 6
0
        public virtual IDictionary getSlots()
        {
            IDictionary dict = new SortedList();

            foreach (DictionaryEntry entry in CSharpExpertAbstract.getSlots(this.getName()))
            {
                dict.Add(((String)entry.Key).Split(new char[] { '.' })[1], entry.Value);
            }

            foreach (string frameName in this.isA)
            {
                IDictionary slots = CSharpExpertAbstract.getDataFrame(frameName).getSlots();
                foreach (DictionaryEntry entry in slots)
                {
                    if (!dict.Contains(entry.Key))                     // in case of instance slot, the defined value must be used
                    {
                        dict.Add((String)entry.Key, entry.Value);
                    }
                }
            }

            return(dict);
        }
Ejemplo n.º 7
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));
        }
Ejemplo n.º 8
0
        public static void consult(string ruleset)
        {
            RulesetFrame frame = CSharpExpertAbstract.getRulesetFrame(ruleset);

            consult(ruleset, frame.Goal);
        }