@@ -86,11 +86,12 @@ object KB {
86
86
87
87
private lazy val logger = Logger (this .getClass)
88
88
89
- private lazy val formulaRegex =
90
- Pattern .compile(" ^[^(//)](.*\\ s=>\\ s.*|.*\\ s<=>\\ s.*|.*\\ s:-\\ s.*|.*\\ s^\\ s.*|.*\\ sv\\ s.*|.*\\ s!\\ s.*|\\ d.*|.*\\ .)" )
91
- private lazy val ignoreRegex =
89
+ private final lazy val IgnoreRegex =
92
90
Pattern .compile(" (\\ s*\\ *.*|/\\ *.*|//.*|\\ s+)+" )
93
91
92
+ private final lazy val DomainSchemaRegex =
93
+ Pattern .compile(" (^\\ s*[a-z]\\ w*\\ s*=\\ s*[{].*[}]\\ s*$)|(^\\ s*[A-Z][a-zA-Z0-9(, ]*[)]\\ s*$)|(^\\ s*[a-z][a-zA-Z0-9]*\\ s+[a-z][a-zA-Z0-9(, ]*[)]\\ s*$)" )
94
+
94
95
/**
95
96
* Parse a knowledge base from a given file.
96
97
*
@@ -109,8 +110,9 @@ object KB {
109
110
logger.fatal(s " Cannot read input MLN file ' ${kbFile.getPath}'. " )
110
111
111
112
val fileReader = new BufferedReader (new FileReader (kbFile))
112
- val formulaMatcher = formulaRegex.matcher(" " )
113
- val commentMatcher = ignoreRegex.matcher(" " )
113
+
114
+ val commentMatcher = IgnoreRegex .matcher(" " )
115
+ val domainMatcher = DomainSchemaRegex .matcher(" " )
114
116
115
117
val domainParser = new DomainParser ()
116
118
@@ -174,41 +176,52 @@ object KB {
174
176
// ---------------------------------------------------
175
177
// Parse schema and constants
176
178
// ---------------------------------------------------
179
+
177
180
var stop = false
178
181
var lineIdx = 0
179
182
180
183
while (fileReader.ready() && ! stop) {
184
+
181
185
val line = fileReader.readLine()
182
186
lineIdx += 1
187
+
183
188
if (! line.isEmpty) {
184
- if (formulaMatcher.reset(line).matches()) stop = true
185
- else if (! commentMatcher.reset(line).matches()) {
189
+ if (commentMatcher.reset(line).matches()) {
190
+ logger.trace(s " Parsing comment expressions: ${line}" )
191
+ fileReader.mark(0 )
192
+ } else if (domainMatcher.reset(line).matches()) {
193
+ logger.trace(s " Parsing domain expressions: ${line}" )
186
194
fileReader.mark(0 )
187
195
domainParser.parse(domainParser.definition, line) match {
188
- case domainParser.Success (expr, _) => expr match {
189
- case ConstantTypeDefinition (symbol, cons) => constantsBuilder ++= (symbol, cons)
196
+ case domainParser.Success (expr, _) => {
197
+ expr match {
198
+ case ConstantTypeDefinition (symbol, cons) => constantsBuilder ++= (symbol, cons)
190
199
191
- case IntegerTypeDefinition (symbol, start, end) => constantsBuilder ++= (symbol, (start to end).map(_.toString))
200
+ case IntegerTypeDefinition (symbol, start, end) => constantsBuilder ++= (symbol, (start to end).map(_.toString))
192
201
193
- case FunctionType (retType, functionName, args) =>
194
- kbBuilder.functionSchema += (AtomSignature (functionName, args.size) -> (retType, args))
195
- constantsBuilder addKey retType
196
- constantsBuilder addKeys args
202
+ case FunctionType (retType, functionName, args) =>
203
+ kbBuilder.functionSchema += (AtomSignature (functionName, args.size) -> (retType, args))
204
+ constantsBuilder addKey retType
205
+ constantsBuilder addKeys args
197
206
198
- case AtomicType (predicateName, args) =>
199
- val atomSignature = AtomSignature (predicateName, args.size)
207
+ case AtomicType (predicateName, args) =>
208
+ val atomSignature = AtomSignature (predicateName, args.size)
200
209
201
- kbBuilder.predicateSchema().get(atomSignature) match {
202
- case None =>
203
- kbBuilder.predicateSchema += (atomSignature -> (for (element <- args) yield element))
204
- constantsBuilder addKeys args
210
+ kbBuilder.predicateSchema().get(atomSignature) match {
211
+ case None =>
212
+ kbBuilder.predicateSchema += (atomSignature -> (for (element <- args) yield element))
213
+ constantsBuilder addKeys args
205
214
206
- case _ => stop = true
207
- }
208
- case _ => sys.error(" Cannot parse type definition '" + expr + " ' in line " + lineIdx)
215
+ case _ => stop = true
216
+ }
217
+ case _ => sys.error(" Cannot parse type definition '" + expr + " ' in line " + lineIdx)
218
+ }
209
219
}
210
- case _ => // ignore
220
+ case domainParser. Failure (msg, _) => logger.error(msg)
211
221
}
222
+ } else {
223
+ logger.trace(s " Formulas starting from line: ${lineIdx}" )
224
+ stop = true
212
225
}
213
226
}
214
227
@@ -218,7 +231,9 @@ object KB {
218
231
// Parse formulas and definite clauses
219
232
// ---------------------------------------------------
220
233
234
+ // fileReader.mark(0)
221
235
fileReader.reset()
236
+
222
237
val kbParser = new KBParser (
223
238
kbBuilder.predicateSchema(),
224
239
kbBuilder.functionSchema(),
0 commit comments