
org.apache.tinkerpop.gremlin.jsr223.JavaTranslator Maven / Gradle / Ivy
/*
* 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.
*/
package org.apache.tinkerpop.gremlin.jsr223;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.MapConfiguration;
import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
import org.apache.tinkerpop.gremlin.process.traversal.Translator;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.TraversalStrategyProxy;
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public final class JavaTranslator> implements Translator.StepTranslator {
private final S traversalSource;
private final Class> anonymousTraversal;
private static final Map, Map>> GLOBAL_METHOD_CACHE = new ConcurrentHashMap<>();
private final Map, Map> localMethodCache = new ConcurrentHashMap<>();
private final Method anonymousTraversalStart;
private JavaTranslator(final S traversalSource) {
this.traversalSource = traversalSource;
this.anonymousTraversal = traversalSource.getAnonymousTraversalClass().orElse(null);
this.anonymousTraversalStart = getStartMethodFromAnonymousTraversal();
}
public static > JavaTranslator of(final S traversalSource) {
return new JavaTranslator<>(traversalSource);
}
@Override
public S getTraversalSource() {
return this.traversalSource;
}
@Override
public T translate(final Bytecode bytecode) {
TraversalSource dynamicSource = this.traversalSource;
Traversal.Admin, ?> traversal = null;
for (final Bytecode.Instruction instruction : bytecode.getSourceInstructions()) {
dynamicSource = (TraversalSource) invokeMethod(dynamicSource, TraversalSource.class, instruction.getOperator(), instruction.getArguments());
}
boolean spawned = false;
for (final Bytecode.Instruction instruction : bytecode.getStepInstructions()) {
if (!spawned) {
traversal = (Traversal.Admin) invokeMethod(dynamicSource, Traversal.class, instruction.getOperator(), instruction.getArguments());
spawned = true;
} else
invokeMethod(traversal, Traversal.class, instruction.getOperator(), instruction.getArguments());
}
return (T) traversal;
}
@Override
public String getTargetLanguage() {
return "gremlin-java";
}
@Override
public String toString() {
return StringFactory.translatorString(this);
}
////
private Object translateObject(final Object object) {
if (object instanceof Bytecode.Binding)
return translateObject(((Bytecode.Binding) object).value());
else if (object instanceof Bytecode) {
try {
final Traversal.Admin, ?> traversal = (Traversal.Admin) this.anonymousTraversalStart.invoke(null);
for (final Bytecode.Instruction instruction : ((Bytecode) object).getStepInstructions()) {
invokeMethod(traversal, Traversal.class, instruction.getOperator(), instruction.getArguments());
}
return traversal;
} catch (final Throwable e) {
throw new IllegalStateException(e.getMessage());
}
} else if (object instanceof TraversalStrategyProxy) {
final Map map = new HashMap<>();
final Configuration configuration = ((TraversalStrategyProxy) object).getConfiguration();
configuration.getKeys().forEachRemaining(key -> map.put(key, translateObject(configuration.getProperty(key))));
return invokeStrategyCreationMethod(object, map);
} else if (object instanceof Map) {
final Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy