To jest stara wersja strony!


HMR language

The HMR language has been designed to textual human and machine redable representation of XTT2 models. The syntax of the HMR is a subset of Prolog syntax where a number of operators have been defined.

Each HMR file consists of the following parts:

  • [requied] Types definitions (predicate xtype),
  • [optional] Types groups definitions (predicate xtpgr),
  • [requied] Attributes definitions (predicate xattr),
  • [optional] Attributes groups definitions (predicate xatgr),
  • [requied]Definicje schematów (tabel) (predicate xschm),
  • [requied] Rules definitions (predicate xrule),
  • [optional] States definitions (predicate xstat).
  • [optional] Callbacks definitions (predicate xcall)

Each of predicates (besides xschm and xrule) is a Prolog list, which defines the properities of a XTT object.

In example, the definitions of type weekdays and an attribute day is as follows:

xtype [name: weekdays,
       base: symbolic,
       desc: 'A set of days of the week',
       domain: ['Monday'/1,'Tuesday'/2,'Wednesday'/3,'Thursday'/4,'Friday'/5,'Saturday'/6,'Sunday'/7],
       ordered: yes
      ].
	  
xattr [name: day,
       abbrev: day,
       class: simple,
       type: weekdays,
       comm: in,
       callback: [ask_symbolic_GUI,[day]],
       desc: 'The current day'
      ].
	  
xcall ask_symbolic_GUI : [AttName] >>> (jpl_new('skeleton.RequestInterface',[],T),
	 alsv_domain(AttName,Domain,symbolic),
	 Params = [AttName|Domain],
	 term_to_atom(Params, AtomParams),
	 jpl_call(T,request,['callbacks.input.ComboBoxFetcher',AtomParams],Answer),
	 atom_to_term(Answer,Answer2,_),
	 alsv_new_val(AttName,Answer2)).

Exercise

Compare the HMR file with XTT model:

  • HMR file:

thermostat-clb-pl.pl

  • XTT file:

Callbacks in HMR

Callbacks are the functions that are executed during the XTT tables evaluation in order to get a value or to present a value. The example of the callbacks execution results are showed in the picture below. In the cashpoint system, the callbacks are used for calling a enter PIN dialog as well as for displaying messages to the user.

There are two types of callbacks:

  • in - are executed before (or during) inference process in order to get attributes value.
  • out - are executed for values of attributes presentation, when the inference process is finished.

The type of a callback is determined by value of the attribute comm property.

HMR syntax provides a callback property of the XTT attribute that indicates which callback function should be called. The property has a list structure, where the first element is the name of the callback function and the second is a list of parameters.

xattr [name: day,
       abbrev: day,
       class: simple,
       type: day,
       comm: in,
       callback: [ask_console,[day]]
      ].

The above example defines an attribute day. The value of the property comm of this attribute is in what defines the type of the callback to be in. For this attribute the ask_console callback function will be called, which will get the value of the attribute day.

Callbacks and attributes

In HMR, callbacks are related to attributes. For each attribute two properties must be defined in order to use callbacks:

  • comm - defines when the callback is executed (possible values: in or out)
  • callback - the name and the list of parameters of the required callback.

HMR provides xcall predicate that allows for callback definition. The following example shows the JPL mechnism, which is used to create a callback written in Java.

xcall ask_symbolic_GUI : [AttName] >>> (jpl_new('skeleton.RequestInterface',[],T),
	 alsv_domain(AttName,Domain,symbolic),
	 Params = [AttName|Domain],
	 term_to_atom(Params, AtomParams),
	 jpl_call(T,request,['callbacks.input.ComboBoxFetcher',AtomParams],Answer),
	 atom_to_term(Answer,Answer2,_),
	 alsv_new_val(AttName,Answer2)).

Programming languages

HeaRT supports the callbacks written in:

  • Prolog
  • XPCE
  • Java

Prolog

Callbacks are an ordinary Prolog predicates.

xcall ask_console : [AttName] >>> (write('Type value for attribute:  '),
     write(AttName),nl,
     read(Answer),
     alsv_new_val(AttName,Answer)).

Exercise
In order to run inference process directly from HeaRT interpreter you must:

  • download the source code of HeaRT heart-m3.tar.gz,
  • run it by typing heart,
  • in order to complie XTT model in the form of HMR type in console: [name-of-hmr-file],
  • run the gox command:
    gox(current, ListOfProcessedTables, ddi)

Run the inference for the following model: thermostat-clb-pl.pl

XPCE

Są to predykaty w języku Prolog wzbogacone o funkcje XPCE. Aby zapewnić jednak oprawne działanie interpretera HeaRT nalezy korzystać z tego szkieletu: The XPCE callbacks consist of the ordinary Prolog predicates that are enriched by XPCE functions. In order to assure the correct integration with HaeRT, the following schema must be used:

