Skip to content

Commit 8a25b34

Browse files
committed
minor fix - average returns double successfully
1 parent f35c414 commit 8a25b34

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/main/kotlin/no/uio/microobject/ast/stmt/MonitorStmt.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,23 @@ class MonitorObject(private val name: LiteralExpr, private val declaredType: Typ
5252

5353
private fun iriToLiteral(iri: String, interpreter: Interpreter): LiteralExpr {
5454

55-
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#integer")) return LiteralExpr(iri.split("^^")[0], INTTYPE)
55+
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#integer"))
56+
return LiteralExpr(iri.split("^^")[0].removeSurrounding("\""), INTTYPE)
5657
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#boolean")) return LiteralExpr(iri.split("^^")[0], BOOLEANTYPE)
57-
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#double")) return LiteralExpr(iri.split("^^")[0], DOUBLETYPE)
58+
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#double") ||
59+
iri.endsWith("^^http://www.w3.org/2001/XMLSchema#float") ||
60+
iri.endsWith("^^http://www.w3.org/2001/XMLSchema#decimal")) {
61+
val raw = iri.split("^^")[0]
62+
val inner = raw.removeSurrounding("\"")
63+
// try to remove scientific notation
64+
try {
65+
val normalized = inner.toDouble()
66+
val normalizedStr = "${normalized}"
67+
return LiteralExpr(normalizedStr, DOUBLETYPE)
68+
} catch (e: NumberFormatException) {
69+
throw Exception("Invalid xsd:double literal: $raw")
70+
}
71+
}
5872
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#string")) return LiteralExpr(iri.split("^^")[0], STRINGTYPE)
5973
val literal = iri.removePrefix(interpreter.settings.runPrefix)
6074
for (obj in interpreter.heap.keys + interpreter.simMemory.keys)
@@ -75,7 +89,7 @@ class MonitorObject(private val name: LiteralExpr, private val declaredType: Typ
7589
// only consider first result for now
7690
var literal = iriToLiteral(rdfTuple.get(0), interpreter)
7791
if (literal.tag != declaredType)
78-
throw Exception("Monitor parameter has incorrect type")
92+
throw Exception("Monitor parameter has incorrect type (expected ${declaredType}, got ${literal.tag})")
7993

8094
val name = Names.getObjName("List")
8195
val newMemory: Memory = mutableMapOf()

0 commit comments

Comments
 (0)