use of nars.storage.Memory in project opennars by opennars.
the class LocalRules method trySolution.
/**
* Check if a Sentence provide a better answer to a Question or Goal
*
* @param belief The proposed answer
* @param task The task to be processed
* @param memory Reference to the memory
*/
public static boolean trySolution(Sentence belief, final Task task, final DerivationContext nal, boolean report) {
Sentence problem = task.sentence;
Memory memory = nal.mem();
Sentence oldBest = task.getBestSolution();
if (oldBest != null) {
boolean rateByConfidence = oldBest.getTerm().equals(belief.getTerm());
float newQ = solutionQuality(rateByConfidence, task, belief, memory);
float oldQ = solutionQuality(rateByConfidence, task, oldBest, memory);
if (oldQ >= newQ) {
if (problem.isGoal()) {
memory.emotion.adjustSatisfaction(oldQ, task.getPriority(), nal);
}
memory.emit(Unsolved.class, task, belief, "Lower quality");
return false;
}
}
task.setBestSolution(memory, belief);
// memory.logic.SOLUTION_BEST.commit(task.getPriority());
BudgetValue budget = solutionEval(task, belief, task, nal);
if ((budget != null) && budget.aboveThreshold()) {
// Solution Activated
if (task.sentence.punctuation == Symbols.QUESTION_MARK || task.sentence.punctuation == Symbols.QUEST_MARK) {
if (task.isInput() && report) {
// only show input tasks as solutions
memory.emit(Answer.class, task, belief);
} else {
// solution to quests and questions can be always showed
memory.emit(OutputHandler.class, task, belief);
}
} else {
// goal things only show silence related
memory.emit(OutputHandler.class, task, belief);
}
nal.addTask(nal.getCurrentTask(), budget, belief, task.getParentBelief());
return true;
} else {
memory.emit(Unsolved.class, task, belief, "Insufficient budget");
}
return false;
}
use of nars.storage.Memory in project opennars by opennars.
the class StructuralRules method contraposition.
/**
* {<A ==> B>, A@(--, A)} |- <(--, B) ==> (--, A)>
*
* @param statement The premise
* @param memory Reference to the memory
*/
protected static boolean contraposition(final Statement statement, final Sentence sentence, final DerivationContext nal) {
Memory memory = nal.mem();
// memory.logic.CONTRAPOSITION.commit(statement.complexity);
Term subj = statement.getSubject();
Term pred = statement.getPredicate();
Statement content = Statement.make(statement, Negation.make(pred), Negation.make(subj), TemporalRules.reverseOrder(statement.getTemporalOrder()));
if (content == null)
return false;
TruthValue truth = sentence.truth;
BudgetValue budget;
if (sentence.isQuestion() || sentence.isQuest()) {
if (content instanceof Implication) {
budget = BudgetFunctions.compoundBackwardWeak(content, nal);
} else {
budget = BudgetFunctions.compoundBackward(content, nal);
}
return nal.singlePremiseTask(content, Symbols.QUESTION_MARK, truth, budget);
} else {
if (content instanceof Implication) {
truth = TruthFunctions.contraposition(truth);
}
budget = BudgetFunctions.compoundForward(truth, content, nal);
return nal.singlePremiseTask(content, Symbols.JUDGMENT_MARK, truth, budget);
}
}
use of nars.storage.Memory in project opennars by opennars.
the class StructuralRules method transformProductImage.
/* -------------------- products and images transform -------------------- */
/**
* Equivalent transformation between products and images {<(*, S, M) --> P>,
* S@(*, S, M)} |- <S --> (/, P, _, M)> {<S --> (/, P, _, M)>, P@(/, P, _,
* M)} |- <(*, S, M) --> P> {<S --> (/, P, _, M)>, M@(/, P, _, M)} |- <M -->
* (/, P, S, _)>
*
* @param inh An Inheritance statement
* @param oldContent The whole content
* @param indices The indices of the TaskLink
* @param task The task
* @param memory Reference to the memory
*/
static void transformProductImage(Inheritance inh, CompoundTerm oldContent, short[] indices, DerivationContext nal) {
final Memory memory = nal.mem();
Term subject = inh.getSubject();
Term predicate = inh.getPredicate();
short index = indices[indices.length - 1];
short side = indices[indices.length - 2];
if (inh.equals(oldContent)) {
if (subject instanceof CompoundTerm) {
transformSubjectPI(index, (CompoundTerm) subject, predicate, nal);
}
if (predicate instanceof CompoundTerm) {
transformPredicatePI(index, subject, (CompoundTerm) predicate, nal);
}
return;
}
Term compT = inh.term[side];
if (!(compT instanceof CompoundTerm))
return;
CompoundTerm comp = (CompoundTerm) compT;
if (comp instanceof Product) {
if (side == 0) {
subject = comp.term[index];
predicate = ImageExt.make((Product) comp, inh.getPredicate(), index);
} else {
subject = ImageInt.make((Product) comp, inh.getSubject(), index);
predicate = comp.term[index];
}
} else if ((comp instanceof ImageExt) && (side == 1)) {
if (index == ((ImageExt) comp).relationIndex) {
subject = Product.make(comp, inh.getSubject(), index);
predicate = comp.term[index];
} else {
subject = comp.term[index];
predicate = ImageExt.make((ImageExt) comp, inh.getSubject(), index);
}
} else if ((comp instanceof ImageInt) && (side == 0)) {
if (index == ((ImageInt) comp).relationIndex) {
subject = comp.term[index];
predicate = Product.make(comp, inh.getPredicate(), index);
} else {
subject = ImageInt.make((ImageInt) comp, inh.getPredicate(), index);
predicate = comp.term[index];
}
} else {
return;
}
CompoundTerm newInh = null;
if (predicate.equals(Term.SEQ_SPATIAL)) {
newInh = (CompoundTerm) Conjunction.make(((CompoundTerm) subject).term, TemporalRules.ORDER_FORWARD, true);
} else if (predicate.equals(Term.SEQ_TEMPORAL)) {
newInh = (CompoundTerm) Conjunction.make(((CompoundTerm) subject).term, TemporalRules.ORDER_FORWARD, false);
} else {
newInh = Inheritance.make(subject, predicate);
}
if (newInh == null)
return;
CompoundTerm content = null;
if (indices.length == 2) {
content = newInh;
} else if ((oldContent instanceof Statement) && (indices[0] == 1)) {
content = Statement.make((Statement) oldContent, oldContent.term[0], newInh, oldContent.getTemporalOrder());
} else {
Term[] componentList;
Term condition = oldContent.term[0];
if (((oldContent instanceof Implication) || (oldContent instanceof Equivalence)) && (condition instanceof Conjunction)) {
componentList = ((CompoundTerm) condition).cloneTerms();
componentList[indices[1]] = newInh;
Term newCond = Terms.term((CompoundTerm) condition, componentList);
content = Statement.make((Statement) oldContent, newCond, ((Statement) oldContent).getPredicate(), oldContent.getTemporalOrder());
} else {
componentList = oldContent.cloneTerms();
componentList[indices[0]] = newInh;
if (oldContent instanceof Conjunction) {
Term newContent = Terms.term(oldContent, componentList);
if (!(newContent instanceof CompoundTerm))
return;
content = (CompoundTerm) newContent;
} else if ((oldContent instanceof Implication) || (oldContent instanceof Equivalence)) {
content = Statement.make((Statement) oldContent, componentList[0], componentList[1], oldContent.getTemporalOrder());
}
}
}
if (content == null)
return;
Sentence sentence = nal.getCurrentTask().sentence;
TruthValue truth = sentence.truth;
BudgetValue budget;
if (sentence.isQuestion() || sentence.isQuest()) {
budget = BudgetFunctions.compoundBackward(content, nal);
} else {
budget = BudgetFunctions.compoundForward(truth, content, nal);
}
nal.singlePremiseTask(content, truth, budget);
}
Aggregations