Skip to content

Commit

Permalink
Merge pull request #81 from okgreece/develop/1
Browse files Browse the repository at this point in the history
Develop/1
  • Loading branch information
skarampatakis authored Apr 11, 2018
2 parents b1d9af1 + d8f6a84 commit 7535503
Show file tree
Hide file tree
Showing 17 changed files with 796 additions and 261 deletions.
126 changes: 5 additions & 121 deletions app/Http/Controllers/CreatelinksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace App\Http\Controllers;

use App\Models\OntologyTypeDriver;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Project;
use App\File;
use DB;
use Cache;
use Storage;
use DB;
use App\Notification;
use App\Project;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class CreatelinksController extends Controller {

Expand All @@ -26,7 +23,6 @@ public function __construct() {
* @return Response
*/
public function index(Project $project) {
session_start();
$this->cacheOntologies();
$nameSource = implode("_", ["project", $project->id, "source", $project->source->id, '']);
$nameTarget = implode("_", ["project", $project->id, "target", $project->target->id, '']);
Expand Down Expand Up @@ -68,9 +64,7 @@ public function json_serializer($file) {
$jsonfile = Storage::disk('public')->get('json_serializer/' . $file);
}
catch (\Exception $ex){
$newfile = explode("_", explode(".", $file)[0]);
$this->D3_convert(Project::find($newfile[1]), $newfile[2], $newfile[4]);
$jsonfile = Storage::disk('public')->get('json_serializer/' . $file);
dd($ex);
}
return (new Response($jsonfile, 200))
->header('Content-Type', 'application/json');
Expand Down Expand Up @@ -141,114 +135,4 @@ public function getGroups()
->get();
return $select;
}

public function D3_convert(Project $project, $dump, $orderBy = null) {
$type = "SKOS";

$file = $project->$dump;
/*
* Read the graph
*/
$graph = $file->cacheGraph();
/*
* Get the parent node
*/
$driver = OntologyTypeDriver::Factory($type);
$parents = $graph->allOfType($driver::root);
/*
* Iterate through all parents
*/
if($dump === "source"){
$score = Cache::get("scores_graph_project" . $project->id);
}
else{
$score = null;
}
foreach ($parents as $parent) {
/*
* Create Root Entry
*/
$name = $this->label($graph, $parent);
$JSON['name'] = "$name";
$JSON['url'] = urlencode($parent);
$children = $this->find_children($graph, $driver::firstLevelPath, $parent, $orderBy, $score, $JSON, $type);
if (sizeOf($children) == 0){
$children = $this->find_children($graph, $driver::inverseFirstLevelPath, $parent, $orderBy, $score, $JSON, $type);
}
$JSON['children'] = $orderBy === null ? $children : collect($children)->sortBy($orderBy)->values()->toArray();
}

/*
* create JSON file
*/
$name = implode("_", ["project", $project->id, $dump, $file->id, $orderBy]);
$filename = 'json_serializer/' . $name . ".json";
Storage::disk('public')->put($filename, json_encode($JSON));
return $filename;
}

function find_children(\EasyRdf_Graph $graph, $hierarchic_link, $parent_url, $orderBy = null, $score = null, $type) {
$driver = OntologyTypeDriver::Factory($type);
$children = $graph->allResources($parent_url, $hierarchic_link);
$counter = 0;
$myJSON = [];

foreach ($children as $child) {
$name = $this->label($graph, $child);
$myJSON[]["name"] = "$name";
if($score !== null){
$suggestions = count($score->resourcesMatching("http://knowledgeweb.semanticweb.org/heterogeneity/alignment#entity1", $child));
}
else{
$suggestions = 0;
}

$myJSON[$counter]['suggestions'] = $suggestions;
$myJSON[$counter]['url'] = urlencode($child);
$children = $this->find_children($graph, $driver::secondLevelPath, $child, $orderBy, $score, $myJSON, $type);
if (sizeOf($children) == 0){
$children = $this->find_children($graph, $driver::inverseSecondLevelPath, $child, $orderBy, $score, $myJSON, $type);
}

$myJSON[$counter]['children'] = $orderBy === null ? $children : collect($children)->sortBy($orderBy)->values()->toArray();
$counter++;
}
return $myJSON;
}

public function parseScore(Project $project, $user_id){
$old_score = storage_path() . "/app/projects/project" . $project->id . "/" . "score.nt";
$score_filepath = storage_path() . "/app/projects/project" . $project->id . "/" . "score_project" . $project->id . ".nt";
try{
$command = 'rapper -i rdfxml -o ntriples ' . $old_score . ' > ' . $score_filepath;
$out = [];
logger($command);
exec( $command, $out);
logger(var_dump($out));
Notification::create([
"message" => 'Converted Score Graph...',
"user_id" => $user_id,
"project_id" => $project->id,
"status" => 2,
]);
}
catch(\Exception $ex){
logger($ex);
}
try{
$scores = new \EasyRdf_Graph;
$scores->parseFile($score_filepath, "ntriples");

Notification::create([
"message" => 'Parsed and Stored Graphs!!!',
"user_id" => $user_id,
"project_id" => $project->id,
"status" => 2,
]);
} catch (\Exception $ex) {
logger($ex);
}
logger("converting files");
Cache::forever("scores_graph_project" . $project->id, $scores);
}
}
59 changes: 9 additions & 50 deletions app/Http/Controllers/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Illuminate\Http\Request;

use App\File;
use App\User;
use Auth;

class FileController extends Controller
{
Expand All @@ -21,21 +19,10 @@ public function __construct()
* @return Response
*/
public function mygraphs()
{
$user = Auth::user();
$files = $this->ownGraphs($user)->merge($this->publicGraphs($user));
return view('files.index',["user"=>$user, "files"=>$files]);
}

public function ownGraphs(User $user){
$files = File::where("user_id", $user->id)->withCount("projects")->with("projects")->get();
return $files;
}

public function publicGraphs(User $user){
$files = File::where("public", true)->where("user_id", "!=", $user->id)->withCount("projects")->with("projects")->get();
return $files;
}
{
$user = auth()->user();
return view('files.index',["user"=>$user, "files"=>$user->userGraphs()]);
}

public function store()
{
Expand All @@ -44,7 +31,7 @@ public function store()
'resource' => 'file',
'resource_url' => 'url',
])->validate();
if($input["resource_url"]){
if($input["resource_url"] !== null){
$input["resource"] = $input["resource_url"];
}
$file = File::create( $input );
Expand Down Expand Up @@ -72,8 +59,6 @@ public function update()
$file->save();

return redirect()->route('mygraphs')->with('notification', 'File updated!!!');


}

public function destroy(File $file)
Expand All @@ -88,34 +73,8 @@ public function download(Request $request, File $file){
}

public function parse(File $file)
{
/*
* Read the graph
*/
try{
if($file->filetype != 'ntriples'){
FileController::convert($file);
$file->cacheGraph();
}
else{
$file->cacheGraph();
}
return redirect()->route('mygraphs')->with('notification', 'Graph Parsed!!!');

} catch (\Exception $ex) {
$file->parsed = false;
$file->save();
error_log($ex);
return redirect()->route('mygraphs')->with('error', 'Failed to parse the graph. Please check the logs.' . $ex);
}
}

public function convert(File $file){
$command = 'rapper -i ' . $file->filetype . ' -o ntriples ' . $file->resource->path() . ' > ' . $file->resource->path(). '.nt';
$out = [];
logger($command);
exec( $command, $out);
logger(var_dump($out));
return;
}
{
dispatch(new \App\Jobs\Parse($file, auth()->user()));
return redirect()->back()->with('notification', 'Parsing Job Dispatched, check your logs.');
}
}
90 changes: 83 additions & 7 deletions app/Jobs/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace App\Jobs;


