示例#1
0
 internal Context(InferenceEngine mind, WildcardTriplet rootWildcard)
 {
     _mind = mind;
     _rootLevel = getState(null, rootWildcard);
     _levelsToExpand.Enqueue(_rootLevel);
     _levelsToGenerate.Enqueue(_rootLevel);
 }
示例#2
0
 internal InferenceLevel(InferenceLevel parent, WildcardTriplet condition, IEnumerable<InferenceStep> steps, Context context)
 {
     Condition = condition;
     Depth = parent == null ? 0 : parent.Depth + 1;
     _context = context;
     _steps = steps.ToArray();
 }
示例#3
0
        internal ImplicationStep(WildcardTriplet implicationCondition, TripletTree implicationResult, WildcardTriplet target, Context context)
            : base(target, context)
        {
            _conditionReader = CreateRequirement(implicationCondition);
            _implicationResult = implicationResult;

            _conditionReader.AttachHandler(_handler_Condition);
        }
 internal IEnumerable<TripletTree> RequestSubstituted(WildcardTriplet condition)
 {
     foreach (var triplet in _availableTriplets)
     {
         var substitution = condition.GetSatisfiingSubstitution(triplet);
         if (substitution != null)
             yield return substitution;
     }
 }
示例#5
0
        internal AndStep(WildcardTriplet condition1, WildcardTriplet condition2, WildcardTriplet target, Context context)
            : base(target, context)
        {
            _reader1 = CreateRequirement(condition1);
            _reader2 = CreateRequirement(condition2);

            _reader1.AttachHandler(reader1_Handler);
            _reader2.AttachHandler(reader2_Handler);
        }
        internal WildcardTriplet Substitute(WildcardTriplet wildcard)
        {
            if (_mapping.Count == 0)
                //there is nothing to substitute
                return wildcard;

            return WildcardTriplet.From(
                substituted(wildcard.SearchedSubject),
                substitutedPredicate(wildcard.SearchedPredicate),
                substituted(wildcard.SearchedObject)
                );
        }
示例#7
0
        internal static IEnumerable<InferenceStep> Provider(WildcardTriplet target, Context context)
        {
            if (!Predicate.And.Equals(target.SearchedPredicate))
                yield break;

            var condition1Tree = target.SearchedSubject as TripletTree;
            var condition2Tree = target.SearchedObject as TripletTree;

            if (condition1Tree == null || condition2Tree == null)
                yield break;

            yield return new AndStep(WildcardTriplet.Exact(condition1Tree), WildcardTriplet.Exact(condition2Tree), target, context);
        }
示例#8
0
        internal static IEnumerable<InferenceStep> Provider(WildcardTriplet target, Context context)
        {
            foreach (var substitutedTree in context.FindSubstitutedSubtreeParents(target))
            {
                //find matching trees
                var thenTrees = new List<TripletTree>();
                substitutedTree.Each(t =>
                {
                    if (Predicate.Then.Equals(t.Predicate))
                        thenTrees.Add(t);
                });

                //create steps according to database
                foreach (var thenTree in thenTrees)
                {
                    var condition = thenTree.Subject as TripletTree;
                    var implicationResult = thenTree.Object as TripletTree;
                    if (condition != null && implicationResult != null && target.IsSatisfiedBy(thenTree.Object as TripletTree))
                    {
                        yield return new ImplicationStep(WildcardTriplet.Exact(condition), implicationResult, target, context);
                    }
                }
            }
        }
示例#9
0
 private LookupStep(WildcardTriplet target, Context context) :
     base(target, context)
 {
     _context = context;
     _triplets = lazyFindTriplets().GetEnumerator();
 }
示例#10
0
        private IEnumerable<InferenceStep> createSteps(WildcardTriplet wildcard)
        {
            var result = new List<InferenceStep>();
            foreach (var stepProvider in _mind.InferenceStepProviders)
            {
                foreach (var step in stepProvider(wildcard, this))
                {
                    result.Add(step);
                }
            }

            return result;
        }
示例#11
0
        /// <summary>
        /// Gets (or creates) state for inference of given wildcard.
        /// </summary>
        /// <param name="wildcard">The wildcard to infer.</param>
        /// <returns>The state.</returns>
        private InferenceLevel getState(InferenceLevel parent, WildcardTriplet wildcard)
        {
            InferenceLevel result;
            if (!_wildcardToLevel.TryGetValue(wildcard, out result))
                _wildcardToLevel[wildcard] = result = new InferenceLevel(parent, wildcard, createSteps(wildcard), this);

            return result;
        }
示例#12
0
 internal IEnumerable<TripletTree> FindSatisfiingSubstitutedRoots(WildcardTriplet wildcard)
 {
     foreach (var rootTriplet in _mind.RootTriplets)
     {
         var substitution = wildcard.GetSatisfiingSubstitution(rootTriplet);
         if (substitution != null)
             yield return substitution;
     }
 }
示例#13
0
 /// <summary>
 /// Finds parents of triplets which can satisfy wildcard after substitution.
 /// </summary>
 /// <param name="wildcard">The searching wildcard.</param>
 /// <returns>The found result.</returns>
 internal IEnumerable<TripletTree> FindSubstitutedSubtreeParents(WildcardTriplet wildcard)
 {
     //TODO PERFORMANCE this should be done by using indexes
     foreach (var rootTriplet in _mind.RootTriplets)
     {
         foreach (var substitution in SubstitutionMapping.GetSubtreeSubstitutions(rootTriplet, wildcard))
         {
             yield return substitution;
         }
     }
 }
