/// <summary> /// Determine whether given information holds according to /// actual mind state. /// </summary> /// <param name="triplet">The tested information</param> /// <returns><c>True</c> if triplet holds, <c>false</c> otherwise.</returns> public bool Holds(TripletTree triplet) { if (_rootTriplets.Contains(triplet)) return true; return Find(WildcardTriplet.Exact(triplet)).Any(); }
public TripletWordGroup(TripletTree triplet) { // TODO: Complete member initialization TextSpan = triplet.Subject.Name + " " + triplet.Predicate.Name + " " + triplet.Object.Name; RawGroup = triplet; }
internal void Receive(TripletTree triplet) { _availableTriplets.Add(triplet); foreach (var handler in _handlers) { handler(triplet); } }
internal bool IsSatisfiedBy(TripletTree triplet) { return ( (SearchedSubject == null || SearchedSubject.Equals(triplet.Subject)) && (SearchedPredicate == null || SearchedPredicate.Equals(triplet.Predicate)) && (SearchedObject == null || SearchedObject.Equals(triplet.Object)) ); }
internal ImplicationStep(WildcardTriplet implicationCondition, TripletTree implicationResult, WildcardTriplet target, Context context) : base(target, context) { _conditionReader = CreateRequirement(implicationCondition); _implicationResult = implicationResult; _conditionReader.AttachHandler(_handler_Condition); }
/// <summary> /// Makes sentence from given triplet. /// </summary> /// <param name="triplet">Triplet which is used for triplet creation.</param> /// <returns>The sentence.</returns> private string makeSentence(TripletTree triplet) { var endMark = isQuestion(triplet) ? "?" : "."; var expression = generateSentence(triplet); expression = smooth(expression); expression = char.ToUpper(expression[0]) + expression.Substring(1) + endMark; return expression; }
/// <summary> /// Recursively substitute given triplet. /// </summary> /// <param name="triplet">Triplet to substitute.</param> /// <returns>The substituted triplet.</returns> internal TripletTree Substitute(TripletTree triplet) { if (_mapping.Count == 0) //there is nothing to substitute return triplet; return TripletTree.From( substituted(triplet.Subject), substitutedPredicate(triplet.Predicate), substituted(triplet.Object) ); }
/// <summary> /// Handler for received triplet on given reader. /// </summary> /// <param name="receivedTriplet">Triplet which was received.</param> /// <param name="handledReader">The reader to handle.</param> private void reader_Handler(TripletTree receivedTriplet, TripletTreeReader handledReader) { var otherReader = handledReader == _reader1 ? _reader2 : _reader1; var generateReverseOrder = handledReader == _reader2; var substitutions = handledReader.Condition.GetSubstitutionMapping(receivedTriplet); var otherConditionRequest = substitutions.Substitute(otherReader.Condition); foreach (var substitutedRequestedTriplet in otherReader.RequestSubstituted(otherConditionRequest)) { var mapping = otherConditionRequest.GetSubstitutionMapping(substitutedRequestedTriplet); var substitutedReceivedTriplet = mapping.Substitute(receivedTriplet); if (generateReverseOrder) Produce(TripletTree.From(substitutedRequestedTriplet, Predicate.And, substitutedReceivedTriplet)); else Produce(TripletTree.From(substitutedReceivedTriplet, Predicate.And, substitutedRequestedTriplet)); } }
/// <summary> /// Determine whether there exist satisfiing substituion in given triplet. /// </summary> /// <param name="triplet"></param> /// <returns></returns> internal bool IsSatisfiedBySubstitution(TripletTree triplet) { //test whether we have comaptible signature for substitution var hasCompatibleSignature = ( (SearchedSubject == null || SearchedSubject.IsVariable || triplet.Subject.IsVariable || SearchedSubject.Equals(triplet.Subject)) && (SearchedPredicate == null || SearchedPredicate.IsVariable || triplet.Predicate.IsVariable || SearchedPredicate.Equals(triplet.Predicate)) && (SearchedObject == null || SearchedObject.IsVariable || triplet.Object.IsVariable || SearchedObject.Equals(triplet.Object)) ); //test which variables are shared between triplet parts var sharedVariable_SP = triplet.Subject.IsVariable && triplet.Predicate.Equals(triplet.Subject); var sharedVariable_SO = triplet.Object.IsVariable && triplet.Predicate.Equals(triplet.Object); var sharedVariable_PO = triplet.Predicate.IsVariable && triplet.Object.Equals(triplet.Predicate); //test whether shared variables are not substituted by different entities return hasCompatibleSignature && ( (!sharedVariable_SP || shareAble(SearchedSubject, SearchedPredicate)) && (!sharedVariable_SO || shareAble(SearchedObject, SearchedPredicate)) && (!sharedVariable_PO || shareAble(SearchedPredicate, SearchedObject)) ); }
/// <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); }
protected void Produce(TripletTree tripletTree) { _producedTriplets.Enqueue(tripletTree); _context.Report(tripletTree, Target); }
/// <summary> /// Adds triplet as ground true (without any checks for absurdum) /// </summary> /// <param name="axiom">The axiom.</param> public void AddAxiom(TripletTree axiom) { registerTriplet(axiom); }
/// <summary> /// Handles acception of the fact. /// </summary> /// <param name="fact">Fact to accept.</param> /// <returns>The response.</returns> private IEnumerable<TripletTree> analyzeFact(TripletTree fact) { var reader = createWildcardReader(fact); if (reader.HasEvidence) { //TODO try to infer new fact to implicit confirmation return triplet(Me, KnowAlready, fact); } else { if (reader.HasNegativeEvidence) throw new NotImplementedException("inconsistency"); acceptFact(fact); var clarifiedAnswer = getClarifiedAnswers().FirstOrDefault(); if (clarifiedAnswer == null) { return triplet(Me, Thank, You); } else { return triplet(fact, Predicate.Then, clarifiedAnswer); } } }
/// <summary> /// Register given triplet into indexes. /// </summary> /// <param name="triplet">Triplet to </param> private void registerTriplet(TripletTree triplet) { _rootTriplets.Add(triplet); }
/// <inheritdoc/> protected override bool acceptFact(TripletTree fact) { _engine.AddAxiom(fact); return true; }
/// <summary> /// Creates wildcard reader for the triplet. /// </summary> /// <param name="triplet">Triplet defining the wildcard.</param> /// <returns>The created reader.</returns> private WildcardReader createWildcardReader(TripletTree triplet) { WildcardTriplet wildcard; if (isQuestion(triplet)) { var questionedFact = triplet.Object; //TODO placeholder replacement wildcard = WildcardTriplet.Exact(triplet.Object as TripletTree); } else { wildcard = WildcardTriplet.Exact(triplet); } return createWildcardReader(wildcard); }
/// <summary> /// Determine whether triplet represents a question. /// </summary> /// <param name="triplet">The tested triplet.</param> /// <returns><c>True</c> whether question is represented by triplet,<c>false</c> otherwise.</returns> private bool isQuestion(TripletTree triplet) { return Entity.Question.Equals(triplet.Subject); }
/// <summary> /// Answers qiven question. /// </summary> /// <param name="question">The question to answer.</param> /// <returns>The response.</returns> private IEnumerable<TripletTree> answerQuestion(TripletTree question) { var reader = createWildcardReader(question); var explicitAnswer = getAnswerWithQuestionConfirmation(reader, question); if (explicitAnswer != null) //we have answer return new[] { explicitAnswer }; //otherwise we don't know var isKnownQuestion = _pendingQuestions.Contains(question); if (!isKnownQuestion) //the question has not been asked during this conversation _pendingQuestions.Add(question); //try to request additional information for question answering. var bestPrecondition = findBestPrecondition(reader); if (bestPrecondition == null) { //we don't have precondition to ask if (isKnownQuestion) return triplet(Me, StillDontKnow, question); else return triplet(Me, Know.Negation, question); } else { _askedPreconditions.Add(bestPrecondition); return triplet(Me, Predicate.Is.Negation, Sure).Concat( triplet(Entity.Question, Predicate.About, bestPrecondition) ); } }
/// <summary> /// Generates string representation of semantic represented by given triplet as a standalone sentence without markers. /// </summary> /// <param name="triplet">The triplet.</param> /// <returns>The string representation.</returns> private string generateSentence(TripletTree triplet) { var tripletSubjectTree = triplet.Subject as TripletTree; var tripletObjectTree = triplet.Object as TripletTree; if (isQuestion(triplet)) { var questionPredicatePart = generateQuestionPredicatePart(tripletObjectTree.Predicate); var dependentPredicatePart = generateDependentPredicatePart(tripletObjectTree.Predicate); if (questionPredicatePart != "") questionPredicatePart += " "; if (dependentPredicatePart != "") dependentPredicatePart += " "; return questionPredicatePart + generate(tripletObjectTree.Subject) + " " + dependentPredicatePart + generate(tripletObjectTree.Object); } //otherwise we generate expression as usuall return generate(triplet); }
/// <summary> /// Asserts that given triplet holds in model. /// </summary> /// <param name="triplet">Asserted triplet</param> internal void AssertHolds(TripletTree triplet) { Assert.IsTrue(_mind.Holds(triplet), triplet.ToString()); }
/// <summary> /// Asserts that given triplet doesn't hold in model. /// </summary> /// <param name="tripletTree">Asserted triplet</param> internal void AssertNotHolds(TripletTree tripletTree) { Assert.IsFalse(_mind.Holds(tripletTree), tripletTree.ToString()); }
/// <summary> /// Adds axiom into tested model. /// </summary> /// <param name="axiom">The axiom.</param> internal void AddAxiom(TripletTree axiom) { _mind.AddAxiom(axiom); }
private void _handler_Condition(TripletTree receivedTriplet) { var mapping = _conditionReader.Condition.GetSubstitutionMapping(receivedTriplet); var substitutedResult = mapping.Substitute(_implicationResult); Produce(substitutedResult); }
/// <summary> /// Handler for reader 2. /// </summary> /// <param name="receivedTriplet">Triplet which was received from reader 2.</param> private void reader2_Handler(TripletTree receivedTriplet) { reader_Handler(receivedTriplet, _reader2); }
/// <summary> /// Gets answer containing the question confirmation. /// </summary> /// <param name="reader">The reader containing question wildcard.</param> /// <param name="question">The question.</param> /// <returns>The answer if available, <c>null</c> otherwise.</returns> private TripletTree getAnswerWithQuestionConfirmation(WildcardReader reader, TripletTree question) { var isYesNoQuestion = Predicate.About.Equals(question.Predicate); //TODO better yes no resolving if (!isYesNoQuestion) throw new NotImplementedException(); var questionObject = question.Object as TripletTree; if (reader.HasEvidence) { //there is evidence for the positive answer return questionObject; } else if (reader.HasNegativeEvidence) { return questionObject.Negation; } return null; }
/// <summary> /// Accepts given fact as true. /// </summary> /// <param name="fact">The accepted fact.</param> /// <returns><c>True</c> whether the fact has been accepted, <c>false</c> otherwise.</returns> protected abstract bool acceptFact(TripletTree fact);
/// <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); } }
/// <summary> /// Determine whether given triplet describes a question. /// </summary> /// <param name="triplet">Triplet to be tested.</param> /// <returns><c>true</c> when triplet is question, <c>false</c> otherwise.</returns> private bool isQuestion(TripletTree triplet) { return Entity.Question.Equals(triplet.Subject) && triplet.Object as TripletTree != null; }
/// <summary> /// Generates string representation of semantic represented by given triplet. /// </summary> /// <param name="triplet">The triplet.</param> /// <returns>The string representation.</returns> private string generate(TripletTree triplet) { var tripletSubjectTree = triplet.Subject as TripletTree; var tripletObjectTree = triplet.Object as TripletTree; var tripletRepresentation = generate(triplet.Subject) + " " + generate(triplet.Predicate) + " " + generate(triplet.Object); if (Predicate.Then.Equals(triplet.Predicate)) tripletRepresentation = "when " + tripletRepresentation; return tripletRepresentation; }
/// <summary> /// Gets answer containing the question confirmation. /// </summary> /// <param name="question">The question.</param> /// <returns>The answer if available, <c>null</c> otherwise.</returns> private TripletTree getAnswerWithQuestionConfirmation(TripletTree question) { var reader = createWildcardReader(question); return getAnswerWithQuestionConfirmation(reader, question); }