Stop average block and calculate average

@param string $block - average block identifier

@throws \Exception if block not defined
175006
public function stopAvg($block)
    {
        if ($this->enabled) {
            if (!isset($this->avgs[$block])) {
                throw new \Exception('Average block '.$block.' has not been started!');
            }

            $this->avgs[$block]['stop'] = microtime(true);
            if (!isset($this->avgs[$block]['stop-line'])) {
                $this->avgs[$block]['stop-line'] = $this->getLineNumber();
            }

            // Calculate average
            $this->avgs[$block]['count']++; // increment count

            $time = $this->avgs[$block]['stop'] - $this->avgs[$block]['start'];

            // Check max and min time
            if ($this->avgs[$block]['max-time'] < $time) {
                $this->avgs[$block]['max-time'] = $time;
            }
            if ($this->avgs[$block]['min-time'] > $time) {
                $this->avgs[$block]['min-time'] = $time;
            }

            $this->avgs[$block]['total'] = $this->avgs[$block]['total'] + $time;
        }
    }
90927
public function stop()
    {
        if ($this->pid) {
            exec('kill ' . $this->pid, $output, $ret);
            if ($ret !== 0) {
                throw new Exception('Error stopping build-in server.');
            }
            $this->pid = null;
        }
    }
367715
private function createBlockCollectionsStatus(Block $block, int $newStatus): void
    {
        $collectionReferences = $this->loadCollectionReferences($block);

        foreach ($collectionReferences as $collectionReference) {
            $collection = $this->collectionHandler->loadCollection(
                $collectionReference->collectionId,
                $collectionReference->collectionStatus
            );

            $collection = $this->collectionHandler->createCollectionStatus(
                $collection,
                $newStatus
            );

            $newCollectionReference = CollectionReference::fromArray(
                [
                    'blockId' => $block->id,
                    'blockStatus' => $newStatus,
                    'collectionId' => $collection->id,
                    'collectionStatus' => $collection->status,
                    'identifier' => $collectionReference->identifier,
                ]
            );

            $this->queryHandler->createCollectionReference($newCollectionReference);
        }
    }
110649
public function process_block($curblock, $block) {

        $curtype = $this->getpath($curblock,
                array('@', 'class'),
                '', true);

        switch($curtype) {
            case 'FORMATTED_TEXT_BLOCK':
                $text = $this->getpath($curblock,
                        array('#', 'material', 0, '#', 'mat_extension', 0, '#', 'mat_formattedtext', 0, '#'),
                        '', true);
                $block->text = $this->strip_applet_tags_get_mathml($text);
                break;
            case 'FILE_BLOCK':
                $block->filename = $this->getpath($curblock,
                        array('#', 'material', 0, '#'),
                        '', true);
                if ($block->filename != '') {
                    // TODO : determine what to do with the file's content.
                    $this->error(get_string('filenothandled', 'qformat_blackboard_six', $block->filename));
                }
                break;
            case 'Block':
                if ($this->getpath($curblock,
                        array('#', 'material', 0, '#', 'mattext'),
                        false, false)) {
                    $block->text = $this->getpath($curblock,
                            array('#', 'material', 0, '#', 'mattext', 0, '#'),
                            '', true);
                } else if ($this->getpath($curblock,
                        array('#', 'material', 0, '#', 'mat_extension', 0, '#', 'mat_formattedtext'),
                        false, false)) {
                    $block->text = $this->getpath($curblock,
                            array('#', 'material', 0, '#', 'mat_extension', 0, '#', 'mat_formattedtext', 0, '#'),
                            '', true);
                } else if ($this->getpath($curblock,
                        array('#', 'response_label'),
                        false, false)) {
                    // This is a response label block.
                    $subblocks = $this->getpath($curblock,
                            array('#', 'response_label', 0),
                            array(), false);
                    if (!isset($block->ident)) {

                        if ($this->getpath($subblocks,
                                array('@', 'ident'), '', true)) {
                            $block->ident = $this->getpath($subblocks,
                                array('@', 'ident'), '', true);
                        }
                    }
                    foreach ($this->getpath($subblocks,
                            array('#', 'flow_mat'), array(), false) as $subblock) {
                        $this->process_block($subblock, $block);
                    }
                } else {
                    if ($this->getpath($curblock,
                                array('#', 'flow_mat'), false, false)
                            || $this->getpath($curblock,
                                array('#', 'flow'), false, false)) {
                        if ($this->getpath($curblock,
                                array('#', 'flow_mat'), false, false)) {
                            $subblocks = $this->getpath($curblock,
                                    array('#', 'flow_mat'), array(), false);
                        } else if ($this->getpath($curblock,
                                array('#', 'flow'), false, false)) {
                            $subblocks = $this->getpath($curblock,
                                    array('#', 'flow'), array(), false);
                        }
                        foreach ($subblocks as $sblock) {
                            // This will recursively grab the sub blocks which should be of one of the other types.
                            $this->process_block($sblock, $block);
                        }
                    }
                }
                break;
            case 'LINK_BLOCK':
                // Not sure how this should be included?
                $link = $this->getpath($curblock,
                            array('#', 'material', 0, '#', 'mattext', 0, '@', 'uri'), '', true);
                if (!empty($link)) {
                    $block->link = $link;
                } else {
                    $block->link = '';
                }
                break;
        }
        return $block;
    }
1113963
public function pass($branchNumber, StatementsBlock $block)
    {
        $this->passStatementBlock($branchNumber, $block->getStatements());
        $this->branches[$branchNumber] = 0;
    }
449107
public final void block() throws RecognitionException {
		int block_StartIndex = input.index();


		            increaseLevel();
		        
		try {
			if ( state.backtracking>0 && alreadyParsedRule(input, 80) ) { return; }

			// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:693:5: ( '{' ( blockStatement )* '}' )
			// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:693:7: '{' ( blockStatement )* '}'
			{
			match(input,121,FOLLOW_121_in_block2769); if (state.failed) return;
			// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:693:11: ( blockStatement )*
			loop104:
			while (true) {
				int alt104=2;
				int LA104_0 = input.LA(1);
				if ( ((LA104_0 >= CharacterLiteral && LA104_0 <= DecimalLiteral)||LA104_0==ENUM||LA104_0==FloatingPointLiteral||(LA104_0 >= HexLiteral && LA104_0 <= Identifier)||(LA104_0 >= OctalLiteral && LA104_0 <= StringLiteral)||LA104_0==29||LA104_0==36||(LA104_0 >= 40 && LA104_0 <= 41)||(LA104_0 >= 44 && LA104_0 <= 45)||(LA104_0 >= 52 && LA104_0 <= 53)||LA104_0==58||(LA104_0 >= 63 && LA104_0 <= 67)||(LA104_0 >= 70 && LA104_0 <= 73)||(LA104_0 >= 75 && LA104_0 <= 77)||(LA104_0 >= 79 && LA104_0 <= 80)||(LA104_0 >= 82 && LA104_0 <= 83)||(LA104_0 >= 85 && LA104_0 <= 87)||LA104_0==90||(LA104_0 >= 92 && LA104_0 <= 98)||(LA104_0 >= 100 && LA104_0 <= 112)||(LA104_0 >= 114 && LA104_0 <= 121)||LA104_0==126) ) {
					alt104=1;
				}

				switch (alt104) {
				case 1 :
					// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:693:11: blockStatement
					{
					pushFollow(FOLLOW_blockStatement_in_block2771);
					blockStatement();
					state._fsp--;
					if (state.failed) return;
					}
					break;

				default :
					break loop104;
				}
			}

			match(input,125,FOLLOW_125_in_block2774); if (state.failed) return;
			}

			if ( state.backtracking==0 ) {
			            if( localVariableLevel <= 1 ) {
			                // this is the top level block, so set the top level declarations
			                rootBlockDescr.setInScopeLocalVars( getLocalDeclarations() );
			            }
			            decreaseLevel();
			        }
		}
		catch (RecognitionException re) {
			reportError(re);
			recover(input,re);
		}
		finally {
			// do for sure before leaving
			if ( state.backtracking>0 ) { memoize(input, 80, block_StartIndex); }

		}
	}
849442
public function publish(BlockInterface $block)
    {
        if (!($block instanceof BlockOwnerInterface) && !$block->isShared()) {
            throw new \Exception ('Can only publish blocks of type BlockOwner or shared');
        }

        $this->killLoggableListener();
        $this->killSoftDeletableListener();

        $owned = $this->findByOwner($block);
        $version = $this->getNewVersion($block);

        foreach ($owned as $descendant) {
            $this->revertSingle($descendant, $version);
            $descendant->setPublish(true);
            $descendant->setVersion($version);
            $descendant->setRootVersion($version);
            $this->save($descendant);
        }

        $this->revertSingle($block, $version);
        $block->setPublish(true);
        $block->setVersion($version);
        $block->setRootVersion($version);
        $this->save($block);
    }
62251
public function offsetSet($key, $value)
    {
        if (is_null($key)) {
            $this->blocks[] = $value;
        } else {
            $this->blocks[$key] = $value;
        }
    }
235092
public function getService($block)
    {
        $blockType = ($block instanceof BlockInterface) ? $block->getBlockType() : $block;
        if (!isset($this->services[$blockType])) {
            throw new \Exception(sprintf("No BlockService available by the alias %s, available: %s", $blockType, implode(', ', array_keys($this->services))));
        }

        return $this->services[$blockType];
    }
797306
public function getBlock($key)
    {
        if (($find = $this->searchBlock($key)) !== null) {
            return $find;
        }

        throw new \Exception(htmlspecialchars("The block $key does not exist."));
    }
1188799
public function run($blocking = false): int
    {
        $logHandler = new StreamHandler(new ResourceOutputStream(\STDOUT));
        $logHandler->setFormatter(new ConsoleFormatter);
        $logger = new Logger('server');
        $logger->pushHandler($logHandler);

        $pid        = null;
        $lambdaLoop = function () use ($blocking, $logger, &$pid) {
            $logger->debug("Process command: {$this->process->getCommand()}");

            $this->process->start();

            $this->process->getStdout()->read()->onResolve(function (\Throwable $reason = null, $value) {
                $this->output .= $value;
            });

            $this->process->getStderr()->read()->onResolve(function (\Throwable $reason = null, $value) {
                $this->stderr .= $value;
            });

            if ($blocking) {
                $exitCode = yield $this->process->join();
                $this->setExitCode($exitCode);
                $logger->debug("Exit code: {$this->getExitCode()}");
            }

            $pid = yield $this->process->getPid();

            if ($blocking) {
                if ($this->getExitCode() !== 0) {
                    throw new \Exception("PactPHP Process returned non-zero exit code: {$this->getExitCode()}");
                }
            }

            Loop::stop();
        };

        Loop::run($lambdaLoop);

        return $pid;
    }
159919
protected function check_known_block_type($blockname, $includeinvisible = false) {
        if (!$this->is_known_block_type($blockname, $includeinvisible)) {
            if ($this->is_known_block_type($blockname, true)) {
                throw new coding_exception('Unknown block type ' . $blockname);
            } else {
                throw new coding_exception('Block type ' . $blockname . ' has been disabled by the administrator.');
            }
        }
    }
1182718
public function appendBlock($block, $indent = 0)
    {
        $lines = explode("\n", $block);
        foreach ($lines as $line) {
            $this->appendLine($line, $indent);
        }
    }
1345702
public function extract($object)
    {
        if (!$object instanceof UserBlock) {
            throw new \Exception('$object must be an instance of UserBlock');
        }
        /* @var $object UserBlock */
        $data = parent::extract($object);

        return $data;
    }
887100
public function isIn($block): bool
    {
        if (!$block instanceof IPBlock) {
            $block = IPBlock::create($block);
        }

        return $block->contains($this);
    }
310110
public function addChild(Block $block)
    {
        $type = get_class($block);
        if (!isset($this->children[$type])) {
            $this->children[$type] = array();
        }
        $this->children[$type][] = $block;
    }
1315335
private function block($textblock)
    {
        $length = 0;
        foreach ($textblock as $line) {
            $n = strlen($line);
            if ($length < $n) {
                $length = $n;
            }
        }

        foreach ($textblock as $i => $line) {
            $textblock[$i] .= str_repeat(' ', $length - strlen($line));
        }

        return $textblock;
    }