Skip to content

Commit 36d778c

Browse files
committed
upgrade to csparql2: impl, deps, compatibility + minor fixes
1 parent a09940f commit 36d778c

File tree

6 files changed

+173
-108
lines changed

6 files changed

+173
-108
lines changed

build.gradle

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id 'org.jetbrains.kotlin.jvm' version '1.6.21'
2+
id 'org.jetbrains.kotlin.jvm' version '1.8.22'
33
id 'com.github.johnrengelman.shadow' version '5.2.0'
44
id 'antlr'
55
id "kr.motd.sphinx" version "2.10.0"
@@ -17,7 +17,7 @@ java {
1717

1818
repositories {
1919
mavenCentral()
20-
maven { url "https://overture.au.dk/artifactory/libs-release/" }
20+
maven { url "https://jitpack.io" }
2121
}
2222

2323
test {
@@ -35,21 +35,25 @@ dependencies {
3535
testImplementation 'io.kotest:kotest-runner-junit5:5.2.3'
3636
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
3737
testImplementation 'org.apache.commons:commons-lang3:3.12.0'
38-
testImplementation 'org.apache.jena:jena-fuseki-main:3.16.0'
38+
testImplementation 'org.apache.jena:jena-fuseki-main:4.10.0'
3939
implementation 'com.github.ajalt.clikt:clikt:3.4.2'
4040
implementation 'org.antlr:antlr4:4.8'
4141
antlr 'org.antlr:antlr4:4.8'
4242
implementation 'net.sourceforge.owlapi:org.semanticweb.hermit:1.4.5.519'
4343
implementation 'org.slf4j:slf4j-simple:1.7.25'
44-
implementation 'org.apache.jena:apache-jena-libs:3.16.0'
45-
implementation 'org.apache.jena:jena-core:3.16.0'
44+
implementation 'org.apache.jena:apache-jena-libs:4.10.0'
45+
implementation 'org.apache.jena:jena-core:4.10.0'
4646
implementation 'org.siani.javafmi:fmu-wrapper:2.26.3'
4747
implementation "com.influxdb:influxdb-client-kotlin:2.3.0"
4848
implementation 'com.sksamuel.hoplite:hoplite-core:1.4.1'
4949
implementation 'com.sksamuel.hoplite:hoplite-yaml:1.4.1'
50-
implementation 'com.github.owlcs:ontapi:2.1.0'
50+
implementation 'com.github.owlcs:ontapi:3.0.5'
5151
implementation 'org.jline:jline:3.21.0'
52-
implementation files('libs/csparql-ui-0.9.7-jar-with-dependencies.jar')
52+
implementation 'com.github.streamreasoning.rsp4j:api:1.1.8'
53+
implementation 'com.github.streamreasoning.rsp4j:yasper:1.1.8'
54+
implementation 'com.github.streamreasoning.rsp4j:io:1.1.8'
55+
implementation 'com.github.streamreasoning.rsp4j:dsms:1.1.8'
56+
implementation 'com.github.streamreasoning.rsp4j:csparql2:1.1.8'
5357
}
5458

5559

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import no.uio.microobject.type.*
1010
import org.apache.jena.datatypes.xsd.XSDDatatype
1111
import java.io.File
1212

13-
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy
14-
import eu.larkc.csparql.common.RDFTuple
15-
1613
data class MonitorStmt(val target : Location, val query: Expression, val params : List<Expression>, val pos : Int = -1, val declares: Type?) :
1714
Statement {
1815
override fun toString(): String = "$target := monitor($query, ${params.joinToString(",")})"
@@ -36,10 +33,9 @@ data class MonitorStmt(val target : Location, val query: Expression, val params
3633

3734
override fun eval(heapObj: Memory, stackFrame: StackEntry, interpreter: Interpreter): EvalResult {
3835
val name = Names.getObjName("Monitor")
39-
interpreter.streamManager.registerQuery(name, query, params, stackFrame.store, interpreter.heap, stackFrame.obj)
4036
if (declares is ComposedType && declares.getPrimary().getNameString().equals("Monitor")) {
4137
// only consider the first type for now. e.g., Monitor<Double>
42-
interpreter.streamManager.addMonitor(name, MonitorObject(name, declares.params[0]))
38+
interpreter.streamManager.registerQuery(name, query, params, stackFrame.store, interpreter.heap, stackFrame.obj, declaredType=declares.params[0])
4339
} else {
4440
throw Exception("Monitor statement can only be assigned to type Monitor<T>")
4541
}
@@ -52,7 +48,10 @@ class MonitorObject(private val name: LiteralExpr, private val declaredType: Typ
5248

5349
private fun iriToLiteral(iri: String, interpreter: Interpreter): LiteralExpr {
5450

55-
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#integer"))
51+
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#integer") ||
52+
iri.endsWith("^^http://www.w3.org/2001/XMLSchema#long") ||
53+
iri.endsWith("^^http://www.w3.org/2001/XMLSchema#int")
54+
)
5655
return LiteralExpr(iri.split("^^")[0].removeSurrounding("\""), INTTYPE)
5756
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#boolean")) return LiteralExpr(iri.split("^^")[0], BOOLEANTYPE)
5857
if (iri.endsWith("^^http://www.w3.org/2001/XMLSchema#double") ||
@@ -82,12 +81,13 @@ class MonitorObject(private val name: LiteralExpr, private val declaredType: Typ
8281

8382
if (rdfTable != null) {
8483

85-
val resIt = rdfTable.iterator()
84+
val resIt = rdfTable.rows().iterator()
85+
val firstVar = rdfTable.varNames.get(0)
8686
while (resIt.hasNext()) {
8787
val rdfTuple = resIt.next()
8888
try {
8989
// only consider first result for now
90-
var literal = iriToLiteral(rdfTuple.get(0), interpreter)
90+
var literal = iriToLiteral(rdfTuple.get(firstVar).toString(), interpreter)
9191
if (literal.tag != declaredType)
9292
throw Exception("Monitor parameter has incorrect type (expected ${declaredType}, got ${literal.tag})")
9393

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import no.uio.microobject.main.ReasonerMode
1212
import org.apache.jena.datatypes.xsd.XSDDatatype
1313
import org.apache.jena.rdf.model.Model
1414
import java.io.File
15-
16-
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy
17-
import eu.larkc.csparql.common.RDFTuple
15+
import java.io.FileWriter
1816

1917
data class PushStaticStmt(val target : Location, val sources: Expression, val pos : Int = -1, val declares: Type?) : Statement {
2018
override fun toString(): String = "$target:=pushStatic(sources=$sources)"
@@ -60,9 +58,14 @@ data class PushStaticStmt(val target : Location, val sources: Expression, val po
6058
)
6159

6260
val model = interpreter.tripleManager.getModel(ts)
61+
// todo implement
62+
val file = "output.ttl"
63+
File(interpreter.settings.outdir).mkdirs()
64+
File("${interpreter.settings.outdir}/${file}").createNewFile()
65+
model.write(FileWriter("${interpreter.settings.outdir}/${file}"),"TTL")
66+
val resultPath = "<file://${interpreter.settings.outdir}/${file}>"
6367

64-
interpreter.streamManager.putStaticNamedGraph(namedIri, model)
65-
val resultLit = LiteralExpr(namedIri, STRINGTYPE)
68+
val resultLit = LiteralExpr(resultPath, STRINGTYPE)
6669
return replaceStmt(AssignStmt(target, resultLit, declares = declares), stackFrame)
6770
}
6871

src/main/kotlin/no/uio/microobject/data/StreamManager.kt

Lines changed: 106 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ import no.uio.microobject.ast.stmt.MonitorObject
77
import no.uio.microobject.main.Settings
88
import no.uio.microobject.type.*
99

10-
11-
import eu.larkc.csparql.core.engine.*
12-
import eu.larkc.csparql.cep.api.*
13-
import eu.larkc.csparql.core.*
14-
import eu.larkc.csparql.common.*
10+
import java.nio.file.*
1511
import java.util.Observer;
1612
import java.util.Observable
1713
import java.util.concurrent.ConcurrentHashMap
14+
import java.util.function.Consumer
1815
import java.io.File
1916
import java.io.StringWriter
2017
import org.slf4j.Logger
@@ -24,32 +21,41 @@ import org.apache.log4j.Level
2421
import org.apache.log4j.Logger as Log4jLogger
2522
import org.apache.log4j.PropertyConfigurator
2623
import org.apache.jena.rdf.model.Model
24+
import org.apache.jena.sparql.algebra.Table
25+
import org.apache.jena.graph.Graph
2726
import kotlin.text.toLong
2827

29-
class ResultPusher(private val name: LiteralExpr, private val resultTable: MutableMap<LiteralExpr, RDFTable?>) : Observer {
30-
public override fun update(o: Observable, arg: Any) {
31-
val results: RDFTable = arg as RDFTable
32-
resultTable[name] = results
33-
}
34-
}
28+
import org.streamreasoning.rsp4j.csparql2.engine.CSPARQLEngine
29+
import org.streamreasoning.rsp4j.csparql2.engine.JenaContinuousQueryExecution
30+
import org.streamreasoning.rsp4j.csparql2.stream.GraphStreamSchema
31+
import org.streamreasoning.rsp4j.csparql2.sysout.ResponseFormatterFactory
32+
import org.streamreasoning.rsp4j.csparql2.syntax.QueryFactory
33+
import org.streamreasoning.rsp4j.csparql2.sysout.GenericResponseSysOutFormatter
34+
import org.streamreasoning.rsp4j.api.engine.config.EngineConfiguration;
35+
import org.streamreasoning.rsp4j.io.DataStreamImpl
36+
import org.streamreasoning.rsp4j.api.stream.data.DataStream
37+
import org.streamreasoning.rsp4j.api.sds.SDSConfiguration
38+
import org.apache.jena.rdf.model.*;
39+
import org.apache.jena.atlas.lib.tuple.Tuple
3540

3641
// Class managing streams
3742
class StreamManager(private val settings: Settings, val staticTable: StaticTable, private val interpreter: Interpreter?) {
3843

39-
private val engine: CsparqlEngineImpl = CsparqlEngineImpl()
44+
private var engine: CSPARQLEngine? = null
4045
private var engineInitialized = false
46+
private var sdsConfig: SDSConfiguration? = null
47+
private var ec: EngineConfiguration? = null
4148
val LOG : Logger? = LoggerFactory.getLogger(StreamManager::class.java)
4249

43-
private var streams: MutableMap<String, MutableMap<LiteralExpr, RdfStream>> = mutableMapOf()
50+
private var streams: MutableMap<String, MutableMap<LiteralExpr, StreamObject>> = mutableMapOf()
4451
private var monitors: MutableMap<LiteralExpr, MonitorObject> = mutableMapOf()
45-
46-
private var queryResults: MutableMap<LiteralExpr, RDFTable?> = ConcurrentHashMap()
52+
53+
private var queryResults: MutableMap<LiteralExpr, Table?> = ConcurrentHashMap()
4754

4855
var clockVar : String? = null
4956
var clockTimestampSec : String? = null
5057
var lastTimestamp: Long = 0
51-
var firstTsPassed: Boolean = false
52-
58+
5359
var nStaticGraphsPushed = 0
5460

5561
init {
@@ -65,22 +71,41 @@ class StreamManager(private val settings: Settings, val staticTable: StaticTable
6571
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "ERROR")
6672
}
6773
}
74+
75+
private fun initEngineIfNeeded() {
76+
if (!engineInitialized) {
77+
val ts = getTimestamp()
78+
79+
val configPath = "src/main/resources/csparql2.properties"
80+
val defaultPath = "src/main/resources/default-csparql2.properties"
81+
82+
if (File(configPath).exists()) {
83+
ec = EngineConfiguration(configPath)
84+
sdsConfig = SDSConfiguration(configPath)
85+
println("Loaded csparql2.properties successfully at ts=$ts.")
86+
} else {
87+
println("Failed to load csparql2.properties, falling back to default-csparql2.properties at ts=$ts.")
88+
ec = EngineConfiguration(defaultPath)
89+
sdsConfig = SDSConfiguration(defaultPath)
90+
}
6891

69-
private fun initEngine() {
70-
71-
engine.initialize(true) // timestamp enabled
72-
engineInitialized = true
92+
engine = CSPARQLEngine(0, ec)
93+
engineInitialized = true
94+
}
7395
}
7496

75-
public fun getQueryResults(name: LiteralExpr): RDFTable? {
97+
public fun getQueryResults(name: LiteralExpr): Table? {
7698
return queryResults[name]
7799
}
78100

79101
public fun registerStream(className: String, obj: LiteralExpr) {
80-
if (!engineInitialized) initEngine()
102+
initEngineIfNeeded()
81103
val streamIri = "${settings.runPrefix}${obj.toString()}"
82-
val stream = RdfStream(streamIri)
83-
engine.registerStream(stream)
104+
105+
val stream = StreamObject(streamIri)
106+
val reg = engine!!.register(stream)
107+
stream.setWritable(reg)
108+
84109
if (!streams.containsKey(className)) streams[className] = mutableMapOf()
85110
streams[className]!![obj] = stream
86111
}
@@ -89,13 +114,10 @@ class StreamManager(private val settings: Settings, val staticTable: StaticTable
89114
var ms: Long
90115
if (clockTimestampSec != null) {
91116
ms = secToMs(clockTimestampSec!!.toLong())
92-
if (!firstTsPassed) ms -= 1 // workaround: first timestamp is decreased by 1 ms
93117
} else {
94118
ms = System.currentTimeMillis()
95119
}
96120

97-
if (ms > lastTimestamp && !firstTsPassed) firstTsPassed = true
98-
99121
lastTimestamp = ms
100122
return ms
101123
}
@@ -115,72 +137,72 @@ class StreamManager(private val settings: Settings, val staticTable: StaticTable
115137
val res = interpreter.eval(expr, stackEntry)
116138

117139
val predIri = "${settings.progPrefix}${className}_${expr.toString().removePrefix("this.").replace('.', '_')}"
118-
val objIri = literalToIri(res)
119140

120-
val quad = RdfQuadruple(subjIri, predIri, objIri, timestamp)
121-
// println(quad.toString())
122-
stream.put(quad)
123-
}
124-
}
141+
val m = ModelFactory.createDefaultModel()
142+
val stmt = m.createStatement(m.createResource(subjIri), m.createProperty(predIri), literalToIri(m, res, settings))
143+
m.add(stmt)
125144

126-
private fun literalToIri(lit: LiteralExpr): String {
127-
if (lit.tag == INTTYPE) return "\"${lit.literal}\"^^http://www.w3.org/2001/XMLSchema#integer"
128-
if (lit.tag == BOOLEANTYPE) return "\"${lit.literal}\"^^http://www.w3.org/2001/XMLSchema#boolean"
129-
if (lit.tag == DOUBLETYPE) return "\"${lit.literal}\"^^http://www.w3.org/2001/XMLSchema#double"
130-
if (lit.tag == STRINGTYPE) return "\"${lit.literal}\"^^http://www.w3.org/2001/XMLSchema#string"
131-
return "${settings.runPrefix}${lit.literal}"
145+
stream.putGraph(m.getGraph(), timestamp)
146+
}
132147
}
133148

134-
public fun addMonitor(name: LiteralExpr, monitor: MonitorObject) {
135-
monitors[name] = monitor
149+
private fun literalToIri(m: Model, lit: LiteralExpr, settings: Settings): RDFNode =
150+
when (lit.tag) {
151+
INTTYPE -> m.createTypedLiteral(lit.literal.toInt())
152+
DOUBLETYPE -> m.createTypedLiteral(lit.literal.toDouble())
153+
BOOLEANTYPE-> m.createTypedLiteral(lit.literal.toBoolean())
154+
STRINGTYPE -> m.createLiteral(lit.literal)
155+
else -> m.createResource("${settings.runPrefix}${lit.literal}")
136156
}
137157

138158
public fun getMonitor(name: LiteralExpr): MonitorObject? {
139159
return monitors[name]
140160
}
141161

142-
public fun registerQuery(name: LiteralExpr, queryExpr : Expression, params: List<Expression>, stackMemory: Memory, heap: GlobalMemory, obj: LiteralExpr, SPARQL : Boolean = true): CsparqlQueryResultProxy {
143-
if (!engineInitialized) initEngine()
162+
public fun registerQuery(name: LiteralExpr, queryExpr : Expression, params: List<Expression>, stackMemory: Memory, heap: GlobalMemory, obj: LiteralExpr, SPARQL : Boolean = true, declaredType: Type): JenaContinuousQueryExecution {
163+
initEngineIfNeeded()
144164
val queryStr = prepareQuery(name, queryExpr, params, stackMemory, heap, obj, SPARQL)
145-
if (settings.verbose) println("Registering query:\n$queryStr")
146-
var resultProxy = engine.registerQuery(queryStr, false) // reasoning disabled
147-
resultProxy.addObserver(ResultPusher(name, queryResults)) // each key is only used by one observer
148-
return resultProxy
165+
166+
val cqe = engine!!.register(queryStr, sdsConfig) as JenaContinuousQueryExecution
167+
monitors[name] = MonitorObject(name, declaredType)
168+
169+
val outputStream = cqe.outstream()
170+
outputStream?.addConsumer { arg, ts ->
171+
val results: Table = arg as Table
172+
queryResults[name] = results
173+
}
174+
175+
return cqe
149176
}
150177

151-
private fun prepareQuery(name: LiteralExpr, queryExpr : Expression, params : List<Expression>, stackMemory: Memory, heap: GlobalMemory, obj: LiteralExpr, SPARQL : Boolean = true) : String{
152-
val queryHeader = "REGISTER QUERY Query${name.literal} AS "
178+
private fun prepareQuery(name: LiteralExpr, queryExpr : Expression, params : List<Expression>,
179+
stackMemory: Memory, heap: GlobalMemory, obj: LiteralExpr, SPARQL : Boolean = true) : String{
180+
181+
var prefixes = ""
182+
for ((key, value) in settings.prefixMap()) prefixes += "PREFIX $key: <$value>\n"
183+
184+
val queryHeader = "REGISTER RSTREAM <${settings.runPrefix}${name.literal}> AS "
153185

154186
val queryBody = interpreter!!.prepareQuery(queryExpr, params, stackMemory, heap, obj, SPARQL)
155187
.removePrefix("\"").removeSuffix("\"")
156188

157-
var queryWithPrefixes = queryHeader
158-
// for ((key, value) in settings.prefixMap()) queryWithPrefixes += "PREFIX $key: <$value>\n"
159-
queryWithPrefixes += queryBody
189+
var queryWithPrefixes = prefixes + queryHeader + queryBody
160190
queryWithPrefixes = queryWithPrefixes.replace("\\\"", "\"")
161191

162-
// Replace occurrences of value:x with <keyx> for each prefix in prefixMap
163-
for ((key, value) in settings.prefixMap()) {
164-
// Regex to match key: followed by a valid identifier (e.g., obj4)
165-
val regex = Regex("""${Regex.escape(key)}:([A-Za-z0-9_]+)""")
166-
queryWithPrefixes = queryWithPrefixes.replace(regex) { matchResult ->
167-
"<$value${matchResult.groupValues[1]}>"
168-
}
169-
}
170-
171192
return queryWithPrefixes
172193
}
173194

174-
public fun putStaticNamedGraph(iri: String, model: Model) {
175-
if (!engineInitialized) initEngine()
195+
// todo implement
196+
// public fun putStaticNamedGraph(iri: String, model: Model) {
197+
// if (!engineInitialized) initEngine()
176198

177-
// serialize the model (RDF/XML matches the engine's first attempt)
178-
val sw = StringWriter()
179-
model.write(sw, "RDF/XML")
199+
// // serialize the model (RDF/XML matches the engine's first attempt)
200+
// val sw = StringWriter()
201+
// model.write(sw, "RDF/XML")
180202

181-
// hand it to the C-SPARQL engine
182-
engine.putStaticNamedModel(iri, sw.toString())
183-
}
203+
// // hand it to the C-SPARQL engine
204+
// engine.putStaticNamedModel(iri, sw.toString())
205+
// }
184206

185207
public fun getStaticNamedIri(): String {
186208
val s = "${settings.runPrefix}loadStatic${nStaticGraphsPushed}"
@@ -189,4 +211,18 @@ class StreamManager(private val settings: Settings, val staticTable: StaticTable
189211
}
190212

191213
}
214+
215+
class StreamObject(var iri: String) : DataStreamImpl<Graph>(iri) {
216+
217+
private var s: DataStream<Graph>? = null
218+
219+
fun setWritable(s: DataStream<Graph>) {
220+
this.s = s
221+
}
222+
223+
fun putGraph(m: Graph, t: Long) {
224+
if (s == null) throw Exception("Stream $iri is not writable")
225+
s!!.put(m, t)
226+
}
227+
}
192228

0 commit comments

Comments
 (0)