Package org.qdl_lang.state
Class XStack<V extends XTable<? extends XKey,? extends XThing>>
- java.lang.Object
-
- org.qdl_lang.state.XStack<V>
-
- All Implemented Interfaces:
Serializable
public abstract class XStack<V extends XTable<? extends XKey,? extends XThing>> extends Object implements Serializable
A stateful stack of things, such as functions. This is the method by which local state is preserved. The zero-th element is the current local table. It is used for functions, variables, modules etc., hence the prefix of X for it and things related to it.Usage
Create a subclass as needed for your objects. This involves an
XStack,XTable,XKeyand anXThing.How state is managed
If we had the following QDL:f(x)->x^2; block[f(x)->x^3;...]ThenXStacksubclass for functions inside the block would look liketable entry 0 f(x)->x^3 1 f(x)->x^2Calls toget(XKey)would peek at 0 and return f(x)->x^3 inside the block. This is how local state overrides the parent state. Blocks of course can be for loops, functions, conditionals etc. Were there no entry for f(x) in the block, thenXStackwould return f(x)->x^2.Created by Jeff Gaynor
on 11/8/21 at 6:27 AM- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description XStack()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddTables(XStack xStack)Take an XStack and prepend in the correct order to the front of the stack.List<XKey>allKeys()Returns the a list of keys (including redundancies) for this stack.voidappend(V v)Append the table to the end of the stack -- this sets the root for the table.voidappendTables(XStack xStack)Similar toaddTables(XStack), but this appends them to the existing set of tables.voidclear()Clears the entire stack and resets it.XStackclone()booleancontainsKey(XKey key)booleancontainsKey(XKey key, int startTableIndex)Check that a specific key is in a table starting at the index.voiddeserializeFromJSON(net.sf.json.JSONObject jsonObject, SerializationState serializationState, State state)voiddeserializeFromJSONNEW(net.sf.json.JSONObject jsonObject, SerializationState serializationState, State state)voiddeserializeFromJSONOLD(net.sf.json.JSONObject jsonObject, SerializationState serializationState, State state)voidfromJSON(net.sf.json.JSONArray array, SerializationState serializationState)Processes the array of arrays.abstract voidfromXML(XMLEventReader xer, QDLInterpreter qi)voidfromXML(XMLEventReader xer, SerializationState SerializationState)protected voidfromXMLNEW(XMLEventReader xer, SerializationState serializationState)XThingget(XKey key)List<? extends XThing>getAll()Get all of the values from all tables.This returns a flat list.protected net.sf.json.JSONArraygetJSON(XMLEventReader xer)XTable<? extends XKey,? extends XThing>getLocal()Get the local table for this stack.XTable<? extends XKey,? extends XThing>getRoot()Since all new tables are added at 0, the initial one, called the root, is last.List<XTable<? extends XKey,? extends XThing>>getStack()abstract XStackgetStateStack(State state)This gets the stack corresponding to this class from the state..abstract StringgetXMLStackTag()abstract StringgetXMLTableTag()Deprecated.booleanisEmpty()Set<XKey>keySet()Returns the unique set of keys over the tables.XThinglocalGet(XKey key)Only returns a non-null element if it is defined in the local (index 0) table.booleanlocalHas(XKey xkey)XThinglocalPut(XThing value)Only add this to the local state.voidlocalRemove(XKey key)Removes only the most local entry.static voidmain(String[] args)abstract XStacknewInstance()abstract XTablenewTableInstance()XThingnonlocalGet(XKey key)searches for the entry every place except the most local state.XTable<XKey,XThing>peek()voidpush(XTable<? extends XKey,? extends XThing> xTable)voidpushNewTable()XThingput(XKey xKey, XThing xThing)XThingput(XThing value)voidremove(XKey key)Removes all references from all tables.protected voidserializeContent(XMLStreamWriter xsw, SerializationState serializationState)Very simple (and simple-minded) way to do it.net.sf.json.JSONObjectserializeToJSON(SerializationState serializationState)voidsetStack(List<XTable<? extends XKey,? extends XThing>> stack)abstract voidsetStateStack(State state, XStack xStack)This sets the stack corresponding to this class from the state with the given stack.intsize()net.sf.json.JSONArraytoJSON(SerializationState serializationState)StringtoString()StringtoString(boolean showKeys)voidtoXML(XMLStreamWriter xsw, SerializationState serializationState)Does the grunt work of writing the stack in the right order.protected voidtoXMLNEW(XMLStreamWriter xsw, SerializationState serializationState)
-
-
-
Method Detail
-
clear
public void clear()
Clears the entire stack and resets it.
-
addTables
public void addTables(XStack xStack)
Take an XStack and prepend in the correct order to the front of the stack. If xStack is [A,B,C,...] And the existing stack is [P,Q,...] the result is [A,B,C,...,P,Q,...] This is needed when, e.g., creating new local state for function reference resolution
Note:get(XKey)starts from index 0, so local overrides are first!- Parameters:
xStack-
-
appendTables
public void appendTables(XStack xStack)
Similar toaddTables(XStack), but this appends them to the existing set of tables. If XStack is [A,B,C,...] And the existing stack is [P,Q,...] the result is [P,Q,...,A,B,C,...,]
Note:get(XKey)starts from index 0, so local overrides are first!- Parameters:
xStack-
-
newInstance
public abstract XStack newInstance()
-
newTableInstance
public abstract XTable newTableInstance()
-
append
public void append(V v)
Append the table to the end of the stack -- this sets the root for the table.- Parameters:
v-
-
containsKey
public boolean containsKey(XKey key, int startTableIndex)
Check that a specific key is in a table starting at the index. This lets you, e.g., skip local state if the start index is positive.- Parameters:
key-startTableIndex-- Returns:
-
containsKey
public boolean containsKey(XKey key)
-
localGet
public XThing localGet(XKey key)
Only returns a non-null element if it is defined in the local (index 0) table.- Parameters:
key-- Returns:
-
getLocal
public XTable<? extends XKey,? extends XThing> getLocal()
Get the local table for this stack.- Returns:
-
localHas
public boolean localHas(XKey xkey)
-
nonlocalGet
public XThing nonlocalGet(XKey key)
searches for the entry every place except the most local state.- Parameters:
key-- Returns:
-
getAll
public List<? extends XThing> getAll()
Get all of the values from all tables.This returns a flat list.- Returns:
-
getRoot
public XTable<? extends XKey,? extends XThing> getRoot()
Since all new tables are added at 0, the initial one, called the root, is last. This gets the rootXTable.- Returns:
-
isEmpty
public boolean isEmpty()
-
pushNewTable
public void pushNewTable()
-
localPut
public XThing localPut(XThing value)
Only add this to the local state.- Parameters:
value-- Returns:
-
localRemove
public void localRemove(XKey key)
Removes only the most local entry.- Parameters:
key-
-
remove
public void remove(XKey key)
Removes all references from all tables. This includes all overrides so at the end of this operation there are no references any place.- Parameters:
key-
-
size
public int size()
-
toString
public String toString(boolean showKeys)
-
toXML
public void toXML(XMLStreamWriter xsw, SerializationState serializationState) throws XMLStreamException
Does the grunt work of writing the stack in the right order. You write the start tag, any comments, invoke this, then the end tag. SeetoXML(XMLStreamWriter, SerializationState)for a canonical basic.- Parameters:
xsw-- Throws:
XMLStreamException
-
fromXML
public void fromXML(XMLEventReader xer, SerializationState SerializationState) throws XMLStreamException
- Throws:
XMLStreamException
-
fromXML
public abstract void fromXML(XMLEventReader xer, QDLInterpreter qi) throws XMLStreamException
- Throws:
XMLStreamException
-
getXMLStackTag
public abstract String getXMLStackTag()
-
getXMLTableTag
public abstract String getXMLTableTag()
Deprecated.- Returns:
-
allKeys
public List<XKey> allKeys()
Returns the a list of keys (including redundancies) for this stack.- Returns:
-
toJSON
public net.sf.json.JSONArray toJSON(SerializationState serializationState) throws Throwable
- Throws:
Throwable
-
setStateStack
public abstract void setStateStack(State state, XStack xStack)
This sets the stack corresponding to this class from the state with the given stack. If the stack is not of the correct type, a class cast exception will result.
We could have tried this with some type of dynamic casting, but that is messy and fragile in Java- Parameters:
state-xStack-
-
getStateStack
public abstract XStack getStateStack(State state)
This gets the stack corresponding to this class from the state..- Parameters:
state-- Returns:
-
fromJSON
public void fromJSON(net.sf.json.JSONArray array, SerializationState serializationState)Processes the array of arrays. Each table is turned into an array of entries. The stack is then an array of these (so we have order of the tables). This will take the entire set of items and recreate the stack structure.- Parameters:
array-serializationState-
-
fromXMLNEW
protected void fromXMLNEW(XMLEventReader xer, SerializationState serializationState) throws XMLStreamException
- Throws:
XMLStreamException
-
getJSON
protected net.sf.json.JSONArray getJSON(XMLEventReader xer) throws XMLStreamException
- Throws:
XMLStreamException
-
toXMLNEW
protected void toXMLNEW(XMLStreamWriter xsw, SerializationState serializationState) throws XMLStreamException
- Throws:
XMLStreamException
-
serializeContent
protected void serializeContent(XMLStreamWriter xsw, SerializationState serializationState) throws XMLStreamException
Very simple (and simple-minded) way to do it. Hand off the serialization so that the tables are converted to JSON arrays with base64 encoded QDL as the entries.. Deserialization then is just interpreting that back. This allows for serializing enormously complex stems and such without having to try and get some XML format for them.- Parameters:
xsw-serializationState-- Throws:
XMLStreamException
-
serializeToJSON
public net.sf.json.JSONObject serializeToJSON(SerializationState serializationState) throws Throwable
- Throws:
Throwable
-
deserializeFromJSON
public void deserializeFromJSON(net.sf.json.JSONObject jsonObject, SerializationState serializationState, State state)
-
deserializeFromJSONNEW
public void deserializeFromJSONNEW(net.sf.json.JSONObject jsonObject, SerializationState serializationState, State state)
-
deserializeFromJSONOLD
public void deserializeFromJSONOLD(net.sf.json.JSONObject jsonObject, SerializationState serializationState, State state)
-
-