コード例 #1
0
        private static IEnumerable<Implication> ApplyAmbiguitiesForLiteral(Expression literal, Implication rule,
            IDictionary<ISentenceForm, ICollection<Ambiguity>> ambiguitiesByOriginalForm, ISentenceFormModel model)
        {
            var results = new List<Implication> { rule };
            var varGen = new UnusedVariableGenerator(rule);

            var fact = literal as Fact;
            if (fact != null)
            {
                ISentenceForm form = model.GetSentenceForm(fact);
                ICollection<Ambiguity> ambiguities;
                if (ambiguitiesByOriginalForm.TryGetValue(form, out ambiguities))
                {
                    IEnumerable<Ambiguity> applicableAmiguities = ambiguities.Where(ambiguity => ambiguity.Applies(fact));
                    IEnumerable<Substitution> substitutions = applicableAmiguities.Select(ambiguity => ambiguity.GetReplacementAssignment(fact, varGen));
                    IEnumerable<Implication> implications = substitutions.Select(substitution => (Implication)rule.ApplySubstitution(substitution));
                    results.AddRange(implications);
                }
            }
            else if (literal is Negation)
            {
                // Do nothing. Variables must appear in a positive literal in the
                // rule, and will be handled there.
            }
            else if (literal is Disjunction)
            {
                throw new Exception("ORs should have been removed");
                //} else if (literal is GdlDistinct) {
                // Do nothing
            }

            return results;
        }
コード例 #2
0
 private static Implication RemoveNotDistinctLiteral(Implication rule, Negation notDistinctLiteral)
 {
     //Figure out the substitution we want...
     //If we have two constantsin Either Remove one or
     //maybe get rid of the ___?
     //One is a variablein Replace the variable with the other thing
     //throughout the rule
     var distinct = (Fact)notDistinctLiteral.Negated;
     Term arg1 = distinct.GetTerm(0);
     Term arg2 = distinct.GetTerm(1);
     if (arg1 == arg2)
     {
         //Just Remove that literal
         var newBody = new List<Expression>();
         newBody.AddRange(rule.Antecedents.Conjuncts);
         newBody.Remove(notDistinctLiteral);
         return new Implication(rule.Consequent, newBody.ToArray());
     }
     var p1 = arg1 as TermVariable;
     if (p1 != null)
     {
         //What we return will still have the not-distinct literal,
         //but it will get replaced in the next pass.
         //(Even if we have two variables, they will be equal next time through.)
         var sub = new Substitution();
         sub.AddMapping(p1, arg2);
         return (Implication)rule.ApplySubstitution(sub);
     }
     var variable = arg2 as TermVariable;
     if (variable != null)
     {
         var sub = new Substitution();
         sub.AddMapping(variable, arg1);
         return (Implication)rule.ApplySubstitution(sub);
     }
     if (arg1 is TermObject || arg2 is TermObject)
     {
         //We have two non-equal constants, or a constant and a function.
         //The rule should have no effect.
         return null;
     }
     //We have two functions. Complicated! (Have to replace them with unified version.)
     //We pass on this case for now.
     //TODO: Implement correctly.
     throw new Exception("We can't currently handle (not (distinct <function> <function>)).");
 }