Search in sources :

Example 16 with Concept

use of nars.entity.Concept in project opennars by opennars.

the class SpiralLayout method vertex.

@Override
public void vertex(AbstractGraphVis<Item, Object> g, VertexVis<Item, Object> v) {
    Item vertex = v.getVertex();
    float priority = vertex.getPriority();
    boolean task = false;
    Concept x = null;
    if (vertex instanceof Concept) {
        x = (Concept) vertex;
    } else if (vertex instanceof Task) {
        task = true;
        x = nar.memory.concept(((Task) vertex).getTerm());
    }
    int i = 0;
    try {
        for (Concept c : nar.memory.concepts) {
            if (x == c) {
                // not elegant and fast but k at least no term equals
                break;
            }
            i++;
        }
    } catch (Exception ex) {
    }
    float count_elems = nar.memory.concepts.size();
    // float ratio = 30.0f*(0.10f + (((float)priority) / (1.0f)));
    float ratio = 30.0f * (0.10f + (((float) i) / (count_elems)));
    v.tx = (float) (ratio * Math.cos(ratio)) * spacing;
    v.ty = (float) (ratio * Math.sin(ratio)) * spacing;
    if (task) {
        v.ty += spacing * Video.hashFloat(vertex.hashCode());
    }
}
Also used : Concept(nars.entity.Concept) Item(nars.entity.Item) Task(nars.entity.Task)

Example 17 with Concept

use of nars.entity.Concept in project opennars by opennars.

the class NARGraph method add.

public NARGraph add(NAR n, Filter filter, Graphize graphize) {
    graphize.onTime(this, n.time());
    // TODO support AbstractBag
    for (Concept c : n.memory) {
        // TODO use more efficient iterator so that the entire list does not need to be traversed when excluding ranges
        float p = c.getPriority();
        if (!filter.includePriority(p)) {
            continue;
        }
        // graphize.preLevel(this, p);
        if (!filter.includeConcept(c)) {
            continue;
        }
        graphize.onConcept(this, c);
    // graphize.postLevel(this, level);
    }
    graphize.onFinish(this);
    return this;
}
Also used : Concept(nars.entity.Concept)

Example 18 with Concept

use of nars.entity.Concept in project opennars by opennars.

the class CompositionalRules method decomposeStatement.

/**
 * {(||, S, P), P} |- S {(&&, S, P), P} |- S
 *
 * @param implication The implication term to be decomposed
 * @param componentCommon The part of the implication to be removed
 * @param compoundTask Whether the implication comes from the task
 * @param nal Reference to the memory
 */
static void decomposeStatement(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
    boolean isTemporalConjunction = (compound instanceof Conjunction) && !((Conjunction) compound).isSpatial;
    if (isTemporalConjunction && (compound.getTemporalOrder() == TemporalRules.ORDER_FORWARD) && (index != 0)) {
        return;
    }
    long occurrence_time = nal.getTheNewStamp().getOccurrenceTime();
    if (isTemporalConjunction && (compound.getTemporalOrder() == TemporalRules.ORDER_FORWARD)) {
        if (!nal.getCurrentTask().sentence.isEternal() && compound.term[index + 1] instanceof Interval) {
            long shift_occurrence = ((Interval) compound.term[1]).time;
            occurrence_time = nal.getCurrentTask().sentence.getOccurenceTime() + shift_occurrence;
        }
    }
    Task task = nal.getCurrentTask();
    Sentence taskSentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    Term content = reduceComponents(compound, component, nal.mem());
    if (content == null) {
        return;
    }
    TruthValue truth = null;
    BudgetValue budget;
    if (taskSentence.isQuestion() || taskSentence.isQuest()) {
        budget = BudgetFunctions.compoundBackward(content, nal);
        nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
        nal.doublePremiseTask(content, truth, budget, false, false);
        // special inference to answer conjunctive questions with query variables
        if (taskSentence.term.hasVarQuery()) {
            Concept contentConcept = nal.mem().concept(content);
            if (contentConcept == null) {
                return;
            }
            Sentence contentBelief = contentConcept.getBelief(nal, task);
            if (contentBelief == null) {
                return;
            }
            Task contentTask = new Task(contentBelief, task.budget, false);
            nal.setCurrentTask(contentTask);
            Term conj = Conjunction.make(component, content);
            truth = intersection(contentBelief.truth, belief.truth);
            budget = BudgetFunctions.compoundForward(truth, conj, nal);
            nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
            nal.doublePremiseTask(conj, truth, budget, false, false);
        }
    } else {
        TruthValue v1, v2;
        if (compoundTask) {
            v1 = taskSentence.truth;
            v2 = belief.truth;
        } else {
            v1 = belief.truth;
            v2 = taskSentence.truth;
        }
        if (compound instanceof Conjunction) {
            if (taskSentence.isGoal()) {
                if (compoundTask) {
                    truth = intersection(v1, v2);
                } else {
                    return;
                }
            } else {
                // isJudgment
                truth = reduceConjunction(v1, v2);
            }
        } else if (compound instanceof Disjunction) {
            if (taskSentence.isGoal()) {
                if (compoundTask) {
                    truth = reduceConjunction(v2, v1);
                } else {
                    return;
                }
            } else {
                // isJudgment
                truth = reduceDisjunction(v1, v2);
            }
        } else {
            return;
        }
        budget = BudgetFunctions.compoundForward(truth, content, nal);
    }
    nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
    nal.doublePremiseTask(content, truth, budget, false, false);
}
Also used : BudgetValue(nars.entity.BudgetValue) Concept(nars.entity.Concept) Task(nars.entity.Task) Disjunction(nars.language.Disjunction) TruthFunctions.reduceDisjunction(nars.inference.TruthFunctions.reduceDisjunction) TruthValue(nars.entity.TruthValue) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 19 with Concept

use of nars.entity.Concept in project opennars by opennars.

the class RuleTables method reason.

/**
 * Entry point of the inference engine
 *
 * @param tLink The selected TaskLink, which will provide a task
 * @param bLink The selected TermLink, which may provide a belief
 * @param memory Reference to the memory
 */
public static void reason(final TaskLink tLink, final TermLink bLink, final DerivationContext nal) {
    final Memory memory = nal.mem();
    final Task task = nal.getCurrentTask();
    final Sentence taskSentence = task.sentence;
    // cloning for substitution
    final Term taskTerm = taskSentence.term;
    // cloning for substitution
    Term beliefTerm = bLink.target;
    final Concept beliefConcept = memory.concept(beliefTerm);
    Sentence belief = (beliefConcept != null) ? beliefConcept.getBelief(nal, task) : null;
    nal.setCurrentBelief(belief);
    if (belief != null) {
        // because interval handling that differs on conceptual level
        beliefTerm = belief.term;
        /*Sentence belief_event = beliefConcept.getBeliefForTemporalInference(task);
            if(belief_event != null) {
                boolean found_overlap = false;
                if(Stamp.baseOverlap(task.sentence.stamp.evidentialBase, belief_event.stamp.evidentialBase)) {
                    found_overlap = true;
                }
                if(!found_overlap) { //temporal rules are inductive so no chance to succeed if there is an overlap
                                     //and since the temporal rule is relatively expensive the check here was good.
                    Sentence inference_belief = belief;
                    nal.setCurrentBelief(belief_event);
                    nal.setTheNewStamp(task.sentence.stamp, belief_event.stamp, nal.memory.time());
                    TemporalRules.temporalInduction(task.sentence, belief_event, nal, true);
                    nal.setCurrentBelief(inference_belief);
                    nal.setTheNewStamp(task.sentence.stamp, belief.stamp, nal.memory.time());
                }
            }*/
        // too restrictive, its checked for non-deductive inference rules in derivedTask (also for single prem)
        nal.evidentalOverlap = Stamp.baseOverlap(task.sentence.stamp.evidentialBase, belief.stamp.evidentialBase);
        if (nal.evidentalOverlap && (!task.sentence.isEternal() || !belief.isEternal())) {
            // only allow for eternal reasoning for now to prevent derived event floods
            return;
        }
        nal.emit(Events.BeliefReason.class, belief, beliefTerm, taskTerm, nal);
        if (LocalRules.match(task, belief, nal)) {
            // new tasks resulted from the match, so return
            return;
        }
    }
    // current belief and task may have changed, so set again:
    nal.setCurrentBelief(belief);
    nal.setCurrentTask(task);
    // put here since LocalRules match should be possible even if the belief is foreign
    if (equalSubTermsInRespectToImageAndProduct(taskTerm, beliefTerm))
        return;
    /*if ((memory.getNewTaskCount() > 0) && taskSentence.isJudgment()) {
            return;
        }*/
    final short tIndex = tLink.getIndex(0);
    short bIndex = bLink.getIndex(0);
    switch(// dispatch first by TaskLink type
    tLink.type) {
        case TermLink.SELF:
            switch(bLink.type) {
                case TermLink.COMPONENT:
                    compoundAndSelf((CompoundTerm) taskTerm, beliefTerm, true, bIndex, nal);
                    break;
                case TermLink.COMPOUND:
                    compoundAndSelf((CompoundTerm) beliefTerm, taskTerm, false, bIndex, nal);
                    break;
                case TermLink.COMPONENT_STATEMENT:
                    if (belief != null) {
                        if (taskTerm instanceof Statement) {
                            SyllogisticRules.detachment(taskSentence, belief, bIndex, nal);
                        }
                    }
                    // else {
                    if (taskSentence.term instanceof Inheritance || taskSentence.term instanceof Similarity) {
                        StructuralRules.transformNegation((CompoundTerm) Negation.make(taskSentence.term), nal);
                    }
                    try {
                        goalFromQuestion(task, taskTerm, nal);
                    } catch (Exception ex) {
                        if (Parameters.DEBUG) {
                            System.out.print("Error in goalFromQuestion");
                        }
                    }
                    // }
                    break;
                case TermLink.COMPOUND_STATEMENT:
                    if (belief != null) {
                        SyllogisticRules.detachment(belief, taskSentence, bIndex, nal);
                    }
                    break;
                case TermLink.COMPONENT_CONDITION:
                    if ((belief != null) && (taskTerm instanceof Implication)) {
                        bIndex = bLink.getIndex(1);
                        SyllogisticRules.conditionalDedInd(task.sentence, (Implication) taskTerm, bIndex, beliefTerm, tIndex, nal);
                    }
                    break;
                case TermLink.COMPOUND_CONDITION:
                    if ((belief != null) && (taskTerm instanceof Implication) && (beliefTerm instanceof Implication)) {
                        bIndex = bLink.getIndex(1);
                        SyllogisticRules.conditionalDedInd(belief, (Implication) beliefTerm, bIndex, taskTerm, tIndex, nal);
                    }
                    break;
            }
            break;
        case TermLink.COMPOUND:
            switch(bLink.type) {
                case TermLink.COMPOUND:
                    compoundAndCompound((CompoundTerm) taskTerm, (CompoundTerm) beliefTerm, tIndex, bIndex, nal);
                    break;
                case TermLink.COMPOUND_STATEMENT:
                    compoundAndStatement((CompoundTerm) taskTerm, tIndex, (Statement) beliefTerm, bIndex, beliefTerm, nal);
                    break;
                case TermLink.COMPOUND_CONDITION:
                    if (belief != null) {
                        if (beliefTerm instanceof Implication) {
                            Term[] u = new Term[] { beliefTerm, taskTerm };
                            if (Variables.unify(VAR_INDEPENDENT, ((Statement) beliefTerm).getSubject(), taskTerm, u, true)) {
                                // only secure place that
                                // allows partial match
                                Sentence newBelief = belief.clone(u[0]);
                                Sentence newTaskSentence = taskSentence.clone(u[1]);
                                detachmentWithVar(newBelief, newTaskSentence, bIndex, false, nal);
                            } else {
                                SyllogisticRules.conditionalDedInd(belief, (Implication) beliefTerm, bIndex, taskTerm, -1, nal);
                            }
                        } else if (beliefTerm instanceof Equivalence) {
                            SyllogisticRules.conditionalAna((Equivalence) beliefTerm, bIndex, taskTerm, -1, nal);
                        }
                    }
                    break;
            }
            break;
        case TermLink.COMPOUND_STATEMENT:
            switch(bLink.type) {
                case TermLink.COMPONENT:
                    if (taskTerm instanceof Statement) {
                        goalFromWantBelief(task, tIndex, bIndex, taskTerm, nal, beliefTerm);
                        componentAndStatement((CompoundTerm) nal.getCurrentTerm(), bIndex, (Statement) taskTerm, tIndex, nal);
                    }
                    break;
                case TermLink.COMPOUND:
                    if (taskTerm instanceof Statement) {
                        compoundAndStatement((CompoundTerm) beliefTerm, bIndex, (Statement) taskTerm, tIndex, beliefTerm, nal);
                    }
                    break;
                case TermLink.COMPOUND_STATEMENT:
                    if (belief != null) {
                        syllogisms(tLink, bLink, taskTerm, beliefTerm, nal);
                    }
                    break;
                case TermLink.COMPOUND_CONDITION:
                    if (belief != null) {
                        bIndex = bLink.getIndex(1);
                        if ((taskTerm instanceof Statement) && (beliefTerm instanceof Implication)) {
                            conditionalDedIndWithVar(belief, (Implication) beliefTerm, bIndex, (Statement) taskTerm, tIndex, nal);
                        }
                    }
                    break;
            }
            break;
        case TermLink.COMPOUND_CONDITION:
            switch(bLink.type) {
                case TermLink.COMPOUND:
                    if (belief != null) {
                        detachmentWithVar(taskSentence, belief, tIndex, nal);
                    }
                    break;
                case TermLink.COMPOUND_STATEMENT:
                    if (belief != null) {
                        if (// TODO maybe put instanceof test within conditionalDedIndWithVar()
                        taskTerm instanceof Implication) {
                            Term subj = ((Statement) taskTerm).getSubject();
                            if (subj instanceof Negation) {
                                if (taskSentence.isJudgment()) {
                                    componentAndStatement((CompoundTerm) subj, bIndex, (Statement) taskTerm, tIndex, nal);
                                } else {
                                    componentAndStatement((CompoundTerm) subj, tIndex, (Statement) beliefTerm, bIndex, nal);
                                }
                            } else {
                                conditionalDedIndWithVar(task.sentence, (Implication) taskTerm, tIndex, (Statement) beliefTerm, bIndex, nal);
                            }
                        }
                        break;
                    }
                    break;
            }
    }
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) Similarity(nars.language.Similarity) Negation(nars.language.Negation) Memory(nars.storage.Memory) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication) Equivalence(nars.language.Equivalence) Events(nars.io.events.Events) Sentence(nars.entity.Sentence)

