-
Notifications
You must be signed in to change notification settings - Fork 2
/
ParserPB.h
165 lines (133 loc) · 4.13 KB
/
ParserPB.h
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
162
163
164
165
/*!
* \author Vasco Manquinho - [email protected]
*
* @section LICENSE
*
* Open-WBO, Copyright (c) 2013-2015, Ruben Martins, Vasco Manquinho, Ines Lynce
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef __PB_PARSER__
#define __PB_PARSER__
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string.h>
#include "MaxSATFormula.h"
using NSPACE::vec;
using std::cout;
using std::endl;
#define MAX_WORD_LENGTH 256
#ifndef _PB_MIN_
#define _PB_MIN_ 1
#define _PB_MAX_ 0
#endif
/*! Definition of possible constraint signs. */
enum pb_Sign { _PB_GREATER_OR_EQUAL_ = 0x1, _PB_LESS_OR_EQUAL_, _PB_EQUAL_ };
namespace openwbo {
/*! Generic parser class in open-wbo. All other parsers inherit from this class.
*/
class ParserPB {
public:
//-------------------------------------------------------------------------
// Constructor/destructor.
//-------------------------------------------------------------------------
ParserPB();
virtual ~ParserPB();
//-------------------------------------------------------------------------
// Interface contract:
//-------------------------------------------------------------------------
virtual int parse(char *fileName);
void parsePBFormula(char *fileName, MaxSATFormula *max) {
maxsat_formula = max;
parse(fileName);
}
protected:
// OPB instance parsing.
virtual int parseLine();
virtual int parseCostFunction();
virtual int parseConstraint();
virtual int parseProduct(int64_t *coeff, char *varName, int *varNameSize);
virtual int getVariableID(char *varName, int varNameSize);
inline char peek_char() { return *_fileStr; }
inline char get_char() { return *_fileStr++; }
inline void unget_char() { _fileStr--; }
inline void skip_spaces() {
while (get_char() == ' ')
;
unget_char();
}
inline void readUntilEndOfLine() {
static char c;
while ((c = get_char()) != '\n' && c != '\0')
;
}
inline void parseNumber(int64_t *coeff) {
static char word[MAX_WORD_LENGTH];
int i = 0, c = peek_char();
int64_t conv;
*coeff = 1;
while ((c == '-') || (c == '+')) {
if (c == '-')
*coeff *= -1;
get_char();
skip_spaces();
c = peek_char();
}
while (isdigit(word[i] = get_char())) {
i++;
}
unget_char();
word[i] = '\0';
assert(i > 0);
std::istringstream ss(word);
ss >> conv;
// sscanf(word, "%d", &i);
*coeff = (*coeff) * conv;
}
inline void parseWord(char *varName, int *varNameSize) {
int i = 0;
while (isgraph(varName[i] = get_char())) {
i++;
}
unget_char();
varName[i] = '\0';
*varNameSize = i;
}
protected:
struct eqstr {
bool operator()(const char *s1, const char *s2) const {
return strcmp(s1, s2) == 0;
}
};
char *_fileStr;
int _fd;
vec<int64_t> _coefficients;
vec<int> _constraintVariables;
int64_t _highestCoeffSum;
MaxSATFormula *maxsat_formula;
};
} // namespace openwbo
#endif // __PB_PARSER__
/*****************************************************************************/