Using FuzzyJess: fuzzy-match is not defined - eclipse

I have downloaded an installed Jess plugin for eclipse and FuzzyJ Toolkit with it. Now I am trying to run the following simple code:
(import nrc.fuzzy.*)
(bind ?temperature (new FuzzyVariable "temperature" 0 100 "°C"))
(bind ?pressure (new FuzzyVariable "pressure" 0.1 50 "MPa"))
(?temperature addTerm "cold" (new RightLinearFuzzySet 6 20))
(?temperature addTerm "warm" (new TrapezoidFuzzySet 15 20 25 30))
(?temperature addTerm "hot" (new LeftLinearFuzzySet 25 50))
(?pressure addTerm "low" (new RightLinearFuzzySet 0.9 5))
(?pressure addTerm "medium" (new TrapezoidFuzzySet 2 8 14 28))
(?pressure addTerm "high" (new LeftLinearFuzzySet 22 50))
(deffunction about (?number ?fuzzyVariable)
(bind ?delta (- (?fuzzyVariable getMaxUOD) (?fuzzyVariable getMinUOD)))
(new FuzzyValue ?fuzzyVariable
(new TriangleFuzzySet
(- ?number (* ?delta 0.01))
?number
(+ ?number (* ?delta 0.01))
)
)
)
; (printout t ((about 34 ?temperature) plotFuzzyValue "*"))
(assert (Temperature (about 43 ?temperature)))
(defrule trivial-rule
(Temperature ?t & :(fuzzy-match ?t "hot"))
=>
(assert (Pressure (new FuzzyValue ?pressure "low")))
)
(defrule printing
(Pressure ?p)
=>
(printout t (?p plotFuzzyValue "*"))
)
(run)
However when I run the code, I only get an exception saying that function fuzzy-match is not defined. I already changed the main class to FuzzyMain. I also tried to run FuzzyConsole and load the code from there, as well as trying to add FuzzyFunctions to the engine using addPackage, but in any case I still get that error.

Although Jess in Action unfortunately doesn't tell you this, you have to explicitly load the fuzzy functions, even when launching with FuzzyMain. Add the following line near the top of your Jess code and it will work as desired:
(load-package nrc.fuzzy.jess.FuzzyFunctions)

Related

How to loop through dates in Racket?

I want to loop through dates from the current date to some stop date, like the following example in Python:
import datetime
def count_dates(stop_date):
step = datetime.date.today()
while step >= stop_date:
yield step
step = step - datetime.timedelta(days=1)
def main():
for step in count_dates(datetime.date(2018, 1, 1)):
print(step.isoformat())
I don't see any date calculation functions in Racket, however. How can this be done?
If you want to use racket/date, you can accomplish your goal with find-seconds, seconds->date, current-seconds, and a simple arithmetic:
#lang racket
(require racket/date
racket/generator)
(define 1day (* 60 60 24))
(define (count-dates year month day)
(define target (find-seconds 0 0 0 day month year))
(in-generator
(let loop ([current (current-seconds)])
(when (>= current target)
(yield (seconds->date current))
(loop (- current 1day))))))
(date-display-format 'iso-8601)
(for ([dt (count-dates 2019 2 1)])
(displayln (date->string dt)))
This outputs:
2019-02-11
2019-02-10
2019-02-09
2019-02-08
2019-02-07
2019-02-06
2019-02-05
2019-02-04
2019-02-03
2019-02-02
2019-02-01
That is, from today (Feb 11, 2019) to Feb 1, 2019.
You can also use gregor or srfi-19 which are alternative date libraries that have a notion of time duration/difference.
Here is how you can do it using the date arithmetic provided by gregor. Specifically the +days and -days functions are helpful here:
#lang racket
(require gregor
racket/generator)
;; Date -> [Sequenceof Date]
(define (count-dates stop-date)
(in-generator
(let loop ([step (today)])
(when (date>=? step stop-date)
(yield step)
(loop (-days step 1))))))
(for ([step (count-dates (date 2018 1 1))])
(displayln (date->iso8601 step)))
And if you need something more general than "some number of days", there's also functions like +date-period and -date-period.

How can I update a chartist chart with new data in ClojureScript?

I am new to ClojureScript and I am trying to update a chart with new data. I am using Chartist and Reagent. Rendering the chart for the first time works fine, but if I change the data for the chartist function, the chart isn't rendered again. I have already tried different things with atoms and tried to use the update function from chartist. Here is the link to the chartist update api
Here is my code so far:
(ns example.core
(:require [clojure.browser.repl :as repl]
[reagent.core :as reagent]
[cljsjs.chartist]
))
(defonce conn
(repl/connect "http://localhost:9000/repl"))
(defonce results(reagent/atom [1 1 6 15 25] ))
(defonce timespan (reagent/atom ["Mar-2012" "Jun-2012" "Nov-2012" "Oct-2013" "Nov-2014"]))
(def global-state (reagent/atom 0))
(defn show-chart
[resultsInChart timespaneInChart]
(let [chart-data {:labels timespaneInChart
:series [resultsInChart]}
options {:width "700px"
:height "380px"}]
(js/Chartist.Line. ".ct-chart" (clj->js chart-data) (clj->js options))))
(defn chart-component
[]
(let [some global-state]
(reagent/create-class
{:component-did-mount #(show-chart #results #timespan)
:component-did-update #(show-chart #results #timespan)
:display-name "chart-component"
:reagent-render (fn []
[:div {:class "ct-chart ct-perfect-fourth"}])})))
(defn home-page []
[:div
[:div
[:p [:button {:on-click #(reset! results [1 1 6 15 5]
)} "Change last Value to 5"]
]
]
]
)
(defn run []
(reagent/render [chart-component](.getElementById js/document "myChart"))
(reagent/render [home-page](.getElementById js/document "app"))
)
Would be great if somebody could help.
Update:
I fixed it with adding figwheel to my project, somehow this solved the problem and the chart gets updated. I also used the update function of Chartist under component-did-update in the end. Here is a link to figwheel https://github.com/bhauman/lein-figwheel

Changing the default :junk-allowed value of parse-integer from a top level function in LISP

I'm working in emacs with LISP and I've come across an error that I'm not sure how to get around. I'll put up the output, what I understand from it and what I'm missing.
Error:
junk in string "(-5 -4 -3 -2 -1 0 1 2 3 4 5 6)"
[Condition of type SB-INT:SIMPLE-PARSE-ERROR]
Backtrace:
0: (PARSE-INTEGER "(-5 -4 -3 -2 -1 0 1 2 3 4 5 6)" :START 0 :END NIL :RADIX 10 :JUNK-ALLOWED NIL)
1: ((:METHOD CLSQL-SYS:READ-SQL-VALUE :AROUND (T T T T)) "(-5 -4 -3 -2 -1 0 1 2 3 4 5 6)" INTEGER #<CLSQL-SQLITE3:SQLITE3-DATABASE C:/Users/sas31/idyom/db/database.sqlite OPEN {1007D61933}> :SQLITE3) [fa..
2: ((:METHOD CLSQL-SYS::UPDATE-SLOT-FROM-DB-VALUE (CLSQL-SYS:STANDARD-DB-OBJECT T T)) #<unavailable argument> #<unavailable argument> #<unavailable argument>) [fast-method]
3: (CLSQL-SYS::BUILD-OBJECTS (#<CLSQL-SYS:SELECT-LIST {100592E993}>) #<unavailable argument> #<CLSQL-SQLITE3:SQLITE3-DATABASE C:/Users/sas31/idyom/db/database.sqlite OPEN {1007D61933}> #<unavailable argu..
4: (CLSQL-SYS::FIND-ALL (IDYOM-DB:MTP-EVENT) :RESULT-TYPES :AUTO :REFRESH NIL :INSTANCES NIL :WHERE #<CLSQL-SYS:SQL-RELATIONAL-EXP (MTP_EVENT.COMPOSITION_ID = 0 AND MTP_EVENT.DATASET_ID = 26)> :FLATP T :..
5: (CLSQL-SYS::FIND-ALL (IDYOM-DB:MTP-EVENT) :RESULT-TYPES :AUTO :REFRESH NIL :INSTANCES NIL :WHERE #<CLSQL-SYS:SQL-RELATIONAL-EXP (MTP_EVENT.COMPOSITION_ID = 0 AND MTP_EVENT.DATASET_ID = 26)> :FLATP T :..
6: (CLSQL-SYS:SELECT IDYOM-DB:MTP-EVENT :WHERE #<CLSQL-SYS:SQL-RELATIONAL-EXP (MTP_EVENT.COMPOSITION_ID = 0 AND MTP_EVENT.DATASET_ID = 26)> :FLATP T :RESULT-TYPES NIL :DATABASE #<CLSQL-SQLITE3:SQLITE3-DA..
7: (CLSQL-SYS::FAULT-JOIN-SLOT #<CLSQL-SYS::STANDARD-DB-CLASS IDYOM-DB:MTP-COMPOSITION> #<IDYOM-DB:MTP-COMPOSITION {10047AAA73}> #<CLSQL-SYS::VIEW-CLASS-EFFECTIVE-SLOT-DEFINITION IDYOM-DB::EVENTS>)
8: ((:METHOD SB-MOP:SLOT-VALUE-USING-CLASS (CLSQL-SYS::STANDARD-DB-CLASS T T)) #<unavailable argument> #<unavailable argument> #<unavailable argument>) [fast-method]
9: (IDYOM-DB::GET-EVENT-SEQUENCE 26 0)
10: (IDYOM-DB:COUNT-EVENTS 26 0)
11: (MIDI2DB::FIX-BIOI-DUR-DELTAST 26)
12: (SB-INT:SIMPLE-EVAL-IN-LEXENV (MIDI2DB::FIX-BIOI-DUR-DELTAST 26) #<NULL-LEXENV>)
13: (EVAL (MIDI2DB::FIX-BIOI-DUR-DELTAST 26))
So, I would like to change :junk-allowed nil to t in parse-integer, but I don't know how to do that because I'm not where when or why it's called. The function get-event-sequence (written for this particular programme) calls get-composition, which reads as follows:
(defun get-composition (dataset-id composition-id)
"Returns the composition whose dataset-id is <dataset-id> and whose
composition-id is <composition-id>."
(car (clsql:select
'mtp-composition
:flatp t
:where [and [= [slot-value 'mtp-composition 'dataset-id] dataset-id]
[= [slot-value 'mtp-composition 'composition-id] composition-id]])))
I don't know where this parse-integer is being called from this function or how to change the value of that keyword. Also, the string of numbers are coded key signatures and represent all the key signatures in this particular data set, but why is that important when this function only seems to deal with dataset-id and composition-id? Thanks for your help!
Looking at the backtrace, we can see that parse-integer is called by a method on the generic function clsql-sys:read-sql-value.
It is not clear, without checking the code, if that :junk-allowed nil is just the backtrace showing you default values or if it's actively called that way from the method.
However, getting that set to a true value would not really help you, since the return value would be nil (because "(" is not a number or a white-space). This means that there's a deeper problem with your database, possibly due to an implicit schema violation (you've said "I want to store numbers", then stored a list of numbers and the ORM did something it cannot recover from).
* (parse-integer " 2 " :junk-allowed t)
2
2
* (parse-integer "(2 " :junk-allowed t)
NIL
0

not able to find package when running from SLIME, but from command line is ok

I'm running the teapot example from cl-opengl package. The only changes I've made are loading the required packages. It works fine when executed from unix shell (sbcl --load "3.cl"), but when I try to compile and load it through SLIME (C-c C-k) i get the error about package GLUT not found.
Curiously, the compiler chokes on the (defclass glut-teapot-window (glut:window).
What gives???
Here's a screenshot of what happens
Here's the code for 3.cl.
;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
;;; glut-teapot.lisp --- Simple usage of glut:solid-teapot.
(ql:quickload :cl-opengl)
(ql:quickload :cl-glu)
(ql:quickload :cl-glut)
;(setf *communication-style* :fd-handler)
(defclass glut-teapot-window (glut:window)
()
(:default-initargs :width 250 :height 250 :title "glut-teapot.lisp"
:mode '(:single :rgb :depth)))
(defmethod glut:display-window :before ((w glut-teapot-window))
(gl:clear-color 0 0 0 0)
(gl:cull-face :back)
(gl:depth-func :less)
(gl:disable :dither)
(gl:shade-model :smooth)
(gl:light-model :light-model-local-viewer 1)
(gl:color-material :front :ambient-and-diffuse)
(gl:enable :light0 :light1 :lighting :cull-face :depth-test))
(defmethod glut:display ((window glut-teapot-window))
(gl:load-identity)
(gl:translate 0 0 -5)
(gl:rotate 30 1 1 0)
(gl:light :light0 :position '(100 1000 1 0))
(gl:light :light0 :diffuse '(1.2 0.4 0.6 0))
(gl:light :light1 :position '(-100 1000 1 0))
(gl:clear :color-buffer :depth-buffer)
(gl:color 1 10 1)
(gl:front-face :cw)
(glut:solid-teapot 1.3)
;(glut:solid-torus 0.5 1.0 50 50)
;(glu:cylinder (glu:new-quadric) 0.5 0.5 0.5 20 20)
(gl:front-face :ccw)
(gl:flush))
(defmethod glut:reshape ((window glut-teapot-window) width height)
(gl:viewport 0 0 width height)
(gl:matrix-mode :projection)
(gl:load-identity)
(glu:perspective 50 (/ width height) 0.5 20)
(gl:matrix-mode :modelview)
(gl:load-identity))
(defmethod glut:keyboard ((window glut-teapot-window) key x y)
(declare (ignore x y))
(when (eql key #\Esc)
(glut:destroy-current-window)))
(defun glut-teapot ()
(glut:display-window (make-instance 'glut-teapot-window)))
(glut-teapot)
If you load the file, the Lisp system reads the file expression by expression and executes them after reading each single expression.
If you compile the file in a fresh Lisp, then it reads the expressions and compiles them. But it does not execute them. Thus it sees the quickload command, compiles it, but does not execute it. This OpenGL code is not loaded and the packages are not known to the compiler. But that makes sense: a compile usually should compile the file, not execute it. Lisp would execute the expressions then when you load the compiled fasl file.
There are two simple ways around it:
put the quickload operation in a separate file and compile/execute it before compiling the next file.
enclose the load operations in an EVAL-WHEN statement. (eval-when (:execute :load-toplevel :compile-toplevel) ... your code here ...).
The :compile-toplevel symbol means, that the code will be executed when the compiler sees it as a top-level form. Which it otherwise would not do. Thus you can have code in a file to compile which creates side effects - here loading other code.

Best way to read (MATLAB) structures from a text file in Clojure / Incanter

I exported my matlab structs to text files so that I can read them in clojure.
I have a text file like:
name
Ali
age
33
friends-ages
30
31
25
47
know I can read this file, but what's the clojure way to convert it into something like:
(def person1
{:name "Ali"
:age 33
:friends-ages [30 31 25 47]})
or lets make it easier:
name
Ali
age
33
to:
(def person1
{:name "Ali"
:age 33})
Assuming each file has a single record,
(defn parse [f]
(let [[_ name _ age _ & friends] (.split (slurp f) "\n")]
{:name name :age age :friends (map read-string friends)}))
(parse "../../../Desktop/t.txt")
you get,
{:name "Ali", :age "33", :friends-ages (30 31 25 47)}
I guess you would need to manually do this conversion from text file (which isn't in any standard serialization formats) to the required set format. If possible, I would suggest that you should extract the MATLAB data in JSON format and that would be easy to read in clojure using the json library.
Here's a solution for your easier problem:
(ns example
(:require [clojure.string :as str]))
(def input "name\nAli\nage\n33\n") ; you'd get this by reading the file
(defn process [data]
(->> (str/split data #"\n")
(partition 2)
(map (fn [[k v]]
[(keyword k)
(try
(Integer/valueOf v)
(catch NumberFormatException _
v))]))
(into {})))
(prn (process input))
The full problem requires better specification of the format: how do you know that the list of ages is done?
I ended up writing an .m file to export my .mat files to text, then imported them by something like the following:
(use '[clojure.string :only (split join)])
(defn kv [[k v]]
[(keyword k) v])
(let [a (split (slurp (java.io.FileReader. fileName)) #"\n")]
(def testNeuron (into {} (map kv (partition 2 a)))))
Thanks to the answers by Hamza and Jouni.
The actual code is more like this:
(def dataPath "/home/ali/Dropbox/Projects/Neurojure/")
(def testFileName "dae062.c1.cylinder.DID.txt")
(defn kv [[k v]]
[(keyword k)
(if (< 0 (count (re-seq #"\d\d\d\dSpikes|\d\d\d\dOcodes|\d\d\d\dOSpikes" k)))
(into [] (map (fn [x] (Integer/valueOf x)) (split v #",")))
(try (Integer/valueOf v)
(catch NumberFormatException _ v)))])
(let [a (split (slurp (java.io.FileReader. (str dataPath testFileName))) #"\n")]
(def testNeuron (into {} (map kv (partition 2 a)))))
and the converted data file looks like this:
ConvertDate
18-Nov-2011
Name
/bgc/data/dae/062/dae062.mat
frameperiod
83.2500000037253
Spike2Version
1.27
unstored
167
rfstr
cm=rf-3.10,9.61:0.00x0.00,-135deg pe20 -2.0,-3.0 fx=0.00,fy=0.00cm=rf-3.10,9.61:0.00x0.00,-135deg pe20 -2.0,-3.0 fx=0.00,fy=0.00
rf
-3.1,9.61,0,0,-135,20,-2,-3,0,0
trange
47371169.75,100193717.5
psych
1
rc
0
Options
expname
cylinder.dxXIdD
Start
94745610.75
End
100193717.5
Area
MT
Ex
perimenter Ali
Trial=>0001Start
47377343
Trial=>0001TrialStart
47377343
Trial=>0001End
47397224.25
Trial=>0001dur
19881.2500000075
Trial=>0001uStim
0
Trial=>0001op
40980
Trial=>0001Trial
1860
Trial=>0001id
15897
Trial=>0001Startev
47377149.5
Trial=>0001serdelay
-46.25
Trial=>0001bstimes
47377195.75
Trial=>0001delay
147.25
Trial=>0001TrueEnd
47397224.25
Trial=>0001CorLoop
0
Trial=>0001dx
0
Trial=>0001Spikes
-1402,-1232,1577,1931,2165,2222,2478,2773,2903,3229,3745,3820,4071,4588,4920,5141,5752,6440,6490,6664,6770,7042,7958,8081,8307,8622,8732,9021,9082,9343,9619,9695,9877,10357,10668,10943,11105,11364,11720,12272,12499,12762,12907,13621,14121,14351,14542,14588,15104,15420,15501,16331,16596,16843,17476,17698,17996,18169,18401,18532,18706,19029,19081,19418,19603,19750,20222
Trial=>0001count
65
Trial=>0001OptionCode
+do+aa+sq+72+se+fc+fb+ap+wt+cf+ws+ts+sw+bw+cs+2a+bm+gm+x2+sp+da+ao+cS+C3+CF
Trial=>0001me
0
Trial=>0001OSpikes
-1976,-1802,-1423,-1360,-1268,-1248,-1140,-244,-220,-164,632,681,730,760,779,786,867,879,924,1062,1161,1252,1268,1431,1533,1632,1946,2210,2235,2273,2285,2296,2305,2496,2532,2541,2732,2787,2806,2822,2840,3095,3292,3431,3598,3614,3837,4100,4482,4504,4515,4651,4768,4794,4936,5020,5160,5184,5300,5314,5710,5764,6431,6453,6471,6553,6561,6584,6791,7018,7124,7880,7905,7940,7968,8011,8315,8330,8352,8568,8666,8748,8756,8766,8797,8836,9038,9297,9328,9360,9471,9632,9639,9721,9939,10196,10363,10375,10387,10410,10931,10953,10969,10986,11038,11118,11135,11405,11692,12018,12163,12258,12492,12512,12525,12884,12899,12919,13156,13183,13638,13674,13842,13988,14110,14298,14310,14321,14606,14617,15124,15132,15150,15289,15341,15620,16293,16305,16342,16364,16441,16604,16692,16932,16997,17059,17086,17210,17368,17495,17626,17639,17651,17677,17718,18013,18247,18353,18553,18691,18722,18887,18941,19438,19774,19938,19959,19967,20004,20240,20306,20500,20623
Trial=>0002Start
47406914
Trial=>0002TrialStart
47406914
Trial=>0002End
47426795.25
Trial=>0002dur
19881.2499999925
...