public Term(String src) { var temp = new Term(Sexp.FromSrc(src)); this.Type = temp.Type; this.Text = temp.Text; }
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()}"); } }
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); }
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; }
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); }
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); }
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); }
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]); }
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); }
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; }
private bool IsConjunction(Sexp s) { return(s.Type == Sexp.SexpType.LIST && s.Children.Count > 2 && s.Children[0].Repr() == "and"); }
private bool IsLiteral(Sexp s) { return(s.Type == Sexp.SexpType.LIST && s.Children.Count > 0 && s.Children[0].Type == Sexp.SexpType.SYMBOL); }
private bool IsDefiniteClause(Sexp s) { return(s.Type == Sexp.SexpType.LIST && s.Children.Count == 3 && s.Children[0].Repr() == "if"); }