Example #1
0
 private IValue read(Type resultType, ISourceLocation loc, IMap options, IEvaluatorContext ctx) {
   setOptions(options);
   Reader reader = null;
   try {
     reader = ctx.getResolverRegistry().getCharacterReader(loc.getURI());
     List<Record> records = loadRecords(reader);
     if (resultType == null) {
       resultType = inferType(records, ctx);
       ctx.getStdOut().println("readCSV inferred the relation type: " + resultType);
       ctx.getStdOut().flush();
     } else if (header) {
       records.remove(0);
     }
     return buildCollection(resultType, records, ctx);
   } catch (IOException e) {
     throw RuntimeExceptionFactory.io(
         values.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace());
   } finally {
     if (reader != null) {
       try {
         reader.close();
       } catch (IOException e) {
         throw RuntimeExceptionFactory.io(
             values.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace());
       }
     }
   }
 }
Example #2
0
 private void checkRecord(Type eltType, Record record, IEvaluatorContext ctx) {
   // TODO: not all inconsistencies are detected yet
   // probably because absent values get type void
   // but are nevertheless initialized eventually
   // to 0, false, or "".
   if (record.getType().isSubtypeOf(eltType)) {
     return;
   }
   if (eltType.isTuple()) {
     int expectedArity = eltType.getArity();
     int actualArity = record.getType().getArity();
     if (expectedArity == actualArity) {
       return;
     }
     throw RuntimeExceptionFactory.illegalTypeArgument(
         "Arities of actual type and requested type are different ("
             + actualArity
             + " vs "
             + expectedArity
             + ")",
         ctx.getCurrentAST(),
         ctx.getStackTrace());
   }
   throw RuntimeExceptionFactory.illegalTypeArgument(
       "Invalid tuple " + record + " for requested field " + eltType,
       ctx.getCurrentAST(),
       ctx.getStackTrace());
 }
Example #3
0
  @Override
  public Result<IValue> fieldSelect(Field[] selectedFields) {
    int nFields = selectedFields.length;
    int fieldIndices[] = new int[nFields];
    Type baseType = this.getType();

    for (int i = 0; i < nFields; i++) {
      Field f = selectedFields[i];
      if (f.isIndex()) {
        fieldIndices[i] =
            ((IInteger) f.getFieldIndex().interpret(this.ctx.getEvaluator()).getValue()).intValue();
      } else {
        String fieldName = org.rascalmpl.interpreter.utils.Names.name(f.getFieldName());
        try {
          fieldIndices[i] = baseType.getFieldIndex(fieldName);
        } catch (UndeclaredFieldException e) {
          throw new UndeclaredFieldError(fieldName, baseType, ctx.getCurrentAST());
        }
      }

      if (fieldIndices[i] < 0 || fieldIndices[i] > baseType.getArity()) {
        throw org.rascalmpl.interpreter.utils.RuntimeExceptionFactory.indexOutOfBounds(
            ValueFactoryFactory.getValueFactory().integer(fieldIndices[i]),
            ctx.getCurrentAST(),
            ctx.getStackTrace());
      }
    }

    return this.fieldSelect(fieldIndices);
  }
Example #4
0
 public IValue nroot(INumber x, IInteger y) {
   try {
     return x.toReal().nroot(y, values.getPrecision());
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
Example #5
0
 public IValue pow(INumber x, IInteger y) {
   try {
     return x.toReal().pow(y);
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
Example #6
0
  public IValue buildCollection(Type type, List<Record> records, IEvaluatorContext ctx) {
    IWriter writer;
    while (type.isAliased()) {
      type = type.getAliased();
    }

    if (type.isList()) {
      writer = values.listWriter(type.getElementType());
    } else if (type.isRelation()) {
      writer = values.relationWriter(type.getElementType());
    } else {
      throw RuntimeExceptionFactory.illegalTypeArgument(
          "Invalid result type for CSV reading: " + type, ctx.getCurrentAST(), ctx.getStackTrace());
    }

    // reverse traversal so that the order in case of ListWriter is correct
    // (IWriter only supports inserts at the front).
    Type eltType = type.getElementType();
    for (int i = records.size() - 1; i >= 0; i--) {
      Record record = records.get(i);
      checkRecord(eltType, record, ctx);
      writer.insert(record.getTuple(eltType));
    }
    return writer.done();
  }
Example #7
0
 private Type inferType(List<Record> records, IEvaluatorContext ctx) throws IOException {
   String[] labels = null;
   if (header) {
     labels = extractLabels(records.remove(0));
   }
   Type[] fieldTypes = null;
   for (Record ri : records) {
     List<Type> ftypes = ri.getFieldTypes();
     if (fieldTypes == null) {
       fieldTypes = new Type[ftypes.size()];
       Arrays.fill(fieldTypes, types.voidType());
     } else if (ftypes.size() != fieldTypes.length) {
       // We assume all records in the CSV file to have the same arity.
       throw RuntimeExceptionFactory.illegalArgument(
           // "Inconsistent tuple in CSV expected " + fieldTypes.length + " but was " +
           // ftypes.size(),
           ctx.getCurrentAST(), ctx.getStackTrace());
     }
     for (int i = 0; i < ftypes.size(); i++) {
       fieldTypes[i] = fieldTypes[i].lub(ftypes.get(i));
       if (fieldTypes[i].isTop()) {
         fieldTypes[i] = types.stringType();
       }
     }
   }
   if (labels == null) {
     labels = makeUpLabels(fieldTypes.length);
   }
   return types.setType(types.tupleType(fieldTypes, labels));
 }
Example #8
0
 public IValue readATermFromFile(IString fileName) {
   // @doc{readATermFromFile -- read an ATerm from a named file}
   ATermReader atr = new ATermReader();
   try {
     FileInputStream stream = new FileInputStream(fileName.getValue());
     IValue result = atr.read(values, stream);
     stream.close();
     return result;
   } catch (FactTypeUseException e) {
     e.printStackTrace();
     throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
   } catch (IOException e) {
     e.printStackTrace();
     throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
   }
 }
Example #9
0
  @Override
  @SuppressWarnings("unchecked")
  public <U extends IValue, V extends IValue> Result<U> subscript(Result<?>[] subscripts) {
    if (subscripts.length > 1) {
      throw new UnsupportedSubscriptArityError(getType(), subscripts.length, ctx.getCurrentAST());
    }
    Result<IValue> subsBase = (Result<IValue>) subscripts[0];
    if (subsBase == null)
      /*
       * Wild card not allowed as tuple subscript
       */
      throw new UnsupportedSubscriptError(type, null, ctx.getCurrentAST());
    if (!subsBase.getType().isIntegerType()) {
      throw new UnsupportedSubscriptError(
          getTypeFactory().integerType(), subsBase.getType(), ctx.getCurrentAST());
    }
    IInteger index = (IInteger) subsBase.getValue();
    if (index.intValue() >= getValue().arity()) {
      throw RuntimeExceptionFactory.indexOutOfBounds(
          index, ctx.getCurrentAST(), ctx.getStackTrace());
    }

    Type elementType = getType().getFieldType(index.intValue());
    IValue element = getValue().get(index.intValue());
    return makeResult(elementType, element, ctx);
  }
Example #10
0
 public IValue log(INumber x, INumber base) {
   try {
     return x.toReal().log(base.toReal(), values.getPrecision());
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
Example #11
0
 public IValue uriDecode(IString str) {
   try {
     return vf.string(URLDecoder.decode(str.getValue(), "UTF-8"));
   } catch (UnsupportedEncodingException e) {
     throw RuntimeExceptionFactory.illegalArgument(
         str, null, null, "UTF-8 is not a valid encoding");
   }
 }
Example #12
0
 public void writeTextJSonFile(ISourceLocation loc, IValue value, IEvaluatorContext ctx) {
   OutputStream out = null;
   try {
     out = ctx.getResolverRegistry().getOutputStream(loc.getURI(), false);
     new JSonWriter().write(value, new OutputStreamWriter(out, "UTF8"));
   } catch (IOException e) {
     throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
   } finally {
     if (out != null) {
       try {
         out.close();
       } catch (IOException ioex) {
         throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
       }
     }
   }
 }
Example #13
0
 public IValue getLog(IEvaluatorContext e) {
   if (logger == null) {
     throw RuntimeExceptionFactory.permissionDenied(
         vf.string("getLog called before startLog"), e.getCurrentAST(), null);
   }
   IString result = vf.string(logger.getBuffer().toString());
   Evaluator eval = (Evaluator) e;
   eval.revertToDefaultWriters();
   logger = null;
   return result;
 }
Example #14
0
 public IValue readTextJSonFile(IValue type, ISourceLocation loc, IEvaluatorContext ctx) {
   // TypeStore store = new TypeStore();
   TypeStore store = ctx.getCurrentEnvt().getStore();
   Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store);
   Reader read = null;
   try {
     read = ctx.getResolverRegistry().getCharacterReader(loc.getURI());
     return new JSonReader().read(values, store, start, read);
   } catch (IOException e) {
     throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
   } finally {
     if (read != null) {
       try {
         read.close();
       } catch (IOException ioex) {
         throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
       }
     }
   }
 }
Example #15
0
  public IValue readTextATermFile(IValue type, ISourceLocation loc, IEvaluatorContext ctx) {
    TypeStore store = new TypeStore();
    Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store);

    InputStream in = null;
    try {
      in = ctx.getResolverRegistry().getInputStream(loc.getURI());
      return new ATermReader().read(values, store, start, in);
    } catch (IOException e) {
      throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ioex) {
          throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
        }
      }
    }
  }
Example #16
0
 public IValue scale(INumber x) {
   try {
     if (x.getType().isIntegerType()) {
       IInteger k = (IInteger) x;
       return values.integer(k.toReal().scale());
     }
     if (x.getType().isRationalType()) {
       IRational k = (IRational) x;
       return values.integer(k.toReal().scale());
     }
     return values.integer(((IReal) x).scale());
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
  @Override
  public <U extends IValue, V extends IValue> Result<U> fieldUpdate(
      String name, Result<V> repl, TypeStore store) {
    IConstructor tree = getValue();

    if (TreeAdapter.isAppl(tree)) {
      int found = -1;
      IConstructor foundType = null;
      IConstructor prod = TreeAdapter.getProduction(tree);
      IList syms = ProductionAdapter.getSymbols(prod);

      // TODO: find deeper into optionals, alternatives and sequences checking the actual arguments
      // for presence/absence of optional trees.
      for (int i = 0; i < syms.length(); i++) {
        IConstructor sym = (IConstructor) syms.get(i);
        if (SymbolAdapter.isLabel(sym)) {
          if (SymbolAdapter.getLabel(sym).equals(name)) {
            found = i;
            foundType = SymbolAdapter.delabel(sym);
            break;
          }
        }
      }

      if (found != -1) {
        Type nont = RascalTypeFactory.getInstance().nonTerminalType(foundType);
        if (repl.getType().isSubtypeOf(nont)) {
          IList args = TreeAdapter.getArgs(tree).put(found, repl.getValue());
          return makeResult(getType(), tree.set("args", args), ctx);
        }
        throw new UnexpectedType(nont, repl.getType(), ctx.getCurrentAST());
      }

      if (Factory.Tree_Appl.hasField(name)) {
        Type fieldType = Factory.Tree_Appl.getFieldType(name);
        if (repl.getType().isSubtypeOf(fieldType)) {
          throw new UnsupportedOperation(
              "changing " + name + " in concrete tree", ctx.getCurrentAST());
        }
        throw new UnexpectedType(fieldType, repl.getType(), ctx.getCurrentAST());
      }

      throw RuntimeExceptionFactory.noSuchField(name, ctx.getCurrentAST(), ctx.getStackTrace());
    }
    throw new UnsupportedOperation("field update", ctx.getCurrentAST());
  }
  @Override
  public <U extends IValue> Result<U> fieldAccess(String name, TypeStore store) {
    IConstructor tree = getValue();

    if (TreeAdapter.isAppl(tree)) {
      int found = -1;
      IConstructor foundType = null;
      IConstructor prod = TreeAdapter.getProduction(tree);

      if (!ProductionAdapter.isRegular(prod)) {
        IList syms = ProductionAdapter.getSymbols(prod);

        // TODO: find deeper into optionals, checking the actual arguments for presence/absence of
        // optional trees.
        for (int i = 0; i < syms.length(); i++) {
          IConstructor sym = (IConstructor) syms.get(i);

          while (SymbolAdapter.isConditional(sym)) {
            sym = SymbolAdapter.getSymbol(sym);
          }
          if (SymbolAdapter.isLabel(sym)) {
            if (SymbolAdapter.getLabel(sym).equals(name)) {
              found = i;
              foundType = SymbolAdapter.delabel(sym);
            }
          }
        }

        if (found != -1) {
          Type nont = RascalTypeFactory.getInstance().nonTerminalType(foundType);
          IValue child = TreeAdapter.getArgs(tree).get(found);
          return makeResult(nont, child, ctx);
        }
      }
    }

    if (tree.getConstructorType().hasField(name)) {
      return makeResult(tree.getConstructorType().getFieldType(name), tree.get(name), ctx);
    }

    throw RuntimeExceptionFactory.noSuchField(name, ctx.getCurrentAST(), ctx.getStackTrace());
  }
Example #19
0
  public IString prompt(final IString prompt) {
    final String[] value = new String[1];

    PlatformUI.getWorkbench()
        .getDisplay()
        .syncExec(
            new Runnable() {
              public void run() {
                Shell activeShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                final Shell shell = new Shell(activeShell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
                shell.setText(prompt.getValue());

                GridLayout gridLayout = new GridLayout();
                shell.setLayout(gridLayout);
                Label label = new Label(shell, SWT.WRAP);
                label.setText(prompt.getValue());

                GridData data = new GridData();
                Monitor monitor = activeShell.getMonitor();
                int maxWidth = monitor.getBounds().width * 2 / 3;
                int width = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
                data.widthHint = Math.min(width, maxWidth);
                data.horizontalAlignment = GridData.FILL;
                data.grabExcessHorizontalSpace = true;
                label.setLayoutData(data);

                final Text valueText = new Text(shell, SWT.BORDER);

                Listener enterListener =
                    new Listener() {
                      public void handleEvent(Event event) {
                        value[0] = valueText.getText();
                        shell.close();
                      }
                    };

                if (value[0] != null) valueText.setText(value[0]);
                data = new GridData();
                width = valueText.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
                if (width > maxWidth) data.widthHint = maxWidth;
                data.horizontalAlignment = GridData.FILL;
                data.grabExcessHorizontalSpace = true;
                valueText.setLayoutData(data);
                valueText.addListener(SWT.DefaultSelection, enterListener);

                Composite composite = new Composite(shell, SWT.NONE);
                data = new GridData();
                data.horizontalAlignment = GridData.CENTER;
                composite.setLayoutData(data);
                composite.setLayout(new GridLayout(2, true));

                Button button = new Button(composite, SWT.PUSH);
                button.setText("OK");
                button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

                button.addListener(SWT.Selection, enterListener);

                shell.pack();
                shell.open();

                Display display = activeShell.getDisplay();
                while (!shell.isDisposed()) {
                  if (!display.readAndDispatch()) display.sleep();
                }
              }
            });

    if (value[0] != null) {
      return vf.string(value[0]);
    }

    throw RuntimeExceptionFactory.io(vf.string("no input"), null, null);
  }
Example #20
0
  public void write(IValue rel, ISourceLocation loc, IEvaluatorContext ctx) {

    OutputStream out = null;

    Type paramType = ctx.getCurrentEnvt().getTypeBindings().get(types.parameterType("T"));
    if (!paramType.isRelation() && !paramType.isListRelation()) {
      throw RuntimeExceptionFactory.illegalTypeArgument(
          "A relation type is required instead of " + paramType,
          ctx.getCurrentAST(),
          ctx.getStackTrace());
    }

    try {
      boolean isListRel = rel instanceof IList;
      out = ctx.getResolverRegistry().getOutputStream(loc.getURI(), false);
      ISet irel = null;
      IList lrel = null;
      if (isListRel) {
        lrel = (IList) rel;
      } else {
        irel = (ISet) rel;
      }

      int nfields = isListRel ? lrel.asRelation().arity() : irel.asRelation().arity();
      if (header) {
        for (int i = 0; i < nfields; i++) {
          if (i > 0) out.write(separator);
          String label = paramType.getFieldName(i);
          if (label == null || label.isEmpty()) label = "field" + i;
          writeString(out, label);
        }
        out.write('\n');
      }
      String separatorAsString = new String(Character.toChars(separator));
      for (IValue v : (isListRel ? lrel : irel)) {
        ITuple tup = (ITuple) v;
        int sep = 0;
        for (IValue w : tup) {
          if (sep == 0) sep = separator;
          else out.write(sep);
          if (w.getType().isString()) {
            String s = ((IString) w).getValue();

            if (s.contains(separatorAsString)
                || s.contains("\n")
                || s.contains("\r")
                || s.contains("\"")) {
              s = s.replaceAll("\"", "\"\"");
              out.write('"');
              writeString(out, s);
              out.write('"');
            } else writeString(out, s);
          } else {
            writeString(out, w.toString());
          }
        }
        out.write('\n');
      }
      out.flush();
      out.close();
    } catch (IOException e) {
      throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
    } finally {
      if (out != null) {
        try {
          out.flush();
          out.close();
        } catch (IOException ioex) {
          throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
        }
      }
    }
  }