Code: Select all
String equation = "((((15>=1 && 50>=10) || (60>=1 && 40>=21)) && (20=1 && 20>=17))";
Ich möchte wissen, welche Bedingungen für die Rücksendung von true/false;
In diesem Fall das erwartete Ergebnis lautet:- (( 15> = 1 && 50> = 10) && (20
Code: Select all
((((15>=1 && 50>=10) || (60>=1 && 40>=21)) && (20=1 && 20>=17))
in this case:- [WORKING AS PER EXPECTATION]
current output -- (15>=1 && 50>=10) && (20=1 && 50>=10) && (20=1 && 50>=10) || (60>=1 && 40>=21)) && (200=1 && 20>=17))
in this case:- [FAILED]
current output -- (15>=1 && 50>=10), (60>=1 && 20>=17) -- [(15>=1 && 50>=10) // 200=1 && 20>=17)
Motorversion: 11.0.25 < /p>
Warum erzeugt mein Code nicht, dass mein Code die beabsichtigten Erzeugung erzeugt Ergebnis? < /p>
Das habe ich bisher ausprobiert:-< /p>
Code: Select all
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.*;
import java.util.regex.*;
public class ExpressionSplitter
{
public static void Runner()
{
String expression = "((((15>=1 && 50>=10) || (60>=1 && 40>=21)) && (20=1 && 20>=17))";
try
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
String loggedExpression = wrapExpressionsWithLogging(expression);
Bindings bindings = engine.createBindings();
Object result = engine.eval(loggedExpression, bindings);
List trueConditions = (List) bindings.get("trueConditions");
} catch (ScriptException e) {
e.printStackTrace();
}
}
private static String wrapExpressionsWithLogging(String expression) {
Pattern pattern = Pattern.compile("\\(([^()]+)\\)");
Matcher matcher = pattern.matcher(expression);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String subExpression = matcher.group(1);
matcher.appendReplacement(sb, "logResult((" + subExpression + "), '" + subExpression.replace("'", "\\'") + "')");
}
matcher.appendTail(sb);
return "var trueConditions = []; function logResult(result, expression) { if(result){trueConditions.push(expression)}; return result; }" + sb.toString() + "; trueConditions;";
}
}