Files
cvc5/examples/api/java/Statistics.java
Andres Noetzli 62d361071e Fix iterators in Java API (#3000)
Fixes #2989. SWIG 3 seems to have an issue properly resolving
`T::const_iterator::value_type` if that type itself is a `typedef`.
This is for example the case in the `UnsatCore` class, which `typedef`s
`const_iterator` to `std::vector<Expr>::const_iterator`. As a
workaround, this commit changes the `JavaIteratorAdapter` class to take
two template parameters, one of which is the `value_type`. The commit
also adds a compile-time assertion that `T::const_iterator::value_type`
can be converted to `value_type` to avoid nasty surprises. A nice
side-effect of this solution is that explicit `typemap`s are not
necessary anymore, so they are removed. Additionally, the commit adds a
`toString()` method for the Java API of `UnsatCore` and adds examples
that show and test the iteration over the unsat core and the statistics.
Iterating over `Statistics` now returns instances of `Statistic` instead
of `Object[]`, which is a bit cleaner and requires less glue code.
2019-05-15 17:18:48 -07:00

46 lines
1.5 KiB
Java

/********************* */
/*! \file Statistics.java
** \verbatim
** Top contributors (to current version):
** Andres Noetzli
** This file is part of the CVC4 project.
** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
** in the top-level source directory) and their institutional affiliations.
** All rights reserved. See the file COPYING in the top-level source
** directory for licensing information.\endverbatim
**
** \brief An example of accessing CVC4's statistics using the Java API
**
** An example of accessing CVC4's statistics using the Java API.
**/
import edu.nyu.acsys.CVC4.*;
import java.util.Iterator;
public class Statistics {
public static void main(String[] args) {
System.loadLibrary("cvc4jni");
ExprManager em = new ExprManager();
SmtEngine smt = new SmtEngine(em);
Type boolType = em.booleanType();
Expr a = em.mkVar("A", boolType);
Expr b = em.mkVar("B", boolType);
// A ^ B
smt.assertFormula(em.mkExpr(Kind.AND, a, b));
Result res = smt.checkSat();
// Get the statistics from the `SmtEngine` and iterate over them. The
// `Statistics` class implements the `Iterable<Statistic>` interface. A
// `Statistic` is a pair that consists of a name and an `SExpr` that stores
// the value of the statistic.
edu.nyu.acsys.CVC4.Statistics stats = smt.getStatistics();
for (Statistic stat : stats) {
System.out.println(stat.getFirst() + " = " + stat.getSecond());
}
}
}