Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:herminiogg/ShExML into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
herminiogg committed Jun 27, 2024
2 parents 91ea12d + c3e05bb commit 451097a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import scala.reflect.runtime.universe._

class FunctionHubExecuter(val pathToFile: String) {

private val functionsCode = new SourceHelper().getURLContent(pathToFile).fileContent
private val cm = universe.runtimeMirror(getClass.getClassLoader)
private val toolBox = cm.mkToolBox()
private val tree = toolBox.parse(functionsCode)
private val symbol = toolBox.define(tree.asInstanceOf[toolBox.u.ImplDef])
private val theClass = toolBox.eval(toolBox.parse(functionsCode + s"\nscala.reflect.classTag[${symbol.name}].runtimeClass")).asInstanceOf[Class[_]]

private val logger = Logger[FunctionHubExecuter]

def callFunction(name: String, args: String*): List[String] = {
logger.debug(s"Executing function $name in source code $pathToFile")
val functionsCode = new SourceHelper().getURLContent(pathToFile).fileContent
val cm = universe.runtimeMirror(getClass.getClassLoader)
val toolBox = cm.mkToolBox()
val tree = toolBox.parse(functionsCode)
val symbol = toolBox.define(tree.asInstanceOf[toolBox.u.ImplDef])
val theClass = toolBox.eval(toolBox.parse(functionsCode + s"\nscala.reflect.classTag[${symbol.name}].runtimeClass")).asInstanceOf[Class[_]]
val instance = theClass.getConstructors()(0).newInstance().asInstanceOf[AnyRef]
val method = theClass.getMethods.toList.filter(_.getName.matches(".*" + name + ".*"))
.sortWith((a, b) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class RDFGeneratorVisitor(dataset: Dataset, varTable: mutable.HashMap[Variable,
protected val jsonObjectMapperCache = new JsonObjectMapperCache()
protected val xpathQueryResultsCache = new XpathQueryResultsCache(pushedOrPoppedFieldsPresent)
protected val xmlDocumentCache = new XMLDocumentCache()
protected val functionHubExecuterCache = new FunctionHubExecuterCache()
protected val defaultModel = dataset.getDefaultModel

private val xmlProcessor = new Processor(false)
Expand Down Expand Up @@ -379,7 +380,13 @@ class RDFGeneratorVisitor(dataset: Dataset, varTable: mutable.HashMap[Variable,

case f: FunctionCalling => {
val functionsURL = varTable(f.functionHub).asInstanceOf[URL]
val functionHub = new FunctionHubExecuter(functionsURL.url)
val functionHub = functionHubExecuterCache.search(functionsURL.url) match {
case Some(executer) => executer
case None =>
val executer = new FunctionHubExecuter(functionsURL.url)
functionHubExecuterCache.save(functionsURL.url, executer)
executer
}
val argumentsResults = f.arguments.arguments.map(doVisit(_, optionalArgument).asInstanceOf[List[Result]])
val arguments = for (i <- argumentsResults.head.indices) yield {
for (j <- argumentsResults.indices) yield {
Expand Down Expand Up @@ -1242,6 +1249,18 @@ class XpathQueryResultsCache(pushedValues: Boolean) {
}
}

class FunctionHubExecuterCache() {
private val table = mutable.HashMap[String, FunctionHubExecuter]()

def search(url: String): Option[FunctionHubExecuter] = {
table.get(url)
}

def save(url: String, functionHubExecuter: FunctionHubExecuter): Unit = {
table += ((url, functionHubExecuter))
}
}

sealed trait Resultable {
def results: List[String]
}
Expand Down

0 comments on commit 451097a

Please sign in to comment.