Search in sources :

Example 1 with Context

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;
}
Also used : Context(org.matheclipse.core.expression.Context) ContextPath(org.matheclipse.core.expression.ContextPath)

Example 2 with Context

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;
}
Also used : Context(org.matheclipse.core.expression.Context) ContextPath(org.matheclipse.core.expression.ContextPath) ISymbol(org.matheclipse.core.interfaces.ISymbol)

Example 3 with Context

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;
}
Also used : Context(org.matheclipse.core.expression.Context) ContextPath(org.matheclipse.core.expression.ContextPath) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) ISymbol(org.matheclipse.core.interfaces.ISymbol) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with Context

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());
    }
}
Also used : Context(org.matheclipse.core.expression.Context)

Example 5 with Context

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());
    }
}
Also used : Context(org.matheclipse.core.expression.Context) ByteArrayInputStream(java.io.ByteArrayInputStream) EvalEngine(org.matheclipse.core.eval.EvalEngine) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IExpr(org.matheclipse.core.interfaces.IExpr) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

Context (org.matheclipse.core.expression.Context)14 ContextPath (org.matheclipse.core.expression.ContextPath)7 IExpr (org.matheclipse.core.interfaces.IExpr)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)3 IOException (java.io.IOException)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2 IAST (org.matheclipse.core.interfaces.IAST)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)1 ASTNode (org.matheclipse.parser.client.ast.ASTNode)1 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)1