(defclass AND-GATE
(is-a TWO-INPUT ONE-OUTPUT COMPONENT)
(role concrete)
)
(deffunction andt (?x?y)
(if (and (!=?x 0) (!=?y 0)) then 1 else 0))
(defmessage-handler AND-GATE compute-output ()
(if (and (integerp?self: input-1)
(integerp?self: input-2)) then
(send?self put-output-1
(and#?self: input-1?self: input-2))
)
(defclass OR- GATE
(is-a TWO- INPUT ONE-OUTPUT COMPONENT)
(role concrete)
)
(deffunction or# (?x?y)
(if (or (!=?x 0) (!-?y 0)) then 1 else 0))
(defmessage-handler OR-GATE compute-output ()
(if (and (integerp?self: input- 1)
(integerp?self: input-2)) then
(send?self put-output-1
(or#?self:input-l?self: input-2)))
)
(defclass NAND-GATE
(is-a TWO- INPUT ONE-OUTPUT COMPONENT)
(role concrete)
)
(deffunction nand# (?x?y)
(if (not (and (!=?x 0) (!=?y 0))) then 1 else 0))
(defmessage-handler NAND-GATE compute-output ()
(if (and (integerp?self: input-1)
(integerp?self: input-2)) then
(send?self put-output-1
(nand#?self: input-1?self: input-2)))
)
(defclass XOR-GATE
(is-a TWO-INPUT ONE-OUTPUT COMPONENT)
(role concrete)
)
(deffunction xor# (?x?y)
(if (or (and (=?x 1) (=?y 0))
(and (=?x 0) (=?y 1))) then 1 else 0))
(defmessage-handler XOR-GATE compute-output ()
(if (and (integerp?self: input-1)
(integerp?self: input-2)) then
(send?self put-output-1
(xor#?self:input-l?self: input-2)))
)
(defclass SPLITTER
(is-a ONE-INPUT TWO-OUTPUT COMPONENT)
(role concrete)
)
(defmessage-handler SPLITTER compute-output ()
(if (integerp?self: input-1) then
(send?self put-output-1?self:input-1)
(send?self put-output-2?self:input-1))
)
На этом разработку классов, необходимых для представления логических элементов, можно считать завершенной.
Связь логических элементов
После создания набора классов, необходимого для представления всех используемых логических элементов, встает вопрос объединения этих элементов в логическую схему.
Для ввода в систему набора элементов, необходимых для создания заданной логической схемы, будем использовать отдельный файл с конструктором definstances. Ниже приведен пример такого конструктора для схемы, изображенной на рис. 14.2.
Пример 14.6. Набор объектов логических элементов
(definstances circuit
(S-l of SOURCE)
(S-2 of SOURCE)
(P-l of SPLITTER)
(P-2 of SPLITTER)
(N-l of NOT-GATE)
(O-1 of OR-GATE)
(X-l of XOR-GATE)
(L-l of LED)
(L-2 of LED))
Для связи этих элементов между собой можно разработать несколько функций, для каждого типа соединений. Однако гораздо лучшим решением в данном случае будет создание родовой функции с методами, учитывающими различие соединяемых объектов, полученных в качестве параметров.