class Dusa
The main entrypoint to the Dusa JavaScript API is the Dusa class. (The dusa NPM package also includes Typescript definitions.)
Creating a Dusa instance
Dusa()
constructor
A Dusa instance is created by passing a Dusa program to the constructor.
If the program has errors, an error in the DusaError
class will be thrown.
Solving a Dusa instance
Dusa programs can’t be directly queried: they must first be solved. There are several
different ways to direct Dusa to generate solutions, all of which provide access to
DusaSolution
objects.
solution
getter
If a Dusa program has at most one solution, that solution can be accessed with the
solution
getter. The first time this method is accessed it will cause some
computation to happen (and it could even fail to terminate if there aren’t finite
solutions). The result is cached, so subsequent calls will not trigger additional
computation.
Explore this example on StackBlitz
If no solutions exist, the solution
getter will return null
.
Explore this example on StackBlitz
This getter can only be used if a single solution exists. Dusa will check for
additional solutions when the solution
getter is accessed, and will throw an
exception if there are other solutions.
Explore this example on StackBlitz
For programs with multiple solutions, use the sample()
method or the solutions
getter, which returns an iterator.
sample()
method
The sample()
method will return an arbitrary solution to the Dusa program, or
null
if no solution exists.
Each call to sample()
re-computes the program, so even if there are only a finite
(but nonzero) number of solutions, sample()
can be called as many times as desired.
Explore this example on StackBlitz
The current Dusa interpreter does not have a well-defined probabilistic semantics for
complex programs, but the simple program above will print "one"
about 500 times and
will print "two"
about 500 times.
solutions getter
The solutions
getter iterates through all the possible distinct solutions of a Dusa
program. The iterator works in an arbitrary order: this program will either print
[["one"]]
and then [["two"]]
or else it will print [["two"]]
and then [["one"]]
.
Explore this example on StackBlitz
Each time the dusa.solutions
getter is accessed, an iterator is returned that
re-runs solution search, potentially returning solutions in a different order.
Modifying a Dusa instance
The Dusa implementation doesn’t support adding and removing rules after a Dusa
class instance has been created, but it does support adding additional facts,
which can be just as powerful. This can be useful for applications where the actual
set of facts isn’t known ahead of time, but the desired analysis on those facts is
known.
assert() method
The assert
method of a Dusa instance takes an arbitrary number of arguments, each
one a Fact, and adds them to the database.