예제 #1
0
 /// <summary>
 /// Absent bindings should be given the value FALSE
 /// </summary>
 public Derivation(CoreRule rule, Term binding1, Term binding2)
 {
     Contract.Requires(rule != null && binding1 != null && binding2 != null);
     Rule     = rule;
     Binding1 = binding1;
     Binding2 = binding2;
 }
예제 #2
0
        internal ProofTree(Term conclusion, CoreRule coreRule, Map <string, FactSet> factSets, RuleTable rules)
        {
            Contract.Requires(conclusion != null && factSets != null);
            this.factSets = factSets;
            Conclusion    = conclusion;
            CoreRule      = coreRule;
            Rule          = coreRule == null?Factory.Instance.MkId("fact", new Span(0, 0, 0, 0, new ProgramName("fact"))).Node : coreRule.Node;

            SetMaxLocationsPerProof(rules);
        }
예제 #3
0
        /// <summary>
        /// Attempts to insert a symbolic element t.
        /// Returns true if the element t satisfies the pattern and was inserted into the index.
        /// </summary>

        /*
         * public bool TryAdd(SymElement t, Set<Activation> pending, int stratum)
         * {
         *  Contract.Requires(t != null && t.Term.Groundness != Groundness.Type);
         *  Contract.Requires(t.Term.Owner == Pattern.Owner);
         *
         *  if (entryMap.ContainsKey(t))
         *  {
         *      //// If t was already added then done.
         *      if (pending != null)
         *      {
         *          LinkedList<Tuple<CoreRule, int>> triggered;
         *          if (triggers.TryFindValue(stratum, out triggered))
         *          {
         *              foreach (var trig in triggered)
         *              {
         *                  pending.Add(new Activation(trig.Item1, trig.Item2, t));
         *              }
         *          }
         *      }
         *
         *      return true;
         *  }
         *  else if (!Unifier.IsUnifiable(Pattern, t.Term, false))
         *  {
         *      //// Terms t must unify with the pattern for insertion to succeed.
         *      //// Pattern is already standardized apart from t.
         *      return false;
         *  }
         *
         *  var entry = new Entry(t);
         *  entryMap.Add(t, entry);
         *  var nodeStack = new Stack<ShapeNode>();
         *  nodeStack.Push(root);
         *  t.Term.Compute<Unit>(
         *      (x, s) =>
         *      {
         *          return Unfold(x, nodeStack.Peek().Insert(entry, x), nodeStack);
         *      },
         *      (x, ch, s) =>
         *      {
         *          nodeStack.Pop().IncrementEntryCount();
         *          return default(Unit);
         *      });
         *
         *  if (pending != null)
         *  {
         *      LinkedList<Tuple<CoreRule, int>> triggered;
         *      if (triggers.TryFindValue(stratum, out triggered))
         *      {
         *          foreach (var trig in triggered)
         *          {
         *              pending.Add(new Activation(trig.Item1, trig.Item2, t));
         *          }
         *      }
         *  }
         *
         *  return true;
         * }
         */

        public void AddTrigger(CoreRule rule, int findNumber)
        {
            LinkedList <Tuple <CoreRule, int> > rules;

            if (!triggers.TryFindValue(rule.Stratum, out rules))
            {
                rules = new LinkedList <Tuple <CoreRule, int> >();
                triggers.Add(rule.Stratum, rules);
            }

            rules.AddLast(new Tuple <CoreRule, int>(rule, findNumber));
        }
        public void NexState_Should_ReturnDeadCell_If_DeadCellHasNoNeighbours()
        {
            var systemUnderTest = new CoreRule();
            var cell            = new Cell
            {
                Alive = false,
            };
            var neighbours = new List <Cell>();

            Cell cellInNextRound = systemUnderTest.NextState(cell, neighbours);

            cellInNextRound.Should().BeDead();
        }
예제 #5
0
        /*
         * private void IndexFact(SymElement t, Set<Activation> pending, int stratum)
         * {
         *  LinkedList<ShapeTrie> subindices;
         *  if (!symbToIndexMap.TryFindValue(t.Term.Symbol, out subindices))
         *  {
         *      return;
         *  }
         *
         *  foreach (var index in subindices)
         *  {
         *      index.TryAdd(t, pending, stratum);
         *  }
         * }
         */
        /// <summary>
        /// Register a rule without any finds.
        /// </summary>
        private void Register(CoreRule rule)
        {
            Contract.Requires(rule != null && rule.Trigger1 == null && rule.Trigger2 == null);
            LinkedList <CoreRule> untriggered;

            if (!untrigRules.TryFindValue(rule.Stratum, out untriggered))
            {
                untriggered = new LinkedList <CoreRule>();
                untrigRules.Add(rule.Stratum, untriggered);
            }

            untriggered.AddLast(rule);
        }
        public void NextState_Should_ReturnDeadCell_If_LivingCellHasOneAliveNeighbour()
        {
            var systemUnderTest = new CoreRule();
            var cell            = new Cell
            {
                Alive = true,
            };
            var neighbours = new List <Cell>
            {
                new() { Alive = true }
            };

            Cell cellInNextRound = systemUnderTest.NextState(cell, neighbours);

            cellInNextRound.Should().BeDead();
        }
        public void NexState_Should_ThrowTooManyNeighboursException_If_MoreThanEightNeighborsAreGiven()
        {
            var systemUnderTest = new CoreRule();
            var anyCell         = new Cell();
            var neighbours      = new List <Cell>
            {
                new() { Alive = true },
                new() { Alive = false },
                new() { Alive = true },
                new() { Alive = false },
                new() { Alive = true },
                new() { Alive = false },
                new() { Alive = false },
                new() { Alive = false },
                new() { Alive = false },
            };

            Action throwingAction = () => systemUnderTest.NextState(anyCell, neighbours);

            throwingAction.Should().Throw <TooManyNeighboursException>();
        }
