Skip to content

Commit

Permalink
Prolog and R dataframe formats (#697)
Browse files Browse the repository at this point in the history
* Added the Prolog format.

* fixed a wrong format relic.

* Added the R dataframe format.

---------

Co-authored-by: M. Falda <[email protected]>
Co-authored-by: Niklas Laxström <[email protected]>
  • Loading branch information
3 people committed May 4, 2024
1 parent a87854f commit e0d36b9
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 8 deletions.
4 changes: 3 additions & 1 deletion DefaultSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
'interquartilerange',
'interquartilerange.exc',
'mode',
'interquartilemean'
'interquartilemean',
'prolog',
'dataframe'
// Boilerplate
// Enable access to the format identifier
// 'boilerplate',
Expand Down
6 changes: 4 additions & 2 deletions SemanticResultFormats.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ public static function onExtensionFunction() {
'incoming' => 'SRFIncoming',
'media' => 'SRF\MediaPlayer',
'datatables' => 'SRF\DataTables',
'carousel' => 'SRF\Carousel',
'gantt' => 'SRF\Gantt\GanttPrinter'
'carousel' => 'SRF\Carousel',
'gantt' => 'SRF\Gantt\GanttPrinter',
'prolog' => 'SRF\Prolog\PrologPrinter',
'dataframe' => 'SRF\dataframe\DataframePrinter',
];

$formatAliases = [
Expand Down
208 changes: 208 additions & 0 deletions formats/Prolog/PrologPrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

namespace SRF\Prolog;

use SMW\FileExportPrinter;
use SMWQueryResult;

/**
* @author Marco Falda
* @since 3.2
*/
class PrologPrinter extends FileExportPrinter {

const HEADER_ROW_OFFSET = 1;

protected $fileFormats = [
'pl' => [
'writer' => 'pl',
'mimetype' => 'text/prolog',
'extension' => '.pl',
],
'pro' => [
'writer' => 'pl',
'mimetype' => 'text/prolog',
'extension' => '.pro',
],
];

protected $fileFormat;

/**
* Output a human readable label for this printer.
*
* @see ResultPrinter::getName
*
* {@inheritDoc}
*/
public function getName() {
return $this->msg( 'srf-printername-prolog' );
}

/**
* @see ExportPrinter::getMimeType()
*
* @since 1.8
*
* @param SMWQueryResult $queryResult
*
* @return string
*/
public function getMimeType( SMWQueryResult $queryResult ) {
return $this->fileFormat[ 'mimetype' ];
}

/**
* @see ExportPrinter::getFileName
*
* @param SMWQueryResult $queryResult
*
* @return string
*/
public function getFileName( SMWQueryResult $queryResult ) {
return ( $this->params[ 'filename' ] ?: base_convert( uniqid(), 16, 36 ) ) . $this->fileFormat[ 'extension' ];
}

/**
* @see ExportPrinter::outputAsFile
*
* @param SMWQueryResult $queryResult
* @param array $params
*/
public function outputAsFile( SMWQueryResult $queryResult, array $params )
{
if ( array_key_exists( 'fileformat', $params) && array_key_exists( $params[ 'fileformat' ]->getValue(), $this->fileFormats )) {
$this->fileFormat = $this->fileFormats[ $params[ 'fileformat' ]->getValue() ];
} else {
$this->fileFormat = $this->fileFormats[ 'pl' ];
}

parent::outputAsFile( $queryResult, $params );
}

/**
* Defines the list of available parameters to an individual result
* printer.
*
* @see ResultPrinter::getParamDefinitions
*
* {@inheritDoc}
*/
public function getParamDefinitions( array $definitions ) {
$params = parent::getParamDefinitions( $definitions );

$definitions[ 'searchlabel' ]->setDefault( wfMessage( 'srf-prolog-link' )->inContentLanguage()->text() );

$params[ 'filename' ] = [
'type' => 'string',
'name' => 'filename',
'default' => '',
'message' => 'srf-paramdesc-prolog-filename',
];

$params[ 'fileformat' ] = [
'type' => 'string',
'name' => 'fileformat',
'default' => 'pl',
'tolower' => true,
'message' => 'srf-paramdesc-prolog-fileformat',
];

$params[ 'pname' ] = [
'type' => 'string',
'name' => 'pname',
'default' => 'predicate',
'tolower' => true,
'message' => 'srf-paramdesc-prolog-pname',
];

$params[ 'navalue' ] = [
'type' => 'string',
'name' => 'navalue',
'default' => "'NA'",
'message' => 'srf-paramdesc-prolog-navalue',
];

return $params;
}

/**
* This method gets the query result object and is supposed to return
* whatever output the format creates. For example, in the list format, it
* goes through all results and constructs an HTML list, which is then
* returned. Looping through the result object is somewhat complex, and
* requires some understanding of the `QueryResult` class.
*
* @see ResultPrinter::getResultText
*
* {@inheritDoc}
*/
protected function getResultText( SMWQueryResult $queryResult, $outputMode ) {

if ( $outputMode === SMW_OUTPUT_FILE ) {
return $this->getResultFileContents( $queryResult );
}

$this->isHTML = ( $outputMode === SMW_OUTPUT_HTML );
return $this->getLink( $queryResult, $outputMode )->getText( $outputMode, $this->mLinker );
}

/**
* @param SMWQueryResult $queryResult
*
* @return string
*/
protected function getResultFileContents( SMWQueryResult $queryResult )
{
$res = '';
/*if ($this->params['rownames'])
$res .= 'row.names=T, ';*/

$preds = [];
while ($resultRow = $queryResult->getNext()) {

$subject = '';
$i = 0;
foreach ($resultRow as $resultField) {
if ($i === 0)
$subject = $dataItems = $resultField->getContent()[0];
else {
$propertyLabel = $resultField->getPrintRequest()->getLabel();

//$subjectLabel = $resultField->getResultSubject()->getTitle()->getFullText();
$dataItems = $resultField->getContent();

if (count($dataItems) > 1) {
$values = [];

while ($value = $resultField->getNextText(SMW_OUTPUT_FILE))
$values[] = $value;

$rowData = "['" . implode("', '", $values) . "']";
}
else {
$nextDataValue = $resultField->getNextDataValue();
if ($nextDataValue !== false) {
if ($nextDataValue instanceof \SMWNumberValue)
$rowData = $nextDataValue;
else if ($nextDataValue instanceof \SMWTimeValue)
$rowData = "'" . $nextDataValue->getISO8601Date() . "'";
else {
$nextDataValue = str_replace("'", "\'", $nextDataValue);
$rowData = "'$nextDataValue'";
}
} else
$rowData = $this->params['navalue'];
}
$preds[] = $this->params['pname'] . "('$subject', '$propertyLabel', $rowData).";
}

$i++;
}
}

$res = implode("\n", $preds);
return $res;
}

}
Loading

0 comments on commit e0d36b9

Please sign in to comment.