use of org.matheclipse.core.expression.Context in project symja_android_library by axkr.
the class EvalEngine method beginPackage.
public Context beginPackage(String contextName) {
fContextPathStack.push(fContextPath);
Context packageContext = fContextPath.getContext(contextName);
setContextPath(new ContextPath(packageContext));
ContextPath.PACKAGES.add(contextName);
return packageContext;
}
use of org.matheclipse.core.expression.Context in project symja_android_library by axkr.
the class Validate method checkIdentifierHoldPattern.
public static IExpr checkIdentifierHoldPattern(final IExpr expr, IAST ast, EvalEngine engine) {
ISymbol sym = null;
if (expr.isString()) {
String identifier = expr.toString();
if (!Scanner.isIdentifier(identifier)) {
// Argument `1` at position `2` is expected to be a symbol.
return IOFunctions.printMessage(ast.topHead(), "sym", F.list(expr, F.C1), engine);
}
int indx = identifier.lastIndexOf('`');
if (indx > 0) {
ContextPath contextPath = engine.getContextPath();
Context context = contextPath.getContext(identifier.substring(0, indx + 1));
sym = contextPath.symbol(identifier.substring(indx + 1), context, engine.isRelaxedSyntax());
} else {
sym = F.symbol(identifier, engine);
}
} else if (expr.isSymbol()) {
sym = (ISymbol) expr;
} else if (expr.isAST(S.HoldPattern, 2) && expr.first().isSymbol()) {
sym = (ISymbol) expr.first();
} else {
// Symbol, string or HoldPattern(symbol) expected at position `2` in `1`.
return IOFunctions.printMessage(ast.topHead(), "ssle", F.list(expr, F.C1), engine);
}
return sym;
}
use of org.matheclipse.core.expression.Context in project symja_android_library by axkr.
the class IOFunctions method getNamesByPattern.
public static IAST getNamesByPattern(java.util.regex.Pattern pattern, EvalEngine engine) {
ContextPath contextPath = engine.getContextPath();
IASTAppendable list = F.ListAlloc(31);
Map<String, Context> contextMap = contextPath.getContextMap();
for (Map.Entry<String, Context> mapEntry : contextMap.entrySet()) {
Context context = mapEntry.getValue();
for (Map.Entry<String, ISymbol> entry : context.entrySet()) {
String fullName = context.completeContextName() + entry.getKey();
java.util.regex.Matcher matcher = pattern.matcher(fullName);
if (matcher.matches()) {
if (ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS && context.equals(Context.SYSTEM)) {
String str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(entry.getValue().getSymbolName());
if (str != null) {
list.append(F.$str(str));
continue;
}
}
ISymbol value = entry.getValue();
if (context.isGlobal() || context.isSystem()) {
list.append(F.$str(value.toString()));
} else {
list.append(F.$str(fullName));
}
}
}
}
for (Context context : contextPath) {
String completeContextName = context.completeContextName();
if (!contextMap.containsKey(completeContextName)) {
for (Map.Entry<String, ISymbol> entry : context.entrySet()) {
String fullName = completeContextName + entry.getKey();
java.util.regex.Matcher matcher = pattern.matcher(fullName);
if (matcher.matches()) {
if (ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS && context.equals(Context.SYSTEM)) {
String str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(entry.getValue().getSymbolName());
if (str != null) {
list.append(F.$str(str));
continue;
}
}
ISymbol value = entry.getValue();
if (context.isGlobal() || context.isSystem()) {
list.append(F.$str(value.toString()));
} else {
list.append(F.$str(fullName));
}
}
}
}
}
return list;
}
use of org.matheclipse.core.expression.Context in project symja_android_library by axkr.
the class WolframFormFactory method convertSymbol.
@Override
public void convertSymbol(final Appendable buf, final ISymbol symbol) throws IOException {
Context context = symbol.getContext();
if (context == Context.DUMMY) {
append(buf, symbol.getSymbolName());
return;
}
String str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(symbol.getSymbolName());
if (str != null) {
// assuming Wolfram language built-in function
append(buf, str);
return;
}
if (EvalEngine.get().getContextPath().contains(context)) {
append(buf, symbol.getSymbolName());
} else {
append(buf, context.completeContextName() + symbol.getSymbolName());
}
}
use of org.matheclipse.core.expression.Context in project symja_android_library by axkr.
the class SerializableTest method testEvalEngine.
public void testEvalEngine() {
try {
EvalEngine engine = EvalEngine.get();
engine.evaluate("x=10");
Context context = engine.getContextPath().getGlobalContext();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(context);
byte[] bArray = baos.toByteArray();
baos.close();
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(bArray);
ObjectInputStream ois = new ObjectInputStream(bais);
Context copy = (Context) ois.readObject();
bais.close();
ois.close();
engine.getContextPath().setGlobalContext(copy);
IExpr result = engine.evaluate("x");
assertEquals("10", result.toString());
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
assertEquals("", cnfe.toString());
} catch (IOException ioe) {
ioe.printStackTrace();
assertEquals("", ioe.toString());
}
}
Aggregations