private void pageOutput(OutputFormat format, List<String> fieldNames) throws IOException { try (Pager pager = Pager.create(); Writer writer = createWriter(pager); OutputHandler handler = createOutputHandler(format, writer, fieldNames)) { if (!pager.isNullPager()) { // ignore the user pressing ctrl-C while in the pager ignoreUserInterrupt.set(true); Thread clientThread = Thread.currentThread(); pager .getFinishFuture() .thenRun( () -> { userAbortedQuery.set(true); ignoreUserInterrupt.set(false); clientThread.interrupt(); }); } handler.processRows(client); } catch (RuntimeException | IOException e) { if (userAbortedQuery.get() && !(e instanceof QueryAbortedException)) { throw new QueryAbortedException(e); } throw e; } }
@Override public void compute() { if (!p.isDefined()) { setUndefined(); return; } double f = v.getDouble(); if (Kernel.isGreater(f, 1) || Kernel.isGreater(0, f)) { setUndefined(); return; } // update bottom points GeoPolygon bottomFace = p.getBottomFace(); if (bottomFace.isConvex()) { getNet().setDefined(); Coords[] points = getPointsCoords(bottomFace); adjustOutputSize(points.length); outputPointsBottom.setLabels(null); for (int i = 0; i < points.length; i++) { outputPointsBottom.getElement(i).setCoords(points[i]); } compute(f, bottomFace, points); } else { setUndefined(); } }
private void discardResults() { try (OutputHandler handler = new OutputHandler(new NullPrinter())) { handler.processRows(client); } catch (IOException e) { throw Throwables.propagate(e); } }
protected void rotatePoints(int n, double alpha) { // now we have the center point of the polygon and // the center angle alpha between two neighbouring points // let's create the points by rotating A around the center point for (int k = 0; k < n - 2; k++) { // rotate point around center point outputPoints.getElement(k).set((GeoElement) A); rotAngle.set((k + 2) * alpha); rotate((GeoPointND) outputPoints.getElement(k)); } }
/** * update bottom face for new length * * @param newBottomPointsLength new bottom points length */ protected void updateBottom(int newBottomPointsLength) { GeoPolygon polygon = outputPolygonsBottom.getElement(0); GeoPoint3D[] points = new GeoPoint3D[newBottomPointsLength]; GeoSegment3D[] segments = new GeoSegment3D[newBottomPointsLength]; for (int i = 0; i < newBottomPointsLength; i++) { points[i] = outputPointsBottom.getElement(i); segments[i] = outputSegmentsBottom.getElement(i); } polygon.modifyInputPoints(points); polygon.setSegments(segments); polygon.calcArea(); }
/** @param nd number of vertices */ public final void compute(int nd) { GeoPolygon poly = getPoly(); // get integer number of vertices n this.n = Math.max(2, nd); // if number of points changed, we need to update the // points array and the output array updateOutput(n); // check if regular polygon is defined if (checkUnDefined(n)) { return; } this.alpha = Kernel.PI_2 / n; // center angle ACB double beta = (Math.PI - alpha) / 2; // base angle CBA = BAC setCenterPoint(n, beta); rotatePoints(n, alpha); GeoPointND[] points = new GeoPointND[n]; points[0] = A; points[1] = B; for (int i = 2; i < n; i++) points[i] = (GeoPointND) outputPoints.getElement(i - 2); // update new segments for (int i = numOld - 1; i < n; i++) { // App.debug(i+": "+points[i]+" , "+points[(i+1)%n]); ((GeoSegmentND) outputSegments.getElement(i)) .modifyInputPoints(points[i], points[(i + 1) % n]); } // update polygon poly.setPoints(points, null, false); // don't create segments GeoSegmentND[] segments = new GeoSegmentND[n]; for (int i = 0; i < n; i++) { segments[i] = (GeoSegmentND) outputSegments.getElement(i); } poly.setSegments(segments); // compute area of poly calcArea(); // update region coordinate system poly.updateRegionCSWithFirstPoints(); numOld = n; }
private void connect() throws UnknownHostException, IOException { IrcServer = new Socket(serverName, 6667); BufferedReader br = new BufferedReader( new InputStreamReader(IrcServer.getInputStream(), BotStats.getInstance().getCharset())); PrintWriter pw = new PrintWriter( new OutputStreamWriter( IrcServer.getOutputStream(), BotStats.getInstance().getCharset()), true); ih = new InputHandler(br); oh = new OutputHandler(pw); ih.start(); oh.start(); new Message("", "NICK", BotStats.getInstance().getBotname(), "").send(); new Message( "", "USER", "goat" + " nowhere.com " + serverName, BotStats.getInstance().getVersion()) .send(); // we sleep until we are connected, don't want to send these next messages too soon while (!connected) { try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } joinChannels(); }
/** * Creates algo for Vertex[poly] (many output points) Creates new unlabeled vertex algo * * @param cons construction * @param p polygon or polyline */ protected AlgoVertexPolygon(Construction cons, GeoPoly p) { super(cons); this.p = p; outputPoints = createOutputPoints(); outputPoints.adjustOutputSize(1); setInputOutput(); // for AlgoElement compute(); }
private void setLabels(String[] labels) { // if only one label (e.g. "A") for more than one output, new labels // will be A_1, A_2, ... if (labels != null && labels.length == 1 && // outputPoints.size() > 1 && labels[0] != null && !labels[0].equals("")) { outputPoints.setIndexLabels(labels[0]); } else { outputPoints.setLabels(labels); outputPoints.setIndexLabels( outputPoints.getElement(0).getLabel(StringTemplate.defaultTemplate)); } }
public void doExportAs(String extension, File file) throws IOException { CustomElementSecurityManager.addThreadPrivileges(Thread.currentThread(), fileName); try { OutputHandler.createAndOutputToFile(extension, file, this.handler); } catch (Exception e) { throw new IOException(e.getMessage()); } CustomElementSecurityManager.remThreadPrivileges(Thread.currentThread()); }
public static boolean exec(String cmd) throws Exception { int exitVal = -1; try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(new String[] {"/bin/bash", "-c", cmd}); OutputHandler err = new OutputHandler(proc.getErrorStream(), cmd); err.start(); OutputHandler out = new OutputHandler(proc.getInputStream(), cmd); out.start(); exitVal = proc.waitFor(); } catch (Throwable t) { t.printStackTrace(); } return (exitVal == 0); }
private void setOutput(int n) { GeoPolyhedronNet net = getNet(); Collection<GeoPolygon3D> faces = net.getFacesCollection(); int step = 1; for (GeoPolygon polygon : faces) { GeoSegmentND[] segments = polygon.getSegments(); if (step == 1) { // bottom outputPolygonsBottom.addOutput((GeoPolygon3D) polygon, false); for (int i = 0; i < segments.length; i++) { outputSegmentsBottom.addOutput((GeoSegment3D) segments[i], false); } } else { // sides and top setOutputSideTop(n, (GeoPolygon3D) polygon, step, segments); } step++; } refreshOutput(); }
protected void setCharset(Charset cs) { try { BufferedReader br = new BufferedReader(new InputStreamReader(IrcServer.getInputStream(), cs)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(IrcServer.getOutputStream(), cs), true); ih.setBR(br); oh.setOSW(pw); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { System.err.println("Unexpected exception in setCharset :"); e.printStackTrace(); } }
/** @param c construction */ public AlgoPolyhedronNet(Construction c, String[] labels, GeoPolyhedron p, NumberValue v) { super(c); this.p = p; this.v = v; vNum = ChangeableCoordParent.getGeoNumeric(v); outputNet = new OutputHandler<GeoPolyhedronNet>( new elementFactory<GeoPolyhedronNet>() { public GeoPolyhedronNet newElement() { GeoPolyhedronNet p = new GeoPolyhedronNet(cons); p.setParentAlgorithm(AlgoPolyhedronNet.this); return p; } }); outputNet.adjustOutputSize(1); outputPointsBottom = createOutputPoints(); outputPointsSide = createOutputPoints(); outputPointsTop = createOutputPoints(); outputPolygonsBottom = createOutputPolygons(); outputPolygonsSide = createOutputPolygons(); outputPolygonsTop = createOutputPolygons(); outputSegmentsBottom = createOutputSegments(); outputSegmentsSide = createOutputSegments(); outputSegmentsTop = createOutputSegments(); bottomPointsLength = p.getBottomFace().getPointsLength(); createNet(bottomPointsLength); input = new GeoElement[] {p, (GeoElement) v}; for (int i = 0; i < input.length; i++) { input[i].addAlgorithm(this); } getNet().createFaces(); setOutput(bottomPointsLength); // set labels setLabels(labels); update(); updateOutputSegmentsAndPolygonsParentAlgorithms(); }
/** force update for segments and polygons at creation */ private void updateOutputSegmentsAndPolygonsParentAlgorithms() { outputSegmentsBottom.updateParentAlgorithm(); outputSegmentsSide.updateParentAlgorithm(); outputPolygonsBottom.updateParentAlgorithm(); outputPolygonsSide.updateParentAlgorithm(); if (p.getType() == GeoPolyhedron.TYPE_PRISM) { outputSegmentsTop.updateParentAlgorithm(); outputPolygonsTop.updateParentAlgorithm(); } }
/** * Abort the application and wait for it to finish. * * @param t the exception that signalled the problem * @throws IOException A wrapper around the exception that was passed in */ void abort(Throwable t) throws IOException { LOG.info("Aborting because of " + StringUtils.stringifyException(t)); try { downlink.abort(); downlink.flush(); } catch (IOException e) { // IGNORE cleanup problems } try { handler.waitForFinish(); } catch (Throwable ignored) { process.destroy(); } IOException wrapper = new IOException("pipe child exception"); wrapper.initCause(t); throw wrapper; }
@Override public final void compute() { if (index != null) { if (!p.isDefined()) { oneVertex.setUndefined(); } else { int i = (int) Math.floor(index.getDouble()) - 1; if (i >= p.getPoints().length || i < 0) { oneVertex.setUndefined(); } else { setPoint(oneVertex, i); } } oneVertex.update(); return; } if (!p.isDefined()) { for (int i = 0; i < outputPoints.size(); i++) { outputPoints.getElement(i).setUndefined(); } return; } int length = p.getPoints().length; // Log.debug(length); if (length > outputPoints.size()) { outputPoints.adjustOutputSize(length); refreshOutput(); } for (int i = 0; i < length; i++) { GeoPointND point = (GeoPointND) outputPoints.getElement(i); setPoint(point, i); } // other points are undefined for (int i = length; i < outputPoints.size(); i++) { outputPoints.getElement(i).setUndefined(); } }
/** * Outputs detailed information about the application, including configuration, available modules * and handlers, etc. */ public void show(OutputHandler handler) { handler.show(this); }
public void secondPlayerMessage() { outPutter.printOutput("The second player is... type 'human' or 'computer'\n"); }
@Override public int getOutputLength() { if (index != null) return 1; return outputPoints.size(); }
/** * Creates a new regular polygon algorithm * * @param c construction * @param labels labels[0] for polygon, then labels for segments and then for points * @param A1 first input point * @param B1 second input point * @param num number of vertices */ public AlgoPolygonRegularND( Construction c, String[] labels, GeoPointND A1, GeoPointND B1, NumberValue num, GeoDirectionND direction) { super(c); labelsNeedIniting = true; this.A = A1; this.B = B1; this.num = num; setDirection(direction); // labels given by user or loaded from file int labelsLength = labels == null ? 0 : labels.length; // set labels for segments only when points have labels labelPointsAndSegments = A.isLabelSet() || B.isLabelSet() || labelsLength > 1; showNewSegmentsLabels = false; showNewPointsLabels = false; // temp center point of regular polygon centerPoint = (GeoPointND) newGeoPoint(c); rotAngle = new MyDouble(kernel); outputPolygon = new OutputHandler<GeoPolygon>( new elementFactory<GeoPolygon>() { public GeoPolygon newElement() { GeoPolygon p = newGeoPolygon(cons); p.setParentAlgorithm(AlgoPolygonRegularND.this); return p; } }); outputSegments = new OutputHandler<GeoElement>( new elementFactory<GeoElement>() { public GeoElement newElement() { GeoElement segment = (GeoElement) outputPolygon.getElement(0).createSegment(A, B, true); segment.setAuxiliaryObject(true); boolean segmentsVisible = false; int size = outputSegments.size(); if (size > 0) { // check if at least one segment is // visible for (int i = 0; i < size && !segmentsVisible; i++) { segmentsVisible = segmentsVisible || outputSegments.getElement(i).isEuclidianVisible(); } } else { // no segment yet segmentsVisible = true; } segment.setEuclidianVisible(segmentsVisible); segment.setLabelVisible(showNewSegmentsLabels); segment.setViewFlags(((GeoElement) A).getViewSet()); segment.setVisibleInView3D((GeoElement) A); segment.setVisibleInViewForPlane((GeoElement) A); return segment; } }); if (!labelPointsAndSegments) outputSegments.removeFromHandler(); // no segments has output outputPoints = new OutputHandler<GeoElement>( new elementFactory<GeoElement>() { public GeoElement newElement() { GeoElement newPoint = newGeoPoint(cons); newPoint.setParentAlgorithm(AlgoPolygonRegularND.this); newPoint.setAuxiliaryObject(true); ((GeoPointND) newPoint).setPointSize(A.getPointSize()); newPoint.setEuclidianVisible(A.isEuclidianVisible() || B.isEuclidianVisible()); newPoint.setAuxiliaryObject(true); newPoint.setLabelVisible(showNewPointsLabels); newPoint.setViewFlags(((GeoElement) A).getViewSet()); newPoint.setVisibleInView3D((GeoElement) A); newPoint.setVisibleInViewForPlane((GeoElement) A); GeoBoolean conditionToShow = ((GeoElement) A).getShowObjectCondition(); if (conditionToShow == null) conditionToShow = ((GeoElement) B).getShowObjectCondition(); if (conditionToShow != null) { try { newPoint.setShowObjectCondition(conditionToShow); } catch (Exception e) { // circular exception -- do nothing } } return newPoint; } }); if (!labelPointsAndSegments) outputPoints.removeFromHandler(); // no segments has output // create polygon outputPolygon.adjustOutputSize(1); // create 2 first segments outputSegments.augmentOutputSize(2, false); outputSegments.getElement(0).setAuxiliaryObject(false); ((GeoSegmentND) outputSegments.getElement(1)).modifyInputPoints(B, A); // for AlgoElement setInputOutput(); GeoPolygon poly = getPoly(); // set that the poly output can have different points length poly.setNotFixedPointsLength(true); // compute poly if (labelsLength > 1) { compute((labelsLength + 1) / 2); // create maybe undefined outputs poly.setLabel(labels[0]); int d = 1; for (int i = 0; i < outputSegments.size(); i++) outputSegments.getElement(i).setLabel(labels[d + i]); d += outputSegments.size(); for (int i = 0; i < outputPoints.size(); i++) outputPoints.getElement(i).setLabel(labels[d + i]); } else if (labelsLength == 1) { poly.setLabel(labels[0]); } else { poly.setLabel(null); } labelsNeedIniting = false; update(); /* * if (labelPointsAndSegments) { //poly.initLabels(labels); } else if * (labelsLength == 1) { poly.setLabel(labels[0]); } else { * poly.setLabel(null); } * * * labelsNeedIniting = false; */ // make sure that we set all point and segment labels when needed // updateSegmentsAndPointsLabels(points.length); }
@Override public GeoElement getOutput(int i) { if (index != null) return (GeoElement) oneVertex; return outputPoints.getElement(i); }
/** * Ensures that the pointList holds n points. * * @param n */ private void updateOutput(int n) { int nOld = outputPoints.size() + 2; // App.error("nOld="+nOld+", n="+n); if (nOld == n) return; // update points and segments if (n > nOld) { showNewPointsLabels = labelPointsAndSegments && (A.isEuclidianVisible() && A.isLabelVisible() || B.isEuclidianVisible() && B.isLabelVisible()); outputPoints.augmentOutputSize(n - nOld, false); if (labelPointsAndSegments && !labelsNeedIniting) outputPoints.updateLabels(); showNewSegmentsLabels = false; for (int i = 0; i < outputSegments.size(); i++) showNewSegmentsLabels = showNewSegmentsLabels || outputSegments.getElement(i).isLabelVisible(); outputSegments.augmentOutputSize(n - nOld, false); if (labelPointsAndSegments && !labelsNeedIniting) outputSegments.updateLabels(); } else { for (int i = n; i < nOld; i++) { outputPoints.getElement(i - 2).setUndefined(); outputSegments.getElement(i).setUndefined(); } // update last segment if (n > 2) ((GeoSegmentND) outputSegments.getElement(n - 1)) .modifyInputPoints((GeoPointND) outputPoints.getElement(n - 3), A); else ((GeoSegmentND) outputSegments.getElement(n - 1)).modifyInputPoints(B, A); } }
/** @return resulting polygon */ public final GeoPolygon getPoly() { return outputPolygon.getElement(0); }
@Override public void handleLine(String line) { for (OutputHandler handler : _handlers) { handler.handleLine(line); } }
private void sendOutput(PrintStream out, OutputFormat format, List<String> fieldNames) throws IOException { try (OutputHandler handler = createOutputHandler(format, createWriter(out), fieldNames)) { handler.processRows(client); } }
/** * adjust output for n bottom points * * @param n new bottom points length */ protected void adjustOutputSize(int n) { if (n > outputPointsBottom.size()) { // augment output points bottom outputPointsBottom.adjustOutputSize(n, false); } }
/** @return the polyhedron */ public GeoPolyhedronNet getNet() { return outputNet.getElement(0); }
private void setUndefined() { getNet().setUndefined(); outputPointsBottom.setUndefined(); outputPointsSide.setUndefined(); outputPointsTop.setUndefined(); }
public void firstPlayerMessage() { outPutter.printOutput("The first player is... type 'human' or 'computer'\n"); }