use of nars.main.NAR in project opennars by opennars.
the class SymRecognizer method jButton3ActionPerformed.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_jButton3ActionPerformed
resetDetection();
StringBuilder build = new StringBuilder();
StringBuilder build2 = new StringBuilder();
build2.append("<(&|,");
for (int x = 0; x < SZ; x += 1) {
for (int y = 0; y < SZ; y += 1) {
int used_X = x;
int used_Y = y;
Color col1 = new Color(canvasIMG.getRGB(x, y));
float col = ((float) col1.getRed()) / 255.0f;
if (col > 0.0) {
float freq = 0.5f + (col - 0.5f);
if (invar.isSelected()) {
build.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>. :|: %" + String.valueOf(freq) + "%");
build.append("\n");
build2.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>,");
} else {
build.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>. :|: %" + String.valueOf(freq) + "%");
build.append("\n");
build2.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>,");
}
}
}
}
String s2 = build2.toString();
s2 = s2.substring(0, s2.length() - 1);
s2 = s2 + ")";
inputPanel.setText(build.toString());
if (evt == null) {
String question = "<{?what} --> [observed]>?";
additional[exid] = s2 + " ==> <{example" + exid + "} --> [observed]>>.";
inputPanel2.setText(additional[exid] + "\n" + question);
} else {
nar = new NAR();
if (invar1.isSelected()) {
gui = new NARSwing(nar);
}
int u = 0;
inputPanel2.setText("");
// for(String s : questions) {
String s = question;
{
if (s != null) {
AnswerHandler cur = new AnswerHandler() {
@Override
public void onSolution(Sentence belief) {
// System.out.println("solution: " + belief);
System.out.println(belief);
float howconf = belief.truth.getConfidence();
if (howconf > 0.1f) {
// only mark if above 0.1 confidence
// also mark image:
int maxu = Integer.valueOf(belief.getTerm().toString().split("example")[1].split("}")[0]);
clear();
for (int x = 0; x < SZ * scale_palette; x += 1) {
for (int y = 0; y < SZ * scale_palette; y += 1) {
Color col = new Color(canvasIMG.getRGB(x / scale_palette, y / scale_palette));
int k = getK[maxu];
int j = getJ[maxu];
exampleIMG.setRGB(x + k * scale_palette * SZ, y + (3 * j + 1) * scale_palette * SZ, new Color(col.getRed(), 0, 0).getRGB());
}
}
estimate.setIcon(new ImageIcon(fitimage(exampleIMG, estimate.getWidth(), estimate.getHeight())));
estimate.repaint();
}
}
};
q.add(cur);
try {
for (int h = 0; h < exid; h++) {
inputPanel2.setText(inputPanel2.getText() + additional[h] + "\n");
nar.addInput(additional[h]);
}
inputPanel2.setText(inputPanel2.getText() + s + "\n");
nar.ask(s.substring(0, s.length() - 1), cur);
} catch (Narsese.InvalidInputException ex) {
Logger.getLogger(SymRecognizer.class.getName()).log(Level.SEVERE, null, ex);
}
}
u++;
}
nar.param.noiseLevel.set(0);
nar.addInput(inputPanel.getText());
nar.start(0);
}
}
use of nars.main.NAR in project opennars by opennars.
the class SymRecognizerWithVisionChannel method jButton3ActionPerformed.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_jButton3ActionPerformed
resetDetection();
StringBuilder build = new StringBuilder();
StringBuilder build2 = new StringBuilder();
build2.append("<(&|,");
for (int x = 0; x < SZ; x += 1) {
for (int y = 0; y < SZ; y += 1) {
int used_X = x;
int used_Y = y;
Color col1 = new Color(canvasIMG.getRGB(x, y));
float col = ((float) col1.getRed()) / 255.0f;
if (col > 0.0) {
float freq = 0.5f + (col - 0.5f);
if (invar.isSelected()) {
build.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>. :|: %" + String.valueOf(freq) + "%");
build.append("\n");
build2.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>,");
} else {
build.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>. :|: %" + String.valueOf(freq) + "%");
build.append("\n");
build2.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>,");
}
}
}
}
String s2 = build2.toString();
s2 = s2.substring(0, s2.length() - 1);
s2 = s2 + ")";
inputPanel.setText(build.toString());
if (evt == null) {
String question = "<{?what} --> [observed]>?";
additional[exid] = s2 + " ==> <{example" + exid + "} --> [observed]>>.";
inputPanel2.setText(additional[exid] + "\n" + question);
} else {
// add vision channel for [on] property
nar = new NAR();
// to the nar instance, and nar is also the "next higher" sensory channel
// to report the results to
nar.addSensoryChannel("[on]", new SpatialSamplingVisionChannel(nar, nar, SZ, SZ));
if (invar1.isSelected()) {
gui = new NARSwing(nar);
}
int u = 0;
inputPanel2.setText("");
// for(String s : questions) {
String s = question;
{
if (s != null) {
AnswerHandler cur = new AnswerHandler() {
@Override
public void onSolution(Sentence belief) {
// System.out.println("solution: " + belief);
System.out.println(belief);
float howconf = belief.truth.getConfidence();
if (howconf >= 0.001f) {
// only mark if above 0.1 confidence
// also mark image:
int maxu = Integer.valueOf(belief.getTerm().toString().split("example")[1].split("}")[0]);
clear();
for (int x = 0; x < SZ * scale_palette; x += 1) {
for (int y = 0; y < SZ * scale_palette; y += 1) {
Color col = new Color(canvasIMG.getRGB(x / scale_palette, y / scale_palette));
int k = getK[maxu];
int j = getJ[maxu];
exampleIMG.setRGB(x + k * scale_palette * SZ, y + (3 * j + 1) * scale_palette * SZ, new Color(col.getRed(), 0, 0).getRGB());
}
}
estimate.setIcon(new ImageIcon(fitimage(exampleIMG, estimate.getWidth(), estimate.getHeight())));
estimate.repaint();
}
}
};
q.add(cur);
try {
for (int h = 0; h < exid; h++) {
inputPanel2.setText(inputPanel2.getText() + additional[h] + "\n");
nar.addInput(additional[h]);
}
inputPanel2.setText(inputPanel2.getText() + s + "\n");
nar.ask(s.substring(0, s.length() - 1), cur);
} catch (Narsese.InvalidInputException ex) {
Logger.getLogger(SymRecognizerWithVisionChannel.class.getName()).log(Level.SEVERE, null, ex);
}
}
u++;
}
nar.param.noiseLevel.set(0);
nar.addInput(inputPanel.getText());
nar.start(0);
}
}
use of nars.main.NAR in project opennars by opennars.
the class ApplySubstituteTest method test2.
@Test
public void test2() throws Narsese.InvalidInputException {
// substituting: <(*,$1) --> num>. with $1 ==> 0
NAR n = new NAR();
Map<Term, Term> h = new HashMap();
h.put(np.parseTerm("$1"), np.parseTerm("0"));
CompoundTerm c = ((CompoundTerm) np.parseTerm("<(*,$1) --> num>")).applySubstituteToCompound(h);
assertTrue(c != null);
}
use of nars.main.NAR in project opennars by opennars.
the class AttentionTest method testSampleNextConcept.
@Test
public void testSampleNextConcept() {
int numConcepts = 32;
NAR n = new NAR();
for (int i = 0; i < numConcepts; i++) n.addInput("<x" + i + " <-> x" + (i + 1) + ">.");
n.cycles(100);
int c = Iterables.size(n.memory.concepts);
assertTrue(c > 32);
Set<Concept> uniqueconcepts = new HashSet();
for (int i = 0; i < numConcepts; i++) {
Concept s = n.memory.concepts.peekNext();
uniqueconcepts.add(s);
}
assertTrue(uniqueconcepts.size() > 1);
int c2 = Iterables.size(n.memory.concepts);
assertEquals("does not affect # of concepts", c, c2);
}
use of nars.main.NAR in project opennars by opennars.
the class TermTest method testParseOperationInFunctionalForm.
@Test
public void testParseOperationInFunctionalForm() {
Parameters.FUNCTIONAL_OPERATIONAL_FORMAT = true;
NAR n = new NAR();
Narsese p = new Narsese(n);
try {
Term x = p.parseTerm("wonder(a,b)");
assertEquals(Operation.class, x.getClass());
assertEquals("(^wonder,a,b)", x.toString());
} catch (Narsese.InvalidInputException ex) {
ex.printStackTrace();
assertTrue(false);
}
}
Aggregations