File: fill_used_clauses.py

package info (click to toggle)
cryptominisat 5.11.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,432 kB
  • sloc: cpp: 55,148; ansic: 9,642; python: 8,899; sh: 1,336; php: 477; sql: 403; javascript: 173; xml: 34; makefile: 15
file content (161 lines) | stat: -rwxr-xr-x 5,166 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (C) 2009-2020 Authors of CryptoMiniSat, see AUTHORS file
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

from __future__ import print_function
import sqlite3
import argparse
import os
import struct
import time


class Query:
    def __init__(self, dbfname):
        self.conn = sqlite3.connect(dbfname)
        self.c = self.conn.cursor()

        self.cl_used = []
        self.cl_used_num = 0
        self.cl_used_total = 0

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.conn.commit()
        self.conn.close()

    def delete_tbls(self, table):
        queries = """
DROP TABLE IF EXISTS `{table}`;
create table `{table}` ( `clauseID` bigint(20) NOT NULL, `used_at` bigint(20) NOT NULL, `weight` float NOT NULL);
""".format(table=table)

        for l in queries.split('\n'):
            t2 = time.time()

            if options.verbose:
                print("Runnin query: ", l)
            self.c.execute(l)
            if options.verbose:
                print("Query T: %-3.2f s" % (time.time() - t2))

    def get_last_good(self, basefname):
        last_good = -1
        for i in range(100):
            tfname = "%s-%d" % (basefname, i)
            print("Checking if file %s exists" % tfname)
            if os.path.isfile(tfname):
              last_good = i

        if last_good == -1:
            print("ERROR: file does not exist at all")
            exit(-1)

        return last_good

    def add_used_clauses(self, usedClFname):
        last_good = self.get_last_good(usedClFname)
        tfname = "%s-%d" % (usedClFname, last_good)
        tfname_anc = tfname.replace("usedCls", "usedCls-anc")


        for fname,table in [(tfname, "used_clauses"), (tfname_anc, "used_clauses_anc")]:
            t = time.time()
            print("Adding data from file %s to DB %s" % (fname, table))

            self.cl_used = []
            self.cl_used_num = 0
            self.cl_used_total = 0
            with open(fname, "rb") as f:
                while True:
                    b1 = f.read(8)
                    if not b1:
                        # end of file
                        break
                    b2 = f.read(8)
                    cl_id = struct.unpack("<q", b1)[0]
                    conf = struct.unpack("<q", b2)[0]

                    weight = 1.0
                    if "used_clauses_anc" in table:
                        b3 = f.read(4)
                        weight = struct.unpack("<f", b3)[0]

                    dat = (cl_id, conf, weight)
                    if options.verbose:
                        print("Use:", dat)
                    self.cl_used.append(dat)
                    self.cl_used_num += 1
                    self.cl_used_total += 1
                    if self.cl_used_num > 10000:
                        self.dump_used_clauses(table)

            self.dump_used_clauses(table)
            print("Added use data to table %s from file %s: %d time: T: %-3.2f s" %
                  (table, fname, self.cl_used_total, time.time() - t))

    def dump_used_clauses(self, table):
        self.c.executemany("""
        INSERT INTO %s (
        `clauseID`,
        `used_at`,
        `weight`)
        VALUES (?, ?, ?);""" % table, self.cl_used)
        self.cl_used = []
        self.cl_used_num = 0


if __name__ == "__main__":
    usage = """usage: %(prog)s [options] sqlite_db usedCls

Adds used_clauses to the SQLite database"""

    parser = argparse.ArgumentParser(usage=usage)


    parser.add_argument("sqlitedb", type=str, metavar='USEDCLS')
    parser.add_argument("usedcls", type=str, metavar='USEDCLS')
    parser.add_argument("--verbose", "-v", action="store_true", default=False,
                      dest="verbose", help="Print more output")

    options = parser.parse_args()

    if options.sqlitedb is None:
        print("Error you must give an SQLite DB. Please follow usage.")
        print(usage)
        exit(-1)

    if options.usedcls is None:
        print("Error you must give an Used CLS list. Please follow usage.")
        print(usage)
        exit(-1)

    print("Using sqlite3db file %s" % options.sqlitedb)
    print("Base used_clauses file is %s" % options.usedcls)

    with Query(options.sqlitedb) as q:
        q.delete_tbls("used_clauses")
        q.delete_tbls("used_clauses_anc")
        q.add_used_clauses(options.usedcls)

    print("Finished adding good lemma indicators to db %s" % options.sqlitedb)

    exit(0)