Add Emsctipted implementation for algorithm.

This commit is contained in:
Oleg Sh 2021-12-08 21:48:50 +02:00
parent 056d4fce12
commit 513fb80a0c
15 changed files with 128 additions and 70 deletions

View File

@ -154,10 +154,10 @@ function BaseAlgorithmEx(graph, app)
// inheritance.
BaseAlgorithmEx.prototype = Object.create(BaseAlgorithm.prototype);
BaseAlgorithmEx.prototype.CalculateAlgorithm = function(queryString, resultCallback, ignoreSeparateNodes = false)
BaseAlgorithmEx.prototype.CalculateAlgorithm = function(algorithmName, otherParams, resultCallback, ignoreSeparateNodes = false)
{
if (location.hostname === "localhost" || location.hostname === "127.0.0.1")
console.log(queryString);
console.log(algorithmName + " " + otherParams);
var graph = this.graph;
var ignoreNodes = {};
@ -175,14 +175,7 @@ BaseAlgorithmEx.prototype.CalculateAlgorithm = function(queryString, resultCallb
var xml = creator.GetXMLString();
console.log(xml);
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/GraphCGI.exe?" + queryString,
data: xml,
dataType: "text",
})
.done(function( msg )
{
var processResult = function (msg) {
console.log(msg);
$('#debug').text(msg);
xmlDoc = $.parseXML( msg );
@ -249,12 +242,35 @@ BaseAlgorithmEx.prototype.CalculateAlgorithm = function(queryString, resultCallb
console.log(result);
resultCallback(pathObjects, properties, result);
};
if (this.app.isSupportEmscripten()) {
console.log("Use Emscripten");
var delimiter = "<s\\emscript_split\\s>";
var processData = algorithmName + delimiter + xml +
delimiter + "report" + delimiter + "xml";
otherParams.forEach ( (param) => processData += delimiter + param.name + delimiter + param.value);
var res = this.app.processEmscripten(processData);
processResult(res);
} else {
console.log("Use new CGI");
var queryString = algorithmName + "=cgiInput&report=xml";
otherParams.forEach ( (param) => queryString += "&" + param.name + "=" + param.value);
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/GraphCGI.exe?" + queryString,
data: xml,
dataType: "text",
})
.done(function( msg )
{
processResult(msg);
});
}
return true;
}
BaseAlgorithmEx.prototype.GetNodesPath = function(array, start, count)
{
var res = [];

View File

@ -53,6 +53,7 @@ function Application(document, window)
this.defaultVertexSize = null;
this.defaultEdgeWidth = null;
this.processEmscriptenFunction = null;
};
// List of graph.
@ -1840,3 +1841,19 @@ Application.prototype.ResetEdgeWidth = function()
this.graph.edges[i].model.width = this.GetDefaultEdgeWidth();
}
}
Application.prototype.setEmscripten = function(processFunction)
{
this.processEmscriptenFunction = processFunction;
console.log("Emscripten set");
}
Application.prototype.isSupportEmscripten = function ()
{
return this.processEmscriptenFunction != null;
}
Application.prototype.processEmscripten = function (inputData)
{
return this.processEmscriptenFunction(inputData);
}

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -1,5 +1,6 @@
var SiteDir = "";
var DisableEmscripted = false;
var application = new Application(document, window);
@ -686,6 +687,20 @@ $(document).ready(function ()
document.getElementById('canvas').addEventListener("touchend", touchHandler, true);
document.getElementById('canvas').addEventListener("touchcancel", touchHandler, true);
// Try load emscripted implementation
var isMobile = navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i);
if (!isMobile && !DisableEmscripted) {
const jsScript = document.createElement('script');
jsScript.src = 'script/Graphoffline.Emscripten.js';
document.body.appendChild(jsScript);
jsScript.addEventListener('load', () => {
Module['onRuntimeInitialized'] = onRuntimeInitialized;
var process = Module.cwrap('ProcessAlgorithm', 'string', ['string']);
function onRuntimeInitialized() {
application.setEmscripten(process);
}
});
}
/*
$(document).ready(function(){
//set up some basic options for the feedback_me plugin

View File

@ -34,7 +34,7 @@ FindEulerianLoop.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("elloop=cgiInput&report=xml", function (pathObjects, properties, results)
this.CalculateAlgorithm("elloop", [], function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
}, true);

View File

@ -34,7 +34,7 @@ FindEulerianPath.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("elpath=cgiInput&report=xml", function (pathObjects, properties, results)
this.CalculateAlgorithm("elpath", [], function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
}, true);

View File

@ -46,7 +46,11 @@ FindAllPathes.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("prnpaths=cgiInput&start=" + this.firstObject.id + "&finish=" + this.secondObject.id + "&report=xml", function (pathObjects, properties, results)
this.CalculateAlgorithm("prnpaths", [
{name: "start", value: this.firstObject.id},
{name: "finish", value: this.secondObject.id}
],
function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});

View File

@ -46,7 +46,9 @@ FindShortPatchsFromOne.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("blf=cgiInput&start=" + this.firstObject.id + "&report=xml", function (pathObjects, properties, results)
this.CalculateAlgorithm("blf", [
{name: "start", value : this.firstObject.id}
], function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});

View File

@ -76,19 +76,6 @@ FloidAlgorithm.prototype.result = function(resultCallback)
this.app.redrawGraph();
return result;
/*
if (this.firstObject && this.secondObject)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("dsp=cgiInput&start=" + this.firstObject.id + "&finish=" + this.secondObject.id + "&report=xml", function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});
}
return null;
*/
}

View File

@ -34,7 +34,7 @@ FindHamiltonianLoop.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("hamloop=cgiInput&report=xml", function (pathObjects, properties, results)
this.CalculateAlgorithm("hamloop", [], function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});

View File

@ -34,7 +34,7 @@ FindHamiltonianPath.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("hampath=cgiInput&report=xml", function (pathObjects, properties, results)
this.CalculateAlgorithm("hampath", [], function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});

View File

@ -53,7 +53,14 @@ IsomorphismCheck.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("isocheck=cgiInput&graph1=" + this.getGraphEdges(this.firstGraph) + "&graph2=" + this.getGraphEdges(this.secondGraph) + "&report=xml" + (this.searchSubGraphs ? "&searchSubgraphs=true": ""), function (pathObjects, properties, results)
var params = [
{name : "graph1", value: this.getGraphEdges(this.firstGraph)},
{name : "graph2", value: this.getGraphEdges(this.secondGraph)},
];
if (this.searchSubGraphs) {
params.push({name: "searchSubgraphs", value: true});
}
this.CalculateAlgorithm("isocheck", params, function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});

View File

@ -47,7 +47,12 @@ FindMaxFlow.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("mfpr=cgiInput&source=" + this.firstObject.id + "&drain=" + this.secondObject.id + "&report=xml", function (pathObjects, properties, results)
this.CalculateAlgorithm("mfpr",
[
{name: "source", value: this.firstObject.id},
{name: "drain", value: this.secondObject.id}
],
function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});

View File

@ -43,7 +43,11 @@ FindShortPathNew.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("dsp=cgiInput&start=" + this.firstObject.id + "&finish=" + this.secondObject.id + "&report=xml", function (pathObjects, properties, results)
this.CalculateAlgorithm("dsp",
[
{name: "start", value: this.firstObject.id},
{name: "finish", value: this.secondObject.id}
], function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});