Android Open Source - AnkiStats Top Level Parser






From Project

Back to project page AnkiStats.

License

The source code is released under:

GNU General Public License

If you think the Android project AnkiStats listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.wildplot.android.parsing;
/*from   w w w . j  av a  2 s. co  m*/
import com.wildplot.android.rendering.interfaces.Function2D;
import com.wildplot.android.rendering.interfaces.Function3D;

import java.util.HashMap;
import java.util.Random;
import java.util.regex.Pattern;

public class TopLevelParser implements Function2D, Function3D, Cloneable{
    Random random = new Random();
    private HashMap<String, TopLevelParser> parserRegister;
    private HashMap<String, Double> varMap = new HashMap<String, Double>();
    private double x = 0.0, y = 0.0;
    private Expression expression = null;
    private boolean isValid = false;
    private String expressionString;
    private String xName = "x", yName = "y";
    private String funcName = "f" +random.nextInt();


    public TopLevelParser(String expressionString, HashMap<String, TopLevelParser> parserRegister){
        initVarMap();
        this.parserRegister = parserRegister;
        this.expressionString = expressionString;
        boolean isValidExpressionString = initExpressionString();

        this.expression =  new Expression(this.expressionString, this);
        this.isValid = (expression.getExpressionType() != Expression.ExpressionType.INVALID) && isValidExpressionString;

    }

    private void initVarMap(){
        varMap.put("e", Math.E);
        varMap.put("pi", Math.PI);
    }

    private boolean initExpressionString(){
        this.expressionString = expressionString.replace(" ", "");
        int equalPosition = expressionString.indexOf("=");
        if(equalPosition >= 1){
            String leftStatement = expressionString.substring(0, equalPosition);
            this.expressionString = expressionString.substring(equalPosition+1, expressionString.length());
            int commaPos = leftStatement.indexOf(",");
            int leftBracketPos = leftStatement.indexOf("(");
            int rightBracketPos = leftStatement.indexOf(")");

            if(leftBracketPos > 0 && rightBracketPos > leftBracketPos +1){
                String funcName = leftStatement.substring(0, leftBracketPos);
                Pattern p = Pattern.compile("[^a-zA-Z0-9]");
                boolean hasSpecialChar = p.matcher(funcName).find();
                if(hasSpecialChar){
                    return false;
                }
                if(commaPos == -1){
                    String xVarName = leftStatement.substring(leftBracketPos+1, rightBracketPos);
                    hasSpecialChar = p.matcher(xVarName).find();
                    if(hasSpecialChar){
                       return false;
                    }
                    this.xName = xVarName;
                } else {
                    String xVarName = leftStatement.substring(leftBracketPos+1, commaPos);
                    hasSpecialChar = p.matcher(xVarName).find();
                    if(hasSpecialChar){
                        return false;
                    }
                    String yVarName = leftStatement.substring(commaPos+1, rightBracketPos);
                    hasSpecialChar = p.matcher(yVarName).find();
                    if(hasSpecialChar){
                        return false;
                    }


                    this.xName = xVarName;
                    this.yName = yVarName;
                }
                this.funcName = funcName;
            }else {
                return false;
            }



        }

        return true;
    }

    public double getVarVal(String varName){
        return varMap.get(varName);
    }
    public void setVarVal(String varName, String varVal){
        varMap.put(varName, Double.parseDouble(varVal));
    }
    public void setVarVal(String varName, double varVal){
        varMap.put(varName, varVal);
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    @Override
    public double f(double x) {
        this.x = x;
        if(isValid)
            return expression.getValue();
        else
            throw new ExpressionFormatException("illegal Expression, cannot parse and return value");
    }

    public boolean isValid() {
        return isValid;
    }

    @Override
    public double f(double x, double y) {
        this.x = x;
        this.y = y;
        if(isValid)
            return expression.getValue();
        else
            throw new ExpressionFormatException("illegal Expression, cannot parse and return value");
    }


    public TopLevelParser createCopy(){
    HashMap<String, TopLevelParser> newParserRegister = new HashMap<String, TopLevelParser>();
    for(String key : parserRegister.keySet()){
      newParserRegister.put(key, parserRegister.get(key).createCopy(newParserRegister));
    }
    
        return newParserRegister.get(this.funcName);
    }

    public TopLevelParser createCopy(HashMap<String, TopLevelParser> newParserRegister){
        return new TopLevelParser(this.expressionString, newParserRegister);
    }

    public double getFuncVal(String funcName, double xVal){
        TopLevelParser funcParser = this.parserRegister.get(funcName);
        return funcParser.f(xVal);
    }

    public double getFuncVal(String funcName, double xVal, double yVal){
        TopLevelParser funcParser = this.parserRegister.get(funcName);
        return funcParser.f(xVal, yVal);
    }

    public String getxName() {
        return xName;
    }

    public String getyName() {
        return yName;
    }

    public String getFuncName() {
        return funcName;
    }

    public static boolean stringHasValidBrackets(String string){
        int finalBracketCheck = string.replaceAll("\\(", "").length()- string.replaceAll("\\)", "").length();
        if(finalBracketCheck != 0)
            return false;

        int bracketOpeningCheck = 0;
        for(int i = 0; i<string.length(); i++){
            if(string.charAt(i) == '('){
                bracketOpeningCheck++;
            }
            if(string.charAt(i) == ')'){
                bracketOpeningCheck--;
            }
            if(bracketOpeningCheck <0){
                return false;
            }
        }
        return true;
    }
}




Java Source Code List

com.wildplot.android.ankistats.AnkiDb.java
com.wildplot.android.ankistats.AnkiStatsActivity.java
com.wildplot.android.ankistats.AnkiStatsApplication.java
com.wildplot.android.ankistats.AnswerButton.java
com.wildplot.android.ankistats.ApplicationTest.java
com.wildplot.android.ankistats.CardsTypes.java
com.wildplot.android.ankistats.CollectionData.java
com.wildplot.android.ankistats.Forecast.java
com.wildplot.android.ankistats.HourlyBreakdown.java
com.wildplot.android.ankistats.Intervals.java
com.wildplot.android.ankistats.ReviewCount.java
com.wildplot.android.ankistats.Utils.java
com.wildplot.android.ankistats.WeeklyBreakdown.java
com.wildplot.android.parsing.Atom.java
com.wildplot.android.parsing.ExpressionFormatException.java
com.wildplot.android.parsing.Expression.java
com.wildplot.android.parsing.Factor.java
com.wildplot.android.parsing.Pow.java
com.wildplot.android.parsing.Term.java
com.wildplot.android.parsing.TopLevelParser.java
com.wildplot.android.parsing.TreeElement.java
com.wildplot.android.parsing.AtomTypes.FunctionXAtom.java
com.wildplot.android.parsing.AtomTypes.FunctionXYAtom.java
com.wildplot.android.parsing.AtomTypes.MathFunctionAtom.java
com.wildplot.android.parsing.AtomTypes.NumberAtom.java
com.wildplot.android.parsing.AtomTypes.VariableAtom.java
com.wildplot.android.parsing.AtomTypes.XVariableAtom.java
com.wildplot.android.parsing.AtomTypes.YVariableAtom.java
com.wildplot.android.rendering.AdvancedPlotSheet.java
com.wildplot.android.rendering.BarGraph.java
com.wildplot.android.rendering.DrawableContainer.java
com.wildplot.android.rendering.FunctionDrawer.java
com.wildplot.android.rendering.FunctionDrawer_y.java
com.wildplot.android.rendering.Integral.java
com.wildplot.android.rendering.LegendDrawable.java
com.wildplot.android.rendering.LinesPoints.java
com.wildplot.android.rendering.Lines.java
com.wildplot.android.rendering.MultiScreenPart.java
com.wildplot.android.rendering.PieChart.java
com.wildplot.android.rendering.PlotSheet.java
com.wildplot.android.rendering.PointDrawer2D.java
com.wildplot.android.rendering.RelativeColorGradient.java
com.wildplot.android.rendering.ReliefDrawer.java
com.wildplot.android.rendering.XAxisBarGraph.java
com.wildplot.android.rendering.XAxisHistoGram.java
com.wildplot.android.rendering.XAxis.java
com.wildplot.android.rendering.XGrid.java
com.wildplot.android.rendering.YAxisBarGraph.java
com.wildplot.android.rendering.YAxisHistoGram.java
com.wildplot.android.rendering.YAxis.java
com.wildplot.android.rendering.YGrid.java
com.wildplot.android.rendering.graphics.wrapper.BasicStroke.java
com.wildplot.android.rendering.graphics.wrapper.BufferedImage.java
com.wildplot.android.rendering.graphics.wrapper.Color.java
com.wildplot.android.rendering.graphics.wrapper.FontMetrics.java
com.wildplot.android.rendering.graphics.wrapper.Graphics2D.java
com.wildplot.android.rendering.graphics.wrapper.Graphics.java
com.wildplot.android.rendering.graphics.wrapper.Rectangle.java
com.wildplot.android.rendering.graphics.wrapper.Stroke.java
com.wildplot.android.rendering.interfaces.Drawable.java
com.wildplot.android.rendering.interfaces.Function2D.java
com.wildplot.android.rendering.interfaces.Function3D.java
com.wildplot.android.rendering.interfaces.Legendable.java
com.wildplot.android.rendering.interfaces.StepFunction2D.java