use of nars.language.Conjunction in project opennars by opennars.
the class RuleTables method compoundAndStatement.
/**
* Inference between a compound term and a statement
*
* @param compound The compound term
* @param index The location of the current term in the compound
* @param statement The statement
* @param side The location of the current term in the statement
* @param beliefTerm The content of the belief
* @param nal Reference to the memory
*/
private static void compoundAndStatement(CompoundTerm compound, short index, Statement statement, short side, Term beliefTerm, DerivationContext nal) {
if (index >= compound.term.length) {
return;
}
Term component = compound.term[index];
Task task = nal.getCurrentTask();
if (component.getClass() == statement.getClass()) {
if ((compound instanceof Conjunction) && (nal.getCurrentBelief() != null)) {
Conjunction conj = (Conjunction) compound;
Term[] u = new Term[] { compound, statement };
if (Variables.unify(VAR_DEPENDENT, component, statement, u)) {
compound = (Conjunction) u[0];
statement = (Statement) u[1];
if (// only allow dep var elimination
conj.isSpatial || compound.getTemporalOrder() != TemporalRules.ORDER_FORWARD || index == 0) {
// for (&/ on first component!!
SyllogisticRules.elimiVarDep(compound, component, statement.equals(beliefTerm), nal);
}
} else if (task.sentence.isJudgment()) {
// && !compound.containsTerm(component)) {
CompositionalRules.introVarInner(statement, (Statement) component, compound, nal);
}
}
} else {
if (task.sentence.isJudgment()) {
if (statement instanceof Inheritance) {
StructuralRules.structuralCompose1(compound, index, statement, nal);
if (!(compound instanceof SetExt || compound instanceof SetInt || compound instanceof Negation)) {
StructuralRules.structuralCompose2(compound, index, statement, side, nal);
}
// {A --> B, A @ (A&C)} |- (A&C) --> (B&C)
} else if ((statement instanceof Similarity) && !(compound instanceof Conjunction)) {
StructuralRules.structuralCompose2(compound, index, statement, side, nal);
}
// {A <-> B, A @ (A&C)} |- (A&C) <-> (B&C)
}
}
}
use of nars.language.Conjunction in project opennars by opennars.
the class StructuralRules method groupSequence.
/* --------------- Group sequence left and right --------------- */
/**
* {(#,A,B,C,D,E), C@(#,A,B,C,D,E)} |- (#,(#,A,B),C,D,E), (#,A,B,C,(#,D,E))
* Works for all conjunctions
* @param compound The premise
* @param component The recognized component in the premise
* @param compoundTask Whether the compound comes from the task
* @param nal Reference to the memory
*/
static void groupSequence(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
if (compound instanceof Conjunction) {
Conjunction conjCompound = (Conjunction) compound;
if (conjCompound.getTemporalOrder() == TemporalRules.ORDER_FORWARD) {
boolean hasLeft = index > 1;
boolean hasRight = index < compound.size() - 2;
if (hasLeft) {
// if index-1 it would have length 1, no group
int minIndex = Memory.randomNumber.nextInt(index - 1);
Term[] newTermLeft = new Term[(index - minIndex)];
System.arraycopy(conjCompound.term, minIndex, newTermLeft, minIndex - minIndex, index - minIndex);
Term contLeft = Conjunction.make(newTermLeft, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
Term[] totalLeft = new Term[conjCompound.size() - newTermLeft.length + 1];
// 1. add left of min index
int k = 0;
for (int i = 0; i < minIndex; i++) {
totalLeft[k++] = conjCompound.term[i];
}
// add formed group
totalLeft[k] = contLeft;
k += newTermLeft.length - 1;
// and add what is after
for (int i = index; i < conjCompound.size(); i++) {
totalLeft[k++] = conjCompound.term[i];
}
Term cont1 = Conjunction.make(totalLeft, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
if (cont1 instanceof Conjunction && totalLeft.length != conjCompound.size()) {
TruthValue truth = nal.getCurrentTask().sentence.truth.clone();
BudgetValue budget = BudgetFunctions.forward(truth, nal);
nal.singlePremiseTask(cont1, truth, budget);
}
}
if (hasRight) {
int maxIndex = compound.term.length - 1 - (Memory.randomNumber.nextInt(1 + (compound.term.length - 1) - (index + 2)));
Term[] newTermRight = new Term[maxIndex - index];
System.arraycopy(conjCompound.term, index + 1, newTermRight, index + 1 - (index + 1), maxIndex + 1 - (index + 1));
Term contRight = Conjunction.make(newTermRight, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
Term[] totalRight = new Term[conjCompound.size() - newTermRight.length + 1];
// 2. add left of index
int k = 0;
for (int i = 0; i <= index; i++) {
totalRight[k++] = conjCompound.term[i];
}
// add formed group
totalRight[k] = contRight;
k += newTermRight.length - 1 - 1;
// and add what is after
for (int i = maxIndex + 1; i < conjCompound.size(); i++) {
totalRight[k++] = conjCompound.term[i];
}
Term cont2 = Conjunction.make(totalRight, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
if (cont2 instanceof Conjunction && totalRight.length != conjCompound.size()) {
TruthValue truth = nal.getCurrentTask().sentence.truth.clone();
BudgetValue budget = BudgetFunctions.forward(truth, nal);
nal.singlePremiseTask(cont2, truth, budget);
}
}
}
}
}
use of nars.language.Conjunction in project opennars by opennars.
the class StructuralRules method takeOutFromConjunction.
/* --------------- Take out from conjunction --------------- */
/**
* {(&&,A,B,C), B@(&&,A,B,C)} |- (&&,A,C)
* Works for all conjunctions
* @param compound The premise
* @param component The recognized component in the premise
* @param compoundTask Whether the compound comes from the task
* @param nal Reference to the memory
*/
static void takeOutFromConjunction(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
if (compound instanceof Conjunction) {
Conjunction conjCompound = (Conjunction) compound;
Term[] newTerm = new Term[conjCompound.size() - 1];
System.arraycopy(conjCompound.term, 0, newTerm, 0, index);
System.arraycopy(conjCompound.term, index + 1, newTerm, index, newTerm.length - index);
Term cont = Conjunction.make(newTerm, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
TruthValue truth = TruthFunctions.deduction(nal.getCurrentTask().sentence.truth, Parameters.reliance);
BudgetValue budget = BudgetFunctions.forward(truth, nal);
nal.singlePremiseTask(cont, truth, budget);
}
}
use of nars.language.Conjunction in project opennars by opennars.
the class StructuralRules method splitConjunctionApart.
/* --------------- Split sequence apart --------------- */
/**
* {(#,A,B,C,D,E), C@(#,A,B,C,D,E)} |- (#,A,B,C), (#,C,D,E)
* Works for all conjunctions
* @param compound The premise
* @param component The recognized component in the premise
* @param compoundTask Whether the compound comes from the task
* @param nal Reference to the memory
*/
static void splitConjunctionApart(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
if (compound instanceof Conjunction) {
Conjunction conjCompound = (Conjunction) compound;
Term[] newTermLeft = new Term[index + 1];
Term[] newTermRight = new Term[conjCompound.size() - index];
if (// since nothing was splitted
newTermLeft.length == compound.size() || newTermRight.length == compound.size()) {
return;
}
System.arraycopy(conjCompound.term, 0, newTermLeft, 0, newTermLeft.length);
System.arraycopy(conjCompound.term, 0 + index, newTermRight, 0, newTermRight.length);
Conjunction cont1 = (Conjunction) Conjunction.make(newTermLeft, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
Conjunction cont2 = (Conjunction) Conjunction.make(newTermRight, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
TruthValue truth = TruthFunctions.deduction(nal.getCurrentTask().sentence.truth, Parameters.reliance);
BudgetValue budget = BudgetFunctions.forward(truth, nal);
nal.singlePremiseTask(cont1, truth, budget);
nal.singlePremiseTask(cont2, truth.clone(), budget.clone());
}
}
use of nars.language.Conjunction in project opennars by opennars.
the class SyllogisticRules method conditionalAbd.
/**
* {<(&&, S2, S3) ==> P>, <(&&, S1, S3) ==> P>} |- <S1 ==> S2>
*
* @param cond1 The condition of the first premise
* @param cond2 The condition of the second premise
* @param taskContent The first premise
* @param st2 The second premise
* @param nal Reference to the memory
* @return Whether there are derived tasks
*/
static boolean conditionalAbd(Term cond1, Term cond2, Statement st1, Statement st2, DerivationContext nal) {
if (!(st1 instanceof Implication) || !(st2 instanceof Implication)) {
return false;
}
if (!(cond1 instanceof Conjunction) && !(cond2 instanceof Conjunction)) {
return false;
}
int order1 = st1.getTemporalOrder();
int order2 = st2.getTemporalOrder();
if (order1 != reverseOrder(order2)) {
return false;
}
Term term1 = null;
Term term2 = null;
if (cond1 instanceof Conjunction) {
term1 = reduceComponents((CompoundTerm) cond1, cond2, nal.mem());
}
if (cond2 instanceof Conjunction) {
term2 = reduceComponents((CompoundTerm) cond2, cond1, nal.mem());
}
if ((term1 == null) && (term2 == null)) {
return false;
}
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
TruthValue value1 = sentence.truth;
TruthValue value2 = belief.truth;
Term content;
boolean keepOrder = Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, st1, task.getTerm());
TruthValue truth = null;
BudgetValue budget;
if (term1 != null) {
if (term2 != null) {
content = Statement.make(st2, term2, term1, st2.getTemporalOrder());
} else {
content = term1;
if (content.hasVarIndep()) {
return false;
}
}
if (sentence.isQuestion() || sentence.isQuest()) {
budget = BudgetFunctions.backwardWeak(value2, nal);
} else {
if (sentence.isGoal()) {
if (keepOrder) {
truth = TruthFunctions.desireDed(value1, value2);
} else {
truth = TruthFunctions.desireInd(value1, value2);
}
} else {
// isJudgment
truth = TruthFunctions.abduction(value2, value1);
}
budget = BudgetFunctions.forward(truth, nal);
}
nal.doublePremiseTask(content, truth, budget, false, false);
}
if (term2 != null) {
if (term1 != null) {
content = Statement.make(st1, term1, term2, st1.getTemporalOrder());
} else {
content = term2;
if (content.hasVarIndep()) {
return false;
}
}
if (sentence.isQuestion() || sentence.isQuest()) {
budget = BudgetFunctions.backwardWeak(value2, nal);
} else {
if (sentence.isGoal()) {
if (keepOrder) {
truth = TruthFunctions.desireDed(value1, value2);
} else {
truth = TruthFunctions.desireInd(value1, value2);
}
} else {
// isJudgment
truth = TruthFunctions.abduction(value1, value2);
}
budget = BudgetFunctions.forward(truth, nal);
}
nal.doublePremiseTask(content, truth, budget, false, false);
}
return true;
}
Aggregations