Search in sources :

Example 16 with Sentence

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

the class SwingLogText method print.

// public class TaskIcon extends NCanvas {
// 
// public TaskIcon() {
// super();
// setMaximumSize(new Dimension(50,10));
// setPreferredSize(new Dimension(50,10));
// setSize(50,10);
// 
// Graphics2D g = getBufferGraphics();
// 
// showBuffer(g);
// }
// 
// 
// 
// }
protected int print(Class c, Object o) {
    float priority = 1f;
    if (c != OUT.class) {
        // pad the channel name to max 6 characters, right aligned
        String n = c.getSimpleName();
        // n = n.substring(0,Math.min(4, n.length()));
        switch(n.length()) {
            case 0:
                break;
            case 1:
                n = "   " + n;
                break;
            case 2:
                n = "  " + n;
                break;
            case 3:
                n = " " + n;
                break;
        }
        Color chanColor = Video.getColor(c.getClass().hashCode(), 0.8f, 0.8f);
        print(chanColor, n);
    } else {
        if (o instanceof Task) {
            Task t = (Task) o;
            Sentence s = t.sentence;
            if (s != null) {
                priority = t.budget.getPriority();
                // printColorBlock(LogPanel.getPriorityColor(priority), "  ");
                TruthValue tv = s.truth;
                if (tv != null) {
                    float evidence = TruthFunctions.c2w(tv.getConfidence());
                    float pos_2 = tv.getConfidence() * tv.getFrequency();
                    float positive_evidence_in_0_1 = TruthFunctions.w2c(evidence * tv.getFrequency());
                    float negative_evidence_in_0_1 = TruthFunctions.w2c(evidence * (1.0f - tv.getFrequency()));
                    printColorBlock(LogPanel.getPositiveEvidenceColor(positive_evidence_in_0_1), "  ");
                    printColorBlock(LogPanel.getNegativeEvidenceColor(negative_evidence_in_0_1), "  ");
                } else if (t.getBestSolution() != null) {
                    float evidence = TruthFunctions.c2w(t.getBestSolution().truth.getConfidence());
                    float pos_2 = t.getBestSolution().truth.getConfidence() * t.getBestSolution().truth.getFrequency();
                    float positive_evidence_in_0_1 = TruthFunctions.w2c(evidence * t.getBestSolution().truth.getFrequency());
                    float negative_evidence_in_0_1 = TruthFunctions.w2c(evidence * (1.0f - t.getBestSolution().truth.getFrequency()));
                    // printColorBlock(LogPanel.getStatementColor('=', priority, t.getBestSolution().truth.get), "    ");
                    printColorBlock(LogPanel.getPositiveEvidenceColor(positive_evidence_in_0_1), "  ");
                    printColorBlock(LogPanel.getNegativeEvidenceColor(negative_evidence_in_0_1), "  ");
                } else {
                    printColorBlock(LogPanel.getStatementColor(s.punctuation, priority), "    ");
                }
            }
        }
    }
    CharSequence text = LogPanel.getText(c, o, showStamp, nar);
    StringBuilder sb = new StringBuilder(text.length() + 2);
    sb.append(' ');
    if (text.length() > maxLineWidth)
        sb.append(text.subSequence(0, maxLineWidth));
    else
        sb.append(text);
    if (sb.charAt(sb.length() - 1) != '\n')
        sb.append('\n');
    if (o instanceof Task) {
        Task t = (Task) o;
        int color = 128;
        Concept cc = nar.memory.concept(t.getTerm());
        color = (int) Math.min(255.0f, 80.0f + t.getPriority() * 255.0f);
        Color col = new Color(color, color, color);
        if (cc != null) {
            color = (int) (128.0f + cc.getPriority() * 128.0f);
            print(col, sb.toString(), new ConceptAction(cc));
            return doc.getLength();
        }
        print(col, sb.toString());
        return doc.getLength();
    }
    // float tc = 0.75f + 0.25f * priority;
    // Color textColor = new Color(tc,tc,tc);
    String s = sb.toString();
    print(Color.GRAY, sb.toString());
    return doc.getLength();
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) TruthValue(nars.entity.TruthValue) Color(java.awt.Color) Sentence(nars.entity.Sentence)

Example 17 with Sentence

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

the class SentenceGraph method event.

@Override
public void event(final Class event, final Object[] a) {
    if (event == Events.ConceptForget.class) {
        // remove all associated beliefs
        Concept c = (Concept) a[0];
        // create a clone of the list for thread safety
        for (Task b : new ArrayList<Task>(c.beliefs)) {
            remove(b.sentence);
        }
    } else if (event == Events.ConceptBeliefAdd.class) {
        Concept c = (Concept) a[0];
        Sentence s = ((Task) a[1]).sentence;
        if (c.getPriority() > minConceptPri.get()) {
            add(s, c);
        }
    } else if (event == Events.ConceptBeliefRemove.class) {
        Concept c = (Concept) a[0];
        Sentence s = (Sentence) a[1];
        remove(s);
    } else /*else if (event == Events.ConceptGoalAdd.class) {
            Concept c = (Concept)a[0];
            Sentence s = ((Task)a[1]).sentence;
            add(s, c);
        }
        else if (event == Events.ConceptGoalRemove.class) {
            Concept c = (Concept)a[0];
            Sentence s = (Sentence)a[1];
            remove(s);
        }*/
    if (event == Events.CyclesEnd.class) {
        if (needInitialConcepts)
            getInitialConcepts();
    } else if (event == Events.ResetEnd.class) {
        reset();
    }
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) Events(nars.io.events.Events) ArrayList(java.util.ArrayList) Sentence(nars.entity.Sentence)

Example 18 with Sentence

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

