public void addRule(string ruleMatch, PrintRule rule)
 {
     if (!termTranslations.ContainsKey(ruleMatch))
     {
         termTranslations[ruleMatch] = new List <PrintRule>();
     }
     if (!termTranslations[ruleMatch].Contains(rule))
     {
         termTranslations[ruleMatch].Add(rule);
     }
 }
        public PrintRule getPrintRule(Term t)
        {
            if (t == null)
            {
                return(null);
            }
            // no rule -> default
            if (!printRuleDict.hasRule(t))
            {
                return(PrintRule.DefaultRewriteRule(t, this));
            }

            IEnumerable <PrintRule> rules = printRuleDict.getRewriteRules(t);

            // userdefined & disabled --> default
            if (!rewritingEnabled)
            {
                rules = rules.Where(rule => !rule.isUserdefined);
            }
            rules = rules.Reverse().OrderByDescending(rule => rule.historyConstraints.Count);
            // history constraint ok --> rule ok

            if (!printContextSensitive)
            {
                return(rules.FirstOrDefault() ?? PrintRule.DefaultRewriteRule(t, this));
            }
            var contextRule = rules.FirstOrDefault(rule => historyConstraintSatisfied(rule));

            if (contextRule != null)
            {
                return(contextRule);
            }

            // freeVar --> no usable id, therefore specific rule is on name
            // there is therefore no generale rule to fall back on.
            if (t.id == -1)
            {
                return(PrintRule.DefaultRewriteRule(t, this));
            }

            // history constraint violated --> find less specific rules (but more specific than default)
            if (printRuleDict.hasRule(t.Name + t.GenericType))
            {
                return(printRuleDict.getRewriteRules(t.Name + t.GenericType).Single());
            }
            return(printRuleDict.hasRule(t.Name) ?
                   printRuleDict.getRewriteRules(t.Name).Single() : PrintRule.DefaultRewriteRule(t, this));
        }
        private bool historyConstraintSatisfied(PrintRule rule)
        {
            if (rule.historyConstraints.Count == 0)
            {
                return(true);
            }
            var constraint = rule.historyConstraints;

            if (constraint.Count != history.Count)
            {
                return(false);
            }
            var intermediate = history.Zip(constraint, (term1, term2) => term1.Item1.id == term2.Item1.id && term1.Item2 == term2.Item2);

            return(intermediate.All(val => val));
        }
        public void addTemporaryRule(string match, PrintRule rule)
        {
            // save old rule
            PrintRule oldRule = null;

            if (printRuleDict.hasRule(match))
            {
                oldRule = printRuleDict.getRewriteRules(match).FirstOrDefault();
            }
            // save original rule only if it was not already a temporary rule.
            if (!originalRulesReplacedByTemp.ContainsKey(match))
            {
                originalRulesReplacedByTemp.Add(match, oldRule);
            }

            // add new one
            printRuleDict.addRule(match, rule);
        }