use of nars.language.CompoundTerm in project opennars by opennars.
the class RuleTables method goalFromQuestion.
private static void goalFromQuestion(final Task task, final Term taskTerm, final DerivationContext nal) {
if (task.sentence.punctuation == Symbols.QUESTION_MARK && (taskTerm instanceof Implication || taskTerm instanceof Equivalence)) {
// <a =/> b>? |- a!
Term goalterm = null;
Term goalterm2 = null;
if (taskTerm instanceof Implication) {
Implication imp = (Implication) taskTerm;
if (imp.getTemporalOrder() != TemporalRules.ORDER_BACKWARD || imp.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT) {
if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getSubject() instanceof Operation) {
goalterm = imp.getSubject();
}
if (goalterm instanceof Variable && goalterm.hasVarQuery() && (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getPredicate() instanceof Operation)) {
// overwrite, it is a how question, in case of <?how =/> b> it is b! which is desired
goalterm = imp.getPredicate();
}
} else if (imp.getTemporalOrder() == TemporalRules.ORDER_BACKWARD) {
if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getPredicate() instanceof Operation) {
goalterm = imp.getPredicate();
}
if (goalterm instanceof Variable && goalterm.hasVarQuery() && (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getSubject() instanceof Operation)) {
// overwrite, it is a how question, in case of <?how =/> b> it is b! which is desired
goalterm = imp.getSubject();
}
}
} else if (taskTerm instanceof Equivalence) {
Equivalence qu = (Equivalence) taskTerm;
if (qu.getTemporalOrder() == TemporalRules.ORDER_FORWARD || qu.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT) {
if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || qu.getSubject() instanceof Operation) {
goalterm = qu.getSubject();
}
if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || qu.getPredicate() instanceof Operation) {
goalterm2 = qu.getPredicate();
}
}
}
TruthValue truth = new TruthValue(1.0f, Parameters.DEFAULT_GOAL_CONFIDENCE * Parameters.CURIOSITY_DESIRE_CONFIDENCE_MUL);
if (goalterm != null && !(goalterm instanceof Variable) && goalterm instanceof CompoundTerm) {
goalterm = ((CompoundTerm) goalterm).transformIndependentVariableToDependentVar((CompoundTerm) goalterm);
Sentence sent = new Sentence(goalterm, Symbols.GOAL_MARK, truth, new Stamp(task.sentence.stamp, nal.memory.time()));
nal.singlePremiseTask(sent, new BudgetValue(task.getPriority() * Parameters.CURIOSITY_DESIRE_PRIORITY_MUL, task.getDurability() * Parameters.CURIOSITY_DESIRE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth)));
}
if (goalterm2 != null && !(goalterm2 instanceof Variable) && goalterm2 instanceof CompoundTerm) {
goalterm2 = ((CompoundTerm) goalterm).transformIndependentVariableToDependentVar((CompoundTerm) goalterm2);
Sentence sent = new Sentence(goalterm2, Symbols.GOAL_MARK, truth.clone(), new Stamp(task.sentence.stamp, nal.memory.time()));
nal.singlePremiseTask(sent, new BudgetValue(task.getPriority() * Parameters.CURIOSITY_DESIRE_PRIORITY_MUL, task.getDurability() * Parameters.CURIOSITY_DESIRE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth)));
}
}
}
use of nars.language.CompoundTerm 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.CompoundTerm in project opennars by opennars.
the class StructuralRules method transformSetRelation.
/* -------------------- set transform -------------------- */
/**
* {<S --> {P}>} |- <S <-> {P}>
*
* @param compound The set compound
* @param statement The premise
* @param side The location of the indicated term in the premise
* @param nal Reference to the memory
*/
static void transformSetRelation(CompoundTerm compound, Statement statement, short side, DerivationContext nal) {
if (compound.size() > 1) {
return;
}
if (statement instanceof Inheritance) {
if (((compound instanceof SetExt) && (side == 0)) || ((compound instanceof SetInt) && (side == 1))) {
return;
}
}
Term sub = statement.getSubject();
Term pre = statement.getPredicate();
Statement content;
if (statement instanceof Inheritance) {
content = Similarity.make(sub, pre);
} else {
if (((compound instanceof SetExt) && (side == 0)) || ((compound instanceof SetInt) && (side == 1))) {
content = Inheritance.make(pre, sub);
} else {
content = Inheritance.make(sub, pre);
}
}
if (content == null) {
return;
}
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
TruthValue truth = sentence.truth;
BudgetValue budget;
if (sentence.isJudgment()) {
budget = BudgetFunctions.compoundForward(truth, content, nal);
} else {
budget = BudgetFunctions.compoundBackward(content, nal);
}
nal.singlePremiseTask(content, truth, budget);
}
use of nars.language.CompoundTerm in project opennars by opennars.
the class StructuralRules method structuralDecompose1.
/**
* {<(S|T) --> P>, S@(S|T)} |- <S --> P> {<S --> (P&T)>, P@(P&T)} |- <S --> P>
*
* @param compound The compound term
* @param index The location of the indicated term in the compound
* @param statement The premise
* @param nal Reference to the memory
*/
static void structuralDecompose1(CompoundTerm compound, short index, Statement statement, DerivationContext nal) {
if (index >= compound.term.length) {
return;
}
Term component = compound.term[index];
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
int order = sentence.getTemporalOrder();
TruthValue truth = sentence.truth;
if (truth == null) {
return;
}
final float reliance = Parameters.reliance;
TruthValue truthDed = TruthFunctions.deduction(truth, reliance);
TruthValue truthNDed = TruthFunctions.negation(TruthFunctions.deduction(truth, reliance));
Term subj = statement.getSubject();
Term pred = statement.getPredicate();
if (compound.equals(subj)) {
if (compound instanceof IntersectionInt) {
structuralStatement(component, pred, order, truthDed, nal);
} else if ((compound instanceof SetExt) && (compound.size() > 1)) {
Term[] t1 = new Term[] { component };
structuralStatement(new SetExt(t1), pred, order, truthDed, nal);
} else if (compound instanceof DifferenceInt) {
if (index == 0) {
structuralStatement(component, pred, order, truthDed, nal);
} else {
structuralStatement(component, pred, order, truthNDed, nal);
}
}
} else if (compound.equals(pred)) {
if (compound instanceof IntersectionExt) {
structuralStatement(subj, component, order, truthDed, nal);
} else if ((compound instanceof SetInt) && (compound.size() > 1)) {
structuralStatement(subj, new SetInt(component), order, truthDed, nal);
} else if (compound instanceof DifferenceExt) {
if (index == 0) {
structuralStatement(subj, component, order, truthDed, nal);
} else {
structuralStatement(subj, component, order, truthNDed, nal);
}
}
}
}
use of nars.language.CompoundTerm in project opennars by opennars.
the class StructuralRules method structuralCompose2.
/* -------------------- transform between compounds and term -------------------- */
/**
* {<S --> P>, S@(S&T)} |- <(S&T) --> (P&T)> {<S --> P>, S@(M-S)} |- <(M-P)
* --> (M-S)>
*
* @param compound The compound term
* @param index The location of the indicated term in the compound
* @param statement The premise
* @param side The location of the indicated term in the premise
* @param nal Reference to the memory
*/
static void structuralCompose2(CompoundTerm compound, short index, Statement statement, short side, DerivationContext nal) {
if (compound.equals(statement.term[side])) {
return;
}
Term sub = statement.getSubject();
Term pred = statement.getPredicate();
List<Term> components = compound.asTermList();
if (((side == 0) && components.contains(pred)) || ((side == 1) && components.contains(sub))) {
return;
}
if (side == 0) {
if (components.contains(sub)) {
sub = compound;
components.set(index, pred);
pred = Terms.term(compound, components);
}
} else {
if (components.contains(pred)) {
components.set(index, sub);
sub = Terms.term(compound, components);
pred = compound;
}
}
if ((sub == null) || (pred == null))
return;
Statement content;
int order = statement.getTemporalOrder();
if (switchOrder(compound, index)) {
content = Statement.make(statement, pred, sub, TemporalRules.reverseOrder(order));
} else {
content = Statement.make(statement, sub, pred, order);
}
if (content == null)
return;
Sentence sentence = nal.getCurrentTask().sentence;
TruthValue truth = TruthFunctions.deduction(sentence.truth, Parameters.reliance);
BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.singlePremiseTask(content, truth, budget);
}
Aggregations