Search in sources :

Example 11 with Inheritance

use of nars.language.Inheritance 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;
}
Also used : BudgetValue(nars.entity.BudgetValue) CompoundTerm(nars.language.CompoundTerm) Task(nars.entity.Task) Variable(nars.language.Variable) HashMap(java.util.HashMap) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) ArrayList(java.util.ArrayList) Product(nars.language.Product) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Operation(nars.operator.Operation) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 12 with Inheritance

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

the class Counting method setEnabled.

@Override
public boolean setEnabled(NAR n, boolean enabled) {
    Memory memory = n.memory;
    if (obs == null) {
        obs = new EventObserver() {

            @Override
            public void event(Class event, Object[] a) {
                if ((event != Events.TaskDerive.class && event != Events.TaskAdd.class))
                    return;
                Task task = (Task) a[0];
                if (task.getPriority() < InternalExperience.MINIMUM_PRIORITY_TO_CREATE_WANT_BELIEVE_ETC) {
                    return;
                }
                if (task.sentence.punctuation == Symbols.JUDGMENT_MARK) {
                    // lets say we have <{...} --> M>.
                    if (task.sentence.term instanceof Inheritance) {
                        Inheritance inh = (Inheritance) task.sentence.term;
                        if (inh.getSubject() instanceof SetExt) {
                            SetExt set_term = (SetExt) inh.getSubject();
                            // this gets the cardinality of M
                            int cardinality = set_term.size();
                            // now create term <(*,M,cardinality) --> CARDINALITY>.
                            Term[] product_args = new Term[] { inh.getPredicate(), Term.get(cardinality) };
                            // TODO CARDINATLITY can be a static final instance shared by all
                            Term new_term = Inheritance.make(new Product(product_args), /* --> */
                            CARDINALITY);
                            if (new_term == null) {
                                // this usually happens when product_args contains the term CARDINALITY in which case it is an invalid Inheritance statement
                                return;
                            }
                            TruthValue truth = task.sentence.truth.clone();
                            Stamp stampi = task.sentence.stamp.clone();
                            Sentence j = new Sentence(new_term, Symbols.JUDGMENT_MARK, truth, stampi);
                            BudgetValue budg = task.budget.clone();
                            Task newTask = new Task(j, budg, true);
                            memory.addNewTask(newTask, "Derived (Cardinality)");
                        }
                    }
                }
            }
        };
    }
    memory.event.set(obs, enabled, Events.TaskDerive.class);
    return true;
}
Also used : BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Stamp(nars.entity.Stamp) Memory(nars.storage.Memory) Inheritance(nars.language.Inheritance) Product(nars.language.Product) Term(nars.language.Term) EventObserver(nars.io.events.EventEmitter.EventObserver) Events(nars.io.events.Events) TruthValue(nars.entity.TruthValue) SetExt(nars.language.SetExt) Sentence(nars.entity.Sentence)

Example 13 with Inheritance

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

the class CompositionalRules method composeCompound.

/* -------------------- intersections and differences -------------------- */
/**
 * {<S ==> M>, <P ==> M>} |- {<(S|P) ==> M>, <(S&P) ==> M>, <(S-P) ==>
 * M>,
 * <(P-S) ==> M>}
 *
 * @param taskSentence The first premise
 * @param belief The second premise
 * @param index The location of the shared term
 * @param nal Reference to the memory
 */