Example 20 with Concept

use of nars.entity.Concept in project opennars by opennars.

the class TemporalInferenceControl method NewOperationFrame.

public static void NewOperationFrame(Memory mem, Task task) {
    // can there be more than one? I don't think so..
    List<Task> toRemove = new LinkedList<Task>();
    float priorityGain = 0.0f;
    for (Task t : mem.recent_operations) {
        // when made sure, make single element and add break
        if (t.getTerm().equals(task.getTerm())) {
            priorityGain = BudgetFunctions.or(priorityGain, t.getPriority());
            toRemove.add(t);
        }
    }
    for (Task t : toRemove) {
        mem.recent_operations.take(t);
    }
    // this way operations priority of previous exections
    task.setPriority(BudgetFunctions.or(task.getPriority(), priorityGain));
    // contributes to the current (enhancement)
    mem.recent_operations.putIn(task);
    mem.lastDecision = task;
    Concept c = (Concept) mem.concept(task.getTerm());
    if (c != null) {
        if (c.seq_before == null) {
            c.seq_before = new LevelBag<>(Parameters.SEQUENCE_BAG_LEVELS, Parameters.SEQUENCE_BAG_SIZE);
        }
        for (Task t : mem.seq_current) {
            if (task.sentence.getOccurenceTime() > t.sentence.getOccurenceTime()) {
                c.seq_before.putIn(t);
            }
        }
    }
    mem.seq_current.clear();
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) LinkedList(java.util.LinkedList)

Aggregations

Concept (nars.entity.Concept)30 Task (nars.entity.Task)18 Term (nars.language.Term)14 Sentence (nars.entity.Sentence)10 BudgetValue (nars.entity.BudgetValue)7 Events (nars.io.events.Events)6 CompoundTerm (nars.language.CompoundTerm)6 TruthValue (nars.entity.TruthValue)5 Interval (nars.language.Interval)4 ArrayList (java.util.ArrayList)3 Conjunction (nars.language.Conjunction)3 Color (java.awt.Color)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 DerivationContext (nars.control.DerivationContext)2 Item (nars.entity.Item)2 Stamp (nars.entity.Stamp)2 Implication (nars.language.Implication)2 NAR (nars.main.NAR)2 Memory (nars.storage.Memory)2