Implements an n-cube. This is a hyper (n-dimensional) cube of cells, made up of 'n' number of axes. Each Axis is composed of Columns that denote discrete nodes along an axis. Use NCubeManager to manage a list of NCubes. Documentation on Github. Useful for pricing, rating, and configuration modeling.
Modifiers | Name | Description |
---|---|---|
static class |
NCube.NCubeReader |
Permanently add Custom Reader / Writer to json-io so that n-cube will use its native JSON format when written or read with json-io. |
static class |
NCube.NCubeWriter |
Custom writer for NCube when used with json-io |
Modifiers | Name | Description |
---|---|---|
static java.lang.String |
DEFAULT_CELL_VALUE |
|
static java.lang.String |
DEFAULT_CELL_VALUE_CACHE |
|
static java.lang.String |
DEFAULT_CELL_VALUE_TYPE |
|
static java.lang.String |
DEFAULT_CELL_VALUE_URL |
|
static java.lang.String |
METAPROPERTY_TEST_DATA |
|
static java.lang.String |
METAPROPERTY_TEST_UPDATED |
|
static java.lang.String |
RULE_EXEC_INFO |
|
static java.lang.String |
validCubeNameChars |
Constructor and description |
---|
NCube
(java.lang.String name) Creata a new NCube instance with the passed in name |
Type Params | Return Type | Name and description |
---|---|---|
|
void |
addAxis(Axis axis) Add an Axis to this NCube. |
|
Column |
addColumn(java.lang.String axisName, java.lang.Comparable value, java.lang.String colName = null, java.lang.Long suggestedId = null) Add a column to the n-cube |
|
void |
addMetaProperties(java.util.Map<java.lang.String, java.lang.Object> allAtOnce) Add a Map of meta properties all at once. |
|
T |
at(java.util.Map coordinate, java.util.Map output = [:], java.lang.Object defaultValue = null) Fetch the contents of the cell at the location specified by the coordinate argument. |
|
void |
breakAxisReference(java.lang.String axisName) Convert a reference axis to a non-reference axis. |
|
void |
clearCells() Clear all cell values. |
|
void |
clearMetaProperties() Remove all meta properties associated to this n-cube. |
|
void |
clearSha1() |
|
CompileInfo |
compile() Pre-compile command cells, meta-properties, and rule conditions that are expressions |
|
boolean |
containsCell(java.util.Map coordinate, boolean useDefault = false) Test to see if a value is mapped at the given coordinate. |
|
boolean |
containsCellById(java.util.Collection<java.lang.Long> coordinate)
|
|
boolean |
containsMetaProperty(java.lang.String key) Test for existence of a given meta-property key. |
<T> |
static NCube<T> |
createCubeFromBytes(byte[] bytes, int pos = 0, int length = bytes.length) Create a cube from a byte[] of JSON bytes, or a gzip byte[] of JSON bytes, both are JSON content representing an n-cube. |
<T> |
static NCube<T> |
createCubeFromStream(java.io.InputStream stream) Create an n-cube from a stream of bytes. |
|
boolean |
deleteAxis(java.lang.String axisName) Remove an axis from an NCube. |
|
boolean |
deleteColumn(java.lang.String axisName, java.lang.Comparable value) Delete a column from the named axis. |
|
NCube |
duplicate(java.lang.String newName = name) Create an equivalent n-cube as 'this'. |
|
boolean |
equals(java.lang.Object other) |
|
java.lang.Object |
extractMetaPropertyValue(java.lang.Object value, java.util.Map input = [:], java.util.Map output = [:]) If a meta property value is fetched from an Axis or a Column, the value should be extracted using this API, so as to allow executable values to be retrieved. |
|
Column |
findColumn(java.lang.String axisName, java.lang.Comparable value) Convenience method to locate column when you have the axis name as a String and the value to find. |
<T> |
static NCube<T> |
fromSimpleJson(java.lang.String json) Use this API to create NCubes from a simple JSON format. |
<T> |
static NCube<T> |
fromSimpleJson(java.io.InputStream stream) Use this API to create NCubes from a simple JSON format. |
|
Axis |
get(java.lang.String axisName) This API allows Groovy code to do this: ncube.axisName or ncube['axisName'] to fetch an axis. |
|
java.util.List<Advice> |
getAdvices(java.lang.String method) @return List |
|
ApplicationID |
getApplicationID()
|
|
java.util.List<Axis> |
getAxes() @return List |
|
Axis |
getAxis(java.lang.String axisName) Retrieve an axis (by name) from this NCube. |
|
Axis |
getAxisFromColumnId(long id, boolean columnMustExist = true) Given the passed in Column ID, return the axis that contains the column. |
|
java.util.Set<java.lang.String> |
getAxisNames() @return Set |
|
T |
getCell(java.util.Map coordinate, java.util.Map output = [:], java.lang.Object defaultValue = null) Fetch the contents of the cell at the location specified by the coordinate argument. |
|
java.lang.Object |
getCellByIdNoExecute(java.util.Set<java.lang.Long> coordinate) Mainly useful for displaying an ncube within an editor. |
|
java.util.Map<java.util.Set<java.lang.Long>, T> |
getCellMap() @return read-only copy of the n-cube cells. |
|
java.lang.Object |
getCellNoExecute(java.util.Map coordinate) Fetch the actual 'formula' at the given cell. |
|
java.lang.Object |
getColumnDefault(java.util.Set<java.lang.Long> colIds) Given the passed in column IDs, return the column level default value if one exists or null otherwise. |
|
java.util.Map |
getCoordinateFromIds(java.util.Set<java.lang.Long> idCoord) Turn a Set of column IDs into a 'normal' coordinate that has values that will bind to axes the 'normal' way. |
|
java.util.Set<java.lang.Long> |
getCoordinateKey(java.util.Map coordinate, java.util.Map output = new CaseInsensitiveMap()
|
|
byte[] |
getCubeAsGzipJsonBytes() @return byte[] containing the bytes of this N-Cube when converted to JSON format and then gzipped. |
|
T |
getDefaultCellValue()
|
|
java.util.Map<java.lang.String, java.lang.Object> |
getDisplayCoordinateFromIds(java.util.Set<java.lang.Long> idCoord) Fetch a 'display' coordinate from the passed in Set of IDs. |
|
java.util.Map<java.lang.Object, T> |
getMap(java.util.Map coordinate, java.util.Map output = [:], java.lang.Object defaultValue = null) Get a Map of column values and corresponding cell values where all axes but one are held to a fixed (single) column, and one axis allows more than one value to match against it. |
|
long |
getMaxAxisId() Determine highest ID used by the axes. |
|
java.util.Map |
getMetaProperties() Fetch n-cube meta properties (SHA1, HEAD_SHA1, and CHANGE_TYPE are not meta-properties. |
|
java.lang.Object |
getMetaProperty(java.lang.String key) Fetch the value associated to the passed in Key from the MetaProperties (if any exist). |
|
java.lang.String |
getName() @return String name of the NCube |
|
int |
getNumCells() @return int total number of cells that are uniquely set (non default) within this NCube. |
|
int |
getNumDimensions() @return int the number of axis (dimensions) for this n-cube. |
|
long |
getNumPotentialCells() @return long number of potential cells this n-cube potentially has |
|
java.util.Set<java.lang.String> |
getOptionalScope(java.util.Map input, java.util.Map output) Get the optional scope keys. |
|
java.util.List<java.util.Map<java.lang.String, T>> |
getPopulatedCellCoordinates()
|
|
java.util.Map<java.util.Map, java.util.Set<java.lang.String>> |
getReferencedCubeNames()
|
|
java.util.Set<java.lang.String> |
getRequiredScope(java.util.Map input, java.util.Map output) Determine the required 'scope' needed to access all cells within this NCube. |
|
static RuleInfo |
getRuleInfo(java.util.Map output) Get / Create the RuleInfo Map stored at output[NCube.RULE_EXEC_INFO] |
|
java.lang.String |
getStatus() @return ReleaseStatus of this n-cube as it was loaded. |
|
java.util.List<NCubeTest> |
getTestData() |
|
java.lang.String |
getVersion() @return String version of this n-cube as it was loaded. |
|
boolean |
hasRuleAxis() @return boolean true if there is at least one rule axis, false if there are no rule axes. |
|
int |
hashCode() |
|
boolean |
isComparableCube(NCube<T> other) Test if another n-cube is 'comparable' with this n-cube. |
|
static boolean |
isTrue(java.lang.Object ruleValue) Follow the exact same treatment of TRUTH as Groovy |
|
java.util.Map |
mapReduce(java.lang.String rowAxisName, java.lang.String colAxisName, groovy.lang.Closure where = { true }, java.util.Map input = [:], java.util.Map output = [:], java.util.Set columnsToSearch = null, java.util.Set columnsToReturn = null) Filter rows of an n-cube. |
|
void |
mergeDeltas(java.util.List<Delta> deltas) Merge the passed in List of Delta's into this n-cube. |
|
static java.util.Map |
objectToMap(java.lang.Object o) Convert an Object to a Map. |
|
void |
removeAxisReferenceTransform(java.lang.String axisName) Remove transform from a reference axis. |
|
T |
removeCell(java.util.Map coordinate) Clear (remove) the cell at the given coordinate. |
|
T |
removeCellById(java.util.Set<java.lang.Long> coordinate) Clear a cell directly from the cell sparse-matrix specified by the passed in Column IDs. |
|
java.lang.Object |
removeMetaProperty(java.lang.String key) Remove a meta-property entry |
|
void |
renameAxis(java.lang.String oldName, java.lang.String newName) Rename an axis |
|
void |
setApplicationID(ApplicationID appId) |
|
T |
setCell(T value, java.util.Map coordinate) Store a value in the cell at the passed in coordinate. |
|
T |
setCellById(T value, java.util.Set<java.lang.Long> coordinate) Set a cell directly into the cell sparse-matrix specified by the passed in Column IDs. |
|
void |
setDefaultCellValue(T defaultCellValue) Set the default cell value for this n-cube. |
|
java.lang.Object |
setMetaProperty(java.lang.String key, java.lang.Object value) Set (add / overwrite) a Meta Property associated to this n-cube. |
|
void |
setName(java.lang.String name) Set the name of this n-cube |
|
static void |
setStackEntryCoordinateValueMaxSize(int maxSize) Sets the max size for coordinate value strings in StackEntry. |
|
void |
setTestData(java.lang.Object[] tests) |
|
java.lang.String |
sha1()
|
|
java.lang.String |
toFormattedJson(java.util.Map options = null) Format this n-cube into JSON using the passed in 'options' Map to control the desired output. |
|
java.lang.String |
toHtml(java.lang.String... headers) Use this API to generate an HTML view of this NCube. |
|
java.lang.String |
toString() |
|
void |
updateColumn(long id, java.lang.Comparable value, java.lang.String name = null, int order = -1i) Change the value of a Column along an axis. |
|
java.util.Set<java.lang.Long> |
updateColumns(java.lang.String axisName, java.util.Collection<Column> newCols, boolean allowPositiveColumnIds = false) Update all of the columns along an axis at once. |
|
T |
use(java.util.Map altInput, java.util.Map input, java.util.Map output, java.lang.Object defaultCellValue) Grab the cell located at altInput, then run it in terms of the input. |
|
static void |
validateCubeName(java.lang.String cubeName) Ensure that the passed in name is a valid n-cube name, |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Creata a new NCube instance with the passed in name
name
- String name to use for the NCube.Add an Axis to this NCube. If the axis has a default column, all cells will be added to the default column. Otherwise, all cells will be cleared.
axis
- Axis to addAdd a column to the n-cube
axisName
- String name of the Axis to which the column will be added.value
- Comparable that will be the value for the given column. Cannot be null.colName
- The optional name of the column, useful for RULE axis columns. Optional.suggestedId
- Long id. If id is not valid for the column (unique id, and axis portion matches axis on which
it is added), then the ID will be generated. Optional.Add a Map of meta properties all at once.
allAtOnce
- Map of meta properties to addFetch the contents of the cell at the location specified by the coordinate argument. Be aware that if you have any rule cubes in the execution path, they can execute more than one cell. The cell value returned is the value of the last cell executed. Typically, in a rule cube, you are writing to specific keys within the rule cube, and the calling code then accesses the 'output' Map to fetch the values at these specific keys.
coordinate
- Map of String keys to values meant to bind to each axis of the n-cube.output
- Map that can be written to by the code within the the n-cubes (for example,
GroovyExpressions.defaultValue
- Object placed here will be returned if there is no cell at the location
pinpointed by the input coordinate. Normally, the defaulValue of the
n-cube is returned, but if this parameter is passed a non-null value,
then it will be returned.Convert a reference axis to a non-reference axis. 'Break the Reference.'
axisName
- String name of reference axis to convert.Clear all cell values. All axes and columns remain.
Remove all meta properties associated to this n-cube.
Pre-compile command cells, meta-properties, and rule conditions that are expressions
Test to see if a value is mapped at the given coordinate. The 2nd argument allows you to take into account the n-cube level Default Cell value or not. Set to true to have the default value considered, false otherwise.
coordinate
- Map (coordinate) of a celluseDefault
- (optional, defaults to false. Set to true, then if a non-default
value for the n-cube is set, this method will return true).
Test for existence of a given meta-property key.
key
- String name of keyCreate a cube from a byte[] of JSON bytes, or a gzip byte[] of JSON bytes, both are JSON content representing an n-cube. Calling ncube.toFormattedJson() is the source of the JSON format used.
Create an n-cube from a stream of bytes. The stream can be either a JSON stream of an n-cube or a g-zip JSON stream.
Remove an axis from an NCube. All cells will be cleared when an axis is deleted.
axisName
- String name of axis to removeDelete a column from the named axis. All cells that reference this column will be deleted.
axisName
- String name of Axis contains column to be removed.value
- Comparable value used to identify column (Long if identifying a RULE column)Create an equivalent n-cube as 'this'.
If a meta property value is fetched from an Axis or a Column, the value should be extracted using this API, so as to allow executable values to be retrieved.
value
- Object value to be extracted.Convenience method to locate column when you have the axis name as a String and the value to find. If the named axis is a rule axis, then it is expected that value is either a String name of the rule or the long ID of the rule column.
axisName
- String name of axis. Case does not matter when locating by name.value
- Comparable value used to find the column.Use this API to create NCubes from a simple JSON format.
json
- Simple JSON formatUse this API to create NCubes from a simple JSON format.
stream
- Simple JSON formatThis API allows Groovy code to do this: ncube.axisName or ncube['axisName'] to fetch an axis.
axisName
- String name of axis to get (case ignored)
Retrieve an axis (by name) from this NCube.
axisName
- String name of Axis to fetch.Given the passed in Column ID, return the axis that contains the column.
id
- Long id of a Column on one of the Axes within this n-cube.columnMustExist
- boolean, defaults to true. The axis will only be
returned if the column id passed in is that of a column on the axis. For
example, a deleted column ID, while it may contain the correct axis id,
it is no longer on the axis. If this is false, then the axis will still
be returned even if the column id represents a column no longer on the
axis. If true, both the axis and column must exist.
Fetch the contents of the cell at the location specified by the coordinate argument. Note that if you have any rule axes, they can execute more than one time. The value returned by this method is the value of the last cell executed. Typically, in a rule cube, you are writing to specific keys within the output Map, and the calling code then accesses the 'output' Map to fetch the values at these specific keys. A rule axis name can have a String, Collection, Map, or nothing associated to it. - If the value is a String, then it is the name of the rule to begin execution (skips past rules ahead of it). - If the value associated to a rule axis name is a Collection, then it is considered a Collection of rule names to run (orchestration). In that case, only the named rules will be executed (their conditions evaluated, and if true, the associated statements). - If the associated value is a Map, then the keys will be the names of meta-property keys associated to the rule condition column meta-property keys, and the values must match the value associated to the meta-property. The special value NCUBE.DONT_CARE can be associated to the key, in which case only the key name of the meta-property must match the key name in the passed in map in order for the rule to be selected. If there is more than one entry in the passed in Map, then the rules must match on all entries. This is a conjunction (or AND) - the rules match all keys are selected. - If nothing is associated to the rule axis name (or null), then all rules are selected. Once the rules are selected, all rule conditions are executed. If the condition is true, then the associated statement is executed. Note: More than one rule axis can be added to an n-cube. In this case, each rule axis name can have its own orchestration (rule list) to select the rules on the given axis.
coordinate
- Map of String keys to values meant to bind to each axis of the n-cube.output
- Map that can be written to by the code within the the n-cubes (for example,
GroovyExpressions.defaultValue
- Object placed here will be returned if there is no cell at the location
pinpointed by the input coordinate. Normally, the defaulValue of the
n-cube is returned, but if this parameter is passed a non-null value,
then it will be returned.Mainly useful for displaying an ncube within an editor. This will get the actual stored cell, not execute it. The caller will get CommandCell instances for example, as opposed to the return value of the executed CommandCell.
Fetch the actual 'formula' at the given cell. In the case of primitives, the primitive will be returned. However, in the case of an Expression, the Expression will be returned, not executed.
Given the passed in column IDs, return the column level default value if one exists or null otherwise. In the case of intersection, then null is returned, meaning that the n-cube level default cell value will be returned at intersections.
Turn a Set of column IDs into a 'normal' coordinate that has values that will bind to axes the 'normal' way. For RULE axes, the key will be the rule axis name and the value will be the rule name. If there is no rule name, then the value will be the long ID of the column on the rule axis.
idCoord
- Set
coordinate
- Map containing Axis names as keys, and Comparable's as
values. The coordinate key matches an axis name, and then the column on the
axis is found that best matches the input coordinate value. Use this when
the input is expected to only match one value on an axis. For example, for a
RULE axis, the key name is the RULE axis name, and the value is the rule name
to match.
Fetch a 'display' coordinate from the passed in Set of IDs. The returned coordinate will have the String 'default column' for column ids that represent default columns. If any of the columns have a name (like columns on a rule axis), the name will be included in the String associated to the axis name entry.
idCoord
- SetGet a Map of column values and corresponding cell values where all axes but one are held to a fixed (single) column, and one axis allows more than one value to match against it.
coordinate
- Map - A coordinate where the keys are axis names, and the
values are intended to match a column on each axis, with one exception. One
of the axis values in the coordinate input map must be an instanceof a Set.
If the set is empty, all columns and cell values for the given axis will be
returned in a Map. If the Set has values in it, then only the columns
on the 'wildcard' axis that match the values in the set will be returned (along
with the corresponding cell values).output
- Map that can be written to by the code within the the n-cubes (for example,
GroovyExpressions. Optional.defaultValue
- Object placed here will be returned if there is no cell at the location
pinpointed by the input coordinate. Normally, the defaulValue of the
n-cube is returned, but if this parameter is passed a non-null value,
then it will be returned. Optional.Determine highest ID used by the axes. When adding an additional axis, it is recommended to use this value plus 1 for adding another axis.
Fetch n-cube meta properties (SHA1, HEAD_SHA1, and CHANGE_TYPE are not meta-properties. Use their respective accessor functions to obtain those).
Fetch the value associated to the passed in Key from the MetaProperties (if any exist). If none exist, null is returned.
Get the optional scope keys. These are keys that if supplied, might change the returned value, but if not supplied a value is still returned. For example, an axis that has a Default column is an optional scope. If no value is supplied for that axis, the Default column is chosen. However, supplying a value for it *may* change the column selected. Similarly, a cube may reference another cube, and the 'sub-cube' may use different scope keys than the calling cube. These additional keys are located and added as optional scope.
Determine the required 'scope' needed to access all cells within this NCube. Effectively, you are determining how many axis names (keys in a Map coordinate) are required to be able to access any cell within this NCube. Keep in mind, that CommandCells allow this NCube to reference other NCubes and therefore the referenced NCubes must be checked as well. This code will not get stuck in an infinite loop if one cube has cells that reference another cube, and it has cells that reference back (it has cycle detection).
Get / Create the RuleInfo Map stored at output[NCube.RULE_EXEC_INFO]
Test if another n-cube is 'comparable' with this n-cube. This means that they have the same number of dimensions (axes) and each axis has the same name. This test will allow many operations to be performed on two cubes once it is known they are 'compatible' such as union, intersection, even matrix operations like multiply, etc.
other
- NCube to compare to this ncube.Follow the exact same treatment of TRUTH as Groovy
Filter rows of an n-cube. Use this API to fetch a subset of an n-cube, similar to SQL SELECT.
rowAxisName
- String name of axis acting as the ROW axis.colAxisName
- String name of axis acting as the COLUMN axis.where
- Closure groovy closure. Written as condition in terms of the columns on the colAxisName.
Example: { Map input -> (input.state == 'TX' || input.state == 'OH') && (input.attribute == 'fuzzy')}.
This will only return rows where this condition is true ('state' and 'attribute' are two column values from
the colAxisName). The values for each row in the rowAxis is bound to the where expression for each row. If
the row passes the 'where' condition, it is included in the output.output
- Map the output Map use to write multiple return values to, just like getCell() or at().input
- Map the input Map just like it is used for getCell() or at(). Only needed when there are three (3)
or more dimensions. All values in the input map (excluding the axis specified by rowAxisName and colAxisName) are
bound just as they are in getCell() or at().columnsToSearch
- Set which allows reducing the number of columns bound for use in the where clause. If not
specified, all columns on the colAxisName can be used. For example, if you had an axis named 'attribute', and it
has 10 columns on it, you could list just two (2) of the columns here, and only those columns would be placed into
values accessible to the where clause via input.xxx == 'someValue'. The mapReduce() API runs faster when fewer
columns are included in the columnsToSearch.columnsToReturn
- Set of values to indicate which columns to return. If not specified, the entire 'row' is
returned. For example, if you had an axis named 'attribute', and it has 10 columns on it, you could list just
two (2) of the columns here, in the returned Map of rows, only these two columns will be in the returned Map.
The columnsToSearch and columnsToReturn can be completely different, overlap, or not be specified. This param
is similar to the 'Select List' portion of the SQL SELECT statement. It essentially defaults to '*', but you
can have it return less column/value pairs in the returned Map if you add only the columns you want returned
here.Merge the passed in List of Delta's into this n-cube.
deltas
- List of Delta instances.Convert an Object to a Map. This allows an object to then be passed into n-cube as a coordinate. Of course the returned map can have additional key/value pairs added to it after calling this method, but before calling getCell().
o
- Object any Java object to bind to an NCube.Remove transform from a reference axis.
axisName
- String name of reference axis.Clear (remove) the cell at the given coordinate. The cell is dropped from the internal sparse storage. After this call, containsCell(coord) for the same cell will return false.
coordinate
- Map coordinate of Cell to remove.Clear a cell directly from the cell sparse-matrix specified by the passed in Column IDs. After this call, containsCell(coord) for the same coordinate would return false.
Remove a meta-property entry
Rename an axis
oldName
- String old namenewName
- String new nameStore a value in the cell at the passed in coordinate.
value
- A value to store in the NCube cell.coordinate
- Map coordinate used to identify what cell to update.
The Map contains keys that are axis names, and values that will
locate to the nearest column on the axis.Set a cell directly into the cell sparse-matrix specified by the passed in Column IDs.
Set the default cell value for this n-cube. This is a space-saving technique, as the most common cell value can be set as the defaultCellValue, and then the cells that would have had this value can be left empty.
defaultCellValue
- T the default value that will be returned when a coordinate
specifies a cell that has no entry associated to it.Set (add / overwrite) a Meta Property associated to this n-cube.
key
- String key name of meta propertyvalue
- Object value to associate to keySet the name of this n-cube
name
- String nameSets the max size for coordinate value strings in StackEntry.
maxSize
- int
Format this n-cube into JSON using the passed in 'options' Map to control the desired output. See the JsonFormatter's format() API for available options.
Use this API to generate an HTML view of this NCube.
headers
- String list of axis names to place at top. If more than one is listed, the first axis encountered that
matches one of the passed in headers, will be the axis chosen to be displayed at the top.Change the value of a Column along an axis.
id
- long indicates the column to changevalue
- Comparable new value to set into the columnorder
- int (optional) new display order for columnUpdate all of the columns along an axis at once. Any cell referencing a column that is deleted, will also be deleted from the internal sparse matrix (Map) of cells.
axisName
- String axis name to updatenewCols
- ListGrab the cell located at altInput, then run it in terms of the input.
Ensure that the passed in name is a valid n-cube name,
cubeName
- String cube name to test.