-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from julien-boudry/WORK/CivsFormat
Converters: Support for exporting to Civs Format
- Loading branch information
Showing
3 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CondorcetPHP\Condorcet\Tests; | ||
|
||
use CondorcetPHP\Condorcet\Election; | ||
use CondorcetPHP\Condorcet\Tools\Converters\CivsFormat; | ||
use PHPUnit\Framework\TestCase; | ||
use SplTempFileObject; | ||
|
||
class CivsFormatTest extends TestCase | ||
{ | ||
protected Election $election; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->election = new Election; | ||
$this->election->parseCandidates('A;B;C'); | ||
} | ||
|
||
public function testSimple(): void | ||
{ | ||
$this->election->parseVotes('A>B>C * 2'); | ||
$this->election->parseVotes('C>B>A * 2'); | ||
$this->election->parseVotes('B=C>A * 2'); | ||
|
||
$r = CivsFormat::createFromElection($this->election); | ||
|
||
self::assertSame( | ||
<<<'CIVS' | ||
# Candidates: A / B / C | ||
1,2,3 | ||
1,2,3 | ||
3,2,1 | ||
3,2,1 | ||
2,1,1 | ||
2,1,1 | ||
CIVS | ||
, | ||
$r | ||
); | ||
} | ||
|
||
public function testImplicit(): void | ||
{ | ||
$this->election->parseVotes('A>B'); | ||
$this->election->parseVotes('C'); | ||
$this->election->parseVotes('A=B'); | ||
|
||
$r = CivsFormat::createFromElection($this->election); | ||
|
||
self::assertSame( | ||
<<<'CIVS' | ||
# Candidates: A / B / C | ||
1,2,3 | ||
2,2,1 | ||
1,1,2 | ||
CIVS | ||
, | ||
$r | ||
); | ||
} | ||
|
||
public function testExplicit(): void | ||
{ | ||
$this->election->setImplicitRanking(false); | ||
|
||
$this->election->parseVotes('A>B'); | ||
$this->election->parseVotes('C'); | ||
$this->election->parseVotes('A=B'); | ||
|
||
$r = CivsFormat::createFromElection($this->election); | ||
|
||
self::assertSame( | ||
<<<'CIVS' | ||
# Candidates: A / B / C | ||
1,2,- | ||
-,-,1 | ||
1,1,- | ||
CIVS | ||
, | ||
$r | ||
); | ||
} | ||
|
||
public function testWeight(): void | ||
{ | ||
$this->election->parseVotes('A>B>C ^3'); | ||
|
||
// Deactivated | ||
$r = CivsFormat::createFromElection($this->election); | ||
|
||
self::assertSame( | ||
<<<'CIVS' | ||
# Candidates: A / B / C | ||
1,2,3 | ||
CIVS | ||
, | ||
$r | ||
); | ||
|
||
//A ctivated | ||
$this->election->allowsVoteWeight(true); | ||
|
||
$r = CivsFormat::createFromElection($this->election); | ||
|
||
self::assertSame( | ||
<<<'CIVS' | ||
# Candidates: A / B / C | ||
1,2,3 | ||
1,2,3 | ||
1,2,3 | ||
CIVS | ||
, | ||
$r | ||
); | ||
} | ||
|
||
public function testWriteToFile(): void | ||
{ | ||
$file = new SplTempFileObject; | ||
|
||
self::assertSame(0, $file->key()); | ||
|
||
$this->election->parseVotes('A>B; B>C'); | ||
|
||
CivsFormat::createFromElection(election: $this->election, file: $file); | ||
|
||
$file->seek(0); | ||
self::assertSame("# Candidates: A / B / C\n", $file->current()); | ||
|
||
$file->seek(1); | ||
self::assertSame("1,2,3\n", $file->current()); | ||
|
||
$file->seek(2); | ||
self::assertSame('3,1,2', $file->current()); | ||
|
||
self::assertTrue($file->eof()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/* | ||
Condorcet PHP - Election manager and results calculator. | ||
Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems. | ||
By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt) | ||
https://github.com/julien-boudry/Condorcet | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CondorcetPHP\Condorcet\Tools\Converters; | ||
|
||
use CondorcetPHP\Condorcet\Election; | ||
use CondorcetPHP\Condorcet\Dev\CondorcetDocumentationGenerator\CondorcetDocAttributes\{Description, FunctionParameter, FunctionReturn, PublicAPI}; | ||
use CondorcetPHP\Condorcet\Tools\Converters\Interface\ConverterExport; | ||
|
||
class CivsFormat implements ConverterExport | ||
{ | ||
public const COMMAND_LINE_OPTION_NAME = 'civs-format'; | ||
|
||
////// # Static Export Method ////// | ||
|
||
#[PublicAPI] | ||
#[Description("Create a CondorcetElectionFormat file from an Election object.\n")] | ||
#[FunctionReturn("If the file is not provided, it's return a CondorcetElectionFormat as string, else returning null and working directly on the file object (necessary for very large non-aggregated elections, at the risk of memory saturation).")] | ||
public static function createFromElection( | ||
#[FunctionParameter('Election with data')] | ||
Election $election, | ||
#[FunctionParameter('If provided, the function will return null and the result will be writing directly to the file instead. _Note that the file cursor is not rewinding_')] | ||
?\SplFileObject $file = null | ||
): true|string { | ||
$r = ''; | ||
|
||
$header = '# Candidates: '. implode(' / ', $election->getCandidatesListAsString()); | ||
|
||
if ($file) { | ||
$file->fwrite($header); | ||
} else { | ||
$r .= $header; | ||
} | ||
|
||
$rankedModel = []; | ||
|
||
foreach (array_keys($election->getCandidatesList()) as $candidateId) { | ||
$rankedModel[$candidateId] = null; | ||
} | ||
|
||
foreach ($election->getVotesListGenerator() as $vote) { | ||
$voteRanking = $vote->getContextualRanking($election); | ||
|
||
$ranked = $rankedModel; | ||
|
||
foreach ($voteRanking as $rank => $candidates) { | ||
foreach ($candidates as $oneCandidate) { | ||
$ranked[$election->getCandidateKey($oneCandidate)] = $rank; | ||
} | ||
} | ||
|
||
ksort($ranked, \SORT_NUMERIC); | ||
|
||
for ($weightDone = 0; $weightDone < $vote->getWeight($election); $weightDone++) { | ||
$line = "\n"; | ||
$i = 0; | ||
|
||
foreach ($ranked as $rank) { | ||
$i++ > 0 && $line.= ','; | ||
$line .= $rank ?? '-'; | ||
} | ||
|
||
if ($file) { | ||
$file->fwrite($line); | ||
} else { | ||
$r .= $line; | ||
} | ||
|
||
$line = ''; | ||
} | ||
|
||
|
||
} | ||
|
||
return ($file) ? true : $r; | ||
} | ||
} |