All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rcaller.io.ROutputParserArrow Maven / Gradle / Ivy

The newest version!
/*
 *
RCaller, A solution for calling R from Java
Copyright (C) 2010-2014  Mehmet Hakan Satman

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

This program 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 Lesser General Public License
along with this program.  If not, see .
 *
 *
 * Mehmet Hakan Satman - [email protected]
 * http://www.mhsatman.com
 * Google code project: https://github.com/jbytecode/rcaller
 * Please visit the blog page with rcaller label:
 * http://stdioe.blogspot.com.tr/search/label/rcaller
 */
package com.github.rcaller.io;

import com.github.rcaller.exception.ParseException;
import com.github.rcaller.exception.XMLParseException;
import com.github.rcaller.rstuff.ROutputParser;
import org.apache.commons.lang3.NotImplementedException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.net.URI;
import java.util.ArrayList;

/**
 *
 * @author Kopilov Aleksandr
 */
public class ROutputParserArrow implements ROutputParser {
    ArrowBridge bridge = ArrowBridge.newInstance();
    URI ipcResourceURI;

    @Override
    public Document getDocument() {
        throw new NotImplementedException("Not accessible in Arrow implementation");
    }

    @Override
    public void setDocument(Document document) {
        throw new NotImplementedException("Not accessible in Arrow implementation");
    }

    @Override
    public File getXMLFile() {
        throw new NotImplementedException("Not accessible in Arrow implementation");
    }

    @Override
    public URI getIPCResource() {
        return ipcResourceURI;
    }

    @Override
    public void setIPCResource(URI ipcResourceURI) {
        this.ipcResourceURI = ipcResourceURI;
    }

    @Override
    public String getXMLFileAsString() throws IOException {
        throw new IOException(new NotImplementedException("Not accessible in Arrow implementation"));
    }

    @Override
    public void setXMLFile(File xmlFile) {
        throw new NotImplementedException("Not accessible in Arrow implementation");
    }

    @Override
    public void parse() throws ParseException {
        try {
            bridge.loadArrowData(ipcResourceURI);
        } catch (IOException e) {
            throw new ParseException("Could not load Arrow data", e);
        }
    }

    @Override
    public ArrayList getNames() {
        var names = bridge.getNames();
        if (names instanceof ArrayList) {
            return (ArrayList) names;
        }
        var castedNames = new ArrayList(names);
        return castedNames;
    }

    @Override
    public String getType(String variablename) {
        return bridge.getType(variablename);
    }

    @Override
    public int[] getDimensions(String name) {
        return bridge.getDimensions(name);
    }

    @Override
    public NodeList getValueNodes(String name) {
        throw new NotImplementedException("Not accessible in Arrow implementation");
    }

    @Override
    public String[] getAsStringArray(String name) throws ParseException {
        return bridge.getAsStringArray(name);
    }

    @Override
    public double[] getAsDoubleArray(String name) throws ParseException {
        return bridge.getAsDoubleArray(name);
    }

    @Override
    public float[] getAsFloatArray(String name) throws ParseException {
        return bridge.getAsFloatArray(name);
    }

    @Override
    public int[] getAsIntArray(String name) throws ParseException {
        return bridge.getAsIntArray(name);
    }

    @Override
    public long[] getAsLongArray(String name) throws ParseException {
        return bridge.getAsLongArray(name);
    }

    @Override
    public double[][] getAsDoubleMatrix(String name, int n, int m) throws ParseException {
        int[] dimensions = getDimensions(name);
        if (dimensions[0] == n && dimensions[1] == m) {
            return bridge.getAsDoubleMatrix(name);
        } else {
            double[][] result = new double[n][m];
            double[] arr = this.getAsDoubleArray(name);
            int c = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    result[i][j] = arr[c];
                    c++;
                }
            }
            return (result);
        }
    }

    @Override
    public double[][] getAsDoubleMatrix(String name) throws ParseException {
        return bridge.getAsDoubleMatrix(name);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy