martedì 9 dicembre 2014

Frame language: the knowledge based computer aided design (KBCAD) example (part 2)

To obtain the graphics rendering we have seen in Fig. 2 of my previous post on KBCAD we need to :

  1. define 2D rendering functions for the prototype frames;
  2. modify the prototype frames' structure;
  3. get a graphics library for Clojure standalone application;

1.  2D rendering functions for the prototype frames

We have 3 metaframes or prototypes:
  • Building units
  • Space units
  • Building elements
The simple code I wrote to graphically  render these prototypes is the following 
Loading ....

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:

  • call drawcolostring, we'll see soon its implementation, passing the string to be drawn from the :name slot of f; 
  • 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. 
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.
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.

2.  The prototype frames' structure

To add the rendering capabilities to our KB we need to:

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;
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);
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).

Loading ....

3.  Graphics library for Clojure 

Even if there are already many Clojure wrappers for graphics library, I found that a real graphics library in clojure is still missing.
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 window to viewport transformation .

Loading ....

To play with what we have seen until here and something more that we'll see in future posts you can clone the repo and follow the instruction in the readme.
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.
Hope you'll enjoy playing with this example of the Frame Language library for the Clojure ecosystem.
Comments, opinions and suggestions are greatly appreciated :)

Nessun commento:

Posta un commento