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(); }
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()); }
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)); }
public void registerResource(IString javaClass, IEvaluatorContext ctx) { try { String className = javaClass.getValue(); List<ClassLoader> loaders = ctx.getEvaluator().getClassLoaders(); Class<?> resourceClass = null; for (ClassLoader loader : loaders) { try { resourceClass = loader.loadClass(className); } catch (ClassNotFoundException e) { continue; } break; } if (resourceClass != null) { IResource handler = (IResource) resourceClass.newInstance(); Resource.registerResourceHandler(handler); } else { throw new ClassNotFoundException(className); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
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; }
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); } } } }
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); } } } }
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()); } } } }
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); } } } }
public Object createRootEnvironment() { return ctx.getCurrentEnvt(); }
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); } } } }
public void generateTypedInterfaceInternal( IString tag, ISourceLocation uriLoc, IEvaluatorContext ctx) { IResource handler = Resource.getResourceHandler(uriLoc); String tagStr = tag.getValue(); Type t = handler.getResourceType(ctx, uriLoc); PrintWriter currentOutStream = ctx.getStdOut(); // Declare an alias to the type of the resource TypeStore ts = ctx.getCurrentEnvt().getStore(); Type alias2t = TypeFactory.getInstance().aliasType(ts, tagStr + "Type", t); currentOutStream.println( "Generated type alias " + alias2t.toString() + ": " + alias2t.getAliased().toString()); currentOutStream.flush(); // Declare a function that just uses the given URI. This way, if we provide // the complete URI up front, we don't need to keep providing it later. StringBuilder sb = new StringBuilder(); sb.append("public ").append(tagStr).append("Type ").append(tagStr).append("() { "); sb.append(" return getTypedResource(") .append(uriLoc.toString()) .append(",#") .append(tagStr) .append("Type); }"); IConstructor declTree = ctx.getEvaluator() .parseCommand( ctx.getEvaluator().getMonitor(), sb.toString(), ctx.getCurrentAST().getLocation().getURI()); Command cmd = ctx.getEvaluator().getBuilder().buildCommand(declTree); Environment env = ctx.getCurrentEnvt(); ctx.setCurrentEnvt(env.getRoot()); Result<IValue> fun0 = ctx.getEvaluator().eval(ctx.getEvaluator().getMonitor(), cmd); ctx.unwind(env); currentOutStream.println("Generated function " + fun0.toString()); currentOutStream.flush(); }