xcall xpce_ask_numeric: [AttName] >>> (
	dynamic(end),
        % The body of the callback, which prepares a dialog to appear.
	repeat,
	    % Waiting for OK button
		send(@display,dispatch),
	end,!,
	retractall(end)).

The example of the callback that reads a value from the range [Min; Max] is presented below:

xcall xpce_ask_numeric: [AttName] >>> (alsv_domain(AttName,[Min to Max],numeric),
	dynamic(end),
	new(@dialog,dialog('Select a value')),
	send_list(@dialog,append, 
		[new(I,int_item(AttName,low := Min, high := Max)),
		 new(_,button('Select',and(message(@prolog,assert,end),and(message(@prolog,alsv_new_val,AttName,I?selection),message(@dialog,destroy)))))]),
	send(@dialog,open),
	repeat,
		send(@display,dispatch),
	end,!,
	retractall(end)).

Ćwiczenie Uruchom wnioskowanie dla przykładu z bankomatem i prześledź działanie callbacków:

Java

Są to programy napisane w języku Java uruchamiane przez prolog przy użyciu JPL. Aby wykorzystać ten mechanizm należy korzystać ze specjalnie przygotowanych do tego klasy:

  • DataFetcher.java - klasa abstrakcyjna którą muszą rozszerzać wszystkie callbacki
  • RequestInterface.java - Kontroler
  • Response.java - klasa przechowująca dane odebrane od użytkownika.

W celu ulatwienia implementacji niektórych callbacków, przygotowano zbiór podstawowych komponentów do pobierania danych od użytkownika:

  • ComboBoxFetcher.java - przyjmuje listę wartości z jakich można wybierać. Zwraca jedną wybraną wartość
  • SliderFetcher.java - przyjmuje przedział wartości numerycznych między którymi można wybierać. Zwraca jedna wybraną wartość.
  • TwoListsFetcher.java - przyjmuje listę wartości jakich można wybierać i zwraca również listę wybranych wartości.
  • ComboGUI.java - GUI dla ComboBoxFetcher
  • SliderGUI.java - GUI dla SliderFetcher
  • TwoListsGUI.java - GUI dla TwoListsFetcher

Kod źródłowy klas do pobrania tutaj.

Przykład

Aby wykorzystać któryś z komponentów Java do napisania callbacku, należy wykonać następujące kroki:

  1. Skompilować kod źródłowy
    javac callbacks/input/*.java skeleton/*.java

    i umieścić go w katalogu z plikiem HMR

  2. Odpowiednio zmodyfikować plik HMR dopisując atrybutom pole callback i uzupełniając go nazwą i listą parametrów jakie callback przyjmuje, np.
    callback: [ask_symbolic_GUI,[day]]
  3. Dopisać predykaty xcall do pliku HMR, definiujące wywołanie callbacka, np.:
      xcall ask_symbolic_GUI : [AttName] >>> (
         %Tworzymy obiekt kontrolera:
    	 jpl_new('skeleton.RequestInterface',[],T),
    	 %Przygotowujemy listę parametrów
    	 alsv_domain(AttName,Domain,symbolic),
    	 Params = [AttName|Domain],
    	 term_to_atom(Params, AtomParams),
    	 %Wywołujemy callback i podajemu my listę parametrów znajdującą sie
    	 %w termie AtomPArams. Wartosc zwrócona przez callback znajdzie się
    	 %w termie Answer.
    	 jpl_call(T,request,['callbacks.input.ComboBoxFetcher',AtomParams],Answer),
    	 %Zamieniamy zwróconą wartość na term i dodajemy go do bazy faktów
    	 atom_to_term(Answer,Answer2,_),
    	 alsv_new_val(AttName,Answer2)).
      
  4. Uruchomić HeaRT w katalogu z plikiem HMR, skompilować ten plik i uruchomić wnioskowanie. Callbacki powinny zostać uruchomione.

Ćwiczenie Pobierz poniższy kod kardio.zip. I dla pliku HMR znajdującego się w archiwum uruchom wnioskowanie.

Ćwiczenie

  1. Uruchom interpreter HeaRT, wczytaj plik HMR i uruchom wnioskowanie predykatem (gox/3). Dopisz callback wyświetlający wynik wnioskowania w XPCE.
  2. Pobierz ten plik HMR i dopisz (zmodyfikuj istniejące wersje prologowe) do niego callbacki w Javie bazując na przykładzie z poprzedniej sekcji.

Komentarze

Z braku lepszego miejsca tutaj studenci wpisują komentarze natury ogólnej do tego lab. 8-)

pl/dydaktyka/piw/2010/systemy_ekspertowe/hekate_iii.1302013032.txt.gz · ostatnio zmienione: 2019/06/27 15:54 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0