use of nars.language.Conjunction 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);
}
}
use of nars.language.Conjunction in project opennars by opennars.
the class RuleTables method conditionalDedIndWithVar.
/**
* Conditional deduction or induction, with variable unification
*
* @param conditional The premise that is an Implication with a Conjunction
* as condition
* @param index The location of the shared term in the condition
* @param statement The second premise that is a statement
* @param side The location of the shared term in the statement
* @param nal Reference to the memory
*/
private static void conditionalDedIndWithVar(Sentence conditionalSentence, Implication conditional, short index, Statement statement, short side, DerivationContext nal) {
if (!(conditional.getSubject() instanceof CompoundTerm))
return;
CompoundTerm condition = (CompoundTerm) conditional.getSubject();
if (condition instanceof Conjunction) {
// conditionalDedIndWithVar
for (Term t : condition.term) {
// does not support the case where
if (t instanceof Variable) {
// (this can happen since we have # due to image transform,
return;
}
// although not for other conjunctions)
}
}
Term component = condition.term[index];
Term component2 = null;
if (statement instanceof Inheritance) {
component2 = statement;
side = -1;
} else if (statement instanceof Implication) {
component2 = statement.term[side];
}
if (component2 != null) {
Term[] u = new Term[] { conditional, statement };
if (Variables.unify(VAR_INDEPENDENT, component, component2, u)) {
conditional = (Implication) u[0];
statement = (Statement) u[1];
SyllogisticRules.conditionalDedInd(conditionalSentence, conditional, index, statement, side, nal);
}
}
}
use of nars.language.Conjunction in project opennars by opennars.
the class RuleTables method transformTask.
/* ----- inference with one TaskLink only ----- */
/**
* The TaskLink is of type TRANSFORM, and the conclusion is an equivalent
* transformation
*
* @param tLink The task link
* @param nal Reference to the memory
*/
public static void transformTask(TaskLink tLink, DerivationContext nal) {
CompoundTerm content = (CompoundTerm) nal.getCurrentTask().getTerm();
short[] indices = tLink.index;
Term inh = null;
if ((indices.length == 2) || (content instanceof Inheritance)) {
// <(*, term, #) --> #>
inh = content;
} else if (indices.length == 3) {
// <<(*, term, #) --> #> ==> #>
inh = content.term[indices[0]];
} else if (indices.length == 4) {
// <(&&, <(*, term, #) --> #>, #) ==> #>
Term component = content.term[indices[0]];
if ((component instanceof Conjunction) && (((content instanceof Implication) && (indices[0] == 0)) || (content instanceof Equivalence))) {
Term[] cterms = ((CompoundTerm) component).term;
if (indices[1] < cterms.length - 1) {
inh = cterms[indices[1]];
} else {
return;
}
} else {
return;
}
}
if (inh instanceof Inheritance) {
StructuralRules.transformProductImage((Inheritance) inh, content, indices, nal);
}
}
use of nars.language.Conjunction in project opennars by opennars.
the class StructuralRules method flattenSequence.
/* --------------- Flatten sequence transform --------------- */
/**
* {(#,(#,A,B),C), (#,A,B)@(#,(#,A,B), C)} |- (#,A,B,C)
* (same for &/)
* @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 flattenSequence(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
if (compound instanceof Conjunction && component instanceof Conjunction) {
Conjunction conjCompound = (Conjunction) compound;
Conjunction conjComponent = (Conjunction) component;
if (// since parallel conjunction and normal one already is flattened
conjCompound.getTemporalOrder() == TemporalRules.ORDER_FORWARD && conjComponent.getTemporalOrder() == TemporalRules.ORDER_FORWARD && conjCompound.getIsSpatial() == conjComponent.getIsSpatial()) {
// because also when both are tmporal
Term[] newTerm = new Term[conjCompound.size() - 1 + conjComponent.size()];
System.arraycopy(conjCompound.term, 0, newTerm, 0, index);
System.arraycopy(conjComponent.term, 0, newTerm, index + 0, conjComponent.size());
System.arraycopy(conjCompound.term, index + conjComponent.size() - conjComponent.size() + 1, newTerm, index + conjComponent.size(), newTerm.length - (index + conjComponent.size()));
Conjunction cont = (Conjunction) Conjunction.make(newTerm, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
TruthValue truth = nal.getCurrentTask().sentence.truth.clone();
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 structuralCompound.
/* --------------- Disjunction and Conjunction transform --------------- */
/**
* {(&&, A, B), A@(&&, A, B)} |- A, or answer (&&, A, B)? using A {(||, A,
* B), A@(||, A, B)} |- A, or answer (||, A, B)? using A
*
* @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 boolean structuralCompound(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
if (compound instanceof Conjunction) {
if (nal.getCurrentTask().getTerm() == compound) {
// only for # for now, will be gradually applied to &/ later
Conjunction conj = (Conjunction) compound;
if (conj.getTemporalOrder() == TemporalRules.ORDER_FORWARD && conj.isSpatial) {
// and some also to && &|
// flattenSequence(compound, component, compoundTask, index, nal);
groupSequence(compound, component, compoundTask, index, nal);
// takeOutFromConjunction(compound, component, compoundTask, index, nal);
splitConjunctionApart(compound, component, compoundTask, index, nal);
}
if (conj.getTemporalOrder() == TemporalRules.ORDER_FORWARD) {
seqToImage(conj, index, nal);
}
}
}
if (component.hasVarIndep()) {
// moved down here since flattening also works when indep
return false;
}
// and also for &/ with index > 0
if ((compound instanceof Conjunction) && !compound.getIsSpatial() && (compound.getTemporalOrder() == TemporalRules.ORDER_FORWARD) && (index != 0)) {
return false;
}
final Term content = compoundTask ? component : compound;
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
TruthValue truth = sentence.truth;
final float reliance = Parameters.reliance;
BudgetValue budget;
if (sentence.isQuestion() || sentence.isQuest()) {
budget = BudgetFunctions.compoundBackward(content, nal);
} else {
// [03:25] <patham9> <a --> b>. (&&,<a --> b>,<x --> y>). => <x --> y>
if ((sentence.isJudgment() || sentence.isGoal()) && ((!compoundTask && compound instanceof Disjunction) || (compoundTask && compound instanceof Conjunction))) {
truth = TruthFunctions.deduction(truth, reliance);
} else {
TruthValue v1, v2;
v1 = TruthFunctions.negation(truth);
v2 = TruthFunctions.deduction(v1, reliance);
truth = TruthFunctions.negation(v2);
}
budget = BudgetFunctions.forward(truth, nal);
}
return nal.singlePremiseTask(content, truth, budget);
}
Aggregations