use of nars.entity.TruthValue in project opennars by opennars.
the class Counting method setEnabled.
@Override
public boolean setEnabled(NAR n, boolean enabled) {
Memory memory = n.memory;
if (obs == null) {
obs = new EventObserver() {
@Override
public void event(Class event, Object[] a) {
if ((event != Events.TaskDerive.class && event != Events.TaskAdd.class))
return;
Task task = (Task) a[0];
if (task.getPriority() < InternalExperience.MINIMUM_PRIORITY_TO_CREATE_WANT_BELIEVE_ETC) {
return;
}
if (task.sentence.punctuation == Symbols.JUDGMENT_MARK) {
// lets say we have <{...} --> M>.
if (task.sentence.term instanceof Inheritance) {
Inheritance inh = (Inheritance) task.sentence.term;
if (inh.getSubject() instanceof SetExt) {
SetExt set_term = (SetExt) inh.getSubject();
// this gets the cardinality of M
int cardinality = set_term.size();
// now create term <(*,M,cardinality) --> CARDINALITY>.
Term[] product_args = new Term[] { inh.getPredicate(), Term.get(cardinality) };
// TODO CARDINATLITY can be a static final instance shared by all
Term new_term = Inheritance.make(new Product(product_args), /* --> */
CARDINALITY);
if (new_term == null) {
// this usually happens when product_args contains the term CARDINALITY in which case it is an invalid Inheritance statement
return;
}
TruthValue truth = task.sentence.truth.clone();
Stamp stampi = task.sentence.stamp.clone();
Sentence j = new Sentence(new_term, Symbols.JUDGMENT_MARK, truth, stampi);
BudgetValue budg = task.budget.clone();
Task newTask = new Task(j, budg, true);
memory.addNewTask(newTask, "Derived (Cardinality)");
}
}
}
}
};
}
memory.event.set(obs, enabled, Events.TaskDerive.class);
return true;
}
use of nars.entity.TruthValue in project opennars by opennars.
the class InternalExperience method beliefReason.
/**
* used in full internal experience mode only
*/
protected void beliefReason(Sentence belief, Term beliefTerm, Term taskTerm, DerivationContext nal) {
Memory memory = nal.memory;
if (Memory.randomNumber.nextDouble() < INTERNAL_EXPERIENCE_RARE_PROBABILITY) {
// the operators which dont have a innate belief
// also get a chance to reveal its effects to the system this way
Operator op = memory.getOperator(nonInnateBeliefOperators[Memory.randomNumber.nextInt(nonInnateBeliefOperators.length)]);
Product prod = new Product(new Term[] { belief.term });
if (op != null && prod != null) {
Term new_term = Inheritance.make(prod, op);
Sentence sentence = new Sentence(new_term, Symbols.GOAL_MARK, // a naming convension
new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(memory));
float quality = BudgetFunctions.truthToQuality(sentence.truth);
BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_GOAL_DURABILITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, quality);
Task newTask = new Task(sentence, budget, true);
nal.derivedTask(newTask, false, false, false);
}
}
if (beliefTerm instanceof Implication && Memory.randomNumber.nextDouble() <= INTERNAL_EXPERIENCE_PROBABILITY) {
Implication imp = (Implication) beliefTerm;
if (imp.getTemporalOrder() == TemporalRules.ORDER_FORWARD) {
// 1. check if its (&/,term,+i1,...,+in) =/> anticipateTerm form:
boolean valid = true;
if (imp.getSubject() instanceof Conjunction) {
Conjunction conj = (Conjunction) imp.getSubject();
if (!conj.term[0].equals(taskTerm)) {
// the expected needed term is not included
valid = false;
}
for (int i = 1; i < conj.term.length; i++) {
if (!(conj.term[i] instanceof Interval)) {
valid = false;
break;
}
}
} else {
if (!imp.getSubject().equals(taskTerm)) {
valid = false;
}
}
if (valid) {
Operator op = memory.getOperator("^anticipate");
if (op == null)
throw new RuntimeException(this + " requires ^anticipate operator");
Product args = new Product(new Term[] { imp.getPredicate() });
Term new_term = Operation.make(args, op);
Sentence sentence = new Sentence(new_term, Symbols.GOAL_MARK, // a naming convension
new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(memory));
float quality = BudgetFunctions.truthToQuality(sentence.truth);
BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_GOAL_DURABILITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, quality);
Task newTask = new Task(sentence, budget, true);
nal.derivedTask(newTask, false, false, false);
}
}
}
}
use of nars.entity.TruthValue in project opennars by opennars.
the class SentenceTablePanel method output.
@Override
public void output(Class c, Object o) {
if (o instanceof Task) {
Task t = (Task) o;
float priority = t.getPriority();
Sentence s = t.sentence;
float freq = -1;
float conf = -1;
TruthValue truth = s.truth;
if (truth != null) {
freq = truth.getFrequency();
conf = truth.getConfidence();
}
// t.getParentTask();
Task pt = null;
String parentTask = (pt != null) ? pt.toStringExternal() : "";
// TODO use table sort instead of formatting numbers with leading '0's
data.addRow(new Object[] { String.format("%08d", nar.time()), s, s.punctuation, freq == -1 ? "" : freq, conf == -1 ? "" : conf, String.format("%03d", s.term.getComplexity()), priority, parentTask });
}
}
use of nars.entity.TruthValue in project opennars by opennars.
the class CompositionalRules method composeCompound.
/* -------------------- intersections and differences -------------------- */
/**
* {<S ==> M>, <P ==> M>} |- {<(S|P) ==> M>, <(S&P) ==> M>, <(S-P) ==>
* M>,
* <(P-S) ==> M>}
*
* @param taskSentence The first premise
* @param belief The second premise
* @param index The location of the shared term
* @param nal Reference to the memory
*/
static void composeCompound(final Statement taskContent, final Statement beliefContent, final int index, final DerivationContext nal) {
if ((!nal.getCurrentTask().sentence.isJudgment()) || (taskContent.getClass() != beliefContent.getClass())) {
return;
}
final Term componentT = taskContent.term[1 - index];
final Term componentB = beliefContent.term[1 - index];
final Term componentCommon = taskContent.term[index];
int order1 = taskContent.getTemporalOrder();
int order2 = beliefContent.getTemporalOrder();
int order = TemporalRules.composeOrder(order1, order2);
if (order == TemporalRules.ORDER_INVALID) {
return;
}
if ((componentT instanceof CompoundTerm) && ((CompoundTerm) componentT).containsAllTermsOf(componentB)) {
decomposeCompound((CompoundTerm) componentT, componentB, componentCommon, index, true, order, nal);
return;
} else if ((componentB instanceof CompoundTerm) && ((CompoundTerm) componentB).containsAllTermsOf(componentT)) {
decomposeCompound((CompoundTerm) componentB, componentT, componentCommon, index, false, order, nal);
return;
}
final TruthValue truthT = nal.getCurrentTask().sentence.truth;
final TruthValue truthB = nal.getCurrentBelief().truth;
final TruthValue truthOr = union(truthT, truthB);
final TruthValue truthAnd = intersection(truthT, truthB);
TruthValue truthDif = null;
Term termOr = null;
Term termAnd = null;
Term termDif = null;
if (index == 0) {
if (taskContent instanceof Inheritance) {
termOr = IntersectionInt.make(componentT, componentB);
termAnd = IntersectionExt.make(componentT, componentB);
if (truthB.isNegative()) {
if (!truthT.isNegative()) {
termDif = DifferenceExt.make(componentT, componentB);
truthDif = intersection(truthT, negation(truthB));
}
} else if (truthT.isNegative()) {
termDif = DifferenceExt.make(componentB, componentT);
truthDif = intersection(truthB, negation(truthT));
}
} else if (taskContent instanceof Implication) {
termOr = Disjunction.make(componentT, componentB);
termAnd = Conjunction.make(componentT, componentB);
}
processComposed(taskContent, componentCommon, termOr, order, truthOr, nal);
processComposed(taskContent, componentCommon, termAnd, order, truthAnd, nal);
processComposed(taskContent, componentCommon, termDif, order, truthDif, nal);
} else {
// index == 1
if (taskContent instanceof Inheritance) {
termOr = IntersectionExt.make(componentT, componentB);
termAnd = IntersectionInt.make(componentT, componentB);
if (truthB.isNegative()) {
if (!truthT.isNegative()) {
termDif = DifferenceInt.make(componentT, componentB);
truthDif = intersection(truthT, negation(truthB));
}
} else if (truthT.isNegative()) {
termDif = DifferenceInt.make(componentB, componentT);
truthDif = intersection(truthB, negation(truthT));
}
} else if (taskContent instanceof Implication) {
termOr = Conjunction.make(componentT, componentB);
termAnd = Disjunction.make(componentT, componentB);
}
processComposed(taskContent, termOr, componentCommon, order, truthOr, nal);
processComposed(taskContent, termAnd, componentCommon, order, truthAnd, nal);
processComposed(taskContent, termDif, componentCommon, order, truthDif, nal);
}
}
use of nars.entity.TruthValue in project opennars by opennars.
the class CompositionalRules method IntroVarSameSubjectOrPredicate.
static void IntroVarSameSubjectOrPredicate(final Sentence originalMainSentence, final Sentence subSentence, final Term component, final Term content, final int index, final DerivationContext nal) {
Term T1 = originalMainSentence.term;
if (!(T1 instanceof CompoundTerm) || !(content instanceof CompoundTerm)) {
return;
}
CompoundTerm T = (CompoundTerm) T1;
CompoundTerm T2 = (CompoundTerm) content;
if ((component instanceof Inheritance && content instanceof Inheritance) || (component instanceof Similarity && content instanceof Similarity)) {
// CompoundTerm result = T;
if (component.equals(content)) {
// wouldnt make sense to create a conjunction here, would contain a statement twice
return;
}
Variable depIndVar1 = new Variable("#depIndVar1");
Variable depIndVar2 = new Variable("#depIndVar2");
if (((Statement) component).getPredicate().equals(((Statement) content).getPredicate()) && !(((Statement) component).getPredicate() instanceof Variable)) {
CompoundTerm zw = (CompoundTerm) T.term[index];
zw = (CompoundTerm) zw.setComponent(1, depIndVar1, nal.mem());
T2 = (CompoundTerm) T2.setComponent(1, depIndVar1, nal.mem());
Conjunction res = (Conjunction) Conjunction.make(zw, T2);
T = (CompoundTerm) T.setComponent(index, res, nal.mem());
} else if (((Statement) component).getSubject().equals(((Statement) content).getSubject()) && !(((Statement) component).getSubject() instanceof Variable)) {
CompoundTerm zw = (CompoundTerm) T.term[index];
zw = (CompoundTerm) zw.setComponent(0, depIndVar2, nal.mem());
T2 = (CompoundTerm) T2.setComponent(0, depIndVar2, nal.mem());
Conjunction res = (Conjunction) Conjunction.make(zw, T2);
T = (CompoundTerm) T.setComponent(index, res, nal.mem());
}
TruthValue truth = induction(originalMainSentence.truth, subSentence.truth);
BudgetValue budget = BudgetFunctions.compoundForward(truth, T, nal);
nal.doublePremiseTask(T, truth, budget, false, false);
}
}
Aggregations