51 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
require_once "LogicFunction.php";
 | 
						|
 | 
						|
class LuaUtils
 | 
						|
{
 | 
						|
    public static function l2pA(array|null $la): array {
 | 
						|
        if ($la === null) {
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
 | 
						|
        $r = [];
 | 
						|
        $i = 0;
 | 
						|
        foreach ($la as $v) {
 | 
						|
            $r[$i++] = $v;
 | 
						|
        }
 | 
						|
        return $r;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function p2lA(array|null $pa): array {
 | 
						|
        if ($pa === null) {
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
 | 
						|
        $r = [];
 | 
						|
        $i = 1;
 | 
						|
        foreach ($pa as $v) {
 | 
						|
            $r[$i++] = $v;
 | 
						|
        }
 | 
						|
        return $r;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function registerLuaLibs(LuaSandbox &$sandbox): void {
 | 
						|
        self::registerLogicUtils($sandbox);
 | 
						|
    }
 | 
						|
 | 
						|
    public static function registerLogicUtils(LuaSandbox &$sandbox): void {
 | 
						|
        $sandbox->registerLibrary("logic", [
 | 
						|
            "extend" => fn() => [ call_user_func(array(LogicUtils::class, "extend"), ...func_get_args()) ],
 | 
						|
            "complement" => fn() => [ call_user_func(array(LogicUtils::class, "complement"), ...func_get_args()) ],
 | 
						|
            "changeRepresentation" => fn() => [ call_user_func(array(LogicUtils::class, "changeRepresentation"), ...func_get_args()) ],
 | 
						|
            "genRandomLogicFunction" => fn($iv, $mind, $maxd) => [ call_user_func(array(LogicFunction::class, "genRandom"), self::l2pA($iv), $mind, $maxd)->getExpression() ],
 | 
						|
            "genRandomLogicFunctionDNF" => fn($iv) => [ call_user_func(array(LogicFunction::class, "genRandomDNF"), self::l2pA($iv))->getExpression() ],
 | 
						|
            "isLogicFunctionValid" => fn($expr) => [ (new LogicFunction($expr ?? "0"))->isValid() ],
 | 
						|
            "isLogicFunctionADNF" => fn($iv, $expr) => [ call_user_func(array(LogicFunction::class, "isCorrectDNF"), self::l2pA($iv), $expr ?? "0") ],
 | 
						|
            "collectVariablesFromLogicFunction" => fn($expr) => call_user_func(array(LogicFunction::class, "collectVariables"), $expr ?? ""),
 | 
						|
            "convertLogicFunctionToTruthTable" => fn($expr, $iv) => [ (new LogicFunction($expr ?? "0", self::l2pA($iv)))->getTruthTable() ],
 | 
						|
            "drawLogicFunction" => fn($expr, $iv, $ov) => [ (new LogicFunction($expr ?? "0", self::l2pA($iv)))->drawNetwork($ov) ],
 | 
						|
        ]);
 | 
						|
    }
 | 
						|
} |