use of nars.language.Conjunction in project opennars by opennars.
the class SyllogisticRules method conditionalDedInd.
/**
* {<(&&, S1, S2, S3) ==> P>, S1} |- <(&&, S2, S3) ==> P> {<(&&, S2, S3) ==>
* P>, <S1 ==> S2>} |- <(&&, S1, S3) ==> P> {<(&&, S1, S3) ==> P>, <S1 ==>
* S2>} |- <(&&, S2, S3) ==> P>
*
* @param premise1 The conditional premise
* @param index The location of the shared term in the condition of premise1
* @param premise2 The premise which, or part of which, appears in the
* condition of premise1
* @param side The location of the shared term in premise2: 0 for subject, 1
* for predicate, -1 for the whole term
* @param nal Reference to the memory
*/
static void conditionalDedInd(Sentence premise1Sentence, Implication premise1, short index, Term premise2, int side, DerivationContext nal) {
Task task = nal.getCurrentTask();
Sentence taskSentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
boolean deduction = (side != 0);
boolean conditionalTask = Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, premise2, belief.term);
Term commonComponent;
Term newComponent = null;
if (side == 0) {
commonComponent = ((Statement) premise2).getSubject();
newComponent = ((Statement) premise2).getPredicate();
} else if (side == 1) {
commonComponent = ((Statement) premise2).getPredicate();
newComponent = ((Statement) premise2).getSubject();
} else {
commonComponent = premise2;
}
Term subj = premise1.getSubject();
if (!(subj instanceof Conjunction)) {
return;
}
Conjunction oldCondition = (Conjunction) subj;
int index2 = Terms.indexOf(oldCondition.term, commonComponent);
if (index2 >= 0) {
index = (short) index2;
} else {
Term[] u = new Term[] { premise1, premise2 };
boolean match = Variables.unify(Symbols.VAR_INDEPENDENT, oldCondition.term[index], commonComponent, u);
premise1 = (Implication) u[0];
premise2 = u[1];
if (!match && (commonComponent.getClass() == oldCondition.getClass())) {
CompoundTerm compoundCommonComponent = ((CompoundTerm) commonComponent);
if ((oldCondition.term.length > index) && (compoundCommonComponent.term.length > index)) {
// assumption: { was missing
u = new Term[] { premise1, premise2 };
match = Variables.unify(Symbols.VAR_INDEPENDENT, oldCondition.term[index], compoundCommonComponent.term[index], u);
premise1 = (Implication) u[0];
premise2 = u[1];
}
}
if (!match) {
return;
}
}
int conjunctionOrder = subj.getTemporalOrder();
if (conjunctionOrder == ORDER_FORWARD) {
if (index > 0) {
return;
}
if ((side == 0) && (premise2.getTemporalOrder() == ORDER_FORWARD)) {
return;
}
if ((side == 1) && (premise2.getTemporalOrder() == ORDER_BACKWARD)) {
return;
}
}
Term newCondition;
if (oldCondition.equals(commonComponent)) {
newCondition = null;
} else {
newCondition = oldCondition.setComponent(index, newComponent, nal.mem());
}
Term content;
long delta = 0;
long mintime = 0;
long maxtime = 0;
boolean predictedEvent = false;
if (newCondition != null) {
if (newCondition instanceof Interval) {
content = premise1.getPredicate();
delta = ((Interval) newCondition).time;
if (taskSentence.getOccurenceTime() != Stamp.ETERNAL) {
mintime = taskSentence.getOccurenceTime() + ((Interval) newCondition).time - 1;
maxtime = taskSentence.getOccurenceTime() + ((Interval) newCondition).time + 2;
predictedEvent = true;
}
} else {
while ((newCondition instanceof Conjunction) && (((CompoundTerm) newCondition).term[0] instanceof Interval)) {
Interval interval = (Interval) ((CompoundTerm) newCondition).term[0];
delta += interval.time;
newCondition = ((CompoundTerm) newCondition).setComponent(0, null, nal.mem());
}
content = Statement.make(premise1, newCondition, premise1.getPredicate(), premise1.getTemporalOrder());
}
} else {
content = premise1.getPredicate();
}
if (content == null)
return;
long occurrence_time = nal.getTheNewStamp().getOccurrenceTime();
if (delta != 0) {
long baseTime = taskSentence.getOccurenceTime();
if (baseTime != Stamp.ETERNAL) {
baseTime += delta;
occurrence_time = baseTime;
}
}
TruthValue truth1 = taskSentence.truth;
TruthValue truth2 = belief.truth;
TruthValue truth = null;
BudgetValue budget;
if (taskSentence.isQuestion() || taskSentence.isQuest()) {
budget = BudgetFunctions.backwardWeak(truth2, nal);
} else {
if (taskSentence.isGoal()) {
if (conditionalTask) {
truth = TruthFunctions.desireWeak(truth1, truth2);
} else if (deduction) {
truth = TruthFunctions.desireInd(truth1, truth2);
} else {
truth = TruthFunctions.desireDed(truth1, truth2);
}
} else {
if (deduction) {
truth = TruthFunctions.deduction(truth1, truth2);
} else if (conditionalTask) {
truth = TruthFunctions.induction(truth2, truth1);
} else {
truth = TruthFunctions.induction(truth1, truth2);
}
}
budget = BudgetFunctions.forward(truth, nal);
}
nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
// (allow overlap) when deduction on judgment
List<Task> ret = nal.doublePremiseTask(content, truth, budget, false, taskSentence.isJudgment() && deduction);
if (!nal.evidentalOverlap && ret != null && ret.size() > 0) {
if (predictedEvent && taskSentence.isJudgment() && truth != null && truth.getExpectation() > Parameters.DEFAULT_CONFIRMATION_EXPECTATION && !premise1Sentence.stamp.alreadyAnticipatedNegConfirmation) {
premise1Sentence.stamp.alreadyAnticipatedNegConfirmation = true;
ConceptProcessing.generatePotentialNegConfirmation(nal, premise1Sentence, budget, mintime, maxtime, 1);
}
}
}
use of nars.language.Conjunction in project opennars by opennars.
the class SyllogisticRules method conditionalAna.
/**
* {<(&&, S1, S2, S3) <=> P>, S1} |- <(&&, S2, S3) <=> P> {<(&&, S2, S3) <=> P>,
* <S1 ==> S2>} |- <(&&, S1, S3) <=> P> {<(&&, S1, S3) <=> P>, <S1 ==>
*
* @param premise1 The equivalence premise
* @param index The location of the shared term in the condition of premise1
* @param premise2 The premise which, or part of which, appears in the
* condition of premise1
* @param side The location of the shared term in premise2: 0 for subject, 1
* for predicate, -1 for the whole term
* @param nal Reference to the memory
*/
static void conditionalAna(Equivalence premise1, short index, Term premise2, int side, DerivationContext nal) {
Task task = nal.getCurrentTask();
Sentence taskSentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
boolean conditionalTask = Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, premise2, belief.term);
Term commonComponent;
Term newComponent = null;
if (side == 0) {
commonComponent = ((Statement) premise2).getSubject();
newComponent = ((Statement) premise2).getPredicate();
} else if (side == 1) {
commonComponent = ((Statement) premise2).getPredicate();
newComponent = ((Statement) premise2).getSubject();
} else {
commonComponent = premise2;
}
Term tm = premise1.getSubject();
if (!(tm instanceof Conjunction)) {
return;
}
Conjunction oldCondition = (Conjunction) tm;
Term[] u = new Term[] { premise1, premise2 };
boolean match = Variables.unify(Symbols.VAR_DEPENDENT, oldCondition.term[index], commonComponent, u);
premise1 = (Equivalence) u[0];
premise2 = u[1];
if (!match && (commonComponent.getClass() == oldCondition.getClass())) {
u = new Term[] { premise1, premise2 };
match = Variables.unify(Symbols.VAR_DEPENDENT, oldCondition.term[index], ((CompoundTerm) commonComponent).term[index], u);
premise1 = (Equivalence) u[0];
premise2 = u[1];
}
if (!match) {
return;
}
int conjunctionOrder = oldCondition.getTemporalOrder();
if (conjunctionOrder == ORDER_FORWARD) {
if (index > 0) {
return;
}
if ((side == 0) && (premise2.getTemporalOrder() == ORDER_FORWARD)) {
return;
}
if ((side == 1) && (premise2.getTemporalOrder() == ORDER_BACKWARD)) {
return;
}
}
Term newCondition;
if (oldCondition.equals(commonComponent)) {
newCondition = null;
} else {
newCondition = oldCondition.setComponent(index, newComponent, nal.mem());
}
Term content;
if (newCondition != null) {
content = Statement.make(premise1, newCondition, premise1.getPredicate(), premise1.getTemporalOrder());
} else {
content = premise1.getPredicate();
}
if (content == null)
return;
TruthValue truth1 = taskSentence.truth;
TruthValue truth2 = belief.truth;
TruthValue truth = null;
BudgetValue budget;
if (taskSentence.isQuestion() || taskSentence.isQuest()) {
budget = BudgetFunctions.backwardWeak(truth2, nal);
} else {
if (taskSentence.isGoal()) {
if (conditionalTask) {
truth = TruthFunctions.desireWeak(truth1, truth2);
} else {
truth = TruthFunctions.desireDed(truth1, truth2);
}
} else {
if (conditionalTask) {
truth = TruthFunctions.comparison(truth1, truth2);
} else {
truth = TruthFunctions.analogy(truth1, truth2);
}
}
budget = BudgetFunctions.forward(truth, nal);
}
// (allow overlap) when !conditionalTask on judgment
nal.doublePremiseTask(content, truth, budget, false, taskSentence.isJudgment() && !conditionalTask);
}
use of nars.language.Conjunction in project opennars by opennars.
the class TemporalRules method temporalInduction.
// TODO maybe split &/ case into own function
public static List<Task> temporalInduction(final Sentence s1, final Sentence s2, final nars.control.DerivationContext nal, boolean SucceedingEventsInduction, boolean addToMemory, boolean allowSequence) {
if ((s1.truth == null) || (s2.truth == null) || s1.punctuation != Symbols.JUDGMENT_MARK || s2.punctuation != Symbols.JUDGMENT_MARK || s1.isEternal() || s2.isEternal())
return Collections.EMPTY_LIST;
Term t1 = s1.term;
Term t2 = s2.term;
boolean deriveSequenceOnly = (!addToMemory) || Statement.invalidStatement(t1, t2, true);
if (Statement.invalidStatement(t1, t2, false))
return Collections.EMPTY_LIST;
Term t11 = null;
Term t22 = null;
if (!deriveSequenceOnly && termForTemporalInduction(t1) && termForTemporalInduction(t2)) {
Statement ss1 = (Statement) t1;
Statement ss2 = (Statement) t2;
Variable var1 = new Variable("$0");
Variable var2 = new Variable("$1");
if (ss2.containsTermRecursively(ss1.getSubject())) {
HashMap<Term, Term> subs = new HashMap();
subs.put(ss1.getSubject(), var1);
if (ss2.containsTermRecursively(ss1.getPredicate())) {
subs.put(ss1.getPredicate(), var2);
}
t11 = ss1.applySubstitute(subs);
t22 = ss2.applySubstitute(subs);
}
if (ss1.containsTermRecursively(ss2.getSubject())) {
HashMap<Term, Term> subs = new HashMap();
subs.put(ss2.getSubject(), var1);
if (ss1.containsTermRecursively(ss2.getPredicate())) {
subs.put(ss2.getPredicate(), var2);
}
t11 = ss1.applySubstitute(subs);
t22 = ss2.applySubstitute(subs);
}
// allow also temporal induction on operator arguments:
if (ss2 instanceof Operation ^ ss1 instanceof Operation) {
if (ss2 instanceof Operation && !(ss2.getSubject() instanceof Variable)) {
// it is an operation, let's look if one of the arguments is same as the subject of the other term
Term comp = ss1.getSubject();
Term ss2_term = ((Operation) ss2).getSubject();
boolean applicableVariableType = !(comp instanceof Variable && ((Variable) comp).hasVarIndep());
if (ss2_term instanceof Product) {
Product ss2_prod = (Product) ss2_term;
if (applicableVariableType && Terms.contains(ss2_prod.term, comp)) {
// only if there is one and it isnt a variable already
Term[] ars = ss2_prod.cloneTermsReplacing(comp, var1);
t11 = Statement.make(ss1, var1, ss1.getPredicate());
Operation op = (Operation) Operation.make(new Product(ars), ss2.getPredicate());
t22 = op;
}
}
}
if (ss1 instanceof Operation && !(ss1.getSubject() instanceof Variable)) {
// it is an operation, let's look if one of the arguments is same as the subject of the other term
Term comp = ss2.getSubject();
Term ss1_term = ((Operation) ss1).getSubject();
boolean applicableVariableType = !(comp instanceof Variable && ((Variable) comp).hasVarIndep());
if (ss1_term instanceof Product) {
Product ss1_prod = (Product) ss1_term;
if (applicableVariableType && Terms.contains(ss1_prod.term, comp)) {
// only if there is one and it isnt a variable already
Term[] ars = ss1_prod.cloneTermsReplacing(comp, var1);
t22 = Statement.make(ss2, var1, ss2.getPredicate());
Operation op = (Operation) Operation.make(new Product(ars), ss1.getPredicate());
t11 = op;
}
}
}
}
}
int durationCycles = Parameters.DURATION;
long time1 = s1.getOccurenceTime();
long time2 = s2.getOccurenceTime();
long timeDiff = time2 - time1;
Interval interval = null;
if (!concurrent(time1, time2, durationCycles)) {
interval = new Interval(Math.abs(timeDiff));
if (timeDiff > 0) {
t1 = Conjunction.make(t1, interval, ORDER_FORWARD);
if (t11 != null) {
t11 = Conjunction.make(t11, interval, ORDER_FORWARD);
}
} else {
t2 = Conjunction.make(t2, interval, ORDER_FORWARD);
if (t22 != null) {
t22 = Conjunction.make(t22, interval, ORDER_FORWARD);
}
}
}
int order = order(timeDiff, durationCycles);
TruthValue givenTruth1 = s1.truth;
TruthValue givenTruth2 = s2.truth;
// This code adds a penalty for large time distance (TODO probably revise)
Sentence s3 = s2.projection(s1.getOccurenceTime(), nal.memory.time());
givenTruth2 = s3.truth;
// Truth and priority calculations
TruthValue truth1 = TruthFunctions.induction(givenTruth1, givenTruth2);
TruthValue truth2 = TruthFunctions.induction(givenTruth2, givenTruth1);
TruthValue truth3 = TruthFunctions.comparison(givenTruth1, givenTruth2);
TruthValue truth4 = TruthFunctions.intersection(givenTruth1, givenTruth2);
BudgetValue budget1 = BudgetFunctions.forward(truth1, nal);
budget1.setPriority(budget1.getPriority() * Parameters.TEMPORAL_INDUCTION_PRIORITY_PENALTY);
BudgetValue budget2 = BudgetFunctions.forward(truth2, nal);
budget2.setPriority(budget2.getPriority() * Parameters.TEMPORAL_INDUCTION_PRIORITY_PENALTY);
BudgetValue budget3 = BudgetFunctions.forward(truth3, nal);
budget3.setPriority(budget3.getPriority() * Parameters.TEMPORAL_INDUCTION_PRIORITY_PENALTY);
// this one is sequence in sequenceBag, no need to reduce here
BudgetValue budget4 = BudgetFunctions.forward(truth4, nal);
Statement statement1 = Implication.make(t1, t2, order);
Statement statement2 = Implication.make(t2, t1, reverseOrder(order));
Statement statement3 = Equivalence.make(t1, t2, order);
Term statement4 = null;
switch(order) {
case TemporalRules.ORDER_FORWARD:
statement4 = Conjunction.make(t1, interval, s2.term, order);
break;
case TemporalRules.ORDER_BACKWARD:
statement4 = Conjunction.make(s2.term, interval, t1, reverseOrder(order));
break;
default:
statement4 = Conjunction.make(t1, s2.term, order);
break;
}
// "Perception Variable Introduction Rule" - https://groups.google.com/forum/#!topic/open-nars/uoJBa8j7ryE
if (!deriveSequenceOnly && statement2 != null) {
// there is no general form
// ok then it may be the (&/ =/> case which
// is discussed here: https://groups.google.com/forum/#!topic/open-nars/uoJBa8j7ryE
Statement st = statement2;
if (st.getPredicate() instanceof Inheritance && (st.getSubject() instanceof Conjunction || st.getSubject() instanceof Operation)) {
Term precon = (Term) st.getSubject();
Inheritance consequence = (Inheritance) st.getPredicate();
Term pred = consequence.getPredicate();
Term sub = consequence.getSubject();
// look if subject is contained in precon:
boolean SubsSub = precon.containsTermRecursively(sub);
boolean SubsPred = precon.containsTermRecursively(pred);
Variable v1 = new Variable("$91");
Variable v2 = new Variable("$92");
HashMap<Term, Term> app = new HashMap<Term, Term>();
if (SubsSub || SubsPred) {
if (SubsSub)
app.put(sub, v1);
if (SubsPred)
app.put(pred, v2);
Term res = ((CompoundTerm) statement2).applySubstitute(app);
if (res != null) {
// ok we applied it, all we have to do now is to use it
t22 = ((Statement) res).getSubject();
t11 = ((Statement) res).getPredicate();
}
}
}
}
List<Task> success = new ArrayList<Task>();
if (!deriveSequenceOnly && t11 != null && t22 != null) {
Statement statement11 = Implication.make(t11, t22, order);
Statement statement22 = Implication.make(t22, t11, reverseOrder(order));
Statement statement33 = Equivalence.make(t11, t22, order);
if (!tooMuchTemporalStatements(statement11)) {
List<Task> t = nal.doublePremiseTask(statement11, truth1, budget1, true, false);
if (t != null) {
success.addAll(t);
}
}
if (!tooMuchTemporalStatements(statement22)) {
List<Task> t = nal.doublePremiseTask(statement22, truth2, budget2, true, false);
if (t != null) {
success.addAll(t);
}
}
if (!tooMuchTemporalStatements(statement33)) {
List<Task> t = nal.doublePremiseTask(statement33, truth3, budget3, true, false);
if (t != null) {
success.addAll(t);
}
}
}
if (!deriveSequenceOnly && !tooMuchTemporalStatements(statement1)) {
List<Task> t = nal.doublePremiseTask(statement1, truth1, budget1, true, false);
if (t != null) {
success.addAll(t);
for (Task task : t) {
// we assume here that this function is used for observable events currently
task.setObservablePrediction(true);
}
}
}
if (!deriveSequenceOnly && !tooMuchTemporalStatements(statement2)) {
List<Task> t = nal.doublePremiseTask(statement2, truth2, budget2, true, false);
if (t != null) {
success.addAll(t);
for (Task task : t) {
// we assume here that this function is used for observable events currently
task.setObservablePrediction(true);
}
}
}
if (!deriveSequenceOnly && !tooMuchTemporalStatements(statement3)) {
List<Task> t = nal.doublePremiseTask(statement3, truth3, budget3, true, false);
if (t != null) {
for (Task task : t) {
// we assume here that this function is used for observable events currently
task.setObservablePrediction(true);
}
success.addAll(t);
}
}
if (!tooMuchTemporalStatements(statement4)) {
if (!allowSequence) {
return success;
}
List<Task> tl = nal.doublePremiseTask(statement4, truth4, budget4, true, false, addToMemory);
if (tl != null) {
for (Task t : tl) {
// fill sequenceTask buffer due to the new derived sequence
if (addToMemory && t.sentence.isJudgment() && !t.sentence.isEternal() && t.sentence.term instanceof Conjunction && ((Conjunction) t.sentence.term).getTemporalOrder() != TemporalRules.ORDER_NONE && ((Conjunction) t.sentence.term).getTemporalOrder() != TemporalRules.ORDER_INVALID) {
TemporalInferenceControl.addToSequenceTasks(nal, t);
}
success.add(t);
}
}
}
return success;
}
use of nars.language.Conjunction 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.language.Conjunction in project opennars by opennars.
the class PerceptionAccel method handleConjunctionSequence.
public void handleConjunctionSequence(Term t, boolean Add) {
if (!(t instanceof Conjunction)) {
return;
}
Conjunction c = (Conjunction) t;
if (debugMechanism) {
System.out.println("handleConjunctionSequence with " + t.toString() + " " + String.valueOf(Add));
}
if (Add) {
// manage concept counter
sv[c.term.length]++;
} else {
sv[c.term.length]--;
}
// determine cur_maxlen
// by finding the first complexity which exists
// minimum size is 1 (the events itself), in which case only chaining of two will happen
cur_maxlen = 1;
for (int i = sz - 1; i >= 2; i--) {
// >=2 because a conjunction with size=1 doesnt exist
if (sv[i] > 0) {
// dont using the index 0 in sv makes it easier here
cur_maxlen = i;
break;
}
}
if (debugMechanism) {
System.out.println("determined max len is " + String.valueOf(cur_maxlen));
}
}
Aggregations