예제 #8
0
        /// <summary>
        /// Register a rule triggered by a find in position findnumber
        /// </summary>
        private void Register(CoreRule rule, Term trigger, int findNumber)
        {
            Contract.Requires(rule != null && trigger != null);

            ShapeTrie index;

            if (!trigIndices.TryFindValue(trigger, out index))
            {
                index = new ShapeTrie(trigger);
                trigIndices.Add(trigger, index);

                LinkedList <ShapeTrie> subindices;
                if (!symbToIndexMap.TryFindValue(index.Pattern.Symbol, out subindices))
                {
                    subindices = new LinkedList <ShapeTrie>();
                    symbToIndexMap.Add(index.Pattern.Symbol, subindices);
                }

                subindices.AddLast(index);
            }

            index.AddTrigger(rule, findNumber);
        }
        public void NexState_Should_ReturnLivingCell_If_DeadCellHasThreeLivingAndFiveDeadNeighbours()
        {
            var systemUnderTest = new CoreRule();
            var cell            = new Cell
            {
                Alive = false,
            };
            var neighbours = new List <Cell>
            {
                new() { Alive = true },
                new() { Alive = false },
                new() { Alive = true },
                new() { Alive = false },
                new() { Alive = true },
                new() { Alive = false },
                new() { Alive = false },
                new() { Alive = false },
            };

            Cell cellInNextRound = systemUnderTest.NextState(cell, neighbours);

            cellInNextRound.Should().BeAlive();
        }
예제 #10
0
 public Activation(CoreRule rule, SymElement binding1, SymElement binding2)
 {
     Rule     = rule;
     Binding1 = binding1;
     Binding2 = binding2;
 }
예제 #11
0
 public Activation(CoreRule rule, SymElement binding)
 {
     Rule     = rule;
     Binding1 = binding;
     Binding2 = null;
 }
예제 #12
0
 public Activation(CoreRule rule)
 {
     Rule     = rule;
     Binding1 = Binding2 = null;
 }
예제 #13
0
 public override CoreRule OptInlinePartialRule(CoreRule eliminator, out bool succeeded)
 {
     succeeded = false;
     return(this);
 }
예제 #14
0
        /// <summary>
        /// Register a rule with a findnumber. The trigger may be constrained only by type constraints.
        /// </summary>
        private void Register(CoreRule rule, int findNumber)
        {
            Term trigger;
            Term type;

            switch (findNumber)
            {
            case 0:
                trigger = rule.Trigger1;
                type    = rule.Find1.Type;
                break;

            case 1:
                trigger = rule.Trigger2;
                type    = rule.Find2.Type;
                break;

            default:
                throw new Impossible();
            }

            if (!trigger.Symbol.IsVariable)
            {
                Register(rule, trigger, findNumber);
                return;
            }

            Set <Term> patternSet;

            if (typesToTriggersMap.TryFindValue(type, out patternSet))
            {
                foreach (var p in patternSet)
                {
                    trigIndices[p].AddTrigger(rule, findNumber);
                }

                return;
            }

            Set <Symbol> triggerSymbols = new Set <Symbol>(Symbol.Compare);

            type.Visit(
                x => x.Symbol == Index.TypeUnionSymbol ? x.Args : null,
                x =>
            {
                if (x.Symbol != Index.TypeUnionSymbol)
                {
                    triggerSymbols.Add(x.Symbol);
                }
            });

            Term pattern;

            patternSet = new Set <Term>(Term.Compare);
            foreach (var s in triggerSymbols)
            {
                if (s.Kind == SymbolKind.UserSortSymb)
                {
                    pattern = MkPattern(((UserSortSymb)s).DataSymbol, false);
                    patternSet.Add(pattern);
                    Register(rule, pattern, findNumber);
                }
                else
                {
                    Contract.Assert(s.IsDataConstructor || s.IsNonVarConstant);
                    pattern = MkPattern(s, false);
                    patternSet.Add(pattern);
                    Register(rule, pattern, findNumber);
                }
            }

            typesToTriggersMap.Add(type, patternSet);
        }
예제 #15
0
 internal ActivationStatistics GetStatistics(CoreRule rule)
 {
     throw new NotImplementedException();
 }
예제 #16
0
 internal ActivationStatistics GetActivations(CoreRule rule)
 {
     return(Read(() => activations[rule]));
 }
예제 #17
0
 internal ActivationStatistics(CoreRule rule)
 {
     this.rule = rule;
 }