1.0.0
Represents a single constraint satisfaction problem that can be solved by topo-strict. Tracks all ids, group keys, and constraint keys to ensure that invalid keys are never added, that collisions between keys are not possible, and that all constraint keys reference some existing id or group before solving.
An object containing all keys in the Problem, categorized by their type.
Ids appear in the ids
property, and group keys appear in the groups
property.
Type: Object
An array of all keys in the problem, regardless of category.
Adds new ids to the problem. Will validate all provided ids and options, ensuring that the problem is not put into an illegal state. Will throw a ValidationError if any issues are found.
(Object
= {}
)
Options object for added ids.
Name | Description |
---|---|
options.ids (string | Array<string>)
(default [] )
|
Single non-empty
string or array of non-empty strings to add to the problem. This
option is combined with any
ids
appearing before the options
object.
|
options.group (string | null)
(default null )
|
Group key for associating
all added ids, if any. Group keys may be referenced in
before
and
after
constraints. If provided with a group key that already
exists in the problem, the ids will be added to that group.
|
options.before (string | Array<string>)
(default [] )
|
ids or group keys
that must appear before the provided ids in the final sort. These
may reference ids or groups that do not yet exist, but if a
corresponding id or group is not added by the time
#solve
or
#toGraph
is called, said method will throw a ValidationError.
|
options.after (string | Array<string>)
(default [] )
|
Similar to
options.before
, except that it forces all provided ids to appear
after
the referenced ids or groups in the final sort.
|
Returns a canonical plain object representation of the problem, with all
keys sorted alphabetically. This is used as the basis for #toString
, so overriding it will also change the behavior of
#toString`.
Object
:
Object representation of the problem with two
properties:
ids
: An array of objects for id entries, each with a key
property and a constraints
property that lists constraints. Each
constraint is itself an object with type
and key
properties.groups
: An array of objects for group entries, each with a key
property and a ids
property that lists ids contained in the group.Converts the Problem into a Graph instance, with nodes
containing ids and edges representing the constraints. This is the graph
that will be used when calling #solve
. Will throw a ValidationError if
any constraints reference keys that do not exist in the Problem.
Graph
:
Resulting graph.
Solves the problem by converting it to a directional graph and performing a depth-first search. Will return a sequence of ids that satisfies all contraints, if a such solution is possible. Will throw a CycleError if there is a cycle that prevents a solution, or a ValidationError if any constraints reference keys that do not exist in the Problem.
Array<string>
:
Sequence of ids.
Represents a directed graph which is used to solve a Problem.
Returns a canonical plain object representation of the graph, with all
nodes and edges sorted alphabetically. This is used as the basis for
#toString
, so overriding it will also change the behavior of
#toString
.
Object
:
An object representation of the graph with two
properties:
-
nodes
: An array of id strings that have been added as nodes.
-
edges
: An array of edge objects, each with string
from
and
to
properties identifying its source node and target node.
Base class for all errors defined by topo-strict. This class, as well as all other error classes in this project, inherit from NaniError and may use any of the constructor arguments of that class.
Extends NaniError
Error class used to indicate when invalid arguments were passed to Problem#add, or when the problem is in an invalid state-- i.e. missing keys referenced by constraints-- when calling Problem#solve or Problem#toGraph.
The reasons for failure will be instances of KeyError in the ValidationError's cause chain.
Extends TopoStrictError
Error class used to indicate when a Problem key is not valid. Instances will be contained in the cause chain of ValidationErrors produced when calling Problem#add, Problem#solve, or Problem#toGraph.
The offending key will appear in the instance's info
property.
Extends TopoStrictError
Error class used to indicate when a cycle is detected while trying to solve a Problem or Graph.
Extends TopoStrictError