use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class RulesData method removeRule.
public void removeRule(final ISymbol.RuleType setSymbol, final boolean equalRule, final IExpr leftHandSide) {
if (equalRule) {
if (fEqualDownRules != null) {
fEqualDownRules.remove(leftHandSide);
return;
}
}
final PatternMatcherAndEvaluator pmEvaluator = new PatternMatcherAndEvaluator(setSymbol, leftHandSide, null);
if (pmEvaluator.isRuleWithoutPatterns()) {
if (fEqualDownRules != null) {
fEqualDownRules.remove(leftHandSide);
return;
}
}
Set<ISymbol> headerSymbols = new HashSet<ISymbol>();
if (!isComplicatedPatternRule(leftHandSide, headerSymbols)) {
if (fSimplePatternDownRules != null) {
final int hash = ((IAST) leftHandSide).patternHashCode();
if (fSimplePatternDownRules.containsEntry(hash, pmEvaluator)) {
fSimplePatternDownRules.remove(hash, pmEvaluator);
}
}
return;
} else {
if (headerSymbols.size() > 0) {
if (fSimpleOrderlesPatternDownRules != null) {
for (ISymbol head : headerSymbols) {
final int hash = head.hashCode();
if (fSimpleOrderlesPatternDownRules.containsEntry(hash, pmEvaluator)) {
fSimpleOrderlesPatternDownRules.remove(hash, pmEvaluator);
}
}
}
return;
}
if (fPatternDownRules != null) {
fPatternDownRules.remove(pmEvaluator);
return;
}
}
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class PatternMatching method setDelayedDownRule.
public static Object[] setDelayedDownRule(IExpr leftHandSide, IExpr rightHandSide, boolean packageMode) {
final Object[] result = new Object[] { null, rightHandSide };
if (leftHandSide.isAST()) {
final ISymbol lhsSymbol = ((IAST) leftHandSide).topHead();
result[0] = lhsSymbol.putDownRule(ISymbol.RuleType.SET_DELAYED, false, leftHandSide, rightHandSide, packageMode);
return result;
}
if (leftHandSide.isSymbol()) {
final ISymbol lhsSymbol = (ISymbol) leftHandSide;
if (lhsSymbol.hasLocalVariableStack()) {
lhsSymbol.set(rightHandSide);
return result;
}
result[0] = lhsSymbol.putDownRule(ISymbol.RuleType.SET_DELAYED, true, leftHandSide, rightHandSide, packageMode);
return result;
}
throw new RuleCreationError(leftHandSide);
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Programming method checkModuleCondition.
/**
* Check the (possible nested) module condition in pattern matcher without evaluating a result.
*
* @param arg1
* @param arg2
* @param engine
* @return
*/
public static boolean checkModuleCondition(IExpr arg1, IExpr arg2, final EvalEngine engine) {
if (arg1.isList()) {
IAST intializerList = (IAST) arg1;
final int moduleCounter = engine.incModuleCounter();
final String varAppend = "$" + moduleCounter;
final java.util.Map<ISymbol, ISymbol> moduleVariables = new IdentityHashMap<ISymbol, ISymbol>();
try {
rememberVariables(intializerList, varAppend, moduleVariables, engine);
IExpr result = F.subst(arg2, Functors.rules(moduleVariables));
if (result.isCondition()) {
return checkCondition(result.getAt(1), result.getAt(2), engine);
} else if (result.isModule()) {
return checkModuleCondition(result.getAt(1), result.getAt(2), engine);
}
} finally {
removeUserVariables(moduleVariables);
}
}
return true;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Get method loadPackage.
/**
* Load a package from the given reader
*
* @param engine
* @param is
* @return the last evaluated expression result
*/
public static IExpr loadPackage(final EvalEngine engine, final Reader is) {
final BufferedReader r = new BufferedReader(is);
Context packageContext = null;
try {
final List<ASTNode> node = parseReader(r, engine);
IExpr temp;
int i = 0;
AST2Expr ast2Expr = AST2Expr.CONST;
if (engine.isRelaxedSyntax()) {
ast2Expr = AST2Expr.CONST_LC;
}
IExpr result = F.Null;
while (i < node.size()) {
temp = ast2Expr.convert(node.get(i++), engine);
if (temp.isAST()) {
IAST ast = (IAST) temp;
IExpr head = temp.head();
if (head.equals(F.BeginPackage) && ast.size() >= 2) {
String contextName = Validate.checkContextName(ast, 1);
packageContext = new Context(contextName);
ISymbol endSymbol = F.EndPackage;
for (int j = 2; j < ast.size(); j++) {
FileReader reader = new FileReader(ast.get(j).toString());
Get.loadPackage(engine, reader);
reader.close();
}
i = addContextToPath(new ContextPath(packageContext), node, i, engine, endSymbol);
continue;
} else if (head.equals(F.Begin) && ast.size() >= 2) {
String contextName = Validate.checkContextName(ast, 1);
ISymbol endSymbol = F.End;
i = addContextToPath(new ContextPath(contextName), node, i, engine, endSymbol);
continue;
}
}
result = engine.evaluate(temp);
}
return result;
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (packageContext != null) {
engine.getContextPath().add(packageContext);
}
try {
r.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return F.Null;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Package method evalPackage.
public static void evalPackage(IAST publicSymbols, IAST list, EvalEngine engine) {
HashMap<String, ISymbol> convertedSymbolMap = new HashMap<String, ISymbol>();
HashSet<ISymbol> publicSymbolSet = new HashSet<ISymbol>();
ISymbol toSymbol;
for (int i = 1; i < publicSymbols.size(); i++) {
IExpr expr = publicSymbols.get(i);
if (expr.isSymbol()) {
publicSymbolSet.add((ISymbol) expr);
toSymbol = F.predefinedSymbol(((ISymbol) expr).toString());
convertedSymbolMap.put(expr.toString(), toSymbol);
} else if (expr instanceof IStringX) {
String symbolStr = ((IStringX) expr).toString();
toSymbol = F.predefinedSymbol(symbolStr);
publicSymbolSet.add(toSymbol);
if (Config.PARSER_USE_LOWERCASE_SYMBOLS) {
symbolStr = symbolStr.toLowerCase(Locale.ENGLISH);
}
convertedSymbolMap.put(symbolStr, toSymbol);
}
}
// determine "private package rule headers" in convertedSymbolMap
for (int i = 1; i < list.size(); i++) {
if (list.get(i).isAST()) {
determineRuleHead((IAST) list.get(i), publicSymbolSet, convertedSymbolMap);
}
}
// convert the rules into a new list:
IAST resultList = F.List();
for (int i = 1; i < list.size(); i++) {
resultList.append(convertSymbolsInExpr(list.get(i), convertedSymbolMap));
}
try {
engine.setPackageMode(true);
// evaluate the new converted rules
for (int i = 1; i < resultList.size(); i++) {
engine.evaluate(resultList.get(i));
}
} finally {
engine.setPackageMode(false);
}
}
Aggregations