use of nars.language.Variable in project opennars by opennars.
the class CompositionalRules method introVarInner.
/**
* {<M --> S>, <C ==> <M --> P>>} |- <(&&, <#x --> S>, C) ==> <#x --> P>>
* {<M --> S>, (&&, C, <M --> P>)} |- (&&, C, <<#x --> S> ==> <#x --> P>>)
*
* @param taskContent The first premise directly used in internal induction,
* <M --> S>
* @param beliefContent The componentCommon to be used as a premise in
* internal induction, <M --> P>
* @param oldCompound The whole contentInd of the first premise, Implication
* or Conjunction
* @param nal Reference to the memory
*/
static boolean introVarInner(Statement premise1, Statement premise2, CompoundTerm oldCompound, DerivationContext nal) {
Task task = nal.getCurrentTask();
Sentence taskSentence = task.sentence;
if (!taskSentence.isJudgment() || (premise1.getClass() != premise2.getClass()) || oldCompound.containsTerm(premise1)) {
return false;
}
Term subject1 = premise1.getSubject();
Term subject2 = premise2.getSubject();
Term predicate1 = premise1.getPredicate();
Term predicate2 = premise2.getPredicate();
Term commonTerm1, commonTerm2;
if (subject1.equals(subject2)) {
commonTerm1 = subject1;
commonTerm2 = secondCommonTerm(predicate1, predicate2, 0);
} else if (predicate1.equals(predicate2)) {
commonTerm1 = predicate1;
commonTerm2 = secondCommonTerm(subject1, subject2, 0);
} else {
return false;
}
Sentence belief = nal.getCurrentBelief();
HashMap<Term, Term> substitute = new HashMap<>();
boolean b1 = false, b2 = false;
{
Variable varDep2 = new Variable("#varDep2");
Term content = Conjunction.make(premise1, oldCompound);
if (!(content instanceof CompoundTerm))
return false;
substitute.put(commonTerm1, varDep2);
content = ((CompoundTerm) content).applySubstitute(substitute);
TruthValue truth = intersection(taskSentence.truth, belief.truth);
BudgetValue budget = BudgetFunctions.forward(truth, nal);
b1 = (nal.doublePremiseTask(content, truth, budget, false, false)) != null;
}
substitute.clear();
{
Variable varInd1 = new Variable("$varInd1");
Variable varInd2 = new Variable("$varInd2");
substitute.put(commonTerm1, varInd1);
if (commonTerm2 != null) {
substitute.put(commonTerm2, varInd2);
}
Term content = Implication.make(premise1, oldCompound);
if ((content == null) || (!(content instanceof CompoundTerm))) {
return false;
}
content = ((CompoundTerm) content).applySubstituteToCompound(substitute);
TruthValue truth;
if (premise1.equals(taskSentence.term)) {
truth = induction(belief.truth, taskSentence.truth);
} else {
truth = induction(taskSentence.truth, belief.truth);
}
BudgetValue budget = BudgetFunctions.forward(truth, nal);
b2 = nal.doublePremiseTask(content, truth, budget, false, false) != null;
}
return b1 || b2;
}
use of nars.language.Variable 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);
}
use of nars.language.Variable 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.Variable in project opennars by opennars.
the class DerivationContext method doublePremiseTask.
public List<Task> doublePremiseTask(final Term newContent, final TruthValue newTruth, final BudgetValue newBudget, boolean temporalInduction, boolean overlapAllowed, boolean addToMemory) {
List<Task> ret = new ArrayList<Task>();
if (newContent == null) {
return null;
}
if (!newBudget.aboveThreshold()) {
return null;
}
if ((newContent != null) && (!(newContent instanceof Interval)) && (!(newContent instanceof Variable))) {
if (newContent.subjectOrPredicateIsIndependentVar()) {
return null;
}
// because occurrence time will be reset:
Stamp derive_stamp = getTheNewStamp().clone();
// stamp was already obsorbed into task
this.resetOccurrenceTime();
try {
final Sentence newSentence = new Sentence(newContent, getCurrentTask().sentence.punctuation, newTruth, derive_stamp);
newSentence.producedByTemporalInduction = temporalInduction;
final Task newTask = Task.make(newSentence, newBudget, getCurrentTask(), getCurrentBelief());
if (newTask != null) {
boolean added = derivedTask(newTask, false, false, overlapAllowed, addToMemory);
if (added) {
ret.add(newTask);
}
}
} catch (CompoundTerm.UnableToCloneException e) {
return null;
}
// "Since in principle it is always valid to eternalize a tensed belief"
if (temporalInduction && Parameters.IMMEDIATE_ETERNALIZATION) {
try {
TruthValue truthEt = TruthFunctions.eternalize(newTruth);
Stamp st = derive_stamp.clone();
st.setEternal();
final Sentence newSentence = new Sentence(newContent, getCurrentTask().sentence.punctuation, truthEt, st);
newSentence.producedByTemporalInduction = temporalInduction;
final Task newTask = Task.make(newSentence, newBudget, getCurrentTask(), getCurrentBelief());
if (newTask != null) {
boolean added = derivedTask(newTask, false, false, overlapAllowed, addToMemory);
if (added) {
ret.add(newTask);
}
}
} catch (CompoundTerm.UnableToCloneException e) {
return null;
}
}
return ret;
}
return null;
}
use of nars.language.Variable in project opennars by opennars.
the class FunctionOperator method execute.
// abstract protected int getMinArity();
// abstract protected int getMaxArity();
@Override
protected ArrayList<Task> execute(Operation operation, Term[] args, Memory m) {
// TODO make memory access optional by constructor argument
// TODO allow access to NAR instance?
int numArgs = args.length - 1;
if (numArgs < 1) {
throw new RuntimeException("Requires at least 1 arguments");
}
if (numArgs < 2) /*&& !(this instanceof Javascript)*/
{
throw new RuntimeException("Requires at least 2 arguments");
}
// last argument a variable?
Term lastTerm = args[numArgs];
boolean variable = lastTerm instanceof Variable;
if (!variable) /*&& !(this instanceof Javascript)*/
{
throw new RuntimeException("output can not be specified");
}
int numParam = numArgs - 1;
/*if(this instanceof Javascript && !variable) {
numParam++;
}*/
Term[] x = new Term[numParam];
System.arraycopy(args, 1, x, 0, numParam);
Term y;
// try {
y = function(m, x);
if (y == null) {
return null;
}
/*if(!variable && this instanceof Javascript) {
return null;
}*/
// m.emit(SynchronousFunctionOperator.class, Arrays.toString(x) + " | " + y);
/*}
catch (Exception e) {
throw e;
}*/
Variable var = new Variable("$1");
// Term actual_part = Similarity.make(var, y);
// Variable vardep=new Variable("#1");
// Term actual_dep_part = Similarity.make(vardep, y);
operation = (Operation) operation.setComponent(0, ((CompoundTerm) operation.getSubject()).setComponent(numArgs, y, m), m);
float confidence = Parameters.DEFAULT_JUDGMENT_CONFIDENCE;
if (variable) {
Sentence s = new Sentence(operation, Symbols.JUDGMENT_MARK, new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(m));
return Lists.newArrayList(new Task(s, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_FEEDBACK_DURABILITY, truthToQuality(s.getTruth())), true));
} else {
return null;
}
}
Aggregations