Class AstPrinter

java.lang.Object
org.clafer.ast.AstPrinter

public class AstPrinter extends Object
Prints a model in a Clafer-like syntax with nothing left implicit: the clafers by their names (the compiler's UIDs), every card and group card, every this, parent and dref. The output is a Clafer model, which the Clafer compiler reads with -f (the names are already UIDs) and -l (no layout). Two models print the same when they have the same clafers, constraints, objectives and assertions, so the output can be compared, e.g., between two compilers of the same Clafer model. The scopes, all of them, go to the model's .cfr-scope file (ScopeFile). The solver options (e.g., branching priorities) are not printed.

The clafers, their children and their constraints are printed in the order they were added, which is the order of the source and of the instances, abstract and concrete clafers interleaved (see sourceOrder(List, List)). Source spans are not printed.

Nesting is in braces, as in Clafer without layout: every clafer has its block, {} if it is empty. The indentation is only for reading; whitespace and line breaks carry no meaning.

Values are printed as they apply, declarations where they are declared. A group card and a multiplicity are values: a clafer without its own group card has its super clafer's, as chocosolver solves it, and 0..* if no super clafer has one; an abstract clafer's multiplicity is 0..*. Children, constraints and references are declarations: they stay under the clafer that declares them. A reference is printed only where it is declared or redefined, since in Clafer a reference written on a sub clafer redefines the inherited one, even with the same target.

Expressions are fully parenthesized, except for navigation (.), which binds tightest. sum and product take a set of clafers and add (multiply) the dref of each instance, so two instances with the same value count twice; this is printed as in Clafer, e.g., sum c0_Phone.c0_cost.dref. An explicit dereference inside would print as .dref.dref.

  • Field Details

    • ExplicitHeader

      public static final String ExplicitHeader
      The first line of an explicit model, which declares it to the Clafer compiler (its options -f -k -l --ss=none) and to chocosolver (read with ToAst, not compiled by clafer).
      See Also:
  • Method Details

    • print

      public static String print(AstModel model, Objective[] objectives, Assertion[] assertions)
      Prints a model with its objectives and assertions, as an explicit model: ExplicitHeader, then the model. Its scopes go to a separate file, see ScopeFile.print(AstModel, Scope).
      Parameters:
      model - the model
      objectives - the objectives
      assertions - the assertions
      Returns:
      the model as text, one line per clafer, constraint, objective and assertion
    • print

      public static String print(AstModel model)
      Prints the clafers and constraints of a model.
      Parameters:
      model - the model
      Returns:
      the model as text
    • print

      public static String print(AstExpr expr)
      Prints an expression.
      Parameters:
      expr - the expression
      Returns:
      the expression as text