Exemplo n.º 1
0
        public Term(String src)
        {
            var temp = new Term(Sexp.FromSrc(src));

            this.Type = temp.Type;
            this.Text = temp.Text;
        }
Exemplo n.º 2
0
 public Term(Sexp s)
 {
     if (s.Type == Sexp.SexpType.SYMBOL)
     {
         this.Text = s.Text;
         if (this.IsVariable(s.Text))
         {
             this.Type = TermType.VARIABLE;
         }
         else
         {
             this.Type = TermType.CONSTANT;
         }
     }
     else if (s.Type == Sexp.SexpType.STRING)
     {
         this.Text = s.Text;
         this.Type = TermType.CONSTANT;
     }
     else if (s.Type == Sexp.SexpType.NUMBER)
     {
         this.Text = s.Number.ToString();
         this.Type = TermType.CONSTANT;
     }
     else
     {
         throw new Exception($"Unrecognizable term type: {s.Repr()}");
     }
 }
Exemplo n.º 3
0
        public List <Literal> Add(string src)
        {
            var sexps        = Sexp.FromSrc("(" + src + ")").Children; // wrapped
            var observations = new List <Literal>();

            foreach (Sexp s in sexps)
            {
                if (this.IsDefiniteClause(s))
                {
                    this.Axioms.Add(new DefiniteClause(s));
                }
                else if (this.IsConjunction(s))
                {
                    for (int i = 1; i < s.Children.Count; i++)
                    {
                        observations.Add(new Literal(s.Children[i]));
                    }
                }
                else if (this.IsLiteral(s))
                {
                    observations.Add(new Literal(s));
                }
                else
                {
                    throw new Exception($"Unrecognizable logic expression: {s.Repr()}");
                }
            }
            this.ComputeIndexByConsequent();
            return(observations);
        }
Exemplo n.º 4
0
        public Literal(string src)
        {
            var temp = new Literal(Sexp.FromSrc(src));

            this.Predicate         = temp.Predicate;
            this.Terms             = temp.Terms;
            this.IsEtceteraLiteral = temp.IsEtceteraLiteral;
            this.LnProbability     = temp.LnProbability;
        }
Exemplo n.º 5
0
        private Sexp ParseList()
        {
            this.ConsumeChar(); // eat opening paren
            this.Depth += 1;
            var children = this.ParseSexps();

            this.ConsumeChar();; // eat closing paren
            this.Depth -= 1;
            var res_sexp = new Sexp();

            res_sexp.Type     = Sexp.SexpType.LIST;
            res_sexp.Children = children;
            return(res_sexp);
        }
Exemplo n.º 6
0
        private Sexp ParseNumber() // funny! Can't handle negative numbers
        {
            var res      = "";
            var cur_char = this.NextChar();

            while (" ;)\n\t".Contains(cur_char) == false)
            {
                res     += this.ConsumeChar();
                cur_char = this.NextChar();
            }
            var res_number = Double.Parse(res);
            var res_sexp   = new Sexp();

            res_sexp.Type   = Sexp.SexpType.NUMBER;
            res_sexp.Number = res_number;
            return(res_sexp);
        }
Exemplo n.º 7
0
        private Sexp ParseSymbol()
        {
            var res      = "";
            var cur_char = this.NextChar();

            while (" ;)\n\t".Contains(cur_char) == false)
            {
                res     += this.ConsumeChar();
                cur_char = this.NextChar();
            }
            var res_sexp = new Sexp();

            res_sexp.Type = Sexp.SexpType.SYMBOL;
            res_sexp.Text = res;

            return(res_sexp);
        }
Exemplo n.º 8
0
        public DefiniteClause(Sexp s)
        {
            var ants = new List <Literal>();

            if (IsConjunction(s.Children[1]))
            {
                for (int i = 1; i < s.Children[1].Children.Count; i++)
                {
                    ants.Add(new Literal(s.Children[1].Children[i]));
                }
            }
            else
            {
                ants.Add(new Literal(s.Children[1]));
            }
            this.Antecedents = ants;
            this.Consequent  = new Literal(s.Children[2]);
        }
Exemplo n.º 9
0
        private Sexp ParseString()
        {
            var res = "";

            this.ConsumeChar(); // eat opening "
            var cur_char = this.NextChar();

            while (cur_char != '\"')
            {
                res     += this.ConsumeChar();
                cur_char = this.NextChar();
            }
            this.ConsumeChar(); // eat ending "
            var res_sexp = new Sexp();

            res_sexp.Type = Sexp.SexpType.STRING;
            res_sexp.Text = res;
            return(res_sexp);
        }
Exemplo n.º 10
0
        public Literal(Sexp s)
        {
            if (s.Children.Count > 1 &&
                s.Children[1].Type == SexpType.NUMBER)
            {
                this.IsEtceteraLiteral = true;
                this.LnProbability     = Math.Log(s.Children[1].Number);
            }
            else
            {
                this.IsEtceteraLiteral = false;
            }
            this.Predicate = s.Children[0].Text;
            var res = new List <Term>();

            for (int i = 1; i < s.Children.Count; i++)
            {
                res.Add(new Term(s.Children[i]));
            }
            this.Terms = res;
        }
Exemplo n.º 11
0
 private bool IsConjunction(Sexp s)
 {
     return(s.Type == Sexp.SexpType.LIST &&
            s.Children.Count > 2 &&
            s.Children[0].Repr() == "and");
 }
Exemplo n.º 12
0
 private bool IsLiteral(Sexp s)
 {
     return(s.Type == Sexp.SexpType.LIST &&
            s.Children.Count > 0 &&
            s.Children[0].Type == Sexp.SexpType.SYMBOL);
 }
Exemplo n.º 13
0
 private bool IsDefiniteClause(Sexp s)
 {
     return(s.Type == Sexp.SexpType.LIST &&
            s.Children.Count == 3 &&
            s.Children[0].Repr() == "if");
 }