Search in sources :

Example 1 with ImageExt

use of nars.language.ImageExt in project opennars by opennars.

the class CompositionalRules method decomposeCompound.

/**
 * {<(S|P) ==> M>, <P ==> M>} |- <S ==> M>
 *
 * @param implication The implication term to be decomposed
 * @param componentCommon The part of the implication to be removed
 * @param term1 The other term in the contentInd
 * @param index The location of the shared term: 0 for subject, 1 for
 * predicate
 * @param compoundTask Whether the implication comes from the task
 * @param nal Reference to the memory
 */
private static void decomposeCompound(CompoundTerm compound, Term component, Term term1, int index, boolean compoundTask, int order, DerivationContext nal) {
    if ((compound instanceof Statement) || (compound instanceof ImageExt) || (compound instanceof ImageInt)) {
        return;
    }
    Term term2 = reduceComponents(compound, component, nal.mem());
    if (term2 == null) {
        return;
    }
    long delta = 0;
    while ((term2 instanceof Conjunction) && (((CompoundTerm) term2).term[0] instanceof Interval)) {
        Interval interval = (Interval) ((CompoundTerm) term2).term[0];
        delta += interval.time;
        term2 = ((CompoundTerm) term2).setComponent(0, null, nal.mem());
    }
    Task task = nal.getCurrentTask();
    Sentence sentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    Statement oldContent = (Statement) task.getTerm();
    TruthValue v1, v2;
    if (compoundTask) {
        v1 = sentence.truth;
        v2 = belief.truth;
    } else {
        v1 = belief.truth;
        v2 = sentence.truth;
    }
    TruthValue truth = null;
    Term content;
    if (index == 0) {
        content = Statement.make(oldContent, term1, term2, order);
        if (content == null) {
            return;
        }
        if (oldContent instanceof Inheritance) {
            if (compound instanceof IntersectionExt) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof IntersectionInt) {
                truth = reduceDisjunction(v1, v2);
            } else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
                truth = reduceConjunction(v1, v2);
            } else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
                truth = reduceDisjunction(v1, v2);
            } else if (compound instanceof DifferenceExt) {
                if (compound.term[0].equals(component)) {
                    truth = reduceDisjunction(v2, v1);
                } else {
                    truth = reduceConjunctionNeg(v1, v2);
                }
            }
        } else if (oldContent instanceof Implication) {
            if (compound instanceof Conjunction) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof Disjunction) {
                truth = reduceDisjunction(v1, v2);
            }
        }
    } else {
        content = Statement.make(oldContent, term2, term1, order);
        if (content == null) {
            return;
        }
        if (oldContent instanceof Inheritance) {
            if (compound instanceof IntersectionInt) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof IntersectionExt) {
                truth = reduceDisjunction(v1, v2);
            } else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
                truth = reduceConjunction(v1, v2);
            } else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
                truth = reduceDisjunction(v1, v2);
            } else if (compound instanceof DifferenceInt) {
                if (compound.term[1].equals(component)) {
                    truth = reduceDisjunction(v2, v1);
                } else {
                    truth = reduceConjunctionNeg(v1, v2);
                }
            }
        } else if (oldContent instanceof Implication) {
            if (compound instanceof Disjunction) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof Conjunction) {
                truth = reduceDisjunction(v1, v2);
            }
        }
    }
    if (truth != null) {
        BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
        if (delta != 0) {
            long baseTime = task.sentence.getOccurenceTime();
            if (baseTime != Stamp.ETERNAL) {
                baseTime += delta;
                nal.getTheNewStamp().setOccurrenceTime(baseTime);
            }
        }
        // (allow overlap), a form of detachment
        nal.doublePremiseTask(content, truth, budget, false, true);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) ImageInt(nars.language.ImageInt) SetInt(nars.language.SetInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) IntersectionInt(nars.language.IntersectionInt) Implication(nars.language.Implication) DifferenceInt(nars.language.DifferenceInt) Disjunction(nars.language.Disjunction) TruthFunctions.reduceDisjunction(nars.inference.TruthFunctions.reduceDisjunction) IntersectionExt(nars.language.IntersectionExt) TruthValue(nars.entity.TruthValue) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) SetExt(nars.language.SetExt) ImageExt(nars.language.ImageExt) DifferenceExt(nars.language.DifferenceExt) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 2 with ImageExt

use of nars.language.ImageExt 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);
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Variable(nars.language.Variable) HashMap(java.util.HashMap) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) ImageInt(nars.language.ImageInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) ImageExt(nars.language.ImageExt)

Example 3 with ImageExt

use of nars.language.ImageExt in project opennars by opennars.

the class StructuralRules method transformPredicatePI.