static void composeCompound(final Statement taskContent, final Statement beliefContent, final int index, final DerivationContext nal) {
    if ((!nal.getCurrentTask().sentence.isJudgment()) || (taskContent.getClass() != beliefContent.getClass())) {
        return;
    }
    final Term componentT = taskContent.term[1 - index];
    final Term componentB = beliefContent.term[1 - index];
    final Term componentCommon = taskContent.term[index];
    int order1 = taskContent.getTemporalOrder();
    int order2 = beliefContent.getTemporalOrder();
    int order = TemporalRules.composeOrder(order1, order2);
    if (order == TemporalRules.ORDER_INVALID) {
        return;
    }
    if ((componentT instanceof CompoundTerm) && ((CompoundTerm) componentT).containsAllTermsOf(componentB)) {
        decomposeCompound((CompoundTerm) componentT, componentB, componentCommon, index, true, order, nal);
        return;
    } else if ((componentB instanceof CompoundTerm) && ((CompoundTerm) componentB).containsAllTermsOf(componentT)) {
        decomposeCompound((CompoundTerm) componentB, componentT, componentCommon, index, false, order, nal);
        return;
    }
    final TruthValue truthT = nal.getCurrentTask().sentence.truth;
    final TruthValue truthB = nal.getCurrentBelief().truth;
    final TruthValue truthOr = union(truthT, truthB);
    final TruthValue truthAnd = intersection(truthT, truthB);
    TruthValue truthDif = null;
    Term termOr = null;
    Term termAnd = null;
    Term termDif = null;
    if (index == 0) {
        if (taskContent instanceof Inheritance) {
            termOr = IntersectionInt.make(componentT, componentB);
            termAnd = IntersectionExt.make(componentT, componentB);
            if (truthB.isNegative()) {
                if (!truthT.isNegative()) {
                    termDif = DifferenceExt.make(componentT, componentB);
                    truthDif = intersection(truthT, negation(truthB));
                }
            } else if (truthT.isNegative()) {
                termDif = DifferenceExt.make(componentB, componentT);
                truthDif = intersection(truthB, negation(truthT));
            }
        } else if (taskContent instanceof Implication) {
            termOr = Disjunction.make(componentT, componentB);
            termAnd = Conjunction.make(componentT, componentB);
        }
        processComposed(taskContent, componentCommon, termOr, order, truthOr, nal);
        processComposed(taskContent, componentCommon, termAnd, order, truthAnd, nal);
        processComposed(taskContent, componentCommon, termDif, order, truthDif, nal);
    } else {
        // index == 1
        if (taskContent instanceof Inheritance) {
            termOr = IntersectionExt.make(componentT, componentB);
            termAnd = IntersectionInt.make(componentT, componentB);
            if (truthB.isNegative()) {
                if (!truthT.isNegative()) {
                    termDif = DifferenceInt.make(componentT, componentB);
                    truthDif = intersection(truthT, negation(truthB));
                }
            } else if (truthT.isNegative()) {
                termDif = DifferenceInt.make(componentB, componentT);
                truthDif = intersection(truthB, negation(truthT));
            }
        } else if (taskContent instanceof Implication) {
            termOr = Conjunction.make(componentT, componentB);
            termAnd = Disjunction.make(componentT, componentB);
        }
        processComposed(taskContent, termOr, componentCommon, order, truthOr, nal);
        processComposed(taskContent, termAnd, componentCommon, order, truthAnd, nal);
        processComposed(taskContent, termDif, componentCommon, order, truthDif, nal);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication)

Example 14 with Inheritance

use of nars.language.Inheritance 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);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Variable(nars.language.Variable) Similarity(nars.language.Similarity) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 15 with Inheritance

use of nars.language.Inheritance 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);
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) Variable(nars.language.Variable) Inheritance(nars.language.Inheritance) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication)

Aggregations

Inheritance (nars.language.Inheritance)19 Term (nars.language.Term)18 CompoundTerm (nars.language.CompoundTerm)15 TruthValue (nars.entity.TruthValue)12 BudgetValue (nars.entity.BudgetValue)11 Statement (nars.language.Statement)9 Sentence (nars.entity.Sentence)8 Task (nars.entity.Task)8 Conjunction (nars.language.Conjunction)6 Implication (nars.language.Implication)6 Product (nars.language.Product)5 SetExt (nars.language.SetExt)4 Similarity (nars.language.Similarity)4 Variable (nars.language.Variable)4 Stamp (nars.entity.Stamp)3 Equivalence (nars.language.Equivalence)3 ImageExt (nars.language.ImageExt)3 ImageInt (nars.language.ImageInt)3 Interval (nars.language.Interval)3 SetInt (nars.language.SetInt)3