use Cache;
use Storage;
use App\Project;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Project;
use App\Models\OntologyTypeDriver;

class Convert extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;

use \App\RDFTrait;

protected $project,$user, $dump;
/**
Expand All @@ -37,9 +41,8 @@ public function handle()
"user_id" => $this->user,
"project_id" => $this->project->id,
"status" => 2,
]);
$controller = new \App\Http\Controllers\CreatelinksController();
$controller->D3_convert($this->project, $this->dump);
]);
$this->D3_convert($this->project, $this->dump);

if($this->dump === "target"){
\App\Notification::create([
Expand All @@ -48,12 +51,85 @@ public function handle()
"project_id" => $this->project->id,
"status" => 3,
]);

$this->project->processed = 1;
$this->project->save();
}
else{
dispatch(new \App\Jobs\Convert($this->project, $this->user, "target"));
dispatch(new Convert($this->project, $this->user, "target"));
}
}

public function D3_convert(Project $project, $dump, $orderBy = null) {
$type = "SKOS";
$file = $project->$dump;
/*
* Get the parent node
*/
$driver = OntologyTypeDriver::Factory($type);
/*
* Read the graph
*/
$graph = $file->cacheGraph();

$parents = $graph->allOfType($driver::root);
/*
* Iterate through all parents
*/
if($dump === "source"){
$score = Cache::get("scores_graph_project" . $project->id);
}
else{
$score = null;
}
foreach ($parents as $parent) {
/*
* Create Root Entry
*/
$name = $this->label($graph, $parent);
$JSON['name'] = "$name";
$JSON['url'] = urlencode($parent);
$children = $this->find_children($graph, $driver::firstLevelPath, $parent, $orderBy, $score, $JSON, $type);
if (sizeOf($children) == 0){
$children = $this->find_children($graph, $driver::inverseFirstLevelPath, $parent, $orderBy, $score, $JSON, $type);
}
$JSON['children'] = $orderBy === null ? $children : collect($children)->sortBy($orderBy)->values()->toArray();
}

/*
* create JSON file
*/
$name = implode("_", ["project", $project->id, $dump, $file->id, $orderBy]);
$filename = 'json_serializer/' . $name . ".json";
Storage::disk('public')->put($filename, json_encode($JSON));
return $filename;
}

function find_children(\EasyRdf_Graph $graph, $hierarchic_link, $parent_url, $orderBy = null, $score = null, $type) {
$driver = OntologyTypeDriver::Factory($type);
$children = $graph->allResources($parent_url, $hierarchic_link);
$counter = 0;
$myJSON = [];

foreach ($children as $child) {
$name = $this->label($graph, $child);
$myJSON[]["name"] = "$name";
if($score !== null){
$suggestions = count($score->resourcesMatching("http://knowledgeweb.semanticweb.org/heterogeneity/alignment#entity1", $child));
}
else{
$suggestions = 0;
}

$myJSON[$counter]['suggestions'] = $suggestions;
$myJSON[$counter]['url'] = urlencode($child);
$children = $this->find_children($graph, $driver::secondLevelPath, $child, $orderBy, $score, $myJSON, $type);
if (sizeOf($children) == 0){
$children = $this->find_children($graph, $driver::inverseSecondLevelPath, $child, $orderBy, $score, $myJSON, $type);
}

$myJSON[$counter]['children'] = $orderBy === null ? $children : collect($children)->sortBy($orderBy)->values()->toArray();
$counter++;
}
return $myJSON;
}
}
Loading

0 comments on commit 7535503

Please sign in to comment.