/**
 * Equivalent transformation between products and images when the predicate
 * is a compound {<(*, S, M) --> P>, S@(*, S, M)} |- <S --> (/, P, _, M)>
 * {<S --> (/, P, _, M)>, P@(/, P, _, M)} |- <(*, S, M) --> P> {<S --> (/,
 * P, _, M)>, M@(/, P, _, M)} |- <M --> (/, P, S, _)>
 *
 * @param subject The subject term
 * @param predicate The predicate term
 * @param nal Reference to the memory
 */
private static void transformPredicatePI(short index, Term subject, CompoundTerm predicate, DerivationContext nal) {
    TruthValue truth = nal.getCurrentTask().sentence.truth;
    BudgetValue budget;
    Inheritance inheritance;
    Term newSubj, newPred;
    if (predicate instanceof Product) {
        Product product = (Product) predicate;
        short i = index;
        /*for (short i = 0; i < product.size(); i++)*/
        {
            newSubj = ImageInt.make(product, subject, i);
            newPred = product.term[i];
            inheritance = Inheritance.make(newSubj, newPred);
            if (inheritance != null) {
                if (truth == null) {
                    budget = BudgetFunctions.compoundBackward(inheritance, nal);
                } else {
                    budget = BudgetFunctions.compoundForward(truth, inheritance, nal);
                }
                nal.singlePremiseTask(inheritance, truth, budget);
            }
        }
    } else if (predicate instanceof ImageExt) {
        ImageExt image = (ImageExt) predicate;
        int relationIndex = image.relationIndex;
        for (short i = 0; i < image.size(); i++) {
            if (i == relationIndex) {
                newSubj = Product.make(image, subject, relationIndex);
                newPred = image.term[relationIndex];
            } else {
                newSubj = image.term[i];
                newPred = ImageExt.make(image, subject, i);
            }
            if (newSubj instanceof CompoundTerm && (newPred.equals(Term.SEQ_TEMPORAL) || newPred.equals(Term.SEQ_SPATIAL))) {
                Term seq = Conjunction.make(((CompoundTerm) newSubj).term, TemporalRules.ORDER_FORWARD, newPred.equals(Term.SEQ_SPATIAL));
                if (truth == null) {
                    budget = BudgetFunctions.compoundBackward(seq, nal);
                } else {
                    budget = BudgetFunctions.compoundForward(truth, seq, nal);
                }
                nal.singlePremiseTask(seq, truth, budget);
                return;
            }
            inheritance = Inheritance.make(newSubj, newPred);
            if (inheritance != null) {
                // jmv <<<<<
                if (truth == null) {
                    budget = BudgetFunctions.compoundBackward(inheritance, nal);
                } else {
                    budget = BudgetFunctions.compoundForward(truth, inheritance, nal);
                }
                nal.singlePremiseTask(inheritance, truth, budget);
            }
        }
    }
}
Also used : BudgetValue(nars.entity.BudgetValue) CompoundTerm(nars.language.CompoundTerm) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) Product(nars.language.Product) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) ImageExt(nars.language.ImageExt)

Example 4 with ImageExt

use of nars.language.ImageExt in project opennars by opennars.

the class StructuralRules method transformProductImage.

/* -------------------- products and images transform -------------------- */
/**
 * Equivalent transformation between products and images {<(*, S, M) --> P>,
 * S@(*, S, M)} |- <S --> (/, P, _, M)> {<S --> (/, P, _, M)>, P@(/, P, _,
 * M)} |- <(*, S, M) --> P> {<S --> (/, P, _, M)>, M@(/, P, _, M)} |- <M -->
 * (/, P, S, _)>
 *
 * @param inh An Inheritance statement
 * @param oldContent The whole content
 * @param indices The indices of the TaskLink
 * @param task The task
 * @param memory Reference to the memory
 */
