gremlin_patch
Version:
JavaScript Gremlin Language Variant
157 lines (125 loc) • 6.92 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: process/translator.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: process/translator.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
'use strict';
const Traversal = require('./traversal').Traversal;
const Bytecode = require('./bytecode');
/**
* Class to translate glv bytecode steps into executable Gremlin-Groovy script
*/
class Translator {
constructor(traversalSource) {
this._traversalSource = traversalSource;
}
getTraversalSource() {
return this._traversalSource;
}
getTargetLanguage() {
return "gremlin-groovy";
}
of(traversalSource) {
this._traversalSource = traversalSource;
}
/**
* Returns a script representation of the given bytecode instructions.
* @param {Object} bytecodeOrTraversal The traversal or bytecode of a traversal containing step instructions.
* @returns {string} Gremlin-Groovy script
*/
translate(bytecodeOrTraversal, child = false) {
let script = child ? "__" : this._traversalSource;
const bc = bytecodeOrTraversal instanceof Bytecode ? bytecodeOrTraversal : bytecodeOrTraversal.getBytecode();
let instructions = bc.stepInstructions;
// build the script from the glv instructions.
for (let i = 0; i < instructions.length; i++) {
const params = instructions[i].slice(1);
script += '.' + instructions[i][0] + '(';
if (params.length) {
for (let k = 0; k < params.length; k++) {
if (k > 0) {
script += ', ';
}
if (Object(params[k]) === params[k]) {
if (params[k] instanceof Traversal) {
script += this.translate(params[k].getBytecode(), true);
} else if (params[k].toString() === '[object Object]') {
Object.keys(params[k]).forEach(function (key, index) {
if (index > 0) script += ', ';
script += '(\'' + key + '\', ';
if (params[k][key] instanceof String || typeof params[k][key] === 'string') {
script += '\'' + params[k][key] + '\'';
} else {
script += params[k][key];
}
script += ')';
});
} else if (Array.isArray(params[k])) {
const parts = [];
for (const param of params[k]) {
parts.push(translate(param.getBytecode(), true));
}
script += parts.join(",");
} else {
script += params[k].toString();
}
} else if (params[k] === undefined) {
script += '';
} else if (typeof params[k] === 'number' || typeof params[k] === 'boolean') {
script += params[k];
} else {
script += '\'' + params[k] + '\'';
}
}
}
script += ')';
}
return script;
}
}
module.exports = Translator;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnonymousTraversalSource.html">AnonymousTraversalSource</a></li><li><a href="Authenticator.html">Authenticator</a></li><li><a href="Bytecode.html">Bytecode</a></li><li><a href="Client.html">Client</a></li><li><a href="Connection.html">Connection</a></li><li><a href="DriverRemoteConnection.html">DriverRemoteConnection</a></li><li><a href="EdgeLabelVerificationStrategy.html">EdgeLabelVerificationStrategy</a></li><li><a href="Graph.html">Graph</a></li><li><a href="GraphSON2Reader.html">GraphSON2Reader</a></li><li><a href="GraphSON2Writer.html">GraphSON2Writer</a></li><li><a href="GraphSON3Reader.html">GraphSON3Reader</a></li><li><a href="GraphSON3Writer.html">GraphSON3Writer</a></li><li><a href="GraphTraversal.html">GraphTraversal</a></li><li><a href="GraphTraversalSource.html">GraphTraversalSource</a></li><li><a href="HaltedTraverserStrategy.html">HaltedTraverserStrategy</a></li><li><a href="MatchAlgorithmStrategy.html">MatchAlgorithmStrategy</a></li><li><a href="P.html">P</a></li><li><a href="PartitionStrategy.html">PartitionStrategy</a></li><li><a href="Path.html">Path</a></li><li><a href="PlainTextSaslAuthenticator.html">PlainTextSaslAuthenticator</a></li><li><a href="RemoteConnection.html">RemoteConnection</a></li><li><a href="RemoteStrategy.html">RemoteStrategy</a></li><li><a href="RemoteTraversal.html">RemoteTraversal</a></li><li><a href="ReservedKeysVerificationStrategy.html">ReservedKeysVerificationStrategy</a></li><li><a href="ResponseError.html">ResponseError</a></li><li><a href="ResultSet.html">ResultSet</a></li><li><a href="SaslAuthenticator.html">SaslAuthenticator</a></li><li><a href="SaslMechanismBase.html">SaslMechanismBase</a></li><li><a href="SaslMechanismPlain.html">SaslMechanismPlain</a></li><li><a href="SubgraphStrategy.html">SubgraphStrategy</a></li><li><a href="TextP.html">TextP</a></li><li><a href="Transaction.html">Transaction</a></li><li><a href="Translator.html">Translator</a></li><li><a href="TraversalStrategies.html">TraversalStrategies</a></li><li><a href="TraversalStrategy.html">TraversalStrategy</a></li><li><a href="TypeSerializer.html">TypeSerializer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#statics">statics</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Wed May 17 2023 13:49:50 GMT+0800 (China Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>