the class LanguageGUI method jButton1ActionPerformed.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_jButton1ActionPerformed
    String text = this.inputPanel.getText();
    List<String> inputs = new ArrayList<String>();
    this.inputPanel.setText("");
    String[] words = text.split(" ");
    String punctuation = "sentence";
    for (int i = 0; i < words.length; i++) {
        for (int len = words.length; len >= Integer.valueOf(spliceField.getText()); len--) {
            String narsese = "(#,";
            if (i + len > words.length) {
                // this length is not possible
                continue;
            }
            for (int k = i; k < i + len; k++) {
                if (words[k].contains("?")) {
                    punctuation = "question";
                }
                // skip puncutation in words
                words[k] = words[k].replace(".", "").replace("?", "");
                narsese += "\"" + words[k] + "\"" + ",";
            }
            narsese = narsese.substring(0, narsese.length() - 1);
            narsese += ").";
            inputs.add("$1.0;0.99;0.99$" + narsese);
            if (!spliceCheckbox.isSelected()) {
                break;
            }
            break;
        }
        if (!spliceCheckbox.isSelected()) {
            break;
        }
        break;
    }
    languageNAR.stop();
    for (AnswerHandler ans : q) {
        ans.off();
    }
    languageNAR.reset();
    languageNAR.memory.randomNumber.setSeed(seed);
    seed++;
    try {
        HashMap<String, String> wordTypes = new HashMap<String, String>();
        HashSet<String> qWords = new HashSet<String>();
        for (String s : jTextPane1.getText().split("\n")) {
            // if its a word type definition we won't add it directly but only when used in the sentence
            if (s.startsWith("<\"")) {
                String word = s.split("\"")[1];
                wordTypes.put(word, s);
                if (s.contains("QWORD>.")) {
                    qWords.add(word);
                }
            } else {
                languageNAR.addInput(s);
            }
        }
        languageNAR.cycles(100);
        // add word categories of all words that occurred
        for (String word : words) {
            if (wordTypes.containsKey(word)) {
                languageNAR.addInput(wordTypes.get(word));
            }
        }
        languageNAR.cycles(10);
        for (String s : inputs) {
            languageNAR.addInput(s);
            languageNAR.cycles(1);
        }
        boolean isQuestion = punctuation.equals("question");
        String punct = (isQuestion ? "?" : "." + (eventCheck.isSelected() ? " :|:" : ""));
        // Interpretation of Inheritance
        AnswerHandler cur = new AnswerHandler() {

            @Override
            public void onSolution(Sentence belief) {
                // System.out.println("solution: " + belief);
                System.out.println(belief);
                String[] concepts = belief.term.toString().split("\"");
                if (concepts.length < 6 || belief.toString().contains("#")) {
                    // just an example, no " included
                    return;
                }
                String s = considerQWord(concepts[1], qWords).toUpperCase();
                String p = considerQWord(concepts[3], qWords).toUpperCase();
                String v = considerQWord(concepts[5], qWords).toUpperCase();
                String inp = "<(*," + s + "," + p + ") --> " + v + ">" + punct;
                if (v.equals("IS") || v.equals("ARE")) {
                    inp = "<" + s + " --> " + p + ">" + punct;
                }
                reasonerNAR.stop();
                reasonerNAR.addInput(inp + " " + (isQuestion ? "" : belief.truth.toString()));
                reasonerNAR.start(0);
            }
        };
        languageNAR.ask("<(*,(/,REPRESENT,?a,_),(/,REPRESENT,?b,_)) --> (/,REPRESENT,?v,_)>", cur);
        q.add(cur);
        for (String attribute : attributePanel.getText().split(" ")) {
            cur = new AnswerHandler() {

                @Override
                public void onSolution(Sentence belief) {
                    // System.out.println("solution: " + belief);
                    System.out.println(belief);
                    String[] concepts = belief.term.toString().split("\"");
                    if (concepts.length < 2) {
                        // just an example, no " included
                        return;
                    }
                    String s = considerQWord(concepts[1], qWords).toUpperCase();
                    String inp = "<" + s + " --> " + attribute + ">" + punct;
                    reasonerNAR.stop();
                    reasonerNAR.addInput(inp + " " + (isQuestion ? "" : belief.truth.toString()));
                    reasonerNAR.start(0);
                }
            };
            if (!attribute.trim().equals("")) {
                languageNAR.ask("<?what --> " + attribute + ">", cur);
                q.add(cur);
            }
        }
        languageNAR.start(0);
    } catch (Exception ex) {
        Logger.getLogger(LanguageGUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) AnswerHandler(nars.io.events.AnswerHandler) Sentence(nars.entity.Sentence) HashSet(java.util.HashSet)

Example 19 with Sentence

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

Example 20 with Sentence

use of nars.entity.Sentence 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)

Aggregations

Sentence (nars.entity.Sentence)70 Task (nars.entity.Task)54 Term (nars.language.Term)40 BudgetValue (nars.entity.BudgetValue)39 TruthValue (nars.entity.TruthValue)39 Stamp (nars.entity.Stamp)25 CompoundTerm (nars.language.CompoundTerm)24 Statement (nars.language.Statement)13 Conjunction (nars.language.Conjunction)11 Concept (nars.entity.Concept)10 Implication (nars.language.Implication)10 Inheritance (nars.language.Inheritance)8 Interval (nars.language.Interval)8 ArrayList (java.util.ArrayList)6 Equivalence (nars.language.Equivalence)6 HashMap (java.util.HashMap)5 AnswerHandler (nars.io.events.AnswerHandler)5 NARSwing (nars.gui.NARSwing)4 Narsese (nars.io.Narsese)4 Product (nars.language.Product)4