static void transformProductImage(Inheritance inh, CompoundTerm oldContent, short[] indices, DerivationContext nal) {
    final Memory memory = nal.mem();
    Term subject = inh.getSubject();
    Term predicate = inh.getPredicate();
    short index = indices[indices.length - 1];
    short side = indices[indices.length - 2];
    if (inh.equals(oldContent)) {
        if (subject instanceof CompoundTerm) {
            transformSubjectPI(index, (CompoundTerm) subject, predicate, nal);
        }
        if (predicate instanceof CompoundTerm) {
            transformPredicatePI(index, subject, (CompoundTerm) predicate, nal);
        }
        return;
    }
    Term compT = inh.term[side];
    if (!(compT instanceof CompoundTerm))
        return;
    CompoundTerm comp = (CompoundTerm) compT;
    if (comp instanceof Product) {
        if (side == 0) {
            subject = comp.term[index];
            predicate = ImageExt.make((Product) comp, inh.getPredicate(), index);
        } else {
            subject = ImageInt.make((Product) comp, inh.getSubject(), index);
            predicate = comp.term[index];
        }
    } else if ((comp instanceof ImageExt) && (side == 1)) {
        if (index == ((ImageExt) comp).relationIndex) {
            subject = Product.make(comp, inh.getSubject(), index);
            predicate = comp.term[index];
        } else {
            subject = comp.term[index];
            predicate = ImageExt.make((ImageExt) comp, inh.getSubject(), index);
        }
    } else if ((comp instanceof ImageInt) && (side == 0)) {
        if (index == ((ImageInt) comp).relationIndex) {
            subject = comp.term[index];
            predicate = Product.make(comp, inh.getPredicate(), index);
        } else {
            subject = ImageInt.make((ImageInt) comp, inh.getPredicate(), index);
            predicate = comp.term[index];
        }
    } else {
        return;
    }
    CompoundTerm newInh = null;
    if (predicate.equals(Term.SEQ_SPATIAL)) {
        newInh = (CompoundTerm) Conjunction.make(((CompoundTerm) subject).term, TemporalRules.ORDER_FORWARD, true);
    } else if (predicate.equals(Term.SEQ_TEMPORAL)) {
        newInh = (CompoundTerm) Conjunction.make(((CompoundTerm) subject).term, TemporalRules.ORDER_FORWARD, false);
    } else {
        newInh = Inheritance.make(subject, predicate);
    }
    if (newInh == null)
        return;
    CompoundTerm content = null;
    if (indices.length == 2) {
        content = newInh;
    } else if ((oldContent instanceof Statement) && (indices[0] == 1)) {
        content = Statement.make((Statement) oldContent, oldContent.term[0], newInh, oldContent.getTemporalOrder());
    } else {
        Term[] componentList;
        Term condition = oldContent.term[0];
        if (((oldContent instanceof Implication) || (oldContent instanceof Equivalence)) && (condition instanceof Conjunction)) {
            componentList = ((CompoundTerm) condition).cloneTerms();
            componentList[indices[1]] = newInh;
            Term newCond = Terms.term((CompoundTerm) condition, componentList);
            content = Statement.make((Statement) oldContent, newCond, ((Statement) oldContent).getPredicate(), oldContent.getTemporalOrder());
        } else {
            componentList = oldContent.cloneTerms();
            componentList[indices[0]] = newInh;
            if (oldContent instanceof Conjunction) {
                Term newContent = Terms.term(oldContent, componentList);
                if (!(newContent instanceof CompoundTerm))
                    return;
                content = (CompoundTerm) newContent;
            } else if ((oldContent instanceof Implication) || (oldContent instanceof Equivalence)) {
                content = Statement.make((Statement) oldContent, componentList[0], componentList[1], oldContent.getTemporalOrder());
            }
        }
    }
    if (content == null)
        return;
    Sentence sentence = nal.getCurrentTask().sentence;
    TruthValue truth = sentence.truth;
    BudgetValue budget;
    if (sentence.isQuestion() || sentence.isQuest()) {
        budget = BudgetFunctions.compoundBackward(content, nal);
    } else {
        budget = BudgetFunctions.compoundForward(truth, content, nal);
    }
    nal.singlePremiseTask(content, truth, budget);
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Memory(nars.storage.Memory) Statement(nars.language.Statement) Product(nars.language.Product) ImageInt(nars.language.ImageInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication) Equivalence(nars.language.Equivalence) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) ImageExt(nars.language.ImageExt) Sentence(nars.entity.Sentence)

Aggregations

BudgetValue (nars.entity.BudgetValue)4 TruthValue (nars.entity.TruthValue)4 CompoundTerm (nars.language.CompoundTerm)4 ImageExt (nars.language.ImageExt)4 Term (nars.language.Term)4 ImageInt (nars.language.ImageInt)3 Inheritance (nars.language.Inheritance)3 Statement (nars.language.Statement)3 Sentence (nars.entity.Sentence)2 Conjunction (nars.language.Conjunction)2 Implication (nars.language.Implication)2 Product (nars.language.Product)2 HashMap (java.util.HashMap)1 Task (nars.entity.Task)1 TruthFunctions.reduceConjunction (nars.inference.TruthFunctions.reduceConjunction)1 TruthFunctions.reduceDisjunction (nars.inference.TruthFunctions.reduceDisjunction)1 DifferenceExt (nars.language.DifferenceExt)1 DifferenceInt (nars.language.DifferenceInt)1 Disjunction (nars.language.Disjunction)1 Equivalence (nars.language.Equivalence)1