1 /******************************************************************************
2 * Top contributors (to current version):
5 * This file is part of the cvc5 project.
7 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8 * in the top-level source directory and their institutional affiliations.
9 * All rights reserved. See the file COPYING in the top-level source
10 * directory for licensing information.
11 * ****************************************************************************
13 * An example of accessing cvc5's statistics using the Java API.
16 import static io
.github
.cvc5
.Kind
.*;
18 import io
.github
.cvc5
.*;
19 import java
.util
.List
;
22 public class Statistics
24 public static void main(String
[] args
)
26 try (Solver solver
= new Solver())
28 // Get the statistics from the `Solver` and iterate over them. The
29 // `Statistics` class implements the `Iterable<Pair<String, Stat>>` interface.
30 io
.github
.cvc5
.Statistics stats
= solver
.getStatistics();
32 System
.out
.println("Short version:");
33 System
.out
.println(stats
);
35 System
.out
.println("-------------------------------------------------------");
37 System
.out
.println("Long version:");
40 for (Map
.Entry
<String
, Stat
> pair
: stats
)
42 Stat stat
= pair
.getValue();
45 System
.out
.println(pair
.getKey() + " = " + stat
.getInt());
47 else if (stat
.isDouble())
49 System
.out
.println(pair
.getKey() + " = " + stat
.getDouble());
51 else if (stat
.isString())
53 System
.out
.println(pair
.getKey() + " = " + stat
.getString());
55 else if (stat
.isHistogram())
57 System
.out
.println("-------------------------------------------------------");
58 System
.out
.println(pair
.getKey() + " : Map");
59 for (Map
.Entry
<String
, Long
> entry
: stat
.getHistogram().entrySet())
61 System
.out
.println(entry
.getKey() + " = " + entry
.getValue());
63 System
.out
.println("-------------------------------------------------------");
69 private static Solver
getSolver()
71 Solver solver
= new Solver();
74 Sort string
= solver
.getStringSort();
79 Term ab
= solver
.mkString(str_ab
);
80 Term abc
= solver
.mkString("abc");
82 Term x
= solver
.mkConst(string
, "x");
83 Term y
= solver
.mkConst(string
, "y");
84 Term z
= solver
.mkConst(string
, "z");
86 // String concatenation: x.ab.y
87 Term lhs
= solver
.mkTerm(STRING_CONCAT
, x
, ab
, y
);
88 // String concatenation: abc.z
89 Term rhs
= solver
.mkTerm(STRING_CONCAT
, abc
, z
);
91 Term formula1
= solver
.mkTerm(EQUAL
, lhs
, rhs
);
94 Term leny
= solver
.mkTerm(STRING_LENGTH
, y
);
96 Term formula2
= solver
.mkTerm(GEQ
, leny
, solver
.mkInteger(0));
98 // Regular expression: (ab[c-e]*f)|g|h
99 Term r
= solver
.mkTerm(REGEXP_UNION
,
100 solver
.mkTerm(REGEXP_CONCAT
,
101 solver
.mkTerm(STRING_TO_REGEXP
, solver
.mkString("ab")),
102 solver
.mkTerm(REGEXP_STAR
,
103 solver
.mkTerm(REGEXP_RANGE
, solver
.mkString("c"), solver
.mkString("e"))),
104 solver
.mkTerm(STRING_TO_REGEXP
, solver
.mkString("f"))),
105 solver
.mkTerm(STRING_TO_REGEXP
, solver
.mkString("g")),
106 solver
.mkTerm(STRING_TO_REGEXP
, solver
.mkString("h")));
109 Term s1
= solver
.mkConst(string
, "s1");
110 Term s2
= solver
.mkConst(string
, "s2");
111 // String concatenation: s1.s2
112 Term s
= solver
.mkTerm(STRING_CONCAT
, s1
, s2
);
114 // s1.s2 in (ab[c-e]*f)|g|h
115 Term formula3
= solver
.mkTerm(STRING_IN_REGEXP
, s
, r
);
118 Term q
= solver
.mkTerm(AND
, formula1
, formula2
, formula3
);
121 solver
.setOption("produce-models", "true");
122 solver
.setOption("finite-model-find", "true");
123 solver
.setOption("sets-ext", "true");
124 solver
.setOption("output-language", "smt2");
126 // (declare-sort Person 0)
127 Sort personSort
= solver
.mkUninterpretedSort("Person");
130 Sort tupleArity1
= solver
.mkTupleSort(new Sort
[] {personSort
});
131 // (Set (Tuple Person))
132 Sort relationArity1
= solver
.mkSetSort(tupleArity1
);
134 // (Tuple Person Person)
135 Sort tupleArity2
= solver
.mkTupleSort(new Sort
[] {personSort
, personSort
});
136 // (Set (Tuple Person Person))
137 Sort relationArity2
= solver
.mkSetSort(tupleArity2
);
140 Term emptySetTerm
= solver
.mkEmptySet(relationArity1
);
143 Term emptyRelationTerm
= solver
.mkEmptySet(relationArity2
);
146 Term universeSet
= solver
.mkUniverseSet(relationArity1
);
149 Term people
= solver
.mkConst(relationArity1
, "people");
150 Term males
= solver
.mkConst(relationArity1
, "males");
151 Term females
= solver
.mkConst(relationArity1
, "females");
152 Term father
= solver
.mkConst(relationArity2
, "father");
153 Term mother
= solver
.mkConst(relationArity2
, "mother");
154 Term parent
= solver
.mkConst(relationArity2
, "parent");
155 Term ancestor
= solver
.mkConst(relationArity2
, "ancestor");
156 Term descendant
= solver
.mkConst(relationArity2
, "descendant");
158 Term isEmpty1
= solver
.mkTerm(EQUAL
, males
, emptySetTerm
);
159 Term isEmpty2
= solver
.mkTerm(EQUAL
, females
, emptySetTerm
);
161 // (assert (= people (as set.universe (Set (Tuple Person)))))
162 Term peopleAreTheUniverse
= solver
.mkTerm(EQUAL
, people
, universeSet
);
163 // (assert (not (= males (as set.empty (Set (Tuple Person))))))
164 Term maleSetIsNotEmpty
= solver
.mkTerm(NOT
, isEmpty1
);
165 // (assert (not (= females (as set.empty (Set (Tuple Person))))))
166 Term femaleSetIsNotEmpty
= solver
.mkTerm(NOT
, isEmpty2
);
168 // (assert (= (set.inter males females) (as set.empty (Set (Tuple
170 Term malesFemalesIntersection
= solver
.mkTerm(SET_INTER
, males
, females
);
171 Term malesAndFemalesAreDisjoint
= solver
.mkTerm(EQUAL
, malesFemalesIntersection
, emptySetTerm
);
173 // (assert (not (= father (as set.empty (Set (Tuple Person Person))))))
174 // (assert (not (= mother (as set.empty (Set (Tuple Person Person))))))
175 Term isEmpty3
= solver
.mkTerm(EQUAL
, father
, emptyRelationTerm
);
176 Term isEmpty4
= solver
.mkTerm(EQUAL
, mother
, emptyRelationTerm
);
177 Term fatherIsNotEmpty
= solver
.mkTerm(NOT
, isEmpty3
);
178 Term motherIsNotEmpty
= solver
.mkTerm(NOT
, isEmpty4
);
181 // (assert (set.subset (rel.join father people) males))
182 Term fathers
= solver
.mkTerm(RELATION_JOIN
, father
, people
);
183 Term fathersAreMales
= solver
.mkTerm(SET_SUBSET
, fathers
, males
);
185 // mothers are females
186 // (assert (set.subset (rel.join mother people) females))
187 Term mothers
= solver
.mkTerm(RELATION_JOIN
, mother
, people
);
188 Term mothersAreFemales
= solver
.mkTerm(SET_SUBSET
, mothers
, females
);
190 // (assert (= parent (set.union father mother)))
191 Term unionFatherMother
= solver
.mkTerm(SET_UNION
, father
, mother
);
192 Term parentIsFatherOrMother
= solver
.mkTerm(EQUAL
, parent
, unionFatherMother
);
194 // (assert (= descendant (rel.tclosure parent)))
195 Term transitiveClosure
= solver
.mkTerm(RELATION_TCLOSURE
, parent
);
196 Term descendantFormula
= solver
.mkTerm(EQUAL
, descendant
, transitiveClosure
);
198 // (assert (= ancestor (rel.transpose descendant)))
199 Term transpose
= solver
.mkTerm(RELATION_TRANSPOSE
, descendant
);
200 Term ancestorFormula
= solver
.mkTerm(EQUAL
, ancestor
, transpose
);
202 // (assert (forall ((x Person)) (not (set.member (mkTuple x x) ancestor))))
203 Term var
= solver
.mkVar(personSort
, "x");
204 DatatypeConstructor constructor
= tupleArity2
.getDatatype().getConstructor(0);
205 Term xxTuple
= solver
.mkTerm(APPLY_CONSTRUCTOR
, constructor
.getTerm(), var
, var
);
206 Term member
= solver
.mkTerm(SET_MEMBER
, xxTuple
, ancestor
);
207 Term notMember
= solver
.mkTerm(NOT
, member
);
209 Term quantifiedVariables
= solver
.mkTerm(VARIABLE_LIST
, var
);
210 Term noSelfAncestor
= solver
.mkTerm(FORALL
, quantifiedVariables
, notMember
);
213 solver
.assertFormula(peopleAreTheUniverse
);
214 solver
.assertFormula(maleSetIsNotEmpty
);
215 solver
.assertFormula(femaleSetIsNotEmpty
);
216 solver
.assertFormula(malesAndFemalesAreDisjoint
);
217 solver
.assertFormula(fatherIsNotEmpty
);
218 solver
.assertFormula(motherIsNotEmpty
);
219 solver
.assertFormula(fathersAreMales
);
220 solver
.assertFormula(mothersAreFemales
);
221 solver
.assertFormula(parentIsFatherOrMother
);
222 solver
.assertFormula(descendantFormula
);
223 solver
.assertFormula(ancestorFormula
);
224 solver
.assertFormula(noSelfAncestor
);
227 solver
.checkSatAssuming(q
);