Commit 33156b16 authored by Liam Healy's avatar Liam Healy

Add state-value-expand

New function state-value-expand will create a form to compute the
desired functions in the state value. It is more efficient to use this
expansion in code (say from a macro) when the "have" quantities are
known at compile time, because this avoids function construction at run
time.
parent d1b8b428
;; Physical attributes that describe (or partially describe) a physical state.
;; Liam Healy Mon Apr 1 2002 - 16:27
;; Time-stamp: <2013-05-26 19:01:14EDT state.lisp>
;; Time-stamp: <2016-01-16 12:39:46EST state.lisp>
;; Copyright 2011, 2013 Liam M. Healy
;; Copyright 2011, 2013, 2016 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
......@@ -21,7 +21,9 @@
(in-package :antik)
(export '(components constraints
defattribute attributep format-attribute-name state-value show-state
defattribute attributep format-attribute-name
state-value state-value-expand
show-state
make-physical-state make-new-physical-state
make-state-table set-state-value defined-attributes
copy-state map-state
......@@ -245,10 +247,10 @@
"Find the formula for the attribute given the known quantities in state."
(apply state-function-generator attribute-canonical
(loop for att being the hash-keys
in state-table collect att)
in state-table collect att)
(when apply-to-values
(loop for val being the hash-value
in state-table collect val))))
in state-table collect val))))
(defgeneric state-value
(attribute state &optional state-function-generator)
......@@ -259,7 +261,7 @@
(attribute (state-table hash-table)
&optional (state-function-generator *default-sfg*))
(let ((attr (get-canonical-name attribute)))
(or (gethash attr state-table nil)
(or (gethash attr state-table nil) ; note NIL can't be stored as a state value
(when state-function-generator
(let* ((formula
(state-formula
......@@ -273,6 +275,21 @@
(declare (ignore attribute state-function-generator))
nil))
(defun state-value-expand
(attribute state-table will-have state-function-generator)
"Create the expanded form of the function constructor call on the state so that function construction can take place at compile time instead of run time. This is faster provided the attributes that will be present are known. The state-function-generator should return the lambda form of the function."
(alexandria:once-only (state-table)
(let ((attr (get-canonical-name attribute)))
`(or (gethash ',attr ,state-table nil) ; note NIL can't be stored as a state value
(set-state-value
,(apply
state-function-generator
attr
will-have
(mapcar (lambda (x) `(state-value ',x ,state-table)) will-have))
',attr
,state-table)))))
;;;;****************************************************************************
;;;; Parameters
;;;;****************************************************************************
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment