Search in sources :

Example 11 with IAssociation

use of org.matheclipse.core.interfaces.IAssociation in project symja_android_library by axkr.

the class ASTDataset method normal.

@Override
public IASTAppendable normal(boolean nilIfUnevaluated) {
    Cache<IAST, IAST> cache = CacheBuilder.newBuilder().maximumSize(500).build();
    final List<String> names = fTable.columnNames();
    List<IStringX> namesStr = new ArrayList<IStringX>(names.size());
    for (int i = 0; i < names.size(); i++) {
        namesStr.add(F.stringx(names.get(i)));
    }
    if (names.size() == 1) {
        Column<?> column = fTable.column(names.get(0));
        ColumnType t = column.type();
        IASTAppendable resultList = F.ListAlloc(column.size());
        for (int j = 0; j < column.size(); j++) {
            Object obj = column.get(j);
            IExpr expr = F.NIL;
            expr = dataToExpr(obj, t);
            resultList.append(expr);
        }
        return resultList;
    }
    IASTAppendable list = F.ListAlloc(names.size());
    int size = fTable.rowCount();
    for (int k = 0; k < size; k++) {
        Row row = fTable.row(k);
        IAssociation assoc = F.assoc();
        for (int j = 0; j < row.columnCount(); j++) {
            String columnName = names.get(j);
            IStringX colName = namesStr.get(j);
            ColumnType t = row.getColumnType(columnName);
            Object obj = row.getObject(j);
            if (t.equals(ColumnType.EXPR)) {
                IExpr expr = (IExpr) obj;
                ruleCache(cache, assoc, F.Rule(colName, expr));
            } else if (t.equals(ColumnType.BOOLEAN)) {
                Boolean b = row.getBoolean(j);
                if (b) {
                    ruleCache(cache, assoc, F.Rule(colName, S.True));
                } else {
                    ruleCache(cache, assoc, F.Rule(colName, S.False));
                }
            } else if (t.equals(ColumnType.SHORT)) {
                short sValue = row.getShort(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(sValue)));
            } else if (t.equals(ColumnType.INTEGER)) {
                int iValue = row.getInt(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(iValue)));
            } else if (t.equals(ColumnType.LONG)) {
                long lValue = row.getLong(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(lValue)));
            } else if (t.equals(ColumnType.FLOAT)) {
                float fValue = row.getFloat(j);
                ruleCache(cache, assoc, F.Rule(colName, F.num(fValue)));
            } else if (t.equals(ColumnType.DOUBLE)) {
                double dValue = row.getDouble(j);
                ruleCache(cache, assoc, F.Rule(colName, F.num(dValue)));
            } else if (t.equals(ColumnType.STRING)) {
                ruleCache(cache, assoc, F.Rule(colName, F.stringx(row.getString(j))));
            } else if (t.equals(ColumnType.LOCAL_DATE_TIME)) {
                LocalDateTime lDate = row.getDateTime(j);
                ruleCache(cache, assoc, F.Rule(colName, DateObjectExpr.newInstance(lDate)));
            } else if (t.equals(ColumnType.LOCAL_DATE)) {
                LocalDate lDate = row.getDate(j);
                ruleCache(cache, assoc, F.Rule(colName, DateObjectExpr.newInstance(lDate.atStartOfDay())));
            } else if (t.equals(ColumnType.LOCAL_TIME)) {
                LocalTime lTime = row.getTime(j);
                ruleCache(cache, assoc, F.Rule(colName, TimeObjectExpr.newInstance(lTime)));
            } else if (t.equals(ColumnType.SKIP)) {
                // ruleCache(cache, assoc, F.Rule(colName, F.Missing));
                ruleCache(cache, assoc, F.Rule(colName, F.Missing(S.NotAvailable)));
            } else {
                IExpr valueStr = F.stringx(obj.toString());
                ruleCache(cache, assoc, F.Rule(colName, valueStr));
            }
        }
        if (size == 1) {
            return assoc;
        }
        list.append(assoc);
    }
    return list;
}
Also used : LocalDateTime(java.time.LocalDateTime) IAssociation(org.matheclipse.core.interfaces.IAssociation) ColumnType(tech.tablesaw.api.ColumnType) LocalTime(java.time.LocalTime) ArrayList(java.util.ArrayList) LocalDate(java.time.LocalDate) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr) Row(tech.tablesaw.api.Row)

Example 12 with IAssociation

use of org.matheclipse.core.interfaces.IAssociation in project symja_android_library by axkr.

the class VisitorLevelSpecification method visit.

@Override
public IExpr visit(IAssociation assoc) {
    IAssociation[] result = new IAssociation[] { F.NIL };
    if (assoc.isPresent()) {
        int[] minDepth = new int[] { 0 };
        try {
            fCurrentLevel++;
            if (fIncludeHeads) {
            // no include head for associations
            }
            assoc.forEach((x, i) -> {
                final IExpr temp = x.accept(this);
                if (temp.isPresent()) {
                    if (!result[0].isPresent()) {
                        result[0] = createResult(assoc, temp);
                    }
                    result[0].set(i, assoc.getRule(i).setAtCopy(2, temp));
                }
                if (fCurrentDepth < minDepth[0]) {
                    minDepth[0] = fCurrentDepth;
                }
            });
        } finally {
            fCurrentLevel--;
        }
        fCurrentDepth = --minDepth[0];
        if (isInRange(fCurrentLevel, minDepth[0])) {
            if (!result[0].isPresent()) {
                return fFunction.apply(assoc);
            } else {
                IExpr temp = fFunction.apply(result[0]);
                if (temp.isPresent()) {
                    return temp;
                }
            }
        }
    }
    return result[0];
}
Also used : IAssociation(org.matheclipse.core.interfaces.IAssociation) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IAssociation (org.matheclipse.core.interfaces.IAssociation)12 IExpr (org.matheclipse.core.interfaces.IExpr)11 IAST (org.matheclipse.core.interfaces.IAST)8 ArrayList (java.util.ArrayList)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 Row (tech.tablesaw.api.Row)3 HashSet (java.util.HashSet)2 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)2 IStringX (org.matheclipse.core.interfaces.IStringX)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 IntList (it.unimi.dsi.fastutil.ints.IntList)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1