示例#14
0
        internal TripletTreeReader GetReader(WildcardTriplet wildcard, TripletTreeReaderEvent handler = null)
        {
            TripletTreeReader reader;
            if (!_wildcardReaders.TryGetValue(wildcard, out reader))
                _wildcardReaders[wildcard] = reader = new TripletTreeReader(wildcard);

            if (handler != null)
                reader.AttachHandler(handler);
            return reader;
        }
        /// <summary>
        /// Get all possible substitutions induced by subtrees, such that satisfies the wildcard.
        /// </summary>
        /// <param name="triplet">Triplet to substitution.</param>
        /// <param name="wildcard">Wildcard to satisfy.</param>
        /// <returns>The substitutions.</returns>
        internal static IEnumerable<TripletTree> GetSubtreeSubstitutions(TripletTree triplet, WildcardTriplet wildcard)
        {
            var substitutableSubtrees = new List<TripletTree>();
            triplet.Each(t =>
            {
                if (wildcard.IsSatisfiedBySubstitution(t))
                    substitutableSubtrees.Add(t);
            });

            foreach (var substitutableSubtree in substitutableSubtrees)
            {
                //TODO detection of infeasible mappings
                var mapping = wildcard.GetSubstitutionMapping(substitutableSubtree);
                yield return mapping.Substitute(triplet);
            }
        }
示例#16
0
 internal TripletTreeReader(WildcardTriplet condition)
 {
     Condition = condition;
 }
示例#17
0
 /// <summary>
 /// Gets preconditions to given wildcard.
 /// </summary>
 /// <param name="wildcard">Wildcard which preconditions are requested.</param>
 /// <returns></returns>
 public IEnumerable<Precondition> Preconditions(WildcardTriplet wildcard)
 {
     var inference = new Context(this, wildcard);
     return inference.Preconditions();
 }
示例#18
0
 internal Requirement(WildcardTriplet target, InferenceStep inferenceRule, IEnumerable<TripletTree> condition)
 {
     Target = target;
     InferenceRule = inferenceRule;
     Condition = condition.ToArray();
 }
示例#19
0
 /// <summary>
 /// Finds triplets according to given wildcard.
 /// Including the inference.
 /// </summary>
 /// <param name="wildcard">The wildcard for search.</param>
 /// <returns>Found triplets.</returns>
 public IEnumerable<TripletTree> Find(WildcardTriplet wildcard)
 {
     var inference = new Context(this, wildcard);
     return inference.Find();
 }
示例#20
0
 /// <inheritdoc/>
 protected override WildcardReader createWildcardReader(WildcardTriplet wildcard)
 {
     return new InferenceWildcardReader(_engine, wildcard);
 }
示例#21
0
 /// <summary>
 /// Asserts find on wildcard to given expected result.
 /// </summary>
 /// <param name="wildcardTriplet">The searching criterion.</param>
 /// <param name="expectedResult">Expected result.</param>
 internal void AssertFind(WildcardTriplet wildcardTriplet, params TripletTree[] expectedResult)
 {
     var actualResult = _mind.Find(wildcardTriplet).Take(10).ToArray();
     CollectionAssert.AreEquivalent(expectedResult, actualResult, "results of find");
 }
示例#22
0
 /// <summary>
 /// Creates wildcard reader for given wildcard.
 /// </summary>
 /// <param name="wildcard">The wildcard to read.</param>
 /// <returns>The created reader.</returns>
 protected abstract WildcardReader createWildcardReader(WildcardTriplet wildcard);
示例#23
0
 /// <summary>
 /// Transforms wildcard to corresponding triplet.
 /// </summary>
 /// <param name="wildcard">The wildcard to be transformed.</param>
 /// <returns>The triplet.</returns>
 private TripletTree toTriplet(WildcardTriplet wildcard)
 {
     //TODO replace placeholders.
     return TripletTree.From(wildcard.SearchedSubject, wildcard.SearchedPredicate, wildcard.SearchedObject);
 }
示例#24
0
 public Precondition(WildcardTriplet wildcard, double score)
 {
     Wildcard = wildcard;
     Score = score;
 }
示例#25
0
 internal static IEnumerable<InferenceStep> Provider(WildcardTriplet wildcard, Context context)
 {
     yield return new LookupStep(wildcard, context);
 }
示例#26
0
 protected TripletTreeReader CreateRequirement(WildcardTriplet wildcard)
 {
     _requirements.Add(wildcard);
     return _context.GetReader(wildcard);
 }
示例#27
0
        /// <summary>
        /// Reports inference of new triplet.
        /// </summary>
        /// <param name="triplet">The reported triplet.</param>
        /// <param name="inferenceLevel">Level which triplet was generated.</param>
        internal void Report(TripletTree triplet, WildcardTriplet wildcard)
        {
            var inferenceLevel = _wildcardToLevel[wildcard];
            if (inferenceLevel == _rootLevel)
            {
                _tripletsToReport.Enqueue(triplet);
                //we have found new triplet which satisfies the root condition.
                return;
            }

            var reader = GetReader(wildcard);
            reader.Receive(triplet);
        }
示例#28
0
 internal InferenceStep(WildcardTriplet target, Context context)
 {
     Target = target;
     _context = context;
 }
 internal InferenceWildcardReader(InferenceEngine engine, WildcardTriplet wildcard)
 {
     _engine = engine;
     _wildcard = wildcard;
 }