use of org.matheclipse.core.patternmatching.IPatternMatcher in project symja_android_library by axkr.
the class VisitorReplacePart method initPatternMatcher.
private void initPatternMatcher(IAST rule, IExpr.COMPARE_TERNARY heads) {
IExpr fromPositions = rule.arg1();
try {
// try extracting an int[] array of expressions
if (fromPositions.isList()) {
IAST list = (IAST) fromPositions;
if (list.isListOfLists()) {
for (int j = 1; j < list.size(); j++) {
IAST subList = list.getAST(j);
int[] positions = new int[subList.argSize()];
for (int k = 1; k < subList.size(); k++) {
positions[k - 1] = subList.get(k).toIntDefault();
if (positions[k - 1] == Integer.MIN_VALUE) {
throw ReturnException.RETURN_FALSE;
}
if (positions[k - 1] == 0) {
offset = 0;
}
}
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
}
} else {
if (list.argSize() > 0) {
int[] positions = new int[list.argSize()];
for (int j = 1; j < list.size(); j++) {
positions[j - 1] = list.get(j).toIntDefault();
if (positions[j - 1] == Integer.MIN_VALUE) {
throw ReturnException.RETURN_FALSE;
}
if (positions[j - 1] == 0) {
offset = 0;
}
}
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
}
}
} else {
int[] positions = new int[] { rule.arg1().toIntDefault() };
if (positions[0] == Integer.MIN_VALUE) {
throw ReturnException.RETURN_FALSE;
}
if (positions[0] == 0) {
offset = 0;
}
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
}
} catch (ReturnException rex) {
if (fromPositions.isList()) {
IAST list = ((IAST) fromPositions).apply(S.Sequence, 1);
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(list, rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
} else {
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(fromPositions, rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
}
}
}
use of org.matheclipse.core.patternmatching.IPatternMatcher in project symja_android_library by axkr.
the class VisitorReplacePart method visitPatternIndexList.
private IExpr visitPatternIndexList(IAST ast, IASTAppendable positions) {
IASTAppendable result = F.NIL;
for (int i = offset; i < ast.size(); i++) {
final IInteger position = F.ZZ(i);
for (int j = 0; j < patternMatcherList.size(); j++) {
IPatternMatcher matcher = patternMatcherList.get(j);
IASTAppendable positionsToMatch = positions.copyAppendable();
positionsToMatch.append(position);
IASTAppendable temp = patternIndexRecursive(matcher, ast, positionsToMatch, i, result);
if (temp.isPresent()) {
result = temp;
break;
}
IInteger negativePart = F.ZZUniqueReference(i - ast.size());
positionsToMatch.set(positionsToMatch.size() - 1, negativePart);
temp = patternIndexRecursive(matcher, ast, positionsToMatch, i, result);
if (temp.isPresent()) {
IExpr ex = temp.replaceAll(x -> {
if (x == negativePart) {
// compare unique reference from F.ZZUniqueReference(); don't use equals() method!
return position;
}
return F.NIL;
});
if (ex.isPresent() && (ex instanceof IASTAppendable)) {
result = (IASTAppendable) ex;
} else {
result = temp;
}
break;
}
if (ast.isAssociation() && i > 0) {
// for associations the key instead of the position can also match
positionsToMatch.set(positionsToMatch.size() - 1, ast.getRule(i).first());
temp = patternIndexRecursive(matcher, ast, positionsToMatch, i, result);
if (temp.isPresent()) {
result = temp;
break;
}
}
}
}
return result;
}
Aggregations