use of nars.language.Inheritance in project opennars by opennars.
the class TermTest method invalidTermIndep.
@Test
public void invalidTermIndep() {
String t = "<$1 --> (~,{place4},$1)>";
NAR n = new NAR();
Narsese p = new Narsese(n);
try {
p.parseNarsese(new StringBuilder(t + "."));
assertTrue(false);
} catch (Narsese.InvalidInputException ex) {
assertTrue(true);
}
Term subj = null, pred = null;
try {
subj = p.parseTerm("$1");
pred = p.parseTerm("(~,{place4},$1)");
assertTrue(true);
} catch (Narsese.InvalidInputException ex) {
assertTrue(false);
}
Statement s = Statement.make(NativeOperator.INHERITANCE, subj, pred, false, 0);
assertEquals(null, s);
Inheritance i = Inheritance.make(subj, pred);
assertEquals(null, i);
try {
CompoundTerm forced = (CompoundTerm) p.parseTerm("<a --> b>");
assertTrue(true);
forced.term[0] = subj;
forced.term[1] = pred;
forced.invalidateName();
assertEquals(t, forced.toString());
CompoundTerm cloned = forced.clone();
assertEquals(null, cloned);
} catch (Narsese.InvalidInputException ex) {
assertTrue(false);
}
}
use of nars.language.Inheritance in project opennars by opennars.
the class Emotions method adjustBusy.
public void adjustBusy(float newValue, float weight, DerivationContext nal) {
busy += newValue * weight;
busy /= (1.0f + weight);
if (!enabled) {
return;
}
float frequency = -1;
if (Math.abs(busy - lastbusy) > CHANGE_THRESHOLD && nal.memory.time() - last_busy_time > change_steps_demanded) {
if (busy > Parameters.BUSY_EVENT_HIGHER_THRESHOLD && lastbusy <= Parameters.BUSY_EVENT_HIGHER_THRESHOLD) {
frequency = 1.0f;
}
if (busy < Parameters.BUSY_EVENT_LOWER_THRESHOLD && lastbusy >= Parameters.BUSY_EVENT_LOWER_THRESHOLD) {
frequency = 0.0f;
}
lastbusy = busy;
last_busy_time = nal.memory.time();
}
if (frequency != -1) {
// ok lets add an event now
Term predicate = SetInt.make(new Term("busy"));
Term subject = new Term("SELF");
Inheritance inh = Inheritance.make(subject, predicate);
TruthValue truth = new TruthValue(busy, Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
Sentence s = new Sentence(inh, Symbols.JUDGMENT_MARK, truth, new Stamp(nal.memory));
s.stamp.setOccurrenceTime(nal.memory.time());
Task t = new Task(s, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, BudgetFunctions.truthToQuality(truth)), true);
nal.addTask(t, "emotion");
}
}
use of nars.language.Inheritance in project opennars by opennars.
the class Emotions method adjustSatisfaction.
public void adjustSatisfaction(float newValue, float weight, DerivationContext nal) {
// float oldV = happyValue;
happy += newValue * weight;
happy /= 1.0f + weight;
if (!enabled) {
return;
}
float frequency = -1;
if (Math.abs(happy - lasthappy) > CHANGE_THRESHOLD && nal.memory.time() - last_happy_time > change_steps_demanded) {
if (happy > Parameters.HAPPY_EVENT_HIGHER_THRESHOLD && lasthappy <= Parameters.HAPPY_EVENT_HIGHER_THRESHOLD) {
frequency = 1.0f;
}
if (happy < Parameters.HAPPY_EVENT_LOWER_THRESHOLD && lasthappy >= Parameters.HAPPY_EVENT_LOWER_THRESHOLD) {
frequency = 0.0f;
}
lasthappy = happy;
last_happy_time = nal.memory.time();
}
if (frequency != -1) {
// ok lets add an event now
Term predicate = SetInt.make(new Term("satisfied"));
Term subject = Term.SELF;
Inheritance inh = Inheritance.make(subject, predicate);
TruthValue truth = new TruthValue(happy, Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
Sentence s = new Sentence(inh, Symbols.JUDGMENT_MARK, truth, new Stamp(nal.memory));
s.stamp.setOccurrenceTime(nal.memory.time());
Task t = new Task(s, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, BudgetFunctions.truthToQuality(truth)), true);
nal.addTask(t, "emotion");
/*if(Parameters.REFLECT_META_HAPPY_GOAL) { //remind on the goal whenever happyness changes, should suffice for now
TruthValue truth2=new TruthValue(1.0f,Parameters.DEFAULT_GOAL_CONFIDENCE);
Sentence s2=new Sentence(inh,Symbols.GOAL_MARK,truth2,new Stamp(nal.memory));
s2.stamp.setOccurrenceTime(nal.memory.time());
Task t2=new Task(s2,new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY,Parameters.DEFAULT_GOAL_DURABILITY,BudgetFunctions.truthToQuality(truth2)));
nal.addTask(t2, "metagoal");
//this is a good candidate for innate belief for consider and remind:
Operator consider=nal.memory.getOperator("^consider");
Operator remind=nal.memory.getOperator("^remind");
Term[] arg=new Term[1];
arg[0]=inh;
if(InternalExperience.enabled && Parameters.CONSIDER_REMIND) {
Operation op_consider=Operation.make(consider, arg, true);
Operation op_remind=Operation.make(remind, arg, true);
Operation[] op=new Operation[2];
op[0]=op_remind; //order important because usually reminding something
op[1]=op_consider; //means it has good chance to be considered after
for(Operation o : op) {
TruthValue truth3=new TruthValue(1.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
Sentence s3=new Sentence(o,Symbols.JUDGMENT_MARK,truth3,new Stamp(nal.memory));
s3.stamp.setOccurrenceTime(nal.memory.time());
//INTERNAL_EXPERIENCE_DURABILITY_MUL
BudgetValue budget=new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY,Parameters.DEFAULT_JUDGMENT_DURABILITY,BudgetFunctions.truthToQuality(truth3));
budget.setPriority(budget.getPriority()*InternalExperience.INTERNAL_EXPERIENCE_PRIORITY_MUL);
budget.setDurability(budget.getPriority()*InternalExperience.INTERNAL_EXPERIENCE_DURABILITY_MUL);
Task t3=new Task(s3,budget);
nal.addTask(t3, "internal experience for consider and remind");
}
}
}*/
}
// if (Math.abs(oldV - happyValue) > 0.1) {
// Record.append("HAPPY: " + (int) (oldV*10.0) + " to " + (int) (happyValue*10.0) + "\n");
}
use of nars.language.Inheritance in project opennars by opennars.
the class CompositionalRules method decomposeCompound.
/**
* {<(S|P) ==> M>, <P ==> M>} |- <S ==> M>
*
* @param implication The implication term to be decomposed
* @param componentCommon The part of the implication to be removed
* @param term1 The other term in the contentInd
* @param index The location of the shared term: 0 for subject, 1 for
* predicate
* @param compoundTask Whether the implication comes from the task
* @param nal Reference to the memory
*/
private static void decomposeCompound(CompoundTerm compound, Term component, Term term1, int index, boolean compoundTask, int order, DerivationContext nal) {
if ((compound instanceof Statement) || (compound instanceof ImageExt) || (compound instanceof ImageInt)) {
return;
}
Term term2 = reduceComponents(compound, component, nal.mem());
if (term2 == null) {
return;
}
long delta = 0;
while ((term2 instanceof Conjunction) && (((CompoundTerm) term2).term[0] instanceof Interval)) {
Interval interval = (Interval) ((CompoundTerm) term2).term[0];
delta += interval.time;
term2 = ((CompoundTerm) term2).setComponent(0, null, nal.mem());
}
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
Statement oldContent = (Statement) task.getTerm();
TruthValue v1, v2;
if (compoundTask) {
v1 = sentence.truth;
v2 = belief.truth;
} else {
v1 = belief.truth;
v2 = sentence.truth;
}
TruthValue truth = null;
Term content;
if (index == 0) {
content = Statement.make(oldContent, term1, term2, order);
if (content == null) {
return;
}
if (oldContent instanceof Inheritance) {
if (compound instanceof IntersectionExt) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof IntersectionInt) {
truth = reduceDisjunction(v1, v2);
} else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
truth = reduceConjunction(v1, v2);
} else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
truth = reduceDisjunction(v1, v2);
} else if (compound instanceof DifferenceExt) {
if (compound.term[0].equals(component)) {
truth = reduceDisjunction(v2, v1);
} else {
truth = reduceConjunctionNeg(v1, v2);
}
}
} else if (oldContent instanceof Implication) {
if (compound instanceof Conjunction) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof Disjunction) {
truth = reduceDisjunction(v1, v2);
}
}
} else {
content = Statement.make(oldContent, term2, term1, order);
if (content == null) {
return;
}
if (oldContent instanceof Inheritance) {
if (compound instanceof IntersectionInt) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof IntersectionExt) {
truth = reduceDisjunction(v1, v2);
} else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
truth = reduceConjunction(v1, v2);
} else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
truth = reduceDisjunction(v1, v2);
} else if (compound instanceof DifferenceInt) {
if (compound.term[1].equals(component)) {
truth = reduceDisjunction(v2, v1);
} else {
truth = reduceConjunctionNeg(v1, v2);
}
}
} else if (oldContent instanceof Implication) {
if (compound instanceof Disjunction) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof Conjunction) {
truth = reduceDisjunction(v1, v2);
}
}
}
if (truth != null) {
BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
if (delta != 0) {
long baseTime = task.sentence.getOccurenceTime();
if (baseTime != Stamp.ETERNAL) {
baseTime += delta;
nal.getTheNewStamp().setOccurrenceTime(baseTime);
}
}
// (allow overlap), a form of detachment
nal.doublePremiseTask(content, truth, budget, false, true);
}
}
use of nars.language.Inheritance in project opennars by opennars.
the class CompositionalRules method introVarOuter.
/* --------------- rules used for variable introduction --------------- */
/**
* Introduce a dependent variable in an outer-layer conjunction {<S --> P1>,
* <S --> P2>} |- (&&, <#x --> P1>, <#x --> P2>)
*
* @param taskContent The first premise <M --> S>
* @param beliefContent The second premise <M --> P>
* @param index The location of the shared term: 0 for subject, 1 for
* predicate
* @param nal Reference to the memory
*/
public static void introVarOuter(final Statement taskContent, final Statement beliefContent, final int index, final DerivationContext nal) {
if (!(taskContent instanceof Inheritance)) {
return;
}
Variable varInd1 = new Variable("$varInd1");
Variable varInd2 = new Variable("$varInd2");
Term term11dependent = null, term12dependent = null, term21dependent = null, term22dependent = null;
Term term11, term12, term21, term22, commonTerm = null;
HashMap<Term, Term> subs = new HashMap<>();
if (index == 0) {
term11 = varInd1;
term21 = varInd1;
term12 = taskContent.getPredicate();
term22 = beliefContent.getPredicate();
term12dependent = term12;
term22dependent = term22;
if (term12 instanceof ImageExt) {
if ((/*(ImageExt)*/
term12).containsTermRecursively(term22)) {
commonTerm = term22;
}
if (commonTerm == null && term12 instanceof ImageExt) {
commonTerm = ((ImageExt) term12).getTheOtherComponent();
if (!(term22.containsTermRecursively(commonTerm))) {
commonTerm = null;
}
if (term22 instanceof ImageExt && ((commonTerm == null) || !(term22).containsTermRecursively(commonTerm))) {
commonTerm = ((ImageExt) term22).getTheOtherComponent();
if ((commonTerm == null) || !(term12).containsTermRecursively(commonTerm)) {
commonTerm = null;
}
}
}
if (commonTerm != null) {
subs.put(commonTerm, varInd2);
term12 = ((CompoundTerm) term12).applySubstitute(subs);
if (!(term22 instanceof CompoundTerm)) {
term22 = varInd2;
} else {
term22 = ((CompoundTerm) term22).applySubstitute(subs);
}
}
}
if (commonTerm == null && term22 instanceof ImageExt) {
if ((/*(ImageExt)*/
term22).containsTermRecursively(term12)) {
commonTerm = term12;
}
if (commonTerm == null && term22 instanceof ImageExt) {
commonTerm = ((ImageExt) term22).getTheOtherComponent();
if (!(term12.containsTermRecursively(commonTerm))) {
commonTerm = null;
}
if (term12 instanceof ImageExt && ((commonTerm == null) || !(term12).containsTermRecursively(commonTerm))) {
commonTerm = ((ImageExt) term12).getTheOtherComponent();
if ((commonTerm == null) || !(term22).containsTermRecursively(commonTerm)) {
commonTerm = null;
}
}
}
if (commonTerm != null) {
subs.put(commonTerm, varInd2);
term22 = ((CompoundTerm) term22).applySubstitute(subs);
if (!(term12 instanceof CompoundTerm)) {
term12 = varInd2;
} else {
term12 = ((CompoundTerm) term12).applySubstitute(subs);
}
}
}
} else {
term11 = taskContent.getSubject();
term21 = beliefContent.getSubject();
term12 = varInd1;
term22 = varInd1;
term11dependent = term11;
term21dependent = term21;
if (term21 instanceof ImageInt) {
if ((/*(ImageInt)*/
term21).containsTermRecursively(term11)) {
commonTerm = term11;
}
if (term11 instanceof ImageInt && commonTerm == null && term21 instanceof ImageInt) {
commonTerm = ((ImageInt) term11).getTheOtherComponent();
if (!(term21.containsTermRecursively(commonTerm))) {
commonTerm = null;
}
if ((commonTerm == null) || !(term21).containsTermRecursively(commonTerm)) {
commonTerm = ((ImageInt) term21).getTheOtherComponent();
if ((commonTerm == null) || !(term11).containsTermRecursively(commonTerm)) {
commonTerm = null;
}
}
}
if (commonTerm != null) {
subs.put(commonTerm, varInd2);
term21 = ((CompoundTerm) term21).applySubstitute(subs);
if (!(term11 instanceof CompoundTerm)) {
term11 = varInd2;
} else {
term11 = ((CompoundTerm) term11).applySubstitute(subs);
}
}
}
if (commonTerm == null && term11 instanceof ImageInt) {
if ((/*(ImageInt)*/
term11).containsTermRecursively(term21)) {
commonTerm = term21;
}
if (term21 instanceof ImageInt && commonTerm == null && term11 instanceof ImageInt) {
commonTerm = ((ImageInt) term21).getTheOtherComponent();
if (!(term11.containsTermRecursively(commonTerm))) {
commonTerm = null;
}
if ((commonTerm == null) || !(term11).containsTermRecursively(commonTerm)) {
commonTerm = ((ImageInt) term11).getTheOtherComponent();
if ((commonTerm == null) || !(term21).containsTermRecursively(commonTerm)) {
commonTerm = null;
}
}
}
if (commonTerm != null) {
subs.put(commonTerm, varInd2);
term11 = ((CompoundTerm) term11).applySubstitute(subs);
if (!(term21 instanceof CompoundTerm)) {
term21 = varInd2;
} else {
term21 = ((CompoundTerm) term21).applySubstitute(subs);
}
}
}
}
Statement state1 = Inheritance.make(term11, term12);
Statement state2 = Inheritance.make(term21, term22);
Term content = Implication.make(state1, state2);
if (content == null) {
return;
}
TruthValue truthT = nal.getCurrentTask().sentence.truth;
TruthValue truthB = nal.getCurrentBelief().truth;
if ((truthT == null) || (truthB == null)) {
if (Parameters.DEBUG) {
System.out.println("ERROR: Belief with null truth value. (introVarOuter)");
}
return;
}
TruthValue truth = induction(truthT, truthB);
BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.doublePremiseTask(content, truth, budget, false, false);
content = Implication.make(state2, state1);
truth = induction(truthB, truthT);
budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.doublePremiseTask(content, truth, budget, false, false);
content = Equivalence.make(state1, state2);
truth = comparison(truthT, truthB);
budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.doublePremiseTask(content, truth, budget, false, false);
Variable varDep = new Variable("#varDep");
if (index == 0) {
state1 = Inheritance.make(varDep, term12dependent);
state2 = Inheritance.make(varDep, term22dependent);
} else {
state1 = Inheritance.make(term11dependent, varDep);
state2 = Inheritance.make(term21dependent, varDep);
}
if ((state1 == null) || (state2 == null))
return;
content = Conjunction.make(state1, state2);
truth = intersection(truthT, truthB);
budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.doublePremiseTask(content, truth, budget, false, false);
}
Aggregations