
org.xowl.infra.engine.ClojureEvaluator Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2015 Laurent Wouters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program.
* If not, see .
*
* Contributors:
* Laurent Wouters - [email protected]
******************************************************************************/
package org.xowl.infra.engine;
import clojure.lang.Compiler;
import clojure.lang.*;
import org.xowl.infra.lang.actions.DynamicExpression;
import org.xowl.infra.lang.actions.FunctionExpression;
import org.xowl.infra.lang.actions.OpaqueExpression;
import org.xowl.infra.lang.owl2.*;
import org.xowl.infra.lang.runtime.*;
import org.xowl.infra.lang.runtime.Literal;
import org.xowl.infra.store.Evaluator;
import org.xowl.infra.store.ProxyObject;
import org.xowl.infra.store.Vocabulary;
import org.xowl.infra.utils.Files;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.*;
/**
* Represents the Clojure evaluator for xOWL ontologies
*
* @author Laurent Wouters
*/
public class ClojureEvaluator implements Evaluator {
/**
* The root namespace for the Clojure symbols
*/
private static final String CLJ_NAMESPACE_ROOT_NAME = "org.xowl.engine.clojure";
/**
* The root namespace for the Clojure symbols
*/
private static final Namespace CLJ_NAMESPACE_ROOT = Namespace.findOrCreate(Symbol.intern(CLJ_NAMESPACE_ROOT_NAME));
/**
* The Clojure function definitions that are not yet compiled
*/
private static final List OUTSTANDING_DEFINITIONS = new ArrayList<>();
/**
* Registers a function definition
*
* @param name The function's name
* @param definition The function's content definition as a string
* @return The managing object for the function
*/
protected static ClojureFunction register(String name, String definition) {
ClojureFunction result = new ClojureFunction(name, definition);
OUTSTANDING_DEFINITIONS.add(result);
return result;
}
/**
* Compiles the outstanding function definitions
*/
protected static void compileOutstandings() {
if (OUTSTANDING_DEFINITIONS.isEmpty())
return;
//Var ns = RT.CURRENT_NS; // forces the initialization of the runtime before any call to the compiler
StringBuilder builder = new StringBuilder();
builder.append("(ns ");
builder.append(CLJ_NAMESPACE_ROOT);
builder.append(")");
builder.append(Files.LINE_SEPARATOR);
builder.append("(declare & ");
for (ClojureFunction function : OUTSTANDING_DEFINITIONS) {
builder.append(function.getName());
builder.append(" ");
}
builder.append(")");
builder.append(Files.LINE_SEPARATOR);
builder.append("[ ");
for (ClojureFunction function : OUTSTANDING_DEFINITIONS) {
builder.append(function.getContent());
builder.append(Files.LINE_SEPARATOR);
}
builder.append(" ]");
Reader reader = new StringReader(builder.toString());
Iterator iterator = ((Iterable) Compiler.load(reader)).iterator();
try {
reader.close();
} catch (IOException ex) {
// do nothing
}
for (ClojureFunction function : OUTSTANDING_DEFINITIONS) {
IFn definition = (IFn) iterator.next();
Var.intern(CLJ_NAMESPACE_ROOT, Symbol.intern(function.getName()), definition);
function.setClojure(definition);
}
OUTSTANDING_DEFINITIONS.clear();
}
/**
* The stack of lexical bindings
*/
private final Stack
© 2015 - 2025 Weber Informatics LLC | Privacy Policy