internal Context(InferenceEngine mind, WildcardTriplet rootWildcard) { _mind = mind; _rootLevel = getState(null, rootWildcard); _levelsToExpand.Enqueue(_rootLevel); _levelsToGenerate.Enqueue(_rootLevel); }
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(); }
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; } }
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) ); }
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); }
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); } } } }
private LookupStep(WildcardTriplet target, Context context) : base(target, context) { _context = context; _triplets = lazyFindTriplets().GetEnumerator(); }
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; }
/// <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; }
internal IEnumerable<TripletTree> FindSatisfiingSubstitutedRoots(WildcardTriplet wildcard) { foreach (var rootTriplet in _mind.RootTriplets) { var substitution = wildcard.GetSatisfiingSubstitution(rootTriplet); if (substitution != null) yield return substitution; } }
/// <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; } } }
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); } }
internal TripletTreeReader(WildcardTriplet condition) { Condition = condition; }
/// <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(); }
internal Requirement(WildcardTriplet target, InferenceStep inferenceRule, IEnumerable<TripletTree> condition) { Target = target; InferenceRule = inferenceRule; Condition = condition.ToArray(); }
/// <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(); }
/// <inheritdoc/> protected override WildcardReader createWildcardReader(WildcardTriplet wildcard) { return new InferenceWildcardReader(_engine, wildcard); }
/// <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"); }
/// <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);
/// <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); }
public Precondition(WildcardTriplet wildcard, double score) { Wildcard = wildcard; Score = score; }
internal static IEnumerable<InferenceStep> Provider(WildcardTriplet wildcard, Context context) { yield return new LookupStep(wildcard, context); }
protected TripletTreeReader CreateRequirement(WildcardTriplet wildcard) { _requirements.Add(wildcard); return _context.GetReader(wildcard); }
/// <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); }
internal InferenceStep(WildcardTriplet target, Context context) { Target = target; _context = context; }
internal InferenceWildcardReader(InferenceEngine engine, WildcardTriplet wildcard) { _engine = engine; _wildcard = wildcard; }