Skip to content

Commit

Permalink
Add JS binding SimRank library. (#135)
Browse files Browse the repository at this point in the history
This change is a good example of how we can easily bind Native code to
JS side if using Bacardi.

ISSUE=#129
  • Loading branch information
romandev authored Oct 10, 2017
1 parent 15303ff commit 1ef8d71
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
2 changes: 0 additions & 2 deletions examples/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,3 @@ app.on('activate', () => {

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
const bacardi = require('bindings')('bacardi.node');
let electronNative = new bacardi.ElectronNative();
16 changes: 16 additions & 0 deletions examples/electron/native/electron_native.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@
#include "examples/electron/native/electron_native.h"

ElectronNative::ElectronNative() {}

ElectronNative::ElectronNative(int32_t k, double c)
: SimRank<std::string>(k, c) {}

void ElectronNative::AddEdge(const std::string& head, const std::string& tail) {
add_edge(head, tail);
}

void ElectronNative::CalculateSimRank() {
calculate_simrank();
}

double ElectronNative::Similarity(const std::string& node1,
const std::string& node2) {
return similarity(node1, node2);
}
1 change: 1 addition & 0 deletions examples/electron/native/electron_native.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'examples_electron_native_cpp_files': [
'electron_native.cc',
'electron_native.h',
'simrank/simrank.hpp',
],

'examples_electron_native_idl_files': [
Expand Down
11 changes: 10 additions & 1 deletion examples/electron/native/electron_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
#ifndef EXAMPLES_ELECTRON_NATIVE_ELECTRON_NATIVE_H_
#define EXAMPLES_ELECTRON_NATIVE_ELECTRON_NATIVE_H_

class ElectronNative {
#include <string>

#include "examples/electron/native/simrank/simrank.hpp"

class ElectronNative : public SimRank<std::string> {
public:
ElectronNative();
ElectronNative(int32_t k, double c);

void AddEdge(const std::string& head, const std::string& tail);
void CalculateSimRank();
double Similarity(const std::string& node1, const std::string& node2);
};

#endif // EXAMPLES_ELECTRON_NATIVE_ELECTRON_NATIVE_H_
7 changes: 7 additions & 0 deletions examples/electron/native/electron_native.idl
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@
* limitations under the License.
*/

[
Constructor(),
Constructor(long k, double c)
]
interface ElectronNative {
void addEdge(string head, string tail);
void calculateSimRank();
double similarity(string node1, string node2);
};
46 changes: 43 additions & 3 deletions examples/electron/renderer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
/**
* Copyright (c) 2017 The Bacardi Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// This is JS porting implementation from simrank/example/main.cpp.

const bacardi = require('bindings')('bacardi.node');

let electronNative = new bacardi.ElectronNative(5, 0.8);
let nodes = ['Univ', 'ProfA', 'ProfB', 'StudentA', 'StudentB'];

electronNative.addEdge(nodes[0], nodes[1]);
electronNative.addEdge(nodes[0], nodes[2]);
electronNative.addEdge(nodes[1], nodes[3]);
electronNative.addEdge(nodes[3], nodes[0]);
electronNative.addEdge(nodes[2], nodes[4]);
electronNative.addEdge(nodes[4], nodes[2]);

electronNative.calculateSimRank();

nodes.forEach(node_a => {
nodes.forEach(node_b => {
if (node_a >= node_b)
return;
let s = electronNative.similarity(node_a, node_b);
if (s > 0) {
document.write(
'similarity(' + node_a + ', ' + node_b + ') = ' + s + '<br>');
}
});
});

0 comments on commit 1ef8d71

Please sign in to comment.