Skip to content

Commit

Permalink
Imported Java code from JPMML-Evaluator-Python and JPMML-Evaluator-R …
Browse files Browse the repository at this point in the history
…projects
  • Loading branch information
vruusmann committed Dec 11, 2024
1 parent 611cbb4 commit caa8f62
Show file tree
Hide file tree
Showing 5 changed files with 541 additions and 0 deletions.
130 changes: 130 additions & 0 deletions pmml-evaluator/src/main/java/org/jpmml/evaluator/Table.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpmml.evaluator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Table {

private List<String> columns = null;

private Map<String, List<?>> values = new HashMap<>();


public Table(){
this(new ArrayList<>());
}

public Table(List<String> columns){
setColumns(columns);
}

public int getNumberOfRows(){
Map<String, List<?>> columnValues = getValues();

int result = 0;

Collection<Map.Entry<String, List<?>>> entries = columnValues.entrySet();
for(Map.Entry<String, List<?>> entry : entries){
List<?> values = entry.getValue();

result = Math.max(result, values.size());
}

return result;
}

public int getNumberOfColumns(){
List<String> columns = getColumns();

return columns.size();
}

public void canonicalize(){
List<String> columns = getColumns();

int numberOfRows = getNumberOfRows();

for(String column : columns){
List<?> values = getValues(column);

if(values == null){
values = new ArrayList<>(numberOfRows);

setValues(column, values);
}

TableUtil.ensureSize(values, numberOfRows);
}
}

public boolean addColumn(String column){
List<String> columns = getColumns();

if(!columns.contains(column)){
columns.add(column);

return true;
}

return false;
}

public boolean removeColumn(String column){
List<String> columns = getColumns();

boolean result = columns.remove(column);
if(result){
Map<String, List<?>> columnValues = getValues();

columnValues.remove(column);
}

return result;
}

public List<?> getValues(String column){
Map<String, List<?>> columnValues = getValues();

return columnValues.get(column);
}

public void setValues(String column, List<?> values){
Map<String, List<?>> columnValues = getValues();

columnValues.put(column, values);
}

public List<String> getColumns(){
return this.columns;
}

void setColumns(List<String> columns){
this.columns = Objects.requireNonNull(columns);
}

public Map<String, List<?>> getValues(){
return this.values;
}
}
167 changes: 167 additions & 0 deletions pmml-evaluator/src/main/java/org/jpmml/evaluator/TableReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpmml.evaluator;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;

public class TableReader extends AbstractMap<String, Object> implements Iterator<Map<String, Object>> {

private Table table = null;

private int position = -1;

private int maxPosition = -1;


public TableReader(Table table){
setTable(table);
}

@Override
public boolean hasNext(){
int position = getPosition();
int maxPosition = getMaxPosition();

return position < maxPosition;
}

@Override
public Map<String, Object> next(){
int position = getPosition();
int maxPosition = getMaxPosition();

if(position < maxPosition){
setPosition(position + 1);
} else

{
throw new NoSuchElementException();
}

return this;
}

@Override
public Object get(Object key){
Table table = getTable();
int position = ensurePosition();

List<?> values = table.getValues((String)key);
if(values != null){
return values.get(position);
}

return null;
}

@Override
public Set<Map.Entry<String, Object>> entrySet(){
Table table = getTable();
int position = ensurePosition();

Set<Map.Entry<String, Object>> result = new AbstractSet<Map.Entry<String, Object>>(){


@Override
public int size(){
List<String> columns = table.getColumns();

return columns.size();
}

@Override
public Iterator<Map.Entry<String, Object>> iterator(){
List<String> columns = table.getColumns();

Iterator<Map.Entry<String, Object>> result = new Iterator<Map.Entry<String, Object>>(){

private Iterator<String> it = columns.iterator();


@Override
public boolean hasNext(){
return this.it.hasNext();
}

@Override
public Map.Entry<String, Object> next(){
String column = this.it.next();

List<?> values = table.getValues(column);
if(values != null){
Object value = values.get(position);

return new SimpleEntry<>(column, value);
}

return new SimpleEntry<>(column, null);
}
};

return result;
}
};

return result;
}

protected int ensurePosition(){
int position = getPosition();

if(position < 0){
throw new IllegalStateException();
}

return position;
}

public Table getTable(){
return this.table;
}

void setTable(Table table){
this.table = Objects.requireNonNull(table);

setPosition(-1);
setMaxPosition(table.getNumberOfRows() - 1);
}

int getPosition(){
return this.position;
}

void setPosition(int position){
this.position = position;
}

int getMaxPosition(){
return this.maxPosition;
}

void setMaxPosition(int maxPosition){
this.maxPosition = maxPosition;
}
}
19 changes: 19 additions & 0 deletions pmml-evaluator/src/main/java/org/jpmml/evaluator/TableUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jpmml.evaluator;

import java.util.List;

class TableUtil {

private TableUtil(){
}

static
<E> List<E> ensureSize(List<E> values, int size){

while(values.size() < size){
values.add(null);
}

return values;
}
}
Loading

0 comments on commit caa8f62

Please sign in to comment.