Package cc.redberry.core.parser

Source Code of cc.redberry.core.parser.ParserTensorField

/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
*   Stanislav Poslavsky   <stvlpos@mail.ru>
*   Bolotin Dmitriy       <bolotin.dmitriy@gmail.com>
*
* This file is part of Redberry.
*
* Redberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Redberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Redberry. If not, see <http://www.gnu.org/licenses/>.
*/
package cc.redberry.core.parser;

import java.util.ArrayList;
import java.util.List;
import cc.redberry.core.context.CC;
import cc.redberry.core.context.NameDescriptor;
import cc.redberry.core.indices.IndicesFactory;
import cc.redberry.core.indices.IndicesTypeStructure;
import cc.redberry.core.indices.SimpleIndices;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.tensor.TensorField;

/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class ParserTensorField implements TensorParser {
    public static final ParserTensorField INSTANCE = new ParserTensorField();
    private final static int ID = 7000;

    private ParserTensorField() {
    }

    @Override
    public int getPriority() {
        return ID;
    }

    public boolean canParse(String expression) {
        char[] expressionChars = expression.toCharArray();
        int level = 0;
        boolean inBrackets = false;
        for (char c : expressionChars) {
            if ((c == '+' || c == '*' || c == '-' || c == '/') && !inBrackets)
                return false;
            if (c == '[') {
                inBrackets = true;
                level++;
            }
            if (c == ']')
                level--;
            if (level == 0)
                inBrackets = false;
            if (level < 0)
                throw new BracketsError();
        }
        return true;
    }

    @Override
    public Tensor parse(String expression, Parser parser) {
        if (!expression.contains("["))
            return null;
        if (!canParse(expression))
            return null;// throw new BracketsError();
        String tensorPart = expression.substring(0, expression.indexOf('['));
        ParserSimpleTensorStructure stp = new ParserSimpleTensorStructure(tensorPart);

        String argString = expression.substring(expression.indexOf("[") + 1, expression.length() - 1);

        List<Tensor> argsList = new ArrayList<>();
        List<SimpleIndices> argIndices = new ArrayList<>();

        int beginIndex = 0, level = 0;
        char[] argsChars = argString.toCharArray();
        for (int i = 0; i < argsChars.length; ++i) {
            char c = argsChars[i];

            if ((c == ',' && level == 0) || i == argsChars.length - 1) {
                String argument = argString.substring(beginIndex, i == argsChars.length - 1 ? i + 1 : i);
                String[] split = argument.split(":");
                if (split.length == 1) {
                    Tensor a = parser.parse(argument);
                    SimpleIndices aIndices = IndicesFactory.createSimple(a.getIndices().getFreeIndices());
                    argsList.add(a);
                    argIndices.add(aIndices);
                } else {
                    if (split.length != 2)
                        throw new ParserException(expression);
                    Tensor a = parser.parse(split[0]);
                    SimpleIndices aIndices = ParserIndices.parse(split[1]);
                    argsList.add(a);
                    argIndices.add(aIndices);
                    //TODO add assertion on indices compatability
                }
                beginIndex = i + 1;
            }
            if (c == '[')
                ++level;
            if (c == ']')
                --level;
        }

        Tensor[] arguments = argsList.toArray(new Tensor[argsList.size()]);
        SimpleIndices[] arumentsIndices = argIndices.toArray(new SimpleIndices[argIndices.size()]);

        IndicesTypeStructure[] structures = new IndicesTypeStructure[1 + arguments.length];
        structures[0] = new IndicesTypeStructure(stp.indices);
        for (int i = 1; i < structures.length; ++i)
            structures[i] = new IndicesTypeStructure(argIndices.get(i - 1));
        NameDescriptor descriptor = new NameDescriptor(stp.name, structures);
        int tensorName = CC.getNameManager().mapNameDescriptor(descriptor);
        return new TensorField(tensorName, stp.indices, arguments, arumentsIndices);
    }
}
TOP

Related Classes of cc.redberry.core.parser.ParserTensorField

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.