tag:blogger.com,1999:blog-55134029046232435392024-03-05T23:36:57.552-08:00High order disfunctions - Disfunzioni di ordine superiorepizzicohttp://www.blogger.com/profile/06456252975067678649noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-5513402904623243539.post-1548005801737760742016-01-29T02:48:00.004-08:002016-01-29T02:49:36.405-08:00Marvin Minsky, one of the early pioneers of neural networks and frame language, died on Sunday at the age of 88What can I say?<br />
Minsky was friends with Richard Feynman, Isaac Asimov, Arthur C. Clarke, and Stanley Kubrick. He studied under Claude Shannon, worked with Alan Turing, had frequent conversations with John Von Neumann, and had lunch with Albert Einstein.<br />
<div>
<br />
<div>
R.I.P.</div>
</div>
pizzicohttp://www.blogger.com/profile/06456252975067678649noreply@blogger.com0tag:blogger.com,1999:blog-5513402904623243539.post-46165549579119983692014-12-09T11:13:00.001-08:002014-12-10T08:41:23.284-08:00Frame language: the knowledge based computer aided design (KBCAD) example (part 2)To obtain the graphics rendering we have seen in <a href="http://highorderdysfunctions.blogspot.com/2014/12/frame-language-knowledge-based-computer.html" target="_blank">Fig. 2 </a>of my previous post on KBCAD we need to :<br />
<br />
<ol>
<li>define 2D rendering functions for the prototype frames;</li>
<li>modify the prototype frames' structure;</li>
<li>get a graphics library for Clojure standalone application;</li>
</ol>
<h3>
1. 2D rendering functions for the prototype frames</h3>
<div>
We have 3 metaframes or prototypes:</div>
<div>
<ul>
<li>Building units</li>
<li>Space units</li>
<li>Building elements</li>
</ul>
</div>
<div>
The simple code I wrote to graphically render these prototypes is the following </div>
<div>
<div class="gistLoad" data-id="aa5951a431064d029cd6" id="gist-aa5951a431064d029cd6">
Loading ....</div>
<br />
The graphical representation of Building and Space units is simply the drawing of the string of their names at the snap points. The stored functions render2dbu and render2dsu, will see very soon how these functions are "stored" and used, do two things:</div>
<div>
<br />
<ul>
<li>call drawcolostring, we'll see soon its implementation, passing the string to be drawn from the :name slot of f; </li>
<li>invoke the execution of the procedure stored in the slot :render2d and facet :proc of all the components (found in the :cof slot) of f. </li>
</ul>
</div>
<div>
For what concern the 2d rendering of the "be" or building unit prototype, since in our KB we have only walls as building elements, the function render2dbe is a simple function that draws a colored line segments of variable width between the two snap points of each wall.<br />
Note that the render2dbe get color and width from the isa frame of the current frame and obtains the snap points from the current frame.</div>
<h3>
2. The prototype frames' structure</h3>
To add the rendering capabilities to our KB we need to:<br />
<br />
modify the ako frames "bu", "su" and "be" adding a :render2d slot with a :proc facet containing the rendering function render2dbu, renderd2dsu and render2dbe respectively;<br />
add the slot :color and :width to the isa frame "sw" (structural wall) and populate with (0 0 255) (= blue color in rgb code) and 10 (the width of a structural wall is set to 10 pixels);<br />
add the slot :color and :width to the isa frame "pw" (structural wall) and populate with (100 100 255) (= light blue color in rgb code) and 5(the width of a partition wall is set to 5pixels).<br />
<br />
<div class="gistLoad" data-id="353c615dcf32733fa91c" id="gist-353c615dcf32733fa91c">
Loading ....</div>
<h3>
3. Graphics library for Clojure </h3>
Even if there are already many Clojure wrappers for graphics library, I found that a real graphics library in clojure is still missing.<br />
I'm not so presumptuous to start from scratch a real graphics library by myself. Nevertheless, I did my best to write a simple wrapper around Java swing that will eventually grow in a real graphics library. For the moment, only few simple graphics primitive has been implemented and there is a first very spartan draft of a <a href="https://www.siggraph.org/education/materials/HyperGraph/viewing/view2d/pwint.htm" target="_blank">window to viewport transformation</a> .<br />
<br />
<div class="gistLoad" data-id="3f361142ccf2e65e7d1b" id="gist-3f361142ccf2e65e7d1b">
Loading ....</div>
<br />
To play with what we have seen until here and something more that we'll see in future posts you can clone the repo <a href="https://github.com/capitancook/clj-fl-examples.git">https://github.com/capitancook/clj-fl-examples.git</a> and follow the instruction in the readme.<br />
In the repo you'll find the implementation of some query of the KB to get calculated property of the building frames such as the area.<br />
Hope you'll enjoy playing with this example of the Frame Language library for the Clojure ecosystem.<br />
Comments, opinions and suggestions are greatly appreciated :)<br />
<script src="https://cdn.rawgit.com/moski/gist-Blogger/master/public/gistLoader.js" type="text/javascript"></script><br />pizzicohttp://www.blogger.com/profile/06456252975067678649noreply@blogger.com0tag:blogger.com,1999:blog-5513402904623243539.post-80015525109048271802014-12-04T04:01:00.000-08:002014-12-04T04:01:30.929-08:00Frame language: the knowledge based computer aided design (KBCAD) example (part 1)Create a new project with<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">lein new app kbcad</span><br />
<br />
Modify the project.clj file so that it contains at least the following dependencies:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">[clj-fl "0.1.0-prealfa9"] ;1</span><br />
<span style="font-family: Courier New, Courier, monospace;">[adamclements/vijual "0.3.0-SNAPSHOT"]] ;2</span><br />
<div>
<br /></div>
<div>
Where, the first dependency is the Frame language library and the second is <i>Vijual, </i>a graph layout engine.</div>
<div>
<br /></div>
Start the repl in the project directory with:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">lein repl</span><br />
<br />
Create a new namespace called clj-fl-e and "require" the necessary namespaces:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">(ns clj-fl-e</span><br />
<span style="font-family: Courier New, Courier, monospace;"> (:require [clj-fl.core :refer :all]</span><br />
<span style="font-family: Courier New, Courier, monospace;"> [vijual :as v]))</span><br />
<br />
This use case, inspired to this paper <a href="http://cumincad.architexturez.net/system/files/pdf/63d0.content.pdf" target="_blank">"Constraint-bounded design search"</a>, is based on the knowledge base (KB) stored in the following gist:<br />
<br />
<div class="gistLoad" data-id="da8ff741afa4734dd7b9" id="gist-da8ff741afa4734dd7b9">
Loading ....</div>
<br />
To feed the FL system with the previous KB you can download the gist and load it at th REPL with the following function:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">(load-kb-vec "filename")</span><br />
<br />
Or you can load the raw gist in one shot giving the address of the raw gist to the load-kb-vec function:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">(load-kb-vec "https://gist.githubusercontent.com/capitancook/da8ff741afa4734dd7b9/raw/d05a4c4db676c295cfc3ed405448373342e4b3c0/kb-kbcad1.clj</span><span style="font-family: 'Courier New', Courier, monospace;">")</span><br />
<br />
At the beginning of this KB we have the definition of the metaclasses or "ancestor" frames:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">;------ AKO frames <br />
<br />
{:frame {:value "bu"} <br />
:name {:value "Building Unit"} <br />
:description {:value "A man-made structure with floors, roofs and walls standing more or</span><br />
<div>
<span style="font-family: Courier New, Courier, monospace;"> less </span><span style="font-family: Courier New, Courier, monospace;">permanently in one place. Can be </span><span style="font-family: 'Courier New', Courier, monospace;">comprised of other building unit</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> and/or space units"}} </span><br />
<div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><br />
{:frame {:value "su"} <br />
:name {:value "Space Unit"} <br />
:description {:value "A space unit is a building unit component. Is composed of building </span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> elements"}} <br />
<br />
{:frame {:value "be"} <br />
:name {:value "Building Element"} <br />
:description {:value "BEs are the physical elements that define the BU and SU. For example,</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> a wall is a BE, a pillar is a BE and so on"}}</span><br />
<div>
<br />
Here, a generic building has been modelled with the frame <span style="font-family: Courier New, Courier, monospace;">"bu"</span>, a building unit, that can be composed of other building unit and/or <span style="font-family: Courier New, Courier, monospace;">"su"</span>,space unit, frames.<br />
A <span style="font-family: Courier New, Courier, monospace;">"be"</span>, building element is any physical element that compose a space unit. For example a perimetral wall, a floor and a pillar are all "<span style="font-family: Courier New, Courier, monospace;">be"</span>.<br />
Then, we have the definition of the isa frames:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">;------ ISA frames <br />
<br />
{:frame {:value "flat"} <br />
:name {:value "Flat"} <br />
:description {:value "A flat"} <br />
:ako {:value "bu"}} <br />
<br />
{:frame {:value "room"} <br />
:name {:value "Room"} <br />
:description {:value "A room"} <br />
:ako {:value "su"}} <br />
<br />
{:frame {:value "sw"} <br />
:name {:value "Structural Wall"} <br />
:description {:value "A structural wall is schematized as a line thicker than a partition </span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> wall"} <br />
:ako {:value "be"}} <br />
</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">{:frame {:value "pw"} <br />
:name {:value "Partition Wall"} <br />
:description {:value "A partition wall is schematized as a line thinner than a structural </span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"> wall"} <br />
:ako {:value "be"}}</span><br />
<br />
Here we can see that a <span style="font-family: Courier New, Courier, monospace;">"flat"</span> is a <span style="font-family: Courier New, Courier, monospace;">"bu"</span>, while a <span style="font-family: Courier New, Courier, monospace;">"room" </span>is a <span style="font-family: Courier New, Courier, monospace;">"su"</span> and a structural wall <span style="font-family: Courier New, Courier, monospace;">"sw"</span> and a partition wall <span style="font-family: Courier New, Courier, monospace;">"pw"</span> are <span style="font-family: Courier New, Courier, monospace;">"be"</span>.<br />
Then we have the instances of the isa frames where we can see that our "flat" <span style="font-family: Courier New, Courier, monospace;">"flat1"</span> is comprised by the frames <span style="font-family: Courier New, Courier, monospace;">"kitchen1"</span>, <span style="font-family: Courier New, Courier, monospace;">"bathroom1"</span> and <span style="font-family: Courier New, Courier, monospace;">"bedroom1"</span>. These frames are listed in the <span style="font-family: Courier New, Courier, monospace;">:cof</span>, comprised-of, slot of the <span style="font-family: Courier New, Courier, monospace;">"flat1"</span> frame.<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">{:frame {:value "flat1"} <br />
:name {:value "Flat 1"} <br />
:isa {:value "flat"} <br />
:cof {:value ("kitchen1" "bedroom1" "bathroom1")}}</span><br />
<br />
Each space unit is described in terms of the walls that surround the unit and in terms of the building unit that composes.<br />
For example <span style="font-family: Courier New, Courier, monospace;">"bedroom1"</span> is comprised of 5 walls <span style="font-family: Courier New, Courier, monospace;">("w5" "w4" "w8" "w7" "w6")</span>, the values of the <span style="font-family: Courier New, Courier, monospace;">:cof</span> slot, and compose the <span style="font-family: Courier New, Courier, monospace;">"flat1" </span>building unit frame contained in the facet <span style="font-family: Courier New, Courier, monospace;">:value</span> of the :isi slot<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">{:frame {:value "bedroom1"} <br />
:name {:value "Bedroom 1"} <br />
:isa {:value "room"} <br />
:cof {:value ("w5" "w4" "w8" "w7" "w6")} </span><br />
<span style="font-family: Courier New, Courier, monospace;"> :isi {:value "flat1"}}</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><br />
</span>The walls are the building elements that physically realise the space units of our knowledge base. There are two kind of walls: perimetral and partition walls. The first is usually thicker and stronger that the latter.<br />
Each wall is defined in terms of the space unit that composes, the <span style="font-family: Courier New, Courier, monospace;">:isi</span> slot, and the two snap points, the <span style="font-family: Courier New, Courier, monospace;">:sps</span> slot, that define the beginning and the end points of the wall.<br />
The tree diagram of our knowledge base is the following:<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXbGVZr1dLRwzv8hYvIHUMeRFHNck-1LnHizQI7crDoMv6OrNnAJqufsWweKXO5e8k5HtNBT1oXGZOd6oIKSXoZnT_eCjjtTZl4uGwEBeOc-ot7YHhjbMEpDULXFTpGl-gwxtRFy0RB9Q/s1600/kbcad1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXbGVZr1dLRwzv8hYvIHUMeRFHNck-1LnHizQI7crDoMv6OrNnAJqufsWweKXO5e8k5HtNBT1oXGZOd6oIKSXoZnT_eCjjtTZl4uGwEBeOc-ot7YHhjbMEpDULXFTpGl-gwxtRFy0RB9Q/s1600/kbcad1.png" height="201" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig. 1 - Tree graph of the KB</td></tr>
</tbody></table>
<br />
At the REPL you can, for example, query the KB asking what are the snap points of the wall <span style="font-family: Courier New, Courier, monospace;">"w1"</span> with the following function:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">clj-fl-examples>(fget "w1" :sps :value)</span><br />
<span style="font-family: Courier New, Courier, monospace;">((0 5) (0 0))</span><br />
<br />
But, if you ask the description slot of the same frame you obtain nil. We can try to see if the slot <span style="font-family: Courier New, Courier, monospace;">"w1"</span> inherited a slot <span style="font-family: Courier New, Courier, monospace;">:description </span>from is <span style="font-family: Courier New, Courier, monospace;">:isa</span> or ancestor frame using the following function:<br />
<br />
<span style="font-family: 'Courier New', Courier, monospace;">clj-fl-examples>(fget-i "w1" :description :value)</span><br />
<span style="font-family: Courier New, Courier, monospace;">"A structural wall is schematized as a line thicker than a partition wall"</span><br />
<br />
Since <span style="font-family: Courier New, Courier, monospace;">"w1"</span> is a <span style="font-family: Courier New, Courier, monospace;">"sw"</span> frame, the system returned the <span style="font-family: Courier New, Courier, monospace;">:description</span> slot of the <span style="font-family: Courier New, Courier, monospace;">"sw"</span>, structural wall, frame.<br />
<br />
Let's add some spices to this example.<br />
<br />
FL supports the use of stored function (SF). They are normal Clojure functions that can exploit all the power of the FL and can be activated both directly that indirectly. The former are stored function that can be invoked directly by the user of the KB while the latter are indirectly activated when certains triggers are fired. A simple function <span style="font-family: Courier New, Courier, monospace;">showcofs </span>that extracts the tree graph of the :cof relation for a given frame <span style="font-family: Courier New, Courier, monospace;">f</span> and that can be stored in the <span style="font-family: Courier New, Courier, monospace;">:shocofs</span> slot of <span style="font-family: Courier New, Courier, monospace;">f</span> or in a <span style="font-family: Courier New, Courier, monospace;">f</span>'s meaframe, is the following:<br />
<br />
<div class="gistLoad" data-id="7861920eb52e5dd7f43a" id="gist-7861920eb52e5dd7f43a">
Loading ....</div>
<br />
In 1 the list of the <span style="font-family: Courier New, Courier, monospace;">:cof </span>frames is stored in <span style="font-family: Courier New, Courier, monospace;">cofslist</span>. In 2, if <span style="font-family: Courier New, Courier, monospace;">cofslist </span>is empty, i.e. there are no comprised-of frames of <span style="font-family: Courier New, Courier, monospace;">f</span>, <span style="font-family: Courier New, Courier, monospace;">f</span> is returned in a vector. In 3, when <span style="font-family: Courier New, Courier, monospace;">cofslist</span> is not empty, step number 2 is "reduced" accumulating the results in a vector of vector. Note that in the generic reducing step, <span style="font-family: Courier New, Courier, monospace;">showproc</span> is initialized to <span style="font-family: Courier New, Courier, monospace;">(fget-ii currentframe :showcofs :proc)</span>. This means that we can have different function stored in different metaframe and that the user doesn't need to know about the function <span style="font-family: Courier New, Courier, monospace;">showcofs</span>. In fact, the user can invoke the extraction of the tree graph using:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">((eval (fget-ii "flat1" :showcofs :proc)) "flat1")</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><br /></span>To test what we have done up to this point you can copy&paste the <span style="font-family: Courier New, Courier, monospace;">showcofs </span>function from the above box to the REPL and load the KB kb-kbcad2.clj that contains a modified version of kb-kbcad1.clj with the addition of a :showcofs slot in the metaframes <span style="font-family: Courier New, Courier, monospace;">"bu"</span>, <span style="font-family: Courier New, Courier, monospace;">"su"</span> and <span style="font-family: Courier New, Courier, monospace;">"be"</span>. To load the new KB copy&paste the following function at the REPL:<br />
<br />
<span style="font-family: 'Courier New', Courier, monospace;">(load-kb-vec "</span><span style="font-family: 'Courier New', Courier, monospace;">https://gist.githubusercontent.com/capitancook/ec3839d32b4d4d33b636/raw/b384ffe5c7e9b781923d3ff87f6bd53152966a5a/kb-kbcad2.clj")</span><br />
<br />
Now, if you type at the REPL<br />
<br />
<span style="font-family: 'Courier New', Courier, monospace;">((eval (fget-ii "flat1" :showcofs :proc)) "flat1")</span><br />
<br />
you'll obtain the following representation of the graph tree of the :cof relation in our KB:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">["flat1" ["kitchen1" ["w1"] ["w2"] ["w3"] ["w4"]] ["bedroom1" ["w5"] ["w4"] ["w8"] ["w7"] ["w6"]] ["bathroom1" ["w3"] ["w10"] ["w9"] ["w8"]]]</span><br />
<br />
To have more information about this particular format used to represent graphs by means of vectors structure, please go to the <a href="http://lisperati.com/vijual/" target="_blank">lisperati web site</a>.<br />
Anyway, pretty-printing the last result you can easily recognize the tree structure of the result:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">(clojure.pprint/pprint *1)</span><br />
<span style="font-family: Courier New, Courier, monospace;">["flat1"</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ["kitchen1" ["w1"] ["w2"] ["w3"] ["w4"]]</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ["bedroom1" ["w5"] ["w4"] ["w8"] ["w7"] ["w6"]]</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ["bathroom1" ["w3"] ["w10"] ["w9"] ["w8"]]]</span><br />
<br />
And you can see a cute ascii art layout of the graph tree typing the following function:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">(v/draw-tree *1)</span><br />
<br />
We have already seen the result of this function in the above Fig.1.<br />
<br />
Ascii art is cute but a CAD should be able to graphically render a design like the one in Fig. 2<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir81hSZ1oEFtyZqEOLIgzPce9MU3mcOtSuUqrFp-NuZg9TNqes1W29m2Yng-DtabvtyzS35XGYNtw3jsRyUio-WIO-OfFTrisM40sL9Nh0_IDVBPD39sv0DtM3k06gSydzjFjW86BXjjo/s1600/flat1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir81hSZ1oEFtyZqEOLIgzPce9MU3mcOtSuUqrFp-NuZg9TNqes1W29m2Yng-DtabvtyzS35XGYNtw3jsRyUio-WIO-OfFTrisM40sL9Nh0_IDVBPD39sv0DtM3k06gSydzjFjW86BXjjo/s1600/flat1.png" height="310" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig. 2 - 2d rendering of the "bu" frame "flat1"</td></tr>
</tbody></table>
Graphic rendering and more on FL stored procedure in the next post.<br />
<br />Hope you enjoyed today's post!Thanks for reading.<script src="https://cdn.rawgit.com/moski/gist-Blogger/master/public/gistLoader.js" type="text/javascript"></script></div>
</div>
</div>
</div>
pizzicohttp://www.blogger.com/profile/06456252975067678649noreply@blogger.com0Castelnuovo di Porto RM, Italia42.1270312 12.50071909999996942.1034757 12.46037859999997 42.1505867 12.541059599999969tag:blogger.com,1999:blog-5513402904623243539.post-63484499698301398502014-11-19T06:02:00.000-08:002014-11-19T09:48:50.414-08:00Clj-fl is out!After a lot of fun I had playing with the Frame Language, I decided to put together all the toys I've built in a Clojure library.<br />
Clj-fl is my first Clojure library. It is on <a href="https://github.com/capitancook/clj-fl" target="_blank">github </a>and <a href="https://clojars.org/clj-fl" target="_blank">clojars</a>.<br />
<br />
<h3>
Examples</h3>
The repo on github includes two simple knowledge bases as examples you can play with.<br />
Assuming you have <a href="http://leiningen.org/" target="_blank">Leiningen </a>installed, start a REPL in the example directory with:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">lein repl</span><br />
<br />
Then, at the repl prompt, type<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">(showorgalfa)</span><br />
<br />
to load the OrgAlfa use case, where the frame language is used to model a simple knowledge based ERP. The OrgAlfa is a hierarchical organisation similar to the European Commission structure.<br />
<br />
Or you can type:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">(showkbcad)</span><br />
<br />
to load the kbcad use case, a simple knowledge based Computer Aided Design system.<br />
<br />
After each of the previous command/function you can play around with the KB you loaded.<br />
<br />
<h3>
Using it</h3>
<blockquote class="tr_bq">
<i>Please note that clj-fl is pre-alpha software!</i></blockquote>
Even if this library is still not ready for production, to use it, assuming you have <a href="http://leiningen.org/" target="_blank">Leiningen </a>installed, your project.clj should include something like the following:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">(defproject foo "0.1.0-SNAPSHOT"</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ;...</span><br />
<span style="font-family: Courier New, Courier, monospace;"> :dependencies [[org.clojure/clojure "1.6.1"]</span><br />
<span style="font-family: Courier New, Courier, monospace;"> [clj-fl "0.1.0-prealfa5"] </span><br />
<span style="font-family: Courier New, Courier, monospace;">; please check on Clojars.org for the latest version</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ...]</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ;...</span><br />
<span style="font-family: Courier New, Courier, monospace;"> )</span><br />
<div>
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
pizzicohttp://www.blogger.com/profile/06456252975067678649noreply@blogger.com0Castelnuovo di Porto RM, Italia42.1270312 12.50071909999996942.1034757 12.46037859999997 42.1505867 12.541059599999969tag:blogger.com,1999:blog-5513402904623243539.post-27883281346129112872014-06-06T09:06:00.000-07:002014-11-19T06:44:36.278-08:00Frame language in Clojure (part 2)<div class="MsoNormal">
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: right; margin-left: 1em; text-align: right;"><tbody>
<tr><td style="text-align: center;"><a href="http://people.csail.mit.edu/phw/pensees/welcomenow.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://people.csail.mit.edu/phw/pensees/welcomenow.jpg" height="150" width="200" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><a href="http://people.csail.mit.edu/phw/" target="_blank">Prof. Patrick Winston</a> </td></tr>
</tbody></table>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
As we saw in the previous <a href="http://highorderdysfunctions.blogspot.it/2014/02/frame-language-in-clojure-can-young-dog.html" target="_blank">post </a> on Frame Language (FL), frames are not only nested associative data structures. but extend the power of a property list adding more sophisticated mechanisms as:</div>
<div style="text-align: justify;">
</div>
<ul>
<li>default values;</li>
<li>demons;</li>
<li>inheritance.</li>
</ul>
<br /></div>
<div class="MsoNormal">
<div style="text-align: justify;">
<u><b>Default values</b></u></div>
<div style="text-align: justify;">
<u><b><br /></b></u></div>
<div style="text-align: justify;">
Default value are very useful when you don't have an information (i.e. a slot in FL) about a certain object, but it is somehow known a default value that can be used. </div>
<div style="text-align: justify;">
It is 8:00 a.m and Bob, a freshly recruited emplyee, have to start his first day of work at our organisation. We need to create a new frame for Bob while we don't know yet what will be is role in the organization (that is the <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:is-a</span> slot). In this case we could leverage the default mechanism and describe Bob as follows:</div>
<br />
<span style="font-size: xx-small;"><span style="font-family: Courier New, Courier, monospace;">(def Bob {</span><span style="font-family: 'Courier New', Courier, monospace;">:is-a {:default 'employee}</span></span><br />
<span style="font-family: 'Courier New', Courier, monospace; font-size: xx-small;"> :working-at {:value 'unit-iii} </span><br />
<span style="font-size: xx-small;"><span style="font-family: 'Courier New', Courier, monospace;"> </span><span style="font-family: Courier New, Courier, monospace;">:</span><span style="font-family: 'Courier New', Courier, monospace;">recruitment-date {:value "20140307"}</span><span style="font-family: 'Courier New', Courier, monospace;">})</span></span><br />
<br />
<div style="text-align: justify;">
In this way, Bob can get his badge and pass through the security check to start his first day of work.</div>
<div style="text-align: justify;">
Since the <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:value </span>facet is not present in Bob's frame, all the queries on Bob's role, like<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> fget(Bob :is-a :value)</span>, would fail. The following function <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">fget-v-d</span> exploit the <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:default </span>facet to give an answer even in the case the only information we have is the default information:</div>
<div style="text-align: justify;">
<br /></div>
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">(defn fget-v-d [frame slot]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (let [v (fget frame slot :value)]; check if there is a :value facet</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (if v v (fget frame slot :default)))); if not, get :default facet</span><br />
<div>
<br /></div>
<div style="text-align: justify;">
Now, we can ask our KB about Bob role with <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(fget-v-d Bob :is-a) </span>and we'll discover that Bob is an employee :).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
At 9:00 a.m. when the competent department has established that Bob will work as accountant in the <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">unit-iii</span>, the <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:default</span> facet into the <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:is-a </span>slot can be removed and a new <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:value</span> facet will be added to the <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:is-a</span> slot. Then, the frame for Bob will be:</div>
<br />
<span style="font-size: xx-small;"><span style="font-family: Courier New, Courier, monospace;">{</span><span style="font-family: 'Courier New', Courier, monospace;">:is-a {</span><span style="font-family: Courier New, Courier, monospace;">:value 'accountant</span><span style="font-family: 'Courier New', Courier, monospace;">}</span></span><br />
<span style="font-family: 'Courier New', Courier, monospace; font-size: xx-small;"> :working-at {:value 'unit-iii} </span><br />
<span style="font-size: xx-small;"><span style="font-family: 'Courier New', Courier, monospace;"> </span><span style="font-family: Courier New, Courier, monospace;">:</span><span style="font-family: 'Courier New', Courier, monospace;">recruitment-date {:value "</span><span style="font-family: 'Courier New', Courier, monospace;">20140307</span><span style="font-family: 'Courier New', Courier, monospace;">"}</span><span style="font-family: 'Courier New', Courier, monospace;">}</span></span><br />
<br />
After 9:00 a.m. all the queries on Bob's role made with the same function <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">fget-v-d </span>will give the updated answer (i.e. accountant).<br />
<br />
<u><b>Demons</b></u><br />
<br />
Demons are functions stored in special facets attached to slots to cause side effects when the slot is accessed. For example, we can have:<span style="background-color: white; font-family: Times, serif; font-size: 16px;"></span><br />
<ul>
<li><span style="background-color: white; font-family: Times, serif; font-size: 16px;"><span style="background-color: transparent; font-family: Courier New, Courier, monospace; font-size: x-small;">:range </span><span style="background-color: transparent;">: demons are triggered if a new value is added to the slot, to check that the value added is permissible for this particular slot;</span></span></li>
<li><span style="background-color: white; font-family: Times, serif; font-size: 16px;"><span style="background-color: transparent; font-size: x-small;"><span style="font-family: Courier New, Courier, monospace;">:if-new </span></span><span style="background-color: transparent;">: demons are triggered when a new frame is created;</span></span></li>
<li><span style="background-color: white; font-family: Times, serif; font-size: 16px;"><span style="background-color: transparent; font-size: x-small;"><span style="font-family: Courier New, Courier, monospace;">:if-added </span></span><span style="background-color: transparent;">: demons are triggered when a new value is put into a slot;</span></span></li>
<li><span style="background-color: white; font-family: Times, serif; font-size: 16px;"><span style="background-color: transparent; font-family: Courier New, Courier, monospace; font-size: x-small;">:if-removed </span><span style="background-color: transparent;">: demons are triggered when a value is removed from a slot;</span></span></li>
<li><span style="background-color: white; font-family: Times, serif; font-size: 16px;"><span style="background-color: transparent; font-family: Courier New, Courier, monospace; font-size: x-small;">:if-replaced </span><span style="background-color: transparent;">: is triggered when a slot value is replaced.</span></span></li>
<li><span style="background-color: white; font-family: Times, serif; font-size: 16px;"><span style="background-color: transparent; font-family: Courier New, Courier, monospace; font-size: x-small;">:if-needed </span><span style="background-color: transparent;">: demons are triggered when there is no value present in an instance frame and a value must be computed from a generic frame.</span></span></li>
</ul>
</div>
<div class="MsoNormal">
<div style="text-align: justify;">
Each demon must follow a certain set of rule to guarantee the robustness and coherence of the Knowledge Base (KB).</div>
<div style="text-align: justify;">
A possible general rule is that every demon must accept only two parameters: the frame end the slot that caused its activation. Another rule , this time specific, may be that <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:if-needed</span> demons must compute the missing value and, before return the results of its computation, store it in the facet <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:value</span> of the frame and slot where it was missing.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Going back to our new employee Bob at his first day of work in our organization, let see an example of <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:range</span> demon that allows the memorization of a recruitment date only if it is not greater than today:</div>
<div style="text-align: justify;">
<br /></div>
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">(defn check-date [frame slot]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (let [date (fget frame slot :value)]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (if (after? (parse basic-formatter date ) (now))</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (do</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (alert "Future date are not allowed for the slot" slot " of the frame " frame)</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (fremove frame slot :value))</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> nil))</span><br />
<div>
<br />
<div style="text-align: center;">
<span style="font-size: xx-small;">**Note that <span style="font-family: Courier New, Courier, monospace;">after?</span> and <span style="font-family: Courier New, Courier, monospace;">parse</span> functions are part of the clj-time library.</span></div>
</div>
<div>
<div style="text-align: justify;">
To store this demon in Bob's frame we can use fput:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">(fput Bob :recruitment-date :range 'chek-date)</span></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Since check-date must be activated every time a value is put into the slot <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:recruitment-date</span> we need a modified version of fput that activate the :range and <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:if-added</span> demon as soon as it adds a new value for the <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">:recruitment-date</span> slot.</div>
<div style="text-align: justify;">
Here come the fput-p function:</div>
<br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">(defn fput-p</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> [frame slot facet v]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (assoc-in frame [slot facet] v) ; "standard" fput</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (if ((fget frame slot :range) frame slot) ; call :range demon</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> nil</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> ((fget frame slot :if-added) frame slot))) ; call :if-added demon</span><br />
<div>
<br /></div>
<br /></div>
<div class="MsoNormal">
<div style="text-align: justify;">
<b><u>Inheritance</u></b></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
One of the first requirements that FL was designed to satisfy was multiple inheritance. This was a natural consequence of the desire to model the world the way humans do. In fact, human conceptualizations of the world seldom fall into rigidly defined non-overlapping taxonomies. </div>
</div>
<div class="MsoNormal">
<div style="text-align: justify;">
There are two different kind of inheritance: a-kind-of (ako) and is-a (isa) inheritance. The former is a relation between two classes of object (e.g., accountant is a-kind-of employee). While, the latter is a relation between an object and its class (e.g., Bob is-a accountant). Here, in both cases, multiple inheritance is welcome.</div>
<div style="text-align: justify;">
Our good friend Bob is at his first day of work and, after all the rumor he has heard about it, he is happy to be finally able to know how much could be the real yearly bonus he can get if he is is a "good" accountant. So, he open his home page on the organization Intranet and discover that the yearly bonus for him is 3000.00 $. Not bad!</div>
<div style="text-align: justify;">
Let see how this has been made possible by the inheritance mechanism of the FL. All starts storing the bonus information for accountants in the "abstract" frame accountant:</div>
<div style="text-align: justify;">
<br /></div>
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">(def accountant {:ako {:value 'employee}</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> :bonus {:value 3000.00}})</span><br />
<div>
<br /></div>
<div style="text-align: justify;">
Then we need a modified version of fget that is able to look for a certain facet in all the class frames Bob is an instance of. </div>
</div>
<div class="MsoNormal">
<div style="text-align: justify;">
The following <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">fget-i</span> function can do this job quite well:</div>
<div style="text-align: justify;">
<br /></div>
</div>
<div class="MsoNormal">
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">(defn fget-i [frame slot]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (if-let [classes (fget frame :isa :value)]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (if (not(list? classes)) </span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (fget (eval classes) slot :value) </span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (fget-i1 classes slot))</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> nil))</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">(defn fget-i1[frames slot]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (if (nil? frames)</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> nil</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (if-let [value (fget (eval (first frames)) slot :value)]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> value</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"> (recur (next frames) slot))))</span><br />
<span style="font-size: xx-small;"> Note that:</span><br />
<ul>
<li><span style="font-size: xx-small;">fget-i examines only one level of inheritance. This means that (fget-i Bob :bonus) look for the :bonus slot only in the class accountant and don't even try to get it from the employee superclass.</span></li>
<li><span style="font-size: xx-small;">in case of multiple inheritance fget-i give stop examining the "mather" class as soon as it gets a non value from one class.</span></li>
</ul>
In this <a href="https://gist.github.com/capitancook/9248111" target="_blank">gist </a>you'll find all the code I translated in Clojure from chap. 22 of the great book <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0201083191/ref=ase_phw-20/104-0389352-5307936?v=glance&s=books" target="_blank">"Lisp"</a> by Prof. Henry Patrick Winston.<br />
<br />
Please feel free to comment and suggest improvements.</div>
</div>
pizzicohttp://www.blogger.com/profile/06456252975067678649noreply@blogger.com0Castelnuovo di Porto RM, Italia42.1270312 12.50071909999996942.1034757 12.46037859999997 42.1505867 12.541059599999969tag:blogger.com,1999:blog-5513402904623243539.post-73716220612915519402014-06-04T02:00:00.000-07:002014-11-19T06:45:08.994-08:00Frame Language in Clojure (part 1)<div class="MsoNormal">
<div style="text-align: justify;">
<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: right; margin-left: 1em; text-align: right;"><tbody>
<tr><td style="text-align: center;"><a href="http://upload.wikimedia.org/wikipedia/commons/thumb/2/28/Marvin_Minsky_at_OLPCb.jpg/300px-Marvin_Minsky_at_OLPCb.jpg" imageanchor="1" style="clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" src="http://upload.wikimedia.org/wikipedia/commons/thumb/2/28/Marvin_Minsky_at_OLPCb.jpg/300px-Marvin_Minsky_at_OLPCb.jpg" height="200" width="200" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><a href="http://web.media.mit.edu/~minsky/" rel="nofollow" target="_blank">Prof. Marvin Minsky</a></td></tr>
</tbody></table>
<a href="http://en.wikipedia.org/wiki/Frame_(artificial_intelligence)" target="_blank">Frames </a>and <a href="http://frame%20language/" rel="nofollow" target="_blank">Frame Language</a> (FL) are components of a framework used in the seventies to represent chunks of knowledge, see Prof. Marvin Minsky seminal paper <a href="http://web.media.mit.edu/~minsky/papers/Frames/frames.html" rel="nofollow" target="_blank">"A framework for representing knowledge"</a>. The first implementations of FL were based on Lisp. The interest about FL survived to all the "fashions of the moment" of the last four decades of AI research. It is actively used in knowledge representation for semantic Web. And, even Clojure has been surely influenced by this paradigm.<br />
<div class="MsoNormal" style="text-align: start;">
<br />
Prof. Winston, <span style="text-align: justify;">in his great book </span><a href="http://people.csail.mit.edu/phw/Books/#Lisp" rel="nofollow" style="text-align: justify;" target="_blank">"Lisp"</a><span style="text-align: justify;">,</span> introduced a simple and clear Lisp formalization of the Frame Language by means of which we could represent knowledge about Henry, a man working at the Unit-I of a certain organization as a System analyst, as follows:<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><br /></span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">setf </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">get </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">'</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Henry </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">'is-a) 'system-analyst)</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">setf </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">get </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">'</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Henry </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">'working-at) 'unit-i)</span></div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">setf </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">get </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">'</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Henry </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">'recruitment-date) "14/03/2010")</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">setf </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">(</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">get </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">'</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Henry </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">'gross-salary) 2500,00)</span><br />
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><br /></span></div>
<div style="text-align: justify;">
Ok. Nice. But what is a Frame?</div>
</div>
<div class="MsoNormal" style="text-align: start;">
<div style="text-align: justify;">
Lets have a look at the implementation of a Frame as proposed by Prof. Winston as a simple associative nested list in LISP:</div>
<br />
<span style="font-size: x-small;"><span style="font-family: Courier New, Courier, monospace;">(Henry </span><span style="font-family: Courier New, Courier, monospace;">(</span><span style="font-family: 'Courier New', Courier, monospace;">is-a </span><span style="font-family: Courier New, Courier, monospace;">(</span><span style="font-family: 'Courier New', Courier, monospace;">value </span><span style="font-family: 'Courier New', Courier, monospace;">system-analyst</span><span style="font-family: Courier New, Courier, monospace;">))</span></span><br />
<span style="font-size: x-small;"><span style="font-family: Courier New, Courier, monospace;"> (</span><span style="font-family: 'Courier New', Courier, monospace;">working-at</span><span style="font-family: Courier New, Courier, monospace;">(</span><span style="font-family: 'Courier New', Courier, monospace;">value </span><span style="font-family: 'Courier New', Courier, monospace;">unit-i</span><span style="font-family: Courier New, Courier, monospace;"> ))</span></span><br />
<span style="font-size: x-small;"><span style="font-family: Courier New, Courier, monospace;"> </span><span style="font-family: 'Courier New', Courier, monospace;">(recruitment-date (value "14/03/2010"))</span></span><br />
<span style="font-size: x-small;"><span style="font-family: Courier New, Courier, monospace;"> (</span><span style="font-family: Courier New, Courier, monospace;">gross-salary (value 3000,00)</span><span style="font-family: 'Courier New', Courier, monospace;">))</span></span><br />
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: start;">
<div style="text-align: justify;">
Probably, many Clojurians are now thinking: “Hey! Came on! We can do that using associative maps. At the end, these Frames are only property lists”.</div>
<div style="text-align: justify;">
That’s not wrong. For example, our old fellow Henry can be described using a nested associative map as follows:</div>
</div>
<div class="MsoNormal" style="text-align: start;">
<br /></div>
<div class="MsoNormal" style="text-align: start;">
<o:p></o:p></div>
<div style="text-align: start;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-size: x-small;">(def Henry {</span></span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">:is-a {:value 'system-analyst} </span></div>
<div style="text-align: start;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-size: x-small;"> :working-at {:value 'unit-i} </span></span></div>
<div style="text-align: start;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-size: x-small;"> :</span></span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">recruitment-date {:value "14/03/2010"} </span></div>
<div style="text-align: start;">
<span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> :</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">gross-salary {:value </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">3000,00}</span><span style="font-family: Courier New, Courier, monospace;"><span style="font-size: x-small;">})</span></span></div>
<div style="text-align: start;">
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<div style="text-align: start;">
Please, note that <span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">system-analyst </span>is itself a frame containing the FL description of a generic employee that works as a system analyst and, for example, has a certain base salary, health insurance, production bonus and other benefits.<br />
<br />
<div>
<div class="MsoNormal">
To get Henry’s salary we can simply do:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: x-small;"><span style="font-family: Courier New, Courier, monospace;">(-> Henry :gross-salary :value)</span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
But, hold on. Frames are not only associative data structures. For example, if we have another person named John that, at the time we are writing his description in FL, has an unknown salary:</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-size: x-small;">(def John {</span></span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">:is-a {:value 'system-analyst}</span><br />
<span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> :working-at {:value 'unit-i} </span><br />
<span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span style="font-family: Courier New, Courier, monospace;"><span style="font-size: x-small;">:years-in-the-role</span></span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> {:value 5}</span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">})</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><span style="font-size: x-small;"><br /></span></span></div>
<div class="MsoNormal">
If we try to use the same approach we used for Henry to get the salary information from the John's FL description we'll fail miserably.<br />
We could estimate John's base salary using some general knowledge about the base salary of a System Analyst with 5 years of experience in the role of system-analyst.<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
If we can get elsewhere what was the average salary for a system analyst, for example 2000,00 EURO, and if it is known that the annual increment is 3%, we could calculate John's base salary as follows:<br />
<br />
<span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">gross-salary</span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> = system-analyst-salary*(1+0,03)^(years-in-the-role) </span><br />
<br /></div>
<div class="MsoNormal">
And, if we could store somewhere the above formula, when we wont to know John's wage we could, because John <span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">:isa 'system-analyst</span> , adopt the the previous formula to estimate his basic wage.<br />
In this case the simple property list we used to represent a person's features is not enough. The FL extend the power of a property list adding more sophisticated mechanisms as: default values, demons and inheritance.<br />
We'll see all these mechanisms in this short series of posts but, I suggest you to read this <a href="http://steve-yegge.blogspot.it/2008/10/universal-design-pattern.html" rel="nofollow" target="_blank">post</a> by Steve Yegge to open your mind on the generalized property list pattern.<br />
Before to go to the Clojure implementation details, let me introduce some definitions:<br />
<br />
<ul>
<li><i>A frame is a nested "augmented" association list;</i></li>
<li><i>A frame can represent a class of objects or an object; in our example system-analyst is a frame representing a class of employees while Henry frame is representing an instance of <i>system-analyst</i>;</i></li>
<li><i>A frame is described/composed by one or more facts called slot; in our example :<span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">net-monthly-salary</span> is a slot;</i></li>
<li><i>A slot is <i>described/composed </i>by one or more information called facet; in our example :value is a facet.</i></li>
</ul>
<br />
Prof. Winston introduced several function to deal with frames in Lisp, the three basic functions, working on the frames as a simple nested property list, were:<br />
<ul>
<li><span style="font-family: Courier New, Courier, monospace;">fget</span>: fetches information. The user supplies an access path consisting of a frame, a slot, and a facet .</li>
<li><span style="font-family: Courier New, Courier, monospace;">fput</span>: places information. As with fget the user supplies a frame-slot-facet access path.</li>
<li><span style="font-family: Courier New, Courier, monospace;">fremove</span>: removes information. As with fget and fput , the user supplies a frame-slot-facet access path.</li>
</ul>
The Clojure implementation of these three functions is straightforward:<br />
<br />
<div class="gistLoad" data-id="8913725" id="gist-8913725">
Loading ....</div>
<br />
In the next post I'd like to see how the Frame language paradigm extends the power of a property list adding more sophisticated mechanisms as: default values, daemons and inheritance.<br />
<br /></div>
</div>
</div>
</div>
</div>
<script src="https://raw.github.com/moski/gist-Blogger/master/public/gistLoader.js" type="text/javascript"></script>pizzicohttp://www.blogger.com/profile/06456252975067678649noreply@blogger.com0Castelnuovo di Porto, Roma, Italia42.1199568784073 12.49282836914062542.0257098784073 12.331466869140625 42.214203878407304 12.654189869140625tag:blogger.com,1999:blog-5513402904623243539.post-7851973103624142342013-11-07T04:45:00.001-08:002014-02-10T02:45:01.979-08:00Clojurians greeting<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEga5NaaCpCdSUnSTjFTslQNg7ETw7_krbeUA-_0NmA-Eg_XkF_VOLK3zSFK4lnqb6cSH2L_QfC6S-aX79q4wcno_t3o1pwvcCssTYtGuNPn_kX6PvpG40iqGu6KXy7taG5T6SBmaALwLIUA/s1600/lambda+lambda.png" /><br />
<!-- Blogger automated replacement: "https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F3.bp.blogspot.com%2F-fFYbKq-XhiQ%2FUlgLlP3YbVI%2FAAAAAAAAAK4%2FUKTtUOYBprE%2Fs1600%2Flambda%2Blambda.png&container=blogger&gadget=a&rewriteMime=image%2F*" with "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEga5NaaCpCdSUnSTjFTslQNg7ETw7_krbeUA-_0NmA-Eg_XkF_VOLK3zSFK4lnqb6cSH2L_QfC6S-aX79q4wcno_t3o1pwvcCssTYtGuNPn_kX6PvpG40iqGu6KXy7taG5T6SBmaALwLIUA/s1600/lambda+lambda.png" -->pizzicohttp://www.blogger.com/profile/06456252975067678649noreply@blogger.com0