public bool AddTerm(Term term, IDictionary<TermVariable, TermModel> varsToModelsMap) { var termObject = term as TermObject; if (termObject != null) return PossibleConstants.Add(termObject); var function = term as TermFunction; if (function != null) { var sentenceName = new NameAndArity(function); bool changesMade = PossibleFunctions.CreateIfRequired(sentenceName); return changesMade | AddBodyToModel(PossibleFunctions[sentenceName], function.Arguments.ToList(), varsToModelsMap); } var key = term as TermVariable; if (key != null) return MergeIn(varsToModelsMap[key]); throw new Exception(String.Format("Unrecognized term type {0} for term {1}", term.GetType(), term)); }
private bool ApplyInjection(NameAndArity oldName, NameAndArity newName) { Debug.Assert(oldName.Arity == newName.Arity); bool changesMade = false; if (SentencesModel.ContainsKey(oldName)) { List<TermModel> oldModel = SentencesModel[oldName]; changesMade = SentencesModel.CreateIfRequired(newName); List<TermModel> newModel = SentencesModel[newName]; if (oldModel.Count != newModel.Count) throw new Exception(); for (int i = 0; i < oldModel.Count; i++) changesMade |= newModel[i].MergeIn(oldModel[i]); } return changesMade; }
private Dictionary<TermVariable, TermModel> GetVarsToModelsMap(Implication rule) { var varsToUse = new HashSet<TermVariable>(rule.Consequent.VariablesOrEmpty); var varsToModelsMap = new Dictionary<TermVariable, TermModel>(); foreach (TermVariable var in varsToUse) varsToModelsMap[var] = new TermModel(); foreach (Expression literal in rule.Antecedents.Conjuncts) { var fact = literal as Fact; if (fact != null) { List<Term> literalBody = fact.GetTerms(); var nameAndArity = new NameAndArity(fact); SentencesModel.CreateIfRequired(nameAndArity); List<TermModel> literalModel = SentencesModel[nameAndArity]; AddVariablesToMap(literalBody, literalModel, varsToModelsMap); } } return varsToModelsMap; }
private bool AddSentenceToModel(Fact sentence, IDictionary<TermVariable, TermModel> varsToModelsMap) { var sentenceName = new NameAndArity(sentence); bool changesMade = SentencesModel.CreateIfRequired(sentenceName); return changesMade | TermModel.AddBodyToModel(SentencesModel[sentenceName], sentence.GetTerms(), varsToModelsMap); }
public List<TermModel> GetValueOrNull(NameAndArity function) { List<TermModel> result; _functions.TryGetValue(function, out result); return result; }
public bool CreateIfRequired(NameAndArity sentenceName) { if (_functions.ContainsKey(sentenceName)) return false; var result = new List<TermModel>(sentenceName.Arity); for (int i = 0; i < sentenceName.Arity; i++) result.Add(new TermModel()); _functions[sentenceName] = result; return true; }
public bool ContainsKey(NameAndArity nameAndArity) { return _functions.ContainsKey(nameAndArity); }
public List<TermModel> this[NameAndArity sentenceName] { get { return _functions[sentenceName]; } set { _functions[sentenceName] = value; } }