Search in sources :

Example 1 with SetExt

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

the class Count method function.

@Override
protected Term function(Memory memory, Term[] x) {
    if (x.length != 1) {
        throw new RuntimeException(requireMessage);
    }
    Term content = x[0];
    if (!(content instanceof SetExt) && !(content instanceof SetInt)) {
        throw new RuntimeException(requireMessage);
    }
    int n = ((CompoundTerm) content).size();
    return Term.get(n);
}
Also used : CompoundTerm(nars.language.CompoundTerm) SetExt(nars.language.SetExt) SetInt(nars.language.SetInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 2 with SetExt

use of nars.language.SetExt 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 3 with SetExt

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

the class RuleTables method compoundAndStatement.

/**
 * Inference between a compound term and a statement
 *
 * @param compound The compound term
 * @param index The location of the current term in the compound
 * @param statement The statement
 * @param side The location of the current term in the statement
 * @param beliefTerm The content of the belief
 * @param nal Reference to the memory
 */
private static void compoundAndStatement(CompoundTerm compound, short index, Statement statement, short side, Term beliefTerm, DerivationContext nal) {
    if (index >= compound.term.length) {
        return;
    }
    Term component = compound.term[index];
    Task task = nal.getCurrentTask();
    if (component.getClass() == statement.getClass()) {
        if ((compound instanceof Conjunction) && (nal.getCurrentBelief() != null)) {
            Conjunction conj = (Conjunction) compound;
            Term[] u = new Term[] { compound, statement };
            if (Variables.unify(VAR_DEPENDENT, component, statement, u)) {
                compound = (Conjunction) u[0];
                statement = (Statement) u[1];
                if (// only allow dep var elimination
                conj.isSpatial || compound.getTemporalOrder() != TemporalRules.ORDER_FORWARD || index == 0) {
                    // for (&/ on first component!!
                    SyllogisticRules.elimiVarDep(compound, component, statement.equals(beliefTerm), nal);
                }
            } else if (task.sentence.isJudgment()) {
                // && !compound.containsTerm(component)) {
                CompositionalRules.introVarInner(statement, (Statement) component, compound, nal);
            }
        }
    } else {
        if (task.sentence.isJudgment()) {
            if (statement instanceof Inheritance) {
                StructuralRules.structuralCompose1(compound, index, statement, nal);
                if (!(compound instanceof SetExt || compound instanceof SetInt || compound instanceof Negation)) {
                    StructuralRules.structuralCompose2(compound, index, statement, side, nal);
                }
            // {A --> B, A @ (A&C)} |- (A&C) --> (B&C)
            } else if ((statement instanceof Similarity) && !(compound instanceof Conjunction)) {
                StructuralRules.structuralCompose2(compound, index, statement, side, nal);
            }
        // {A <-> B, A @ (A&C)} |- (A&C) <-> (B&C)
        }
    }
}
Also used : Task(nars.entity.Task) Negation(nars.language.Negation) Similarity(nars.language.Similarity) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) Conjunction(nars.language.Conjunction) SetExt(nars.language.SetExt) SetInt(nars.language.SetInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 4 with SetExt

use of nars.language.SetExt 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 5 with SetExt

use of nars.language.SetExt 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);
}
Also used : BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) SetExt(nars.language.SetExt) SetInt(nars.language.SetInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Sentence(nars.entity.Sentence)

Aggregations

SetExt (nars.language.SetExt)7 Term (nars.language.Term)7 Task (nars.entity.Task)6 CompoundTerm (nars.language.CompoundTerm)6 SetInt (nars.language.SetInt)6 Sentence (nars.entity.Sentence)5 TruthValue (nars.entity.TruthValue)5 BudgetValue (nars.entity.BudgetValue)4 Inheritance (nars.language.Inheritance)4 Statement (nars.language.Statement)4 Conjunction (nars.language.Conjunction)2 DifferenceExt (nars.language.DifferenceExt)2 DifferenceInt (nars.language.DifferenceInt)2 IntersectionExt (nars.language.IntersectionExt)2 IntersectionInt (nars.language.IntersectionInt)2 Product (nars.language.Product)2 Stamp (nars.entity.Stamp)1 TruthFunctions.reduceConjunction (nars.inference.TruthFunctions.reduceConjunction)1 TruthFunctions.reduceDisjunction (nars.inference.TruthFunctions.reduceDisjunction)1 EventObserver (nars.io.events.EventEmitter.EventObserver)1