Author SHA1 Message Date
Roman Volosovskyi d9df78c5c0 some kind of tests for rules 2016-04-11 12:00:01 +03:00
Jarrad Hope a7c9d59afc Merge branch 'rule-names' of github.com:syng-im/narjure into rule-names 2016-04-11 10:23:07 +02:00
Jarrad Hope f4f8780f6f hypenation 2016-04-11 10:23:00 +02:00
Roman Volosovskyi 32860e3488 unique names 2016-04-11 11:22:08 +03:00
Roman Volosovskyi fa0b66aa2e compilation of #R R# rules, without names 2016-04-10 15:16:46 +03:00
Roman Volosovskyi 824e8c16de Merge branch 'deriver' into rule-names 2016-04-10 12:49:55 +03:00
Roman Volosovskyi cf73c2a74f #R R# reader 2016-04-10 12:47:57 +03:00
Jarrad Hope f511352a55 find typo in pathams document 2016-04-10 10:45:05 +02:00
Roman Volosovskyi d48a3379fc quest rules 2016-04-09 12:59:55 +03:00
Roman Volosovskyi 76cb5536c1 exclude :matcher key from derivation 2016-04-09 12:51:59 +03:00
Roman Volosovskyi faf010a846 fix empty "when" inside derivers code 2016-04-08 23:48:46 +03:00
Roman Volosovskyi 6508e881bb fix reduce-similarity 2016-04-08 18:38:45 +03:00
Roman Volosovskyi 91bcaa0d0b fix d-identity 2016-04-08 12:00:43 +03:00
Jarrad Hope c3eae016d0 remove judgement, replace with belief 2016-03-29 14:18:56 +02:00
Jarrad Hope 1b7168fa36 remove bracket 2016-03-28 13:05:07 +02:00
Jarrad Hope 7b5f1eab1f task(?) is :question? in doc 2016-03-27 21:45:40 +02:00
Jarrad Hope 901eb1dabc spell check 2016-03-27 21:43:10 +02:00
Jarrad Hope a81df37b52 fix rule and decoding latex abit 2016-03-27 19:48:01 +02:00
Jarrad Hope 11ef13f154 indentation on conclusions 2016-03-27 17:26:56 +02:00
Jarrad Hope 80d82b4df3 more todo comments, normalise :pre 2016-03-27 17:24:10 +02:00
Jarrad Hope 376d60cdbc unrwap rules, derivation alignment, TODO on unresolved duplicate names 2016-03-27 17:09:47 +02:00
Jarrad Hope a18eba9bc4 cleanup part 2 2016-03-27 16:20:57 +02:00
Jarrad Hope a7cf3d9433 cleanup part 1 2016-03-27 15:47:27 +02:00
Jarrad Hope c42fb751cb single premise rules 2016-03-27 14:31:02 +02:00
32 changed files with 1576 additions and 1147 deletions
-8
View File
@@ -1,8 +0,0 @@
language: clojure
# Notify #nars
notifications:
slack:
secure: eI5hj2PABtAUkpeYwiAHcFMa5pNvHFuU87uqe4qBtv0upPtu22/dLpw8l6EhhcMZlW6C8D2irrj6W5C1lujCy7u3Vpu1FeBPZbqzapyU1Hpe8YgSRdCY1dbygawZp/Av/HWzpi0aTket83F5/0dvTZK0fQTBRmAfyx17DEJscYEBH61dKVqdwBy5OVYvhQ1+QtEnt1TRJcT0AA8von9lanzx5/mWdQ6O+3BrKURWG6vvQUYEMZH43NU6lNGNV+PBGV1PSqZqbzCh/6C6/d6HauQdewr4Oubez90OquyCDpCZDoVG8eCAsZtZt+tzfJ5gjtz8+HA6GQeQB6QMLAp89m1bPwSZCqdnmQp9S8TNgm8Io3jREBh+5JIdpDXwukKT1kMPFrdDiPTEhHNJojyK3/BERGyrhf8azow+brPq0EIM9Fi/SkGa0gb9mUXY/BZ1MF7ulxxbzpLxHJYlT9QVlV9Q09/uhH4tIh0kPQhe+ntkMT1uLTfDur7CZakB5270ibreRSeK4RKdpy5SjaIN70hSwyrWoZHiED6aKEJJSO1t9Ve1jTDyWbcUZYewtBi+APcqAraja9NIlFApjBRO1jUneS4/BzNqxWZAFS7MOwMrk1xFft47hMIHoNYd7IgyB7TWSxhloNTwMyOWkdOhnPIqqCvds6M0yQCDKNBiVeM=
services:
- redis-server
Binary file not shown.
Binary file not shown.
-3
View File
@@ -1,3 +0,0 @@
# Introduction to narjure
TODO: write [great documentation](http://jacobian.org/writing/what-to-write/)
+3 -4
View File
@@ -9,14 +9,13 @@
[org.clojure/tools.nrepl "0.2.12"]
[org.clojure/data.priority-map "0.0.7"]
[org.clojure/core.match "0.3.0-alpha4"]
[org.clojure/core.unify "0.5.5"]
[com.taoensso/carmine "2.12.2"]
[mount "0.1.10"]]
[org.clojure/core.unify "0.5.5"]]
:main ^:skip-aot narjure.core
:plugins [[lein-cloverage "1.0.6"]
[jonase/eastwood "0.2.3"]
[lein-kibit "0.1.2"]
[cider/cider-nrepl "0.11.0-SNAPSHOT"]]
[cider/cider-nrepl "0.11.0-SNAPSHOT"]
[michaelblume/lein-marginalia "0.9.0"]]
:eastwood {:exclude-namespaces [nal.rules]}
:target-path "target/%s"
:repl-options {:init-ns narjure.repl
+1 -1
View File
@@ -2,7 +2,7 @@
task ::= [budget] sentence (* task to be processed *)
sentence ::= statement"." [tense] [truth] (* judgement to be remembered *)
sentence ::= statement"." [tense] [truth] (* belief to be remembered *)
| statement"?" [tense] [truth] (* question to be answered, tense added in OpenNARS 1.7 *)
| statement"@" [tense] [truth] (* question on desire value to be answered, tense added in OpenNARS 1.7 *)
| statement"!" [tense] [truth] (* goal to be realized, tense added in OpenNARS 1.7 *)
+5 -3
View File
@@ -1,12 +1,14 @@
(ns nal.core
(:require [nal.deriver.truth :as t]
[nal.deriver :refer [generate-conclusions]]))
[nal.deriver :refer [generate-conclusions]]
[nal.rules :as r]))
(defn choice [[f1 c1] [f2 c2]]
(if (>= c1 c2) [f1 c1] [f2 c2]))
(defn inference
[rules {:keys [task-type] :as task} belief]
(generate-conclusions (rules task-type) task belief))
([task belief] (inference r/rules task belief))
([rules {:keys [task-type] :as task} belief]
(generate-conclusions (rules task-type) task belief)))
(def revision t/revision)
+1 -2
View File
@@ -8,8 +8,7 @@
(defn get-matcher [rules p1 p2]
(let [matchers (->> (mall-paths p1 p2)
(filter rules)
(map rules)
(map (fn [el] (:matcher el))))]
(map rules))]
(case (count matchers)
0 (constantly [])
1 (first matchers)
+9 -5
View File
@@ -1,6 +1,7 @@
(ns nal.deriver.backward-rules
(:require [nal.deriver.key-path :refer [rule-path]]
[clojure.string :as s]))
[clojure.string :as s]
[nal.deriver.utils :as u]))
;http://pastebin.com/3zLX7rPx
(defn allow-backward?
@@ -29,22 +30,25 @@
"If rule allows backward inference it will be expanded to three rules,
where first one is the rule itself, and rest rules will be generated by
swapping conclusion with every premise."
[{:keys [p1 p2 conclusions pre] :as rule}]
[{:keys [p1 p2 conclusions pre id] :as rule}]
(mapcat (fn [{:keys [conclusion post] :as fc}]
(let [post (reduce #(remove (partial has-prefix? %2) %1)
post [":t/" ":d/"])]
(conj (map
(fn [r] (update r :pre conj :question?))
[(assoc rule :conclusions [(assoc fc :post post)])
[(assoc rule :conclusions [(assoc fc :post post)]
:id (u/concat-kw id :q))
(assoc rule :p1 conclusion
:conclusions [{:conclusion p1
:post post}]
:full-path (rule-path conclusion p2)
:pre (check-not-equal pre p1))
:pre (check-not-equal pre p1)
:id (u/concat-kw id :backward1))
(assoc rule :p2 conclusion
:conclusions [{:conclusion p2
:post post}]
:full-path (rule-path p1 conclusion)
:pre (check-not-equal pre p2))])
:pre (check-not-equal pre p2)
:id (u/concat-kw id :backward2))])
rule)))
conclusions))
+16 -7
View File
@@ -1,6 +1,7 @@
(ns nal.deriver.list-expansion
(:require [nal.deriver.utils :refer [walk]]
[clojure.string :as s]))
[clojure.string :as s]
[nal.deriver.utils :as u]))
(def max-elements-in-list 7)
@@ -36,18 +37,26 @@
"Expands :from/ element in rule, so as a result will be created n rules, in
each of them :form/A will be replaced with A1 or A2 or A3, etc."
[statement from-name list-name n]
(map (fn [idx]
(walk statement
(= from-name el) (symbol (str list-name idx))))
(range 1 (inc n))))
(let [st (butlast statement)
id (last statement)]
(map (fn [idx]
(let [sym (str list-name idx)
new-id (u/concat-kw id sym)]
(walk (concat st [new-id])
(= from-name el) (symbol sym))))
(range 1 (inc n)))))
(defn generate-all-lists
"Expands rules with :list/ elements, as a result will be created 5 rules,
where :list/ will be replaced with A1, A1 A2, ..., A1..A5."
[r]
(let [list (get-list ":list" r)
l-name (list-name list)]
(mapcat #(let [st (replace-list-elemets r list l-name %)]
l-name (list-name list)
id (last r)
r-without-id (butlast r)]
(mapcat #(let [new-id (u/concat-kw id (str l-name %))
r (concat r-without-id [new-id])
st (replace-list-elemets r list l-name %)]
(if-let [from-name (get-list ":from" st)]
(expand-:from-element st from-name l-name %)
[st]))
+18 -14
View File
@@ -19,7 +19,7 @@
#{`= `not= `seq? `first `and `let `pos? `> `>= `< `<= `coll? `set `quote
`count 'aops `- `not-empty-diff? `not-empty-inter? `walk `munification-map
`substitute `sets `some `deref `do `vreset! `volatile! `fn `mapv `if
`sort-commutative `n/reduce-ext-inter `n/reduce-symilarity `complement
`sort-commutative `n/reduce-ext-inter `n/reduce-similarity `complement
`n/reduce-int-dif `n/reduce-and `n/reduce-ext-dif `n/reduce-image
`n/reduce-int-inter `n/reduce-neg `n/reduce-or `nil? `not `or `abs
`implications-and-equivalences `get-terms `empty? `intersection
@@ -50,14 +50,14 @@
(defn form-conclusion
"Formation of cocnlusion in terms of task and truth/desire functions"
[{:keys [t1 t2 task-type]}
{c :statement tf :t-function pj :p/judgement df :d-function
{c :statement tf :t-function pj :p/belief df :d-function
sc :shift-conditions}]
(let [conclusion-type (if pj :judgement task-type)
(let [conclusion-type (if pj :belief task-type)
conclusion {:statement c
:task-type conclusion-type
:occurrence :t-occurrence}
conclusion (case conclusion-type
:judgement (assoc conclusion :truth (list tf t1 t2))
:belief (assoc conclusion :truth (list tf t1 t2))
:goal (assoc conclusion :desire (list df t1 t2))
conclusion)]
(if sc
@@ -67,19 +67,24 @@
(defn traverse-node
"Generates code for precondition node."
[vars result {:keys [conclusions children condition]}]
`(when ~(quote-operators condition)
~(when-not (zero? (count conclusions))
`(vswap! ~result concat
~@(set (map #(mapv (partial form-conclusion vars) %)
(quote-operators conclusions)))))
~@(map (fn [n] (traverse-node vars result n)) children)))
(let [conclusions (remove
nil?
[(when-not (zero? (count conclusions))
`(vswap! ~result concat
~@(set (map #(mapv (partial form-conclusion vars) %)
(quote-operators conclusions)))))])
children (mapcat (fn [n] (traverse-node vars result n)) children)]
(if (true? condition)
(concat conclusions children)
[`(when ~(quote-operators condition)
~@(concat conclusions children))])))
(defn traversal
"Walk through preconditions tree and generates code for matcher."
[vars tree]
(let [results (gensym)]
`(let [~results (volatile! [])]
~(traverse-node vars results tree)
~@(traverse-node vars results tree)
@~results)))
(defn replace-occurrences
@@ -262,7 +267,7 @@
sym-map)
:t-function (t/tvtypes (get-truth-fn post))
:d-function (t/dvtypes (get-desire-fn post))
:p/judgement (some #{:p/judgement} post)}
:p/belief (some #{:p/belief} post)}
:conditions (remove nil?
(walk (concat (check-conditions sym-map) pre)
(and (coll? el) (= \a (first (str (first el)))))
@@ -340,6 +345,5 @@
match-fn-code (-> main-pattern
(gen-rules rules)
(match-rules main-pattern task-type))]
[k (assoc v :matcher (eval match-fn-code)
:matcher-code match-fn-code)])))
[k (eval match-fn-code)])))
(into {})))
+3 -3
View File
@@ -111,7 +111,7 @@
[_ ['ext-set & l1] ['ext-set & l2]] (diff 'ext-set l1 l2)
:else st))
(defn reduce-symilarity
(defn reduce-similarity
[st]
(m/match st
['<-> ['ext-set s] ['ext-set p]] ['<-> s p]
@@ -169,7 +169,7 @@
'| `reduce-int-inter
'- `reduce-ext-dif
'int-dif `reduce-int-dif
'<-> `reduce-symilarity
'<-> `reduce-similarity
'* `reduce-production
'int-image `reduce-image
'ext-image `reduce-image
@@ -185,7 +185,7 @@
| (reduce-int-inter st)
- (reduce-ext-dif st)
int-dif (reduce-int-dif st)
<-> (reduce-symilarity st)
<-> (reduce-similarity st)
* (reduce-production st)
int-image (reduce-image st)
ext-image (reduce-image st)
+8 -8
View File
@@ -5,7 +5,7 @@
[nal.deriver.substitution :refer [substitute munification-map]]
[nal.deriver.terms-permutation :refer [implications equivalences]]
[clojure.set :refer [union intersection]]
[narjure.defaults :refer [temporal-window-duration]]
[narjure.defaults :refer [duration]]
[clojure.core.match :as m]
[nal.deriver.normalization :refer [reduce-seq-conj]]))
@@ -88,11 +88,11 @@
[_]
[`(not= :eternal :t-occurrence)
`(not= :eternal :b-occurrence)
`(<= ~temporal-window-duration (abs (- :t-occurrence :b-occurrence)))])
`(<= ~duration (abs (- :t-occurrence :b-occurrence)))])
(defmethod compound-precondition :concurrent
[_]
[`(> ~temporal-window-duration (abs (- :t-occurrence :b-occurrence)))])
[`(> ~duration (abs (- :t-occurrence :b-occurrence)))])
;-------------------------------------------------------------------------------
(defmulti precondition-transformation (fn [arg1 _] (first arg1)))
@@ -164,11 +164,11 @@
(m/match (mapv #(if (and (coll? %) (= 'quote (first %)))
(second %) %) (rest args))
[(:or '=|> '==>)] concl
['pred-impl] `(let [:t-occurrence (+ :t-occurrence ~temporal-window-duration)] ~concl)
['retro-impl] `(let [:t-occurrence (- :t-occurrence ~temporal-window-duration)] ~concl)
['pred-impl] `(let [:t-occurrence (+ :t-occurrence ~duration)] ~concl)
['retro-impl] `(let [:t-occurrence (- :t-occurrence ~duration)] ~concl)
[sym (:or '=|> '==>)] (shift-forward-let sym concl)
[sym 'pred-impl] (shift-forward-let sym `+ concl temporal-window-duration)
[sym 'retro-impl] (shift-forward-let sym `- concl temporal-window-duration)))
[sym 'pred-impl] (shift-forward-let sym `+ concl duration)
[sym 'retro-impl] (shift-forward-let sym `- concl duration)))
(defn backward-interval-check [sym]
`(and (coll? ~sym) (= (first ~sym) (quote ~'seq-conj))
@@ -190,7 +190,7 @@
(defmethod conclusion-transformation :shift-occurrence-backward
[args concl]
(let [duration (- temporal-window-duration)]
(let [duration (- duration)]
(m/match (mapv #(if (and (coll? %) (= 'quote (first %)))
(second %) %) (rest args))
[(:or '=|> '==>)] concl
+6 -4
View File
@@ -1,10 +1,11 @@
(ns nal.deriver.premises-swapping
(:require [nal.deriver.key-path :refer [rule-path]]
[nal.deriver.normalization :refer [commutative-ops]]))
[nal.deriver.normalization :refer [commutative-ops]]
[nal.deriver.utils :as u]))
;the set of keys which prevent premises swapping for rule
(def anti-swapping-keys
#{:question? :judgement? :goal? :measure-time :t/belief-structural-deduction
#{:question? :belief? :goal? :measure-time :t/belief-structural-deduction
:t/structural-deduction :t/belief-structural-difference :t/identity
:t/negation :union :intersection :t/intersection :t/union})
@@ -16,9 +17,10 @@
(not-any? commutative-ops (flatten conclusion)))))
(defn swap-premises
[{:keys [p1 p2] :as rule}]
[{:keys [p1 p2 id] :as rule}]
(assoc rule :p1 p2
:p2 p1
:full-path (rule-path p2 p1)))
:full-path (rule-path p2 p1)
:id (u/concat-kw id :swap)))
(defn swap [rule] [rule (swap-premises rule)])
+73 -52
View File
@@ -1,15 +1,17 @@
(ns nal.deriver.rules
(:require [clojure.string :as s]
[clojure.set :refer [map-invert]]
[clojure.core.match :as m]
[nal.deriver
[key-path :refer [rule-path all-paths path-invariants path]]
[utils :refer [walk]]
[utils :refer [walk concat-kw]]
[list-expansion :refer [contains-list? generate-all-lists]]
[premises-swapping :refer [allow-swapping? swap]]
[matching :refer [generate-matching]]
[backward-rules :refer [allow-backward? expand-backward-rules]]
[normalization :refer [infix->prefix replace-negation]]
[terms-permutation :refer [order-for-all-same? generate-all-orders]]]))
[terms-permutation :refer [order-for-all-same? generate-all-orders]]]
[nal.deriver.utils :as u]))
(defn options
"Generates map from rest of the rule's args."
@@ -24,29 +26,48 @@
(map (fn [[c _ post]] {:conclusion c :post post}) (partition 3 c))
[{:conclusion c :post (:post opts)}]))
(defn rule*
([p1 p2 c other] (rule* p1 p2 c other ""))
([p1 p2 c other pf]
(let [p1 (infix->prefix p1)
p2 (infix->prefix p2)
c (infix->prefix c)
opts (options other)
conclusions (get-conclusions c opts)
id (concat-kw (opts :id) pf)
cnt (count conclusions)]
(for [n (range 0 cnt)]
{:p1 p1
:p2 p2
:conclusions [(nth conclusions n)]
:full-path (rule-path p1 p2)
:pre (infix->prefix (:pre opts))
:origin (opts :origin)
:id (if (zero? n)
id
(u/concat-kw id (str (inc n))))}))))
(defn get-second-premises [p1]
(m/match (vec p1)
[_ ([fp2 _ sp2] :seq)] [fp2 sp2]
[fp2 _ sp2] [fp2 sp2]))
(defn rule
"Generates rule from #R statement."
[data]
(let [[p1 p2 _ c & other] (replace-negation data)]
(let [p1 (infix->prefix p1)
p2 (infix->prefix p2)
c (infix->prefix c)
opts (options other)
conclusions (get-conclusions c opts)]
(map (fn [c]
{:p1 p1
:p2 p2
:conclusions [c]
:full-path (rule-path p1 p2)
:pre (infix->prefix (:pre opts))})
conclusions))))
(m/match (vec (replace-negation data))
[p1 '|- c & other]
(let [[fp2 sp2] (get-second-premises p1)]
(concat (rule* p1 fp2 c other :prem1)
(rule* p1 sp2 c other :prem2)))
[p1 p2 '|- c & other] (rule* p1 p2 c other)))
(defn check-duplication
"Checks if there are rules with same premises and preconditions but with
different conclusions, merges them if they exist."
[rules]
(vals (reduce (fn [ac {:keys [p1 p2 pre conclusions] :as r}]
(let [k [p1 p2 pre]]
(vals (reduce (fn [ac {:keys [p1 p2 pre conclusions id origin] :as r}]
(let [k [p1 p2 (set pre)]]
(if (ac k)
(update-in ac [k :conclusions] concat conclusions)
(assoc ac k r))))
@@ -57,6 +78,12 @@
[{:keys [pre]}]
(some #{:question?} pre))
(defn quest?
"Return true if rule allows only quest as task."
[{:keys [pre] [{post :post}] :conclusions}]
(and (some #{:question?} pre)
(every? #(not (#{:p/judgement} %)) post)))
(defn goal?
"Return true if rule allows only goal as task."
[{pre :pre [{post :post}] :conclusions}]
@@ -65,26 +92,11 @@
(s/starts-with? (str el) ":d/")))
post)))
(defn judgement?
"Return true if rule allows only judgement as task."
(defn belief?
"Return true if rule allows only belief as task."
[{:keys [pre] :as rule}]
(not (or (question? rule) (some #{:goal} pre))))
(defn add-possible-paths
"Selects all rules that will match the same path as current rule and adds
these rules to the set of rules that matches path.
For instance:
current rule's path [[--> [- :any :any] :any] :and [--> [:any :any]]]
so, if we find rule with path [[--> :any :any] :and [--> [:any :any]]],
it matches to current's rule path too, hence it should be added to the set
of rules that matches [[--> [- :any :any] :any] :and [--> [:any :any]]] path."
[ac [k {:keys [all]}]]
(let [rules (mapcat :rules (vals (select-keys ac all)))]
(-> ac
(update-in [k :rules] concat rules)
(update-in [k :rules] set))))
(defn rule->map
"Adds rule to map of rules, conjoin rule to set of rules that
matches to pattern. Rules paths are keys in this map."
@@ -117,27 +129,36 @@
`~raw-rules
pairs)))
(defmacro defrules [name & rules]
(defmacro defrules [name rules]
`(def ~name (quote ~rules)))
(defn expand-rules [rules]
(rules->> (apply concat rules)
contains-list? generate-all-lists
contains-list? generate-all-lists
identity rule
order-for-all-same? generate-all-orders
allow-swapping? swap
allow-backward? expand-backward-rules))
(defn generate-deriver [expanded-rules]
(let [judgement-rules# (check-duplication (filter belief? expanded-rules))
question-rules# (check-duplication (filter question? expanded-rules))
goal-rules# (check-duplication (filter goal? expanded-rules))
quest-rules# (check-duplication (filter quest? expanded-rules))]
(println "Beliefs rules:" (count judgement-rules#))
(println "Questions rules:" (count question-rules#))
(println "Goal rules:" (count goal-rules#))
(println "Quests rules:" (count quest-rules#))
{:belief (rules-map judgement-rules# :belief)
:question (rules-map question-rules# :question)
:goal (rules-map goal-rules# :goal)
:quest (rules-map quest-rules# :quest)
:origin (group-by :origin expanded-rules)
:id (group-by :id expanded-rules)}))
(defn compile-rules
"Define rules. Rules must be #R statements."
;TODO exception on duplication of the rule
[& rules]
(time
(let [rules (rules->> (apply concat rules)
contains-list? generate-all-lists
contains-list? generate-all-lists
identity rule
order-for-all-same? generate-all-orders
allow-swapping? swap
allow-backward? expand-backward-rules)
judgement-rules# (check-duplication (filter judgement? rules))
question-rules# (check-duplication (filter question? rules))
goal-rules# (check-duplication (filter goal? rules))]
(println "Q rules:" (count question-rules#))
(println "J rules:" (count judgement-rules#))
(println "G rules:" (count goal-rules#))
{:judgement (rules-map judgement-rules# :judgement)
:question (rules-map question-rules# :question)
:goal (rules-map goal-rules# :goal)})))
(time (generate-deriver (expand-rules rules))))
+30 -9
View File
@@ -1,5 +1,6 @@
(ns nal.deriver.terms-permutation
(:require [nal.deriver.utils :refer [walk]]))
(:require [nal.deriver.utils :refer [walk]]
[nal.deriver.utils :as u]))
(defn contains-op?
"Checks if statement contains operators from set."
@@ -17,11 +18,28 @@
[statement from to]
(walk statement (= from el) to))
(def names
{'conj :conj
'&| :parallel-conj
'seq-conj :seq-conj
'<=> :equiv
'</> :pred-equiv
'<|> :conc-equiv
'==> :impl
'pred-impl :pred-impl
'=|> :concur-impl
'retro-impl :retro-impl})
(defn permute-op
"Makes permuatation of operators from s in statement."
[statement s]
(if-let [op (contains-op? statement s)]
(map #(replace-op statement op %) s)
(let [id (last statement)
st (butlast statement)]
(map (fn [new-op]
(let [new-id (u/concat-kw id :perm (names new-op))]
(replace-op (concat st [new-id]) op new-op)))
s))
[statement]))
;equivalences, implications, conjunctions - sets of operators that are use in
@@ -32,18 +50,21 @@
(defn generate-all-orders
"Permutes all operators in statement with :order-for-all-same precondition."
[{:keys [p1 p2 conclusions full-path pre] :as rule}]
[{:keys [p1 p2 conclusions full-path pre id origin] :as rule}]
(let [{:keys [conclusion] :as c1} (first conclusions)
statements (->> (permute-op [p1 p2 conclusion full-path pre] equivalences)
(mapcat (fn [st] (permute-op st conjunctions)))
(mapcat (fn [st] (permute-op st implications)))
set)]
(map (fn [[p1 p2 c full-path pre]]
statements
(->> (permute-op [p1 p2 conclusion full-path pre id] equivalences)
(mapcat (fn [st] (permute-op st conjunctions)))
(mapcat (fn [st] (permute-op st implications)))
set)]
(map (fn [[p1 p2 c full-path pre id]]
(assoc rule :p1 p1
:p2 p2
:full-path full-path
:conclusions [(assoc c1 :conclusion c)]
:pre pre))
:pre pre
:id id
:origin origin))
statements)))
(defn order-for-all-same?
+9 -7
View File
@@ -94,11 +94,11 @@
(defn difference [[^double f1 ^double c1] [^double f2 ^double c2]]
[(t-and f1 (- 1 f2)) (t-and c1 c2)])
(defn structual-intersection [_ p2] (deduction p2 [1 d/judgement-confidence]))
(defn structual-intersection [_ p2] (deduction p2 [1 d/belief-confidence]))
(defn structual-deduction [p1 _] (deduction p1 [1 d/judgement-confidence]))
(defn structual-deduction [p1 _] (deduction p1 [1 d/belief-confidence]))
(defn structual-abduction [p1 _] (abduction p1 [1 d/judgement-confidence]))
(defn structual-abduction [p1 _] (abduction p1 [1 d/belief-confidence]))
(defn reduce-conjunction [p1 p2]
(-> (negation p1 p2)
@@ -108,14 +108,16 @@
(defn t-identity [p1 _] p1)
(defn d-identity [p1 _] p1)
(defn belief-identity [p1 p2] (when p2 p1))
(defn belief-structural-deduction [_ p2]
(when p2 (deduction p2 [1 d/judgement-confidence])))
(when p2 (deduction p2 [1 d/belief-confidence])))
(defn belief-structural-difference [_ p2]
(when p2
(let [[^double f ^double c] (deduction p2 [1 d/judgement-confidence])]
(let [[^double f ^double c] (deduction p2 [1 d/belief-confidence])]
[(- 1 f) c])))
(defn belief-negation [_ p2] (when p2 (negation p2 nil)))
@@ -129,7 +131,7 @@
(defn desire-structural-strong
[t _]
(analogy t [1.0 d/judgement-confidence]))
(analogy t [1.0 d/belief-confidence]))
(def tvtypes
{:t/structural-deduction structual-abduction
@@ -166,6 +168,6 @@
:d/deduction intersection
:d/weak desire-weak
:d/induction desire-induction
:d/identity identity
:d/identity d-identity
:d/negation negation
:d/structural-strong desire-structural-strong})
+9 -1
View File
@@ -1,5 +1,13 @@
(ns nal.deriver.utils
(:require [clojure.walk :as w]))
(:require [clojure.walk :as w]
[clojure.string :as s]))
(defn concat-kw [& kws]
(->> (remove nil? kws)
(map name)
(remove #{""})
(s/join "-")
keyword))
(defn not-operator?
"Checks if element is not operator"
+34 -18
View File
@@ -10,18 +10,6 @@
nil)]
(aset dm (int ch) fun)))
(defn fetch-rule
([rdr] (fetch-rule rdr "" 0))
([rdr prev cnt]
(let [c (.read rdr)
cnt (case (char c)
\] (dec cnt)
\[ (inc cnt)
cnt)]
(if (neg? cnt)
prev
(recur rdr (str prev (char c)) cnt)))))
(defn add-brackets [s]
(str "[" s "]"))
@@ -47,10 +35,38 @@
(defn read-rule [s]
(-> s replacements add-brackets read-string))
(defn rule [rdr letter-R opts & other]
(let [c (.read rdr)]
(if (= c (int \[))
(read-rule (fetch-rule rdr))
(throw (Exception. (str "Reader barfed on " (char c)))))))
(defn fetch-rules
([rdr] (fetch-rules rdr "" nil 0))
([rdr prev prev-char cnt]
(let [n (.read rdr)
c (char n)]
(if (and (= c \#) (= \R prev-char))
prev
(recur rdr (str prev c) c cnt)))))
(defn comment->key [comment]
(-> comment
(s/replace #"\s" "-")
(s/replace #";" "")
keyword))
(defn rules [rdr _ _ & _]
(->> (reduce (fn [{:keys [prev-comment prev-not-comment]
:as ac} l]
(if (or (s/starts-with? l ";")
(s/starts-with? l "R"))
(-> (assoc ac :prev-comment l
:prev-not-comment "")
(update :rules conj {:comment prev-comment
:rule prev-not-comment}))
(update ac :prev-not-comment str l)))
{:prev-not-comment ""}
(map s/trim (s/split-lines (fetch-rules rdr))))
:rules
(remove (fn [{c :comment r :rule}] (or (nil? c) (empty? r))))
(map (fn [{:keys [comment rule]}]
(let [id (comment->key comment)]
(vec (concat (read-rule rule) [:origin id :id id])))))))
(dispatch-reader-macro \R rules)
(dispatch-reader-macro \R rule)
+893 -344
View File
File diff suppressed because it is too large Load Diff
+11 -11
View File
@@ -1,18 +1,18 @@
(ns narjure.defaults)
(def judgement-frequency 1.0)
(def judgement-confidence 0.9)
(def belief-frequency 1.0)
(def belief-confidence 0.9)
(def truth-value
[judgement-frequency judgement-confidence])
[belief-frequency belief-confidence])
(def judgement-priority 0.5)
(def judgement-durability 0.8)
(def belief-priority 0.5)
(def belief-durability 0.8)
;todo clarify this
(def judgement-quality 0.5)
(def belief-quality 0.5)
(def judgement-budget
[judgement-priority judgement-durability judgement-quality])
(def belief-budget
[belief-priority belief-durability belief-quality])
(def question-priority 0.5)
(def question-durability 0.9)
@@ -20,16 +20,16 @@
(def question-quality 0.5)
(def question-budget
[judgement-priority judgement-durability judgement-quality])
[belief-priority belief-durability belief-quality])
(def goal-confidence 0.9)
(def goal-priority 0.5)
(def goal-durability 0.8)
(def budgets
{:judgement judgement-budget
{:belief belief-budget
:question question-budget})
(def ^{:type double} horizon 1)
(def temporal-window-duration 80)
(def duration 80)
-32
View File
@@ -1,32 +0,0 @@
(ns narjure.memory.api)
(defprotocol Memory
(term [mem concept])
(select-truth [mem concept occurrence])
(truths [mem concept])
(desires [mem concept])
(tasklinks [mem concept])
(select-tasklink [mem concept])
(termlinks [mem concept])
(select-termlink [mem concept])
(budget [mem concept])
(add-term [mem concept])
(add-truth [mem concept truth])
(add-desire [mem concept desire])
(add-tasklink [mem concept link])
(add-termlink [mem concept link])
(remove-truth [mem concept id])
(remove-desire [mem concept id])
(remove-tasklink [mem concept id])
(remove-termlink [mem concept id])
(update-budget [mem update-fn])
(task [mem id])
(push-task [mem task])
(pop-task [mem])
(activate-concept [mem concept])
(pull-activated-concepts [mem]))
-174
View File
@@ -1,174 +0,0 @@
(ns narjure.memory.redis
(:require [taoensso.carmine :as c]
[narjure.memory.api :refer [Memory]])
(:import (java.util UUID)))
;postfixes for keys
(def truths-pr "_t")
(def desires-pr "_d")
(def tasklinks-pr "_tkl")
(def termlinks-pr "_tml")
(def budget-pr "_bg")
(def task-pr "_tsk")
(def parse-float #(Float/parseFloat %))
(def parse-int #(Integer/parseInt %))
(def parse-boolean #(Boolean/parseBoolean %))
(def truth-schema
{:frequency :float
:confidence :float
:occurrence :int
:evidences :any})
(def desire-schema
{:plausibility :float
:desirability :float
:occurrence :int
:evidences :any})
(def task-schema
{:task-type :keyword
:evidences :vector
:eternal :boolean
:occurrence :int
:frequency :float
:confidence :float
:plausibility :float
:desirability :float
:term :any})
(def termlink-schema
{:priority :float
:durability :float
:quality :float
:concept :string})
(def tasklink-schema
{:priority :float
:durability :float
:quality :float
:task :string})
(def deserialization-fn
{:float parse-float
:int parse-int
:boolean parse-boolean})
(defn apply-schema [val]
(->> val
(map (fn [[k v]]
[k (get deserialization-fn v identity)]))
(into {})))
(def deserialization-map
(reduce (fn [ac [key val]] (assoc ac key (apply-schema val)))
{}
{truths-pr truth-schema
desires-pr desire-schema
task-pr task-schema
tasklinks-pr tasklink-schema
termlinks-pr termlink-schema}))
(defn- check-hash [val]
(if (or (integer? val) (string? val)) val (hash val)))
(defn get-key [concept postfix]
(str (check-hash concept) postfix))
(defn- get-maps-ids [conn concept postfix]
(->> (get-key concept postfix)
c/smembers
(c/wcar conn)))
(defn xf [trans-map]
(comp (partition-all 2)
(map (fn [[k v]]
(let [k (keyword k)
tf (get trans-map k identity)]
[k (tf v)])))))
(defn- get-map-by-key [conn trans-map k]
(into {} (xf trans-map) (c/wcar conn (c/hgetall k))))
(defn- get-maps [conn concept postfix]
(map (partial get-map-by-key conn (deserialization-map postfix))
(get-maps-ids conn concept postfix)))
(defn- get-map [conn concept postfix]
(get-map-by-key conn (deserialization-map postfix) (get-key concept postfix)))
(defn- add-map [conn concept postfix data]
(let [id (str (UUID/randomUUID) postfix)]
(c/wcar conn (c/sadd (get-key concept postfix) id))
(c/wcar conn (c/hmset* id (assoc data :id id)))))
(defn- remove-map [conn concept postfix id]
(c/wcar conn (c/srem (get-key concept postfix) id))
(c/wcar conn (c/del id)))
(defn- push [conn task]
(let [id (str (UUID/randomUUID) "_tsk")]
(c/wcar conn (c/lpush "tasks" id))
(c/wcar conn (c/hmset* id task))))
(defn- tpop [conn]
(let [id (c/wcar conn (c/rpop))]
(get-map-by-key conn (deserialization-map task-pr) id)))
(defn pull-concepts [conn]
(-> (c/wcar
conn
(c/multi)
(c/smembers :active-concepts)
(println val)
(c/del :active-concepts)
(c/exec))
last
first))
(defn- activate-concept* [conn concept]
(c/wcar conn (c/sadd :active-concepts (check-hash concept))))
(defn- select-link [conn concept prefix]
(let [id (->> prefix
(get-key concept)
c/srandmember
(c/wcar conn))]
(get-map-by-key conn (deserialization-map prefix) id)))
(defn get-task [conn id]
(get-map-by-key conn (deserialization-map task-pr) id))
(defrecord RedisMemory
[conn]
Memory
(term [_ concept] (c/wcar conn (c/get (check-hash concept))))
(select-truth [_ concept occurrence] (select-link conn concept truths-pr))
(truths [_ concept] (get-maps conn concept truths-pr))
(desires [_ concept] (get-maps conn concept desires-pr))
(tasklinks [_ concept] (get-maps conn concept tasklinks-pr))
(select-tasklink [_ concept] (select-link conn concept tasklinks-pr))
(termlinks [_ concept] (get-maps conn concept termlinks-pr))
(select-termlink [_ concept] (select-link conn concept termlinks-pr))
(budget [_ concept] (get-map conn concept budget-pr))
(add-term [_ concept] (c/wcar conn (c/set (hash concept) concept)))
(add-truth [_ concept truth] (add-map conn concept truths-pr truth))
(add-desire [_ concept desire] (add-map conn concept desires-pr desire))
(add-tasklink [_ concept link] (add-map conn concept tasklinks-pr link))
(add-termlink [_ concept link] (add-map conn concept termlinks-pr link))
(remove-truth [_ concept id] (remove-map conn concept truths-pr id))
(remove-desire [_ concept id] (remove-map conn concept desires-pr id))
(remove-tasklink [_ concept id] (remove-map conn concept tasklinks-pr id))
(remove-termlink [_ concept id] (remove-map conn concept termlinks-pr id))
(update-budget [_ update-fn])
(task [_ id] (get-task conn id))
(push-task [_ task] (push conn task))
(pop-task [_] (tpop conn))
(activate-concept [_ concept] (activate-concept* conn concept))
(pull-activated-concepts [_] (pull-concepts conn)))
+1 -1
View File
@@ -43,7 +43,7 @@
(defn get-compound-term [[_ operator-srt]]
(compound-terms operator-srt))
(def actions {"." :judgement
(def actions {"." :belief
"?" :question})
(def ^:dynamic *action* (atom nil))
-15
View File
@@ -1,15 +0,0 @@
(ns narjure.system
(:require [mount.core :refer [defstate]]
[narjure.memory.redis :as r]
[nal.deriver.rules :refer [compile-rules]]
[nal.rules :refer [all-rules]]
[nal.core :as c]))
(declare memory inference)
(def redis-config
{:spec {:host "127.0.0.1" :port 6379}})
(defstate memory :start (r/->RedisMemory redis-config))
(defstate inference :start #(let [rules (compile-rules all-rules)]
(partial c/inference rules)))
+82 -195
View File
@@ -1,271 +1,158 @@
(ns nal.test.core
(:require [clojure.test :refer :all]
[nal.core :as c]
[nal.rules :as r]
[nal.deriver.rules :refer [compile-rules]]))
(def inference (partial c/inference (compile-rules r/all-rules)))
[nal.core :refer :all]))
(deftest test-inference
(are [a1 a2] (= (set a1) (set (apply inference a2)))
'({:statement [==>
[&| [--> [ext-set tim] [int-set driving]]]
[--> [ext-set tim] [int-set dead]]]
:truth [1.0 0.81]
:task-type :judgement
'({:statement [==>
[&| [--> [ext-set tim] [int-set driving]]]
[--> [ext-set tim] [int-set dead]]]
:truth [1.0 0.81]
:task-type :belief
:occurrence 1})
'[{:statement [--> [ext-set tim] [int-set drunk]]
:truth [1 0.9]
:task-type :judgement
'[{:statement [--> [ext-set tim] [int-set drunk]]
:truth [1 0.9]
:task-type :belief
:occurrence 1}
{:statement [==> [&| [--> [ind-var X] [int-set drunk]] [--> [ind-var X] [int-set driving]]]
[--> [ind-var X] [int-set dead]]]
:truth [1 0.9]
{:statement [==> [&| [--> [ind-var X] [int-set drunk]] [--> [ind-var X] [int-set driving]]]
[--> [ind-var X] [int-set dead]]]
:truth [1 0.9]
:occurrence 0}]
'({:occurrence 1
:statement [&| a1 [--> [* a1 a2 a3] m]]
:task-type :judgement
:task-type :belief
:truth [1.0
0.81]}
{:occurrence 1
:statement [--> a1 [ext-image m _ a2 a3]]
:task-type :judgement
:task-type :belief
:truth [1
0.9]}
{:occurrence 1
:statement [<|> a1 [--> [* a1 a2 a3] m]]
:task-type :judgement
:task-type :belief
:truth [1.0
0.44751381215469616]}
{:occurrence 1
:statement [=|> [--> [* a1 a2 a3] m] a1]
:task-type :judgement
:task-type :belief
:truth [1
0.44751381215469616]}
{:occurrence 1
:statement [=|> a1 [--> [* a1 a2 a3] m]]
:task-type :judgement
:task-type :belief
:truth [1
0.44751381215469616]})
'[{:statement [--> [* a1 a2 a3] m]
:truth [1 0.9]
:task-type :judgement
'[{:statement [--> [* a1 a2 a3] m]
:truth [1 0.9]
:task-type :belief
:occurrence 1}
{:statement a1
:truth [1 0.9]
{:statement a1
:truth [1 0.9]
:occurrence 0}]
'[{:statement [=|> [--> [* a1 a2 a3] m] a1],
:task-type :judgement,
'[{:statement [=|> [--> [* a1 a2 a3] m] a1],
:task-type :belief,
:occurrence 1,
:truth [1 0.44751381215469616]}
{:statement [<|> a1 [--> [* a1 a2 a3] m]],
:task-type :judgement,
:truth [1 0.44751381215469616]}
{:statement [<|> a1 [--> [* a1 a2 a3] m]],
:task-type :belief,
:occurrence 1,
:truth [1.0 0.44751381215469616]}
{:statement [&| [--> [* a1 a2 a3] m] a1],
:task-type :judgement,
:truth [1.0 0.44751381215469616]}
{:statement [&| [--> [* a1 a2 a3] m] a1],
:task-type :belief,
:occurrence 1,
:truth [1.0 0.81]}
{:statement [=|> a1 [--> [* a1 a2 a3] m]],
:task-type :judgement,
:truth [1.0 0.81]}
{:statement [=|> a1 [--> [* a1 a2 a3] m]],
:task-type :belief,
:occurrence 1,
:truth [1 0.44751381215469616]}]
'[{:statement a1
:truth [1 0.9]
:task-type :judgement
:truth [1 0.44751381215469616]}]
'[{:statement a1
:truth [1 0.9]
:task-type :belief
:occurrence 1}
{:statement [--> [* a1 a2 a3] m]
:truth [1 0.9]
{:statement [--> [* a1 a2 a3] m]
:truth [1 0.9]
:occurrence 0}]
'({:occurrence 1
:statement [&| a1 [conj a1 a2 a3]]
:task-type :judgement
:task-type :belief
:truth [1.0 0.81]}
{:occurrence 1
:statement [<|> a1 [conj a1 a2 a3]]
:task-type :judgement
:task-type :belief
:truth [1.0 0.44751381215469616]}
{:occurrence 1
:statement [=|> [conj a1 a2 a3] a1]
:task-type :judgement
:task-type :belief
:truth [1
0.44751381215469616]}
{:occurrence 1
:statement [=|> a1 [conj a1 a2 a3]]
:task-type :judgement
:task-type :belief
:truth [1
0.44751381215469616]}
{:occurrence 1
:statement a1
:task-type :judgement
:task-type :belief
:truth [1 0.44751381215469616]})
'[{:statement [conj a1 a2 a3]
:truth [1 0.9]
:task-type :judgement
'[{:statement [conj a1 a2 a3]
:truth [1 0.9]
:task-type :belief
:occurrence 1}
{:statement a1
:truth [1 0.9]
{:statement a1
:truth [1 0.9]
:occurrence 0}]
'[{:statement [=|> [--> M S] [[--> M S] [--> M P]]],
:task-type :judgement,
'[{:statement [=|> [--> M S] [[--> M S] [--> M P]]],
:task-type :belief,
:occurrence 1,
:truth [1 0.44751381215469616]}
{:statement [<|> [--> M S] [[--> M S] [--> M P]]],
:task-type :judgement,
:truth [1 0.44751381215469616]}
{:statement [<|> [--> M S] [[--> M S] [--> M P]]],
:task-type :belief,
:occurrence 1,
:truth [1.0 0.44751381215469616]}
{:statement [&| [--> M S] [[--> M S] [--> M P]]],
:task-type :judgement,
:truth [1.0 0.44751381215469616]}
{:statement [&| [--> M S] [[--> M S] [--> M P]]],
:task-type :belief,
:occurrence 1,
:truth [1.0 0.81]}
{:statement [=|> [[--> M S] [--> M P]] [--> M S]],
:task-type :judgement,
:truth [1.0 0.81]}
{:statement [=|> [[--> M S] [--> M P]] [--> M S]],
:task-type :belief,
:occurrence 1,
:truth [1 0.44751381215469616]}]
'[{:statement [[--> M S] [--> M P]]
:truth [1 0.9]
:task-type :judgement
:truth [1 0.44751381215469616]}]
'[{:statement [[--> M S] [--> M P]]
:truth [1 0.9]
:task-type :belief
:occurrence 1}
{:statement [--> M S]
:truth [1 0.9]
{:statement [--> M S]
:truth [1 0.9]
:occurrence 0}]
'({:statement [conj [--> [dep-var Y] [int-set B]]
[==> [--> [ext-set A] [int-set Y]] [--> [dep-var Y] P]]]
:truth [1.0 0.81]
:task-type :judgement
'({:statement [conj [--> [dep-var Y] [int-set B]]
[==> [--> [ext-set A] [int-set Y]] [--> [dep-var Y] P]]]
:truth [1.0 0.81]
:task-type :belief
:occurrence 1}
{:statement [==>
[conj [--> [ext-set A] [int-set Y]] [--> [ind-var X] [int-set B]]]
[--> [ind-var X] P]]
:truth [1 0.44751381215469616]
:task-type :judgement
{:statement [==>
[conj [--> [ext-set A] [int-set Y]] [--> [ind-var X] [int-set B]]]
[--> [ind-var X] P]]
:truth [1 0.44751381215469616]
:task-type :belief
:occurrence 1})
'[{:statement [==> [--> [ext-set A] [int-set Y]] [--> [ext-set A] P]]
:truth [1 0.9]
:task-type :judgement
'[{:statement [==> [--> [ext-set A] [int-set Y]] [--> [ext-set A] P]]
:truth [1 0.9]
:task-type :belief
:occurrence 1}
{:statement [--> [ext-set A] [int-set B]]
:truth [1 0.9]
:occurrence 1}]
'({:statement [</>
[seq-conj [--> chess competition] [:interval 1000]]
[--> sport competition]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.44751381215469616]}
{:statement [seq-conj
[--> chess competition]
[:interval 1000]
[--> sport competition]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]}
{:statement [pred-impl
[seq-conj [--> chess competition] [:interval 1000]]
[--> sport competition]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [retro-impl
[--> sport competition]
[seq-conj [--> chess competition] [:interval 1000]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [--> sport chess],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [--> chess sport],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [<=> [--> chess [ind-var X]] [--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.44751381215469616]}
{:statement [conj [--> chess [dep-var Y]] [--> sport [dep-var Y]]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]}
{:statement [<-> sport chess],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.44751381215469616]}
{:statement [==> [--> chess [ind-var X]] [--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [==> [--> chess [ind-var X]] [--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [==> [--> sport [ind-var X]] [--> chess [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [==> [--> sport [ind-var X]] [--> chess [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [--> [int-dif chess sport] competition],
:task-type :judgement,
:occurrence 1000,
:truth [0.0 0.81]}
{:statement [--> [| chess sport] competition],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]}
{:statement [--> [int-dif sport chess] competition],
:task-type :judgement,
:occurrence 1000,
:truth [0.0 0.81]}
{:statement [--> [ext-inter chess sport] competition],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]}
{:statement [pred-impl
[seq-conj [--> chess [ind-var X]] [:interval 1000]]
[--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [</>
[seq-conj [--> chess [ind-var X]] [:interval 1000]]
[--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.44751381215469616]}
{:statement [retro-impl
[--> sport [ind-var X]]
[seq-conj [--> chess [ind-var X]] [:interval 1000]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [seq-conj
[--> chess [dep-var Y]]
[:interval 1000]
[--> sport [dep-var Y]]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]})
['{:statement [--> sport competition]
:truth [1 0.9]
:task-type :judgement
:occurrence 1000}
'{:statement [--> chess competition]
:truth [1 0.9]
:occurrence 0}]))
{:statement [--> [ext-set A] [int-set B]]
:truth [1 0.9]
:occurrence 1}]))
+106 -11
View File
@@ -1,24 +1,119 @@
(ns nal.test.deriver
(:require [clojure.test :refer :all]
[nal.deriver :refer :all]
[nal.deriver.rules :refer [compile-rules]]
[nal.rules :as r]))
(def rules
(compile-rules '([(P --> M) (S --> M) |- (S <-> P)
:post (:t/comparison :d/weak :allow-backward)
:pre ((:!= S P))])))
(def result
'({:statement [</>
[seq-conj [--> chess competition] [:interval 1000]]
[--> sport competition]]
:task-type :belief
:occurrence 1000
:truth [1.0 0.44751381215469616]}
{:statement [seq-conj
[--> chess competition]
[:interval 1000]
[--> sport competition]]
:task-type :belief
:occurrence 1000
:truth [1.0 0.81]}
{:statement [pred-impl
[seq-conj [--> chess competition] [:interval 1000]]
[--> sport competition]]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [retro-impl
[--> sport competition]
[seq-conj [--> chess competition] [:interval 1000]]]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [--> sport chess]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [--> chess sport]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [<=> [--> chess [ind-var X]] [--> sport [ind-var X]]]
:task-type :belief
:occurrence 1000
:truth [1.0 0.44751381215469616]}
{:statement [conj [--> chess [dep-var Y]] [--> sport [dep-var Y]]]
:task-type :belief
:occurrence 1000
:truth [1.0 0.81]}
{:statement [<-> sport chess]
:task-type :belief
:occurrence 1000
:truth [1.0 0.44751381215469616]}
{:statement [==> [--> chess [ind-var X]] [--> sport [ind-var X]]]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [==> [--> chess [ind-var X]] [--> sport [ind-var X]]]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [==> [--> sport [ind-var X]] [--> chess [ind-var X]]]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [==> [--> sport [ind-var X]] [--> chess [ind-var X]]]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [--> [int-dif chess sport] competition]
:task-type :belief
:occurrence 1000
:truth [0.0 0.81]}
{:statement [--> [| chess sport] competition]
:task-type :belief
:occurrence 1000
:truth [1.0 0.81]}
{:statement [--> [int-dif sport chess] competition]
:task-type :belief
:occurrence 1000
:truth [0.0 0.81]}
{:statement [--> [ext-inter chess sport] competition]
:task-type :belief
:occurrence 1000
:truth [1.0 0.81]}
{:statement [pred-impl
[seq-conj [--> chess [ind-var X]] [:interval 1000]]
[--> sport [ind-var X]]]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [</>
[seq-conj [--> chess [ind-var X]] [:interval 1000]]
[--> sport [ind-var X]]]
:task-type :belief
:occurrence 1000
:truth [1.0 0.44751381215469616]}
{:statement [retro-impl
[--> sport [ind-var X]]
[seq-conj [--> chess [ind-var X]] [:interval 1000]]]
:task-type :belief
:occurrence 1000
:truth [1 0.44751381215469616]}
{:statement [seq-conj
[--> chess [dep-var Y]]
[:interval 1000]
[--> sport [dep-var Y]]]
:task-type :belief
:occurrence 1000
:truth [1.0 0.81]}))
(deftest test-generate-conclusions
(is (= (set [{:occurrence 1000
:statement '[<-> sport chess]
:task-type :judgement
:truth [1.0 0.44751381215469616]}])
(is (= (set result)
(set (generate-conclusions
(rules :judgement)
(r/rules :belief)
'{:statement [--> sport competition]
:truth [1 0.9]
:task-type :judgement
:task-type :belief
:occurrence 1000}
'{:statement [--> chess competition]
+29 -33
View File
@@ -64,11 +64,13 @@
(deftest test-expand-:from-element
(are [a1 a2] (= a1 (apply expand-:from-element a2))
'([--> k A1] [--> k A2] [--> k A3])
['[--> k :from/A] :from/A "A" 3]
'(([--> k A1] :id :id-A1)
([--> k A2] :id :id-A2)
([--> k A3] :id :id-A3))
['[[--> k :from/A] :id :id] :from/A "A" 3]
'([--> k [conj d A1]])
['[--> k [conj d :from/A]] :from/A "A" 1]))
'(([--> k [conj d A1]] :id :id-A1))
['[[--> k [conj d :from/A]] :id :id] :from/A "A" 1]))
(deftest test-generate-all-lists
(are [a1 a2] (= a1 (generate-all-lists a2))
@@ -77,58 +79,51 @@
(W --> B)
|-
(W --> (| B A1))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
:pre (:question?)
:post (:t/belief-structural-deduction :p/judgment)
:id :id-A1]
[(W --> (| B A1 A2))
(W --> B)
|-
(W --> (| B A1 A2))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
:pre (:question?)
:post (:t/belief-structural-deduction :p/judgment)
:id :id-A2]
[(W --> (| B A1 A2 A3))
(W --> B)
|-
(W --> (| B A1 A2 A3))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
:pre (:question?)
:post (:t/belief-structural-deduction :p/judgment)
:id :id-A3]
[(W --> (| B A1 A2 A3 A4))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
:pre (:question?)
:post (:t/belief-structural-deduction :p/judgment)
:id :id-A4]
[(W --> (| B A1 A2 A3 A4 A5))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4 A5))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
:pre (:question?)
:post (:t/belief-structural-deduction :p/judgment)
:id :id-A5]
[(W --> (| B A1 A2 A3 A4 A5 A6))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4 A5 A6))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
:pre (:question?)
:post (:t/belief-structural-deduction :p/judgment)
:id :id-A6]
[(W --> (| B A1 A2 A3 A4 A5 A6 A7))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4 A5 A6 A7))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)])
:pre (:question?)
:post (:t/belief-structural-deduction :p/judgment)
:id :id-A7])
'[(W --> (| B :list/A))
(W --> B)
|-
@@ -136,4 +131,5 @@
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]))
(:t/belief-structural-deduction :p/judgment)
:id :id]))
+18 -6
View File
@@ -27,7 +27,9 @@
(def trule (rule '[(D pred-impl R) (D --> K) |- ((K pred-impl R) :post (:t/abduction)
(R --> K) :post (:t/induction)
(K </> R) :post (:t/comparison))
:pre ((:!= R K))]))
:pre ((:!= R K))
:origin :ok
:id :ok]))
(deftest test-rule
(both-equal
@@ -35,26 +37,36 @@
:p2 (<-> S P),
:conclusions [{:conclusion (--> S P), :post (:t/struct-int :p/judgment)}],
:full-path [(--> :any :any) :and (<-> :any :any)],
:pre (:question?)}]
:pre (:question?)
:origin :ok
:id :ok}]
(rule '[(S --> P) (S <-> P) |- (S --> P) :post (:t/struct-int :p/judgment)
:pre (:question?)])
:pre (:question?)
:origin :ok
:id :ok])
'({:conclusions [{:conclusion (pred-impl K R)
:post (:t/abduction)}]
:full-path [(pred-impl :any :any) :and (--> :any :any)]
:p1 (pred-impl D R)
:p2 (--> D K)
:pre ((:!= R K))}
:pre ((:!= R K))
:origin :ok
:id :ok}
{:conclusions [{:conclusion (--> R K)
:post (:t/induction)}]
:full-path [(pred-impl :any :any) :and (--> :any :any)]
:p1 (pred-impl D R)
:p2 (--> D K)
:pre ((:!= R K))}
:pre ((:!= R K))
:origin :ok
:id :ok-2}
{:conclusions [{:conclusion (</> K R)
:post (:t/comparison)}]
:full-path [(pred-impl :any :any) :and (--> :any :any)]
:p1 (pred-impl D R)
:p2 (--> D K)
:pre ((:!= R K))})
:pre ((:!= R K))
:origin :ok
:id :ok-3})
trule))
+173 -126
View File
@@ -20,27 +20,40 @@
(deftest test-premure-op
(both-equal
'([=|> A B] [retro-impl A B] [==> A B] [pred-impl A B])
(permute-op '[==> A B] implications)
'(([=|> A B] :id :id-perm-concur-impl)
([retro-impl A B] :id :id-perm-retro-impl)
([==> A B] :id :id-perm-impl)
([pred-impl A B] :id :id-perm-pred-impl))
(permute-op '[[==> A B] :id :id] implications)
'([=|> A B] [retro-impl A B] [==> A B] [pred-impl A B])
(permute-op '[=|> A B] implications)
'(([=|> A B] :id :id-perm-concur-impl)
([retro-impl A B] :id :id-perm-retro-impl)
([==> A B] :id :id-perm-impl)
([pred-impl A B] :id :id-perm-pred-impl))
(permute-op '[[=|> A B] :id :id] implications)
'(conj &| seq-conj) (permute-op '&| conjunctions)))
'((conj :id :id-perm-conj)
(&| :id :id-perm-parallel-conj)
(seq-conj :id :id-perm-seq-conj))
(permute-op '[&| :id :id] conjunctions)))
(def rule1
'{:p1 (==> M P)
:p2 (==> S M)
:conclusions [{:conclusion (==> S P)
:post (:t/deduction :order-for-all-same :allow-backward)}]
:full-path [(==> :any :any) :and (==> :any :any)]})
:full-path [(==> :any :any) :and (==> :any :any)]
:origin :rule1
:id :rule1})
(def rule2
'{:p1 (==> M P)
:p2 (==> S M)
:conclusions [{:conclusion (==> S P)
:post (:allow-backward)}]
:full-path [(==> :any :any) :and (==> :any :any)]})
:full-path [(==> :any :any) :and (==> :any :any)]
:origin :rule2
:id :rule2})
(def rule3
'{:p1 (==> S M)
@@ -49,137 +62,171 @@
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(==> :any :any) :and (==> (conj :any :any) :any)]})
:full-path [(==> :any :any) :and (==> (conj :any :any) :any)]
:origin :rule3
:id :rule3})
(deftest test-order-for-all-same?
(is ((complement nil?) (order-for-all-same? rule1)))
(is (nil? (order-for-all-same? rule2))))
(def res1
'({:p1 (=|> M P),
:p2 (=|> S M),
:conclusions [{:conclusion (=|> S P),
:post (:t/deduction :order-for-all-same :allow-backward)}],
:full-path [(=|> :any :any) :and (=|> :any :any)],
'({:p1 (=|> M P)
:p2 (=|> S M)
:conclusions [{:conclusion (=|> S P)
:post (:t/deduction :order-for-all-same :allow-backward)}]
:full-path [(=|> :any :any) :and (=|> :any :any)]
:origin :rule1
:id :rule1-perm-concur-impl
:pre nil}
{:p1 (==> M P),
:p2 (==> S M),
:conclusions [{:conclusion (==> S P),
:post (:t/deduction :order-for-all-same :allow-backward)}],
:full-path [(==> :any :any) :and (==> :any :any)],
{:p1 (pred-impl M P)
:p2 (pred-impl S M)
:conclusions [{:conclusion (pred-impl S P)
:post (:t/deduction :order-for-all-same :allow-backward)}]
:full-path [(pred-impl :any :any) :and (pred-impl :any :any)]
:origin :rule1
:id :rule1-perm-pred-impl
:pre nil}
{:p1 (retro-impl M P),
:p2 (retro-impl S M),
:conclusions [{:conclusion (retro-impl S P),
:post (:t/deduction :order-for-all-same :allow-backward)}],
:full-path [(retro-impl :any :any) :and (retro-impl :any :any)],
{:p1 (retro-impl M P)
:p2 (retro-impl S M)
:conclusions [{:conclusion (retro-impl S P)
:post (:t/deduction :order-for-all-same :allow-backward)}]
:full-path [(retro-impl :any :any) :and (retro-impl :any :any)]
:origin :rule1
:id :rule1-perm-retro-impl
:pre nil}
{:p1 (pred-impl M P),
:p2 (pred-impl S M),
:conclusions [{:conclusion (pred-impl S P),
:post (:t/deduction :order-for-all-same :allow-backward)}],
:full-path [(pred-impl :any :any) :and (pred-impl :any :any)],
{:p1 (==> M P)
:p2 (==> S M)
:conclusions [{:conclusion (==> S P)
:post (:t/deduction :order-for-all-same :allow-backward)}]
:full-path [(==> :any :any) :and (==> :any :any)]
:origin :rule1
:id :rule1-perm-impl
:pre nil}))
(def res2
'({:p1 (pred-impl S M),
:p2 (pred-impl (seq-conj S :list/A) M),
:conclusions [{:conclusion (pred-impl (seq-conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(pred-impl :any :any) :and (pred-impl (seq-conj :any :any) :any)],
:pre nil}
{:p1 (retro-impl S M),
:p2 (retro-impl (seq-conj S :list/A) M),
:conclusions [{:conclusion (retro-impl (seq-conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(retro-impl :any :any)
:and
(retro-impl (seq-conj :any :any) :any)],
:pre nil}
{:p1 (=|> S M),
:p2 (=|> (seq-conj S :list/A) M),
:conclusions [{:conclusion (=|> (seq-conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(=|> :any :any) :and (=|> (seq-conj :any :any) :any)],
:pre nil}
{:p1 (pred-impl S M),
:p2 (pred-impl (conj S :list/A) M),
:conclusions [{:conclusion (pred-impl (conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(pred-impl :any :any) :and (pred-impl (conj :any :any) :any)],
:pre nil}
{:p1 (==> S M),
:p2 (==> (conj S :list/A) M),
:conclusions [{:conclusion (==> (conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(==> :any :any) :and (==> (conj :any :any) :any)],
:pre nil}
{:p1 (=|> S M),
:p2 (=|> (&| S :list/A) M),
:conclusions [{:conclusion (=|> (&| :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(=|> :any :any) :and (=|> (&| :any :any) :any)],
:pre nil}
{:p1 (retro-impl S M),
:p2 (retro-impl (conj S :list/A) M),
:conclusions [{:conclusion (retro-impl (conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(retro-impl :any :any) :and (retro-impl (conj :any :any) :any)],
:pre nil}
{:p1 (==> S M),
:p2 (==> (&| S :list/A) M),
:conclusions [{:conclusion (==> (&| :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(==> :any :any) :and (==> (&| :any :any) :any)],
:pre nil}
{:p1 (=|> S M),
:p2 (=|> (conj S :list/A) M),
:conclusions [{:conclusion (=|> (conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(=|> :any :any) :and (=|> (conj :any :any) :any)],
:pre nil}
{:p1 (pred-impl S M),
:p2 (pred-impl (&| S :list/A) M),
:conclusions [{:conclusion (pred-impl (&| :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(pred-impl :any :any) :and (pred-impl (&| :any :any) :any)],
:pre nil}
{:p1 (==> S M),
:p2 (==> (seq-conj S :list/A) M),
:conclusions [{:conclusion (==> (seq-conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(==> :any :any) :and (==> (seq-conj :any :any) :any)],
:pre nil}
{:p1 (retro-impl S M),
:p2 (retro-impl (&| S :list/A) M),
:conclusions [{:conclusion (retro-impl (&| :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(retro-impl :any :any) :and (retro-impl (&| :any :any) :any)],
:pre nil}))
'({:p1 (==> S M)
:p2 (==> (seq-conj S :list/A) M)
:conclusions [{:conclusion (==> (seq-conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(==> :any :any) :and (==> (seq-conj :any :any) :any)]
:origin :rule3
:id :rule3-perm-seq-conj-perm-impl
:pre nil}
{:p1 (==> S M)
:p2 (==> (&| S :list/A) M)
:conclusions [{:conclusion (==> (&| :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(==> :any :any) :and (==> (&| :any :any) :any)]
:origin :rule3
:id :rule3-perm-parallel-conj-perm-impl
:pre nil}
{:p1 (retro-impl S M)
:p2 (retro-impl (seq-conj S :list/A) M)
:conclusions [{:conclusion (retro-impl (seq-conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(retro-impl :any :any)
:and
(retro-impl (seq-conj :any :any) :any)]
:origin :rule3
:id :rule3-perm-seq-conj-perm-retro-impl
:pre nil}
{:p1 (retro-impl S M)
:p2 (retro-impl (conj S :list/A) M)
:conclusions [{:conclusion (retro-impl (conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(retro-impl :any :any) :and (retro-impl (conj :any :any) :any)]
:origin :rule3
:id :rule3-perm-conj-perm-retro-impl
:pre nil}
{:p1 (retro-impl S M)
:p2 (retro-impl (&| S :list/A) M)
:conclusions [{:conclusion (retro-impl (&| :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(retro-impl :any :any) :and (retro-impl (&| :any :any) :any)]
:origin :rule3
:id :rule3-perm-parallel-conj-perm-retro-impl
:pre nil}
{:p1 (pred-impl S M)
:p2 (pred-impl (conj S :list/A) M)
:conclusions [{:conclusion (pred-impl (conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(pred-impl :any :any) :and (pred-impl (conj :any :any) :any)]
:origin :rule3
:id :rule3-perm-conj-perm-pred-impl
:pre nil}
{:p1 (=|> S M)
:p2 (=|> (conj S :list/A) M)
:conclusions [{:conclusion (=|> (conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(=|> :any :any) :and (=|> (conj :any :any) :any)]
:origin :rule3
:id :rule3-perm-conj-perm-concur-impl
:pre nil}
{:p1 (=|> S M)
:p2 (=|> (seq-conj S :list/A) M)
:conclusions [{:conclusion (=|> (seq-conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(=|> :any :any) :and (=|> (seq-conj :any :any) :any)]
:origin :rule3
:id :rule3-perm-seq-conj-perm-concur-impl
:pre nil}
{:p1 (==> S M)
:p2 (==> (conj S :list/A) M)
:conclusions [{:conclusion (==> (conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(==> :any :any) :and (==> (conj :any :any) :any)]
:origin :rule3
:id :rule3-perm-conj-perm-impl
:pre nil}
{:p1 (=|> S M)
:p2 (=|> (&| S :list/A) M)
:conclusions [{:conclusion (=|> (&| :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(=|> :any :any) :and (=|> (&| :any :any) :any)]
:origin :rule3
:id :rule3-perm-parallel-conj-perm-concur-impl
:pre nil}
{:p1 (pred-impl S M)
:p2 (pred-impl (&| S :list/A) M)
:conclusions [{:conclusion (pred-impl (&| :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(pred-impl :any :any) :and (pred-impl (&| :any :any) :any)]
:origin :rule3
:id :rule3-perm-parallel-conj-perm-pred-impl
:pre nil}
{:p1 (pred-impl S M)
:p2 (pred-impl (seq-conj S :list/A) M)
:conclusions [{:conclusion (pred-impl (seq-conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(pred-impl :any :any) :and (pred-impl (seq-conj :any :any) :any)]
:origin :rule3
:id :rule3-perm-seq-conj-perm-pred-impl
:pre nil}))
(deftest test-generate-all-orders
(both-equal
@@ -0,0 +1,38 @@
(ns nal.test.rules.similarity-to-inheritance
(:require [nal.rules :as r]
[nal.deriver.rules :refer [generate-deriver]]
[nal.core :refer [inference]]
[clojure.test :refer :all]))
(defn get-rules [rules-name]
(generate-deriver (get-in r/rules [:origin rules-name])))
(def rules (get-rules :similarity-to-inheritance))
(deftest test-similarity-to-inheritance
(is (= '({:statement [--> S P]
:task-type :belief
:occurrence 1
:truth [1.0 0.81]})
(inference rules
'{:statement [--> S P]
:truth [1 0.9]
:task-type :question
:occurrence 1}
'{:statement [<-> S P]
:truth [1 0.9]
:occurrence 0})))
(is (= '()
(inference rules
'{:statement [--> S P]
:truth [1 0.9]
:task-type :belief
:occurrence 1}
'{:statement [<-> S P]
:truth [1 0.9]
:occurrence 0}))))
-50
View File
@@ -1,50 +0,0 @@
(ns narjure.test.memory.redis
(:require [clojure.test :refer :all]
[narjure.memory.redis :as r]
[narjure.memory.api :as m]
[taoensso.carmine :as c]))
(def config
{:pool {}
:spec {:host "127.0.0.1" :port 6379}})
(def mem (r/->RedisMemory config))
(def concept1 '[--> tim cat])
(def concept2 'tim)
(def truth1 {:frequency (float 0.9)
:confidence (float 0.2)
:occurrence 1})
(def truth2 {:frequency (float 0.5)
:confidence (float 0.8)
:occurrence 2})
(defn termlink [c]
{:priority (float 0.1)
:durability (float 0.1)
:quality (float 0.1)
:concept (str (hash c))})
(deftest test-redis
(c/wcar config (c/flushall))
(m/add-term mem concept1)
(is (= concept1 (m/term mem (hash concept1))))
(m/add-truth mem concept1 truth1)
(m/add-truth mem concept1 truth2)
(is (= (set [truth1 truth2])
(set (map #(dissoc % :id) (m/truths mem concept1)))))
(m/remove-truth mem concept1 (:id (last (m/truths mem concept1))))
(is (:id (first (m/truths mem concept1))))
(is ((set [truth1 truth2]) (dissoc (first (m/truths mem concept1)) :id)))
(m/add-term mem concept2)
(m/add-termlink mem concept1 (termlink concept2))
(is (= [(termlink concept2)]
(map #(dissoc % :id) (m/termlinks mem concept1))))
(is (= concept2 (m/term mem (:concept (first (m/termlinks mem concept1)))))))