Skip to content

Commit 076572b

Browse files
committed
Fixes bug reported in anskarlGH-21
1 parent 91280a8 commit 076572b

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

src/main/scala/lomrf/mln/model/KB.scala

+39-24
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ object KB {
8686

8787
private lazy val logger = Logger(this.getClass)
8888

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 =
9290
Pattern.compile("(\\s*\\*.*|/\\*.*|//.*|\\s+)+")
9391

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+
9495
/**
9596
* Parse a knowledge base from a given file.
9697
*
@@ -109,8 +110,9 @@ object KB {
109110
logger.fatal(s"Cannot read input MLN file '${kbFile.getPath}'.")
110111

111112
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("")
114116

115117
val domainParser = new DomainParser()
116118

@@ -174,41 +176,52 @@ object KB {
174176
// ---------------------------------------------------
175177
// Parse schema and constants
176178
// ---------------------------------------------------
179+
177180
var stop = false
178181
var lineIdx = 0
179182

180183
while (fileReader.ready() && !stop) {
184+
181185
val line = fileReader.readLine()
182186
lineIdx += 1
187+
183188
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}")
186194
fileReader.mark(0)
187195
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)
190199

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))
192201

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
197206

198-
case AtomicType(predicateName, args) =>
199-
val atomSignature = AtomSignature(predicateName, args.size)
207+
case AtomicType(predicateName, args) =>
208+
val atomSignature = AtomSignature(predicateName, args.size)
200209

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
205214

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+
}
209219
}
210-
case _ => // ignore
220+
case domainParser.Failure(msg, _) => logger.error(msg)
211221
}
222+
} else {
223+
logger.trace(s"Formulas starting from line: ${lineIdx}")
224+
stop = true
212225
}
213226
}
214227

@@ -218,7 +231,9 @@ object KB {
218231
// Parse formulas and definite clauses
219232
// ---------------------------------------------------
220233

234+
// fileReader.mark(0)
221235
fileReader.reset()
236+
222237
val kbParser = new KBParser(
223238
kbBuilder.predicateSchema(),
224239
kbBuilder.functionSchema(),

0 commit comments

Comments
 (0)