8

Does anyone know how one could access fuzzy logic from javascript? I have a good fuzzy library in Java and C++, but I wanted something I could run from HTML5/javascript.

Androider
  • 21,125
  • 36
  • 99
  • 158

3 Answers3

7

There are two projects avaliable:

  1. https://github.com/marcolanaro/JS-Fuzzy - ready to use in browser

  2. https://github.com/sebs/node-fuzzylogic - nodejs module, could be used in browser

Igor S.
  • 3,332
  • 1
  • 25
  • 33
0

My answer for those who want to do in NodeJS as it is familiar with javascript

Please use an awesome nodejs-java, and a miracle jFuzzylite library which is written in Java.

node-java: https://www.npmjs.com/package/java

Fuzzylite: http://www.fuzzylite.com/. It provides jfuzzylite.jar

I've created a membership function in Matlab membership_function_pn.fis, It has two inputs and one output. Type mfedit on the Matlab command interface, it will appear an FIS editor where you can easily make your fuzzy function.

The following is my code which did the work!! (To understand how the code works in Nodejs, do a practice in Java with jfuzzylite.jar first).

var java = require("java");
var fs = require("fs");
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
java.classpath.push("jfuzzylite.jar");


var matlabString = fs.readFileSync("dataMatlab/membership_function_pn.fis", 'utf8');
var FisImporter = java.newInstanceSync("com.fuzzylite.imex.FisImporter");
var engineMatlab = java.callMethodSync(FisImporter, "fromString", matlabString);
var InputVariable = java.newInstanceSync('com.fuzzylite.imex.FisImporter');


var OutputVariable = java.callMethodSync(engineMatlab, "getOutputVariable", 0);
var bandwidthInputVariable = java.callMethodSync(engineMatlab, "getInputVariable", 0);
var timeInputVariable = java.callMethodSync(engineMatlab, "getInputVariable", 1);
java.callMethodSync(bandwidthInputVariable, "setInputValue", -0.5);
java.callMethodSync(timeInputVariable, "setInputValue", 0.5);
java.callMethodSync(engineMatlab, "process");
var resultFuzzy = java.callMethodSync(OutputVariable, "getOutputValue");

console.log("안녕하세요" + resultFuzzy);
John Pekl
  • 75
  • 7
0

You cannot run Java or C++ directly in the browser in an HTML page. As such, your options are:

  1. Put the logic on a server and use ajax http request to that server to access it from a web page.
  2. Rewrite the code in javascript and include it in your page.
  3. Compile the code to WebAssembly which can then be run by a modern browser.
  4. Put the C++ and/or Java into a browser plugin and access the plugin from javascript.

Options 1) or 2) could work fine depending upon the details of the situation.

Option 3) requires the ability to recompile the code into WebAssembly. This is possible for some C++ code (code that uses only library options that can be run in the browser) and there are some projects to similarly compile Java bytecodes into WebAssembly.

Option 4) is generally a bad idea unless it's a very specialized application that somehow makes it worth dealing with the distribution, maintenance, testing and user headaches of a plug-in.

For projects with significant native code that it is either too much work to rewrite entirely in Javascript or it isn't practical, the most common solution would be to put the code on your server (as native code or Java code) and then expose an API on your server that allows the client to send parameters to that code, run the code on the server and then get whatever response the code generates. This gives you the full ability to run C++ and Java code on your server without rewriting it, while still getting the result back to the client.


Note: If your question was meant to solicit alternate libraries that already run in the browser for doing fuzzy logic, then that type of question is considered off-topic here on stackoverflow and could be appropriate in Software Recommendations.

jfriend00
  • 683,504
  • 96
  • 985
  • 979