Esempio n. 1
0
        static void Main(string[] args)
        {
            ConceptBase.Load("concepts");
            RuleBase.Load("rules");

            while (true)
            {
                CParser.Parse(Console.ReadLine());
            }
        }
Esempio n. 2
0
        static public void Parse(Concept IForm)
        {
            if (IForm.Type == "KNOWLEDGE")
            {
                string lhs = Utils.LSplit(IForm.Name, "=");
                string val = Utils.RSplit(IForm.Name, "=");

                string target = Utils.LSplit(lhs, ".");
                string prop   = Utils.RSplit(lhs, ".");

                var    tmp  = Utils.ExtractFromBracket(target, '[', ']');
                string type = null;
                if (tmp != null)
                {
                    type = tmp[0];
                    target.Replace("[" + type + "]", "");
                }

                if (type == null)
                {
                    Concept c = ConceptBase.Find(target);
                    if (c == null)
                    {
                        c = new Concept(target);
                        c.SetProperty(prop, val);
                        ConceptBase.Add(c);
                    }
                    else
                    {
                        c.SetProperty(prop, val);
                    }
                }
                else
                {
                    Concept c = ConceptBase.Find(target, type);
                    if (c == null)
                    {
                        c = new Concept(target, type);
                        c.SetProperty(prop, val);
                        ConceptBase.Add(c);
                    }
                    else
                    {
                        c.SetProperty(prop, val);
                    }
                }
            }
            else if (IForm.Type == "RULE")
            {
                Rule r = Rule.FromString(IForm.Name);
                RuleBase.Remove(r.GetPattern());
                RuleBase.Add(r);
            }
        }
Esempio n. 3
0
        //---------------------------------------------------------------------------------------

        /// <summary>
        /// Return a Concept from a UTF-8 text file.
        /// </summary>
        /// <param name="FilePath">File path</param>
        /// <returns></returns>
        static public Concept FromFile(string FilePath)
        {
            Concept c = new Concept(Path.GetFileNameWithoutExtension(FilePath));

            foreach (string line in File.ReadAllLines(FilePath, Encoding.UTF8))
            {
                c.SetProperty(Utils.LSplit(line, "="), Utils.RSplit(line, "="));
            }

            if (c.GetProperty("PARENT") != null)
            {
                c.Parent = ConceptBase.Find(c.GetProperty("PARENT"));
            }

            return(c);
        }
Esempio n. 4
0
        public static List <Hypothesis <List <Concept> > > Parse(string Source)
        {
            List <Concept> lConcept = ConceptBase.Extract(Source);

            List <Hypothesis <List <Concept> > > CurrentLevel = Hypothesis <List <Concept> > .Hypothesize(Utils.PowerSet(USieve.Filter(Utils.Order(lConcept, Source)), Source), list =>
            {
                return((double)Utils.Stringify(list).Length / (double)Source.Length);
            });

            List <Reaction> Reactions = new List <Reaction>();
            List <Hypothesis <List <Concept> > > NextLevel = new List <Hypothesis <List <Concept> > >();

            for (int i = 0; i < 50; i++)
            {
                // Get all applicable rules and store the reactions for later use
                Reactions.Clear();
                foreach (Hypothesis <List <Concept> > hyp in CurrentLevel)
                {
                    foreach (Rule r in RuleBase.FindApplicableRules(hyp.Claim))
                    {
                        Reaction re = new Reaction(hyp.Claim.ToArray(), r.Apply(hyp.Claim));
                        if (!Reactions.Contains(re))
                        {
                            Reactions.Add(re);
                        }
                    }
                }

                NextLevel.Clear();
                foreach (Hypothesis <List <Concept> > hyp in CurrentLevel)
                {
                    foreach (Reaction re in Reactions)
                    {
                        if (!Utils.Identical(Utils.Substitute(hyp.Claim, re.Reactants, re.Product), hyp.Claim))
                        {
                            NextLevel.Add(new Hypothesis <List <Concept> >(Utils.Substitute(hyp.Claim, re.Reactants, re.Product), hyp.Certainty));
                        }
                    }
                }

                if (NextLevel.Count() == 0)
                {
                    break;
                }
                else
                {
                    CurrentLevel = new List <Hypothesis <List <Concept> > >(NextLevel);
                }
            }

            //DEBUG===========================================
            //Rule ru = Rule.FromString("*+*,TESTTYPE,none");
            foreach (Hypothesis <List <Concept> > hyp in CurrentLevel)
            {
                foreach (Concept c in hyp.Claim)
                {
                    Console.Write(c + " ");
                }
                Console.WriteLine(">>> " + hyp.Certainty);

                Console.WriteLine();
            }
            //================================================

            return(CurrentLevel);
        }