class AnswerQuery_InferenceEffect extends InferenceEffect { constructor(effectParameter:Term, nlcp:NLContextPerformative) { super() this.nlcp = nlcp; this.effectParameter = effectParameter; } execute(inf:InferenceRecord, ai:RuleBasedAI) { console.log("AnswerQuery_InferenceEffect"); console.log("inf.inferences.length: " + inf.inferences.length); console.log("inf.inferences[0].endResults.length: " + inf.inferences[0].endResults.length); if (!(this.effectParameter.attributes[1] instanceof ConstantTermAttribute)) { console.error("AnswerQuery_InferenceEffect.execute: Trying to talk to a character for which we don't know the ID!"); return; } let speakerCharacterID:string = ((this.effectParameter.attributes[1])).value; let queryPerformative:Term = ((this.effectParameter.attributes[2])).term; let queryVariable:VariableTermAttribute = (queryPerformative.attributes[1]); let queryTerm:Term = null; if (queryPerformative.attributes[2] instanceof TermTermAttribute) { queryTerm = ((queryPerformative.attributes[2])).term; } let forAllVariableNames:string[] = []; if (queryPerformative.attributes.length >= 4) { for(let forAll of NLParser.termsInList(((queryPerformative.attributes[3])).term, "#and")) { if (forAll.attributes.length>=1 && forAll.attributes[0] instanceof VariableTermAttribute) { forAllVariableNames.push((forAll.attributes[0]).name); } } } console.log("forAllVariableNames: " + forAllVariableNames); if (inf.inferences.length != 1+forAllVariableNames.length) { console.error("number of inferences is wrong, should be 1 + " + forAllVariableNames.length + ", but is: " + inf.inferences.length); } else { // filter by the forAll results: for(let i:number = 0;i= i+1) { let allValues:TermAttribute[] = []; for(let result of inf.inferences[i+1].endResults) { let v:TermAttribute = result.getValueForVariableName(forAllVariableNames[i]); if (v != null) allValues.push(v); } console.log("forAll values ("+forAllVariableNames[i]+"): " + allValues); inf.inferences[0].filterResultsByForAll([queryVariable.name], forAllVariableNames[i], allValues); } } } let negativeAnswer:string = "'no-matches-found'[symbol]"; if (queryTerm != null) { if (queryTerm.functor.is_a(ai.cache_sort_property_with_value) || queryTerm.functor.is_a(ai.cache_sort_relation_with_value)) { negativeAnswer = "'unknown'[symbol]"; } else if (queryTerm.functor.is_a(ai.o.getSort("verb.happen"))) { negativeAnswer = "'nothing'[symbol]"; } } if (inf.inferences[0].endResults.length != 0) { let results:TermAttribute[] = []; for(let result of inf.inferences[0].endResults) { for(let [variable, value] of result.bindings.l) { if (variable == queryVariable) { // we have a result! check for duplicates: let found:boolean = false; for(let res of results) { if ((res instanceof ConstantTermAttribute) && (value instanceof ConstantTermAttribute)) { if ((res).value == (value).value) { found = true; break; } } } if (!found) results.push(value); } } } console.log("result: " + results); if (results.length > 0) { let resultsTA:TermAttribute = null; if (results.length > ai.maximum_answers_to_give_at_once_for_a_query) { resultsTA = new ConstantTermAttribute("etcetera",ai.o.getSort("etcetera")); for(let i:number = 0;i"; } else { return ""; } } else { return ""; } } static loadFromXML(xml:Element, ai:RuleBasedAI, o:Ontology, variables:TermAttribute[], variableNames:string[]) : InferenceEffect { let t:Term = Term.fromStringInternal(xml.getAttribute("effectParameter"), o, variableNames, variables).term; let speaker:string = xml.getAttribute("speaker"); if (speaker != null) { let nlcp:number = Number(xml.getAttribute("nlcp")); let context:NLContext = ai.contextForSpeaker(speaker); if (context != null) { return new AnswerQuery_InferenceEffect(t, context.performatives[nlcp]); } else { return new AnswerQuery_InferenceEffect(t, null); } } else { return new AnswerQuery_InferenceEffect(t, null); } } nlcp:NLContextPerformative = null; effectParameter:Term = null; }