107 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
require_once "PicturedTask.php";
 | 
						|
 | 
						|
require_once "class/LogicFunction.php";
 | 
						|
 | 
						|
require_once "class/Utils.php";
 | 
						|
 | 
						|
require_once "class/LuaUtils.php";
 | 
						|
 | 
						|
class LogicTaskBase extends PicturedTask
 | 
						|
{
 | 
						|
    private LogicFunction $lf; // logic function
 | 
						|
    private string $output_variable; // output variable
 | 
						|
 | 
						|
    public function __construct(string $type, array $a = null)
 | 
						|
    {
 | 
						|
        parent::__construct($type, $a);
 | 
						|
 | 
						|
        if (isset($a["function"])) { // fetching from a JSON-stored object
 | 
						|
            $this->lf = LogicFunction::fromArray($a["function"]);
 | 
						|
        } else if (isset($a["expression"], $a["input_variables"])) { // building from the scratch
 | 
						|
            $this->lf = new LogicFunction($a["expression"], $a["input_variables"]);
 | 
						|
        } else {
 | 
						|
            $this->lf = new LogicFunction();
 | 
						|
        }
 | 
						|
 | 
						|
        $this->output_variable = $a["output_variable"] ?? "f";
 | 
						|
    }
 | 
						|
 | 
						|
    public function setOutputVariable(string $ovar): void {
 | 
						|
        $this->output_variable = $ovar;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getOutputVariable(): string {
 | 
						|
        return $this->output_variable;
 | 
						|
    }
 | 
						|
 | 
						|
    public function setLogicFunction(LogicFunction $lf): void {
 | 
						|
        $this->lf = $lf;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getLogicFunction(): LogicFunction {
 | 
						|
        return $this->lf;
 | 
						|
    }
 | 
						|
 | 
						|
    protected function getTTDiffCntToCA(string $ott): int {
 | 
						|
        $cans_tt = $this->getTruthTable();
 | 
						|
        $errs = 0;
 | 
						|
        for ($i = 0; $i < $this->getLogicFunction()->getNStates(); $i++) {
 | 
						|
            if (($ott[$i] ?? " ") != $cans_tt[$i]) {
 | 
						|
                $errs++;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return $errs;
 | 
						|
    }
 | 
						|
 | 
						|
    public function toArray(string $mode = "all"): array
 | 
						|
    {
 | 
						|
        $a = parent::toArray($mode);
 | 
						|
 | 
						|
        if ($mode === "all") {
 | 
						|
            $a["function"] = $this->lf->toArray();
 | 
						|
        }
 | 
						|
 | 
						|
        $a["output_variable"] = $this->output_variable;
 | 
						|
        $a["input_variables"] = $this->lf->getInputVars();
 | 
						|
 | 
						|
        return $a;
 | 
						|
    }
 | 
						|
 | 
						|
    public function randomize(): void
 | 
						|
    {
 | 
						|
        parent::randomize();
 | 
						|
 | 
						|
        if ($this->hasFlag("drawnetwork")) {
 | 
						|
            $this->setImageData($this->lf->drawNetwork($this->output_variable));
 | 
						|
            $this->setImageType("html");
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    // ---- Lua specific ----
 | 
						|
 | 
						|
    public function generateRandomFunction(array $input_vars, int $min_depth, int $max_depth): void {
 | 
						|
        $this->lf = LogicFunction::genRandom(LuaUtils::l2pA($input_vars), $min_depth, $max_depth);
 | 
						|
    }
 | 
						|
 | 
						|
    public function generateRandomDF(array $input_vars): void {
 | 
						|
        $this->lf = LogicFunction::genRandomDNF(LuaUtils::l2pA($input_vars));
 | 
						|
    }
 | 
						|
 | 
						|
    public function setLogicFunctionExpr(string $expr, array $input_vars = []): void {
 | 
						|
        $this->lf = new LogicFunction($expr, LuaUtils::l2pA($input_vars));
 | 
						|
    }
 | 
						|
 | 
						|
    public function getLogicFunctionExpr(string $fmt = "verilog_bitwise"): string {
 | 
						|
        return $this->lf->getExpression($fmt);
 | 
						|
    }
 | 
						|
 | 
						|
    public function getLogicFunctionDNF(): string {
 | 
						|
        return $this->lf->toDNF();
 | 
						|
    }
 | 
						|
 | 
						|
    public function getTruthTable(): string {
 | 
						|
        return $this->lf->getTruthTable();
 | 
						|
    }
 | 
						|
} |