Commit 69af0ae7 by Jeff Hagelberg Committed by Dave Kantor

ATLAS-1195 Clean up DSL Translation (jnhagelb via dkantor)

parent aa15cd0a
/**
* 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.atlas.groovy;
/**
* Abstract implementation of GroovyExpression that adds a convenient
* toString method.
*
*/
public abstract class AbstractGroovyExpression implements GroovyExpression {
@Override
public String toString() {
GroovyGenerationContext ctx = new GroovyGenerationContext();
ctx.setParametersAllowed(false);
generateGroovy(ctx);
return ctx.getQuery();
}
}
/**
* 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.atlas.groovy;
import org.apache.atlas.AtlasException;
/**
* Represents an arithmetic expression such as a+b.
*/
public class ArithmeticExpression extends BinaryExpression {
/**
* Allowed arithmetic operators.
*/
public enum ArithmeticOperator {
PLUS("+"),
MINUS("-"),
TIMES("*"),
DIVIDE("/"),
MODULUS("%");
private String groovyValue;
ArithmeticOperator(String groovyValue) {
this.groovyValue = groovyValue;
}
public String getGroovyValue() {
return groovyValue;
}
public static ArithmeticOperator lookup(String groovyValue) throws AtlasException {
for(ArithmeticOperator op : ArithmeticOperator.values()) {
if (op.getGroovyValue().equals(groovyValue)) {
return op;
}
}
throw new AtlasException("Unknown Operator:" + groovyValue);
}
}
public ArithmeticExpression(GroovyExpression left, ArithmeticOperator op, GroovyExpression right) {
super(left, op.getGroovyValue(), right);
}
}
/**
* 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.atlas.groovy;
/**
* Represents any kind of binary expression. This could
* be an arithmetic expression, such as a + 3, a boolean
* expression such as a < 5, or even a comparison function
* such as a <=> b.
*
*/
public abstract class BinaryExpression extends AbstractGroovyExpression {
private GroovyExpression left;
private GroovyExpression right;
private String op;
public BinaryExpression(GroovyExpression left, String op, GroovyExpression right) {
this.left = left;
this.op = op;
this.right = right;
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
left.generateGroovy(context);
context.append(" ");
context.append(op);
context.append(" ");
right.generateGroovy(context);
}
}
/**
* 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.atlas.groovy;
/**
* Groovy expression that represents a cast.
*/
public class CastExpression extends AbstractGroovyExpression {
private GroovyExpression expr;
private String className;
public CastExpression(GroovyExpression expr, String className) {
this.expr = expr;
this.className =className;
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
context.append("((");
context.append(className);
context.append(")");
expr.generateGroovy(context);
context.append(")");
}
}
/**
* 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.atlas.groovy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Groovy expression that represents a Groovy closure.
*/
public class ClosureExpression extends AbstractGroovyExpression {
private List<String> varNames = new ArrayList<>();
private GroovyExpression body;
public ClosureExpression(GroovyExpression body, String... varNames) {
this.body = body;
this.varNames.addAll(Arrays.asList(varNames));
}
public ClosureExpression(List<String> varNames, GroovyExpression body) {
this.body = body;
this.varNames.addAll(varNames);
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
context.append("{");
if (!varNames.isEmpty()) {
Iterator<String> varIt = varNames.iterator();
while(varIt.hasNext()) {
String varName = varIt.next();
context.append(varName);
if (varIt.hasNext()) {
context.append(", ");
}
}
context.append("->");
}
body.generateGroovy(context);
context.append("}");
}
}
/**
* 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.atlas.groovy;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Groovy expression that represents a block of code
* that contains 0 or more statements that are delimited
* by semicolons.
*/
public class CodeBlockExpression extends AbstractGroovyExpression {
private List<GroovyExpression> body = new ArrayList<>();
public void addStatement(GroovyExpression expr) {
body.add(expr);
}
public void addStatements(List<GroovyExpression> exprs) {
body.addAll(exprs);
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
/*
* the L:{} represents a groovy code block; the label is needed
* to distinguish it from a groovy closure.
*/
context.append("L:{");
Iterator<GroovyExpression> stmtIt = body.iterator();
while(stmtIt.hasNext()) {
GroovyExpression stmt = stmtIt.next();
stmt.generateGroovy(context);
if (stmtIt.hasNext()) {
context.append(";");
}
}
context.append("}");
}
}
/**
* 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.atlas.groovy;
import org.apache.atlas.AtlasException;
/**
* Represents an expression that compares two expressions
* using a comparison operator (==, <, >, etc).
*
*/
public class ComparisonExpression extends BinaryExpression {
/**
* Allowed comparison operators.
*
*/
public enum ComparisonOperator {
GREATER_THAN_EQ_(">="),
GREATER_THAN(">"),
EQUALS("=="),
NOT_EQUALS("!="),
LESS_THAN("<"),
LESS_THAN_EQ("<=");
private String groovyValue;
ComparisonOperator(String groovyValue) {
this.groovyValue = groovyValue;
}
public String getGroovyValue() {
return groovyValue;
}
public static ComparisonOperator lookup(String groovyValue) throws AtlasException {
for(ComparisonOperator op : ComparisonOperator.values()) {
if (op.getGroovyValue().equals(groovyValue)) {
return op;
}
}
throw new AtlasException("Unknown Operator:" + groovyValue);
}
}
public ComparisonExpression(GroovyExpression left, ComparisonOperator op, GroovyExpression right) {
super(left, op.getGroovyValue(), right);
}
}
/**
* 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.atlas.groovy;
/**
* Represents an expression that compares two expressions using
* the Groovy "spaceship" operator. This is basically the
* same as calling left.compareTo(right), except that it has
* built-in null handling and some other nice features.
*
*/
public class ComparisonOperatorExpression extends BinaryExpression {
public ComparisonOperatorExpression(GroovyExpression left, GroovyExpression right) {
super(left, "<=>", right);
}
}
/**
* 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.atlas.groovy;
/**
* Groovy expression that accesses a field in an object.
*/
public class FieldExpression extends AbstractGroovyExpression {
private GroovyExpression target;
private String fieldName;
public FieldExpression(GroovyExpression target, String fieldName) {
this.target = target;
this.fieldName = fieldName;
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
target.generateGroovy(context);
context.append(".'");
context.append(fieldName);
context.append("'");
}
}
/**
* 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.atlas.groovy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Groovy expression that calls a method on an object.
*/
public class FunctionCallExpression extends AbstractGroovyExpression {
// null for global functions
private GroovyExpression target;
private String functionName;
private List<GroovyExpression> arguments = new ArrayList<GroovyExpression>();
public FunctionCallExpression(String functionName, List<? extends GroovyExpression> arguments) {
this.target = null;
this.functionName = functionName;
this.arguments.addAll(arguments);
}
public FunctionCallExpression(GroovyExpression target, String functionName,
List<? extends GroovyExpression> arguments) {
this.target = target;
this.functionName = functionName;
this.arguments.addAll(arguments);
}
public FunctionCallExpression(String functionName, GroovyExpression... arguments) {
this.target = null;
this.functionName = functionName;
this.arguments.addAll(Arrays.asList(arguments));
}
public FunctionCallExpression(GroovyExpression target, String functionName, GroovyExpression... arguments) {
this.target = target;
this.functionName = functionName;
this.arguments.addAll(Arrays.asList(arguments));
}
public void addArgument(GroovyExpression expr) {
arguments.add(expr);
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
if (target != null) {
target.generateGroovy(context);
context.append(".");
}
context.append(functionName);
context.append("(");
Iterator<GroovyExpression> it = arguments.iterator();
while (it.hasNext()) {
GroovyExpression expr = it.next();
expr.generateGroovy(context);
if (it.hasNext()) {
context.append(", ");
}
}
context.append(")");
}
}
/**
* 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.atlas.groovy;
/**
* Represents an expression in the Groovy programming language, which
* is the language that Gremlin scripts are written and interpreted in.
*/
public interface GroovyExpression {
/**
* Generates a groovy script from the expression.
*
* @param context
*/
void generateGroovy(GroovyGenerationContext context);
}
/**
* 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.atlas.groovy;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Context used when generating Groovy queries. Keeps track of query parameters
* that are created during the process as well as the portion of the query that
* has been generated so far.
*
*/
public class GroovyGenerationContext {
private boolean parametersAllowed = true;
private int parameterCount = 0;
private Map<String, Object> parameterValues = new HashMap<>();
//used to build up the groovy script
private StringBuilder generatedQuery = new StringBuilder();
public void setParametersAllowed(boolean value) {
this.parametersAllowed = value;
}
public GroovyExpression addParameter(Object value) {
if (this.parametersAllowed) {
String parameterName = "p" + (++parameterCount);
this.parameterValues.put(parameterName, value);
return new IdentifierExpression(parameterName);
} else {
LiteralExpression expr = new LiteralExpression(value);
expr.setTranslateToParameter(false);
return expr;
}
}
public StringBuilder append(String gremlin) {
this.generatedQuery.append(gremlin);
return generatedQuery;
}
public String getQuery() {
return generatedQuery.toString();
}
public Map<String, Object> getParameters() {
return Collections.unmodifiableMap(parameterValues);
}
}
/**
* 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.atlas.groovy;
/**
* Groovy expression that references the variable with the given name.
*
*/
public class IdentifierExpression extends AbstractGroovyExpression {
private String varName;
public IdentifierExpression(String varName) {
this.varName = varName;
}
public String getVariableName() {
return varName;
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
context.append(varName);
}
}
/**
* 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.atlas.groovy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Groovy expression that represents a list literal.
*/
public class ListExpression extends AbstractGroovyExpression {
private List<GroovyExpression> values = new ArrayList<>();
public ListExpression(GroovyExpression... values) {
this.values = Arrays.asList(values);
}
public ListExpression(List<? extends GroovyExpression> values) {
this.values.addAll(values);
}
public void addArgument(GroovyExpression expr) {
values.add(expr);
}
public void generateGroovy(GroovyGenerationContext context) {
context.append("[");
Iterator<GroovyExpression> it = values.iterator();
while(it.hasNext()) {
GroovyExpression expr = it.next();
expr.generateGroovy(context);
if (it.hasNext()) {
context.append(", ");
}
}
context.append("]");
}
}
/**
* 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.atlas.groovy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Represents a literal value.
*/
public class LiteralExpression implements GroovyExpression {
public static final LiteralExpression TRUE = new LiteralExpression(true);
public static final LiteralExpression FALSE = new LiteralExpression(false);
public static final LiteralExpression NULL = new LiteralExpression(null);
private Object value;
private boolean translateToParameter = true;
private boolean addTypeSuffix = false;
public LiteralExpression(Object value, boolean addTypeSuffix) {
this.value = value;
this.translateToParameter = value instanceof String;
this.addTypeSuffix = addTypeSuffix;
}
public LiteralExpression(Object value) {
this.value = value;
this.translateToParameter = value instanceof String;
}
private String getTypeSuffix() {
if (!addTypeSuffix) {
return "";
}
if (value.getClass() == Long.class) {
return "L";
}
if (value.getClass() == Float.class) {
return "f";
}
if (value.getClass() == Double.class) {
return "d";
}
return "";
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
if (translateToParameter) {
GroovyExpression result = context.addParameter(value);
result.generateGroovy(context);
return;
}
if (value instanceof String) {
String escapedValue = getEscapedValue();
context.append("'");
context.append(escapedValue);
context.append("'");
} else {
context.append(String.valueOf(value));
context.append(getTypeSuffix());
}
}
private String getEscapedValue() {
String escapedValue = (String)value;
escapedValue = escapedValue.replaceAll(Pattern.quote("\\"), Matcher.quoteReplacement("\\\\"));
escapedValue = escapedValue.replaceAll(Pattern.quote("'"), Matcher.quoteReplacement("\\'"));
return escapedValue;
}
public void setTranslateToParameter(boolean translateToParameter) {
this.translateToParameter = translateToParameter;
}
}
/**
* 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.atlas.groovy;
/**
* Represents a logical (and/or) expression.
*
*/
public class LogicalExpression extends BinaryExpression {
/**
* Allowed logical operators.
*
*/
public enum LogicalOperator {
AND("&&"),
OR("||");
private String groovyValue;
LogicalOperator(String groovyValue) {
this.groovyValue = groovyValue;
}
public String getGroovyValue() {
return groovyValue;
}
}
public LogicalExpression(GroovyExpression left, LogicalOperator op, GroovyExpression right) {
super(left, op.getGroovyValue(), right);
}
}
/**
* 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.atlas.groovy;
/**
* Represents an "exclusive" range expression, e.g. [0..&lt;10].
*/
public class RangeExpression extends AbstractGroovyExpression {
private GroovyExpression parent;
private int offset;
private int count;
public RangeExpression(GroovyExpression parent, int offset, int count) {
this.parent = parent;
this.offset = offset;
this.count = count;
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
parent.generateGroovy(context);
context.append(" [");
new LiteralExpression(offset).generateGroovy(context);
context.append("..<");
new LiteralExpression(count).generateGroovy(context);
context.append("]");
}
}
/**
* 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.atlas.groovy;
/**
* Groovy expression that represents the ternary operator (expr ? trueValue :
* falseValue)
*/
public class TernaryOperatorExpression extends AbstractGroovyExpression {
private GroovyExpression booleanExpr;
private GroovyExpression trueValue;
private GroovyExpression falseValue;
public TernaryOperatorExpression(GroovyExpression booleanExpr, GroovyExpression trueValue,
GroovyExpression falseValue) {
this.booleanExpr = booleanExpr;
this.trueValue = trueValue;
this.falseValue = falseValue;
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
context.append("((");
booleanExpr.generateGroovy(context);
context.append(") ? (");
trueValue.generateGroovy(context);
context.append(") : (");
falseValue.generateGroovy(context);
context.append("))");
}
public String toString() {
GroovyGenerationContext context = new GroovyGenerationContext();
generateGroovy(context);
return context.getQuery();
}
}
/**
* 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.atlas.groovy;
/**
* Groovy expression that represents a type coersion (e.g obj as Set).
*/
public class TypeCoersionExpression extends AbstractGroovyExpression {
private GroovyExpression expr;
private String className;
public TypeCoersionExpression(GroovyExpression expr, String className) {
this.expr = expr;
this.className =className;
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
context.append("(");
expr.generateGroovy(context);
context.append(")");
context.append(" as ");
context.append(className);
}
}
/**
* 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.atlas.groovy;
/**
* Groovy statement that assigns a value to a variable.
*/
public class VariableAssignmentExpression extends AbstractGroovyExpression {
private String type = null;
private String name;
private GroovyExpression value;
/**
* @param string
* @param v
*/
public VariableAssignmentExpression(String type, String name, GroovyExpression v) {
this.type = type;
this.name = name;
this.value = v;
}
public VariableAssignmentExpression(String name, GroovyExpression v) {
this(null, name, v);
}
@Override
public void generateGroovy(GroovyGenerationContext context) {
if (type == null) {
context.append("def ");
} else {
context.append(type);
context.append(" ");
}
context.append(name);
context.append(" = ");
value.generateGroovy(context);
}
}
......@@ -23,6 +23,7 @@ import java.util.Set;
import javax.script.ScriptException;
import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.typesystem.types.IDataType;
/**
......@@ -204,7 +205,7 @@ public interface AtlasGraph<V, E> {
* @param type
* @return
*/
String generatePersisentToLogicalConversionExpression(String valueExpr, IDataType<?> type);
GroovyExpression generatePersisentToLogicalConversionExpression(GroovyExpression valueExpr, IDataType<?> type);
/**
* Indicates whether or not stored values with the specified type need to be converted
......@@ -240,7 +241,7 @@ public interface AtlasGraph<V, E> {
*
* @return
*/
String getInitialIndexedPredicate();
GroovyExpression getInitialIndexedPredicate(GroovyExpression parent);
/**
* As an optimization, a graph database implementation may want to retrieve additional
......@@ -249,7 +250,7 @@ public interface AtlasGraph<V, E> {
* avoid the need to make an extra REST API call to look up those edges. For implementations
* that do not require any kind of transform, an empty String should be returned.
*/
String getOutputTransformationPredicate(boolean isSelect, boolean isPath);
GroovyExpression addOutputTransformationPredicate(GroovyExpression expr, boolean isSelect, boolean isPath);
/**
* Executes a Gremlin script, returns an object with the result.
......
......@@ -34,6 +34,7 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.repository.graphdb.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.repository.graphdb.AtlasGraphManagement;
......@@ -286,7 +287,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
}
@Override
public String generatePersisentToLogicalConversionExpression(String expr, IDataType<?> type) {
public GroovyExpression generatePersisentToLogicalConversionExpression(GroovyExpression expr, IDataType<?> type) {
//nothing special needed, value is stored in required type
return expr;
......@@ -304,13 +305,13 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
}
@Override
public String getInitialIndexedPredicate() {
return "";
public GroovyExpression getInitialIndexedPredicate(GroovyExpression expr) {
return expr;
}
@Override
public String getOutputTransformationPredicate(boolean inSelect, boolean isPath) {
return "";
public GroovyExpression addOutputTransformationPredicate(GroovyExpression expr, boolean inSelect, boolean isPath) {
return expr;
}
public Iterable<AtlasEdge<Titan0Vertex, Titan0Edge>> wrapEdges(Iterator<Edge> it) {
......
......@@ -9,6 +9,7 @@ ATLAS-1060 Add composite indexes for exact match performance improvements for al
ATLAS-1127 Modify creation and modification timestamps to Date instead of Long(sumasai)
ALL CHANGES:
ATLAS-1195 Clean up DSL Translation (jnhagelb via dkantor)
ATLAS-1139 Parameter name of a HDFS DataSet entity should contain filesystem path (svimal2106 via sumasai)
ATLAS-1200 Error Catalog enhancement (apoorvnaik via sumasai)
ATLAS-1207 Dataset exists query in lineage APIs takes longer (shwethags)
......
......@@ -23,16 +23,16 @@ import java.util.List;
import javax.inject.Inject;
import org.apache.atlas.AtlasException;
import org.apache.atlas.query.Expressions;
import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.query.GraphPersistenceStrategies;
import org.apache.atlas.query.GraphPersistenceStrategies$class;
import org.apache.atlas.query.IntSequence;
import org.apache.atlas.query.TypeUtils;
import org.apache.atlas.repository.Constants;
import org.apache.atlas.repository.MetadataRepository;
import org.apache.atlas.repository.RepositoryException;
import org.apache.atlas.repository.graph.GraphBackedMetadataRepository;
import org.apache.atlas.repository.graph.GraphHelper;
import org.apache.atlas.repository.graphdb.AtlasEdgeDirection;
import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.graphdb.GremlinVersion;
......@@ -105,11 +105,6 @@ public class DefaultGraphPersistenceStrategy implements GraphPersistenceStrategi
}
@Override
public String fieldPrefixInSelect() {
return "it";
}
@Override
public Id getIdFromVertex(String dataTypeName, AtlasVertex vertex) {
return GraphHelper.getIdFromVertex(dataTypeName, vertex);
}
......@@ -212,28 +207,13 @@ public class DefaultGraphPersistenceStrategy implements GraphPersistenceStrategi
}
@Override
public String gremlinCompOp(Expressions.ComparisonExpression op) {
return GraphPersistenceStrategies$class.gremlinCompOp(this, op);
}
@Override
public String gremlinPrimitiveOp(Expressions.ComparisonExpression op) {
return GraphPersistenceStrategies$class.gremlinPrimitiveOp(this, op);
public AtlasEdgeDirection instanceToTraitEdgeDirection() {
return AtlasEdgeDirection.OUT;
}
@Override
public String loopObjectExpression(IDataType<?> dataType) {
return GraphPersistenceStrategies$class.loopObjectExpression(this, dataType);
}
@Override
public String instanceToTraitEdgeDirection() {
return "out";
}
@Override
public String traitToInstanceEdgeDirection() {
return "in";
public AtlasEdgeDirection traitToInstanceEdgeDirection() {
return AtlasEdgeDirection.IN;
}
@Override
......@@ -247,17 +227,12 @@ public class DefaultGraphPersistenceStrategy implements GraphPersistenceStrategi
}
@Override
public scala.collection.Seq<String> typeTestExpression(String typeName, IntSequence intSeq) {
return GraphPersistenceStrategies$class.typeTestExpression(this, typeName, intSeq);
}
@Override
public boolean collectTypeInstancesIntoVar() {
return GraphPersistenceStrategies$class.collectTypeInstancesIntoVar(this);
}
@Override
public boolean addGraphVertexPrefix(scala.collection.Traversable<String> preStatements) {
public boolean addGraphVertexPrefix(scala.collection.Traversable<GroovyExpression> preStatements) {
return GraphPersistenceStrategies$class.addGraphVertexPrefix(this, preStatements);
}
......@@ -267,13 +242,13 @@ public class DefaultGraphPersistenceStrategy implements GraphPersistenceStrategi
}
@Override
public String generatePersisentToLogicalConversionExpression(String expr, IDataType<?> t) {
public GroovyExpression generatePersisentToLogicalConversionExpression(GroovyExpression expr, IDataType<?> t) {
return GraphPersistenceStrategies$class.generatePersisentToLogicalConversionExpression(this,expr, t);
}
@Override
public String initialQueryCondition() {
return GraphPersistenceStrategies$class.initialQueryCondition(this);
public GroovyExpression addInitialQueryCondition(GroovyExpression expr) {
return GraphPersistenceStrategies$class.addInitialQueryCondition(this, expr);
}
@Override
......
/**
* 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.atlas.gremlin;
import java.util.ArrayList;
import java.util.List;
import org.apache.atlas.AtlasException;
import org.apache.atlas.groovy.ClosureExpression;
import org.apache.atlas.groovy.ComparisonExpression;
import org.apache.atlas.groovy.ComparisonOperatorExpression;
import org.apache.atlas.groovy.FieldExpression;
import org.apache.atlas.groovy.FunctionCallExpression;
import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.groovy.IdentifierExpression;
import org.apache.atlas.groovy.ListExpression;
import org.apache.atlas.groovy.LiteralExpression;
import org.apache.atlas.groovy.LogicalExpression;
import org.apache.atlas.groovy.RangeExpression;
import org.apache.atlas.groovy.TernaryOperatorExpression;
import org.apache.atlas.groovy.ComparisonExpression.ComparisonOperator;
import org.apache.atlas.groovy.LogicalExpression.LogicalOperator;
import org.apache.atlas.query.GraphPersistenceStrategies;
import org.apache.atlas.query.TypeUtils.FieldInfo;
import org.apache.atlas.typesystem.types.IDataType;
/**
* Generates gremlin query expressions using Gremlin 2 syntax.
*
*/
public class Gremlin2ExpressionFactory extends GremlinExpressionFactory {
private static final String LOOP_METHOD = "loop";
private static final String CONTAINS = "contains";
private static final String LOOP_COUNT_FIELD = "loops";
private static final String PATH_FIELD = "path";
private static final String ENABLE_PATH_METHOD = "enablePath";
private static final String BACK_METHOD = "back";
@Override
public GroovyExpression generateLogicalExpression(GroovyExpression parent, String operator, List<GroovyExpression> operands) {
return new FunctionCallExpression(parent, operator, operands);
}
@Override
public GroovyExpression generateBackReferenceExpression(GroovyExpression parent, boolean inSelect, String alias) {
if (inSelect) {
return getFieldInSelect();
}
else {
return new FunctionCallExpression(parent, BACK_METHOD, new LiteralExpression(alias));
}
}
@Override
public GroovyExpression getLoopExpressionParent(GroovyExpression inputQry) {
return inputQry;
}
@Override
public GroovyExpression generateLoopExpression(GroovyExpression parent,GraphPersistenceStrategies s, IDataType dataType, GroovyExpression loopExpr, String alias, Integer times) {
GroovyExpression emitExpr = generateLoopEmitExpression(s, dataType);
//note that in Gremlin 2 (unlike Gremlin 3), the parent is not explicitly used. It is incorporated
//in the loopExpr.
GroovyExpression whileFunction = null;
if(times != null) {
GroovyExpression loopsExpr = new FieldExpression(getItVariable(), LOOP_COUNT_FIELD);
GroovyExpression timesExpr = new LiteralExpression(times);
whileFunction = new ClosureExpression(new ComparisonExpression(loopsExpr, ComparisonOperator.LESS_THAN, timesExpr));
}
else {
GroovyExpression pathExpr = new FieldExpression(getItVariable(),PATH_FIELD);
GroovyExpression itObjectExpr = getCurrentObjectExpression();
GroovyExpression pathContainsExpr = new FunctionCallExpression(pathExpr, CONTAINS, itObjectExpr);
whileFunction = new ClosureExpression(new TernaryOperatorExpression(pathContainsExpr, LiteralExpression.FALSE, LiteralExpression.TRUE));
}
GroovyExpression emitFunction = new ClosureExpression(emitExpr);
GroovyExpression loopCall = new FunctionCallExpression(loopExpr, LOOP_METHOD, new LiteralExpression(alias), whileFunction, emitFunction);
return new FunctionCallExpression(loopCall, ENABLE_PATH_METHOD);
}
@Override
public GroovyExpression typeTestExpression(GraphPersistenceStrategies s, String typeName, GroovyExpression itRef) {
GroovyExpression typeAttrExpr = new FieldExpression(itRef, s.typeAttributeName());
GroovyExpression superTypeAttrExpr = new FieldExpression(itRef, s.superTypeAttributeName());
GroovyExpression typeNameExpr = new LiteralExpression(typeName);
GroovyExpression typeMatchesExpr = new ComparisonExpression(typeAttrExpr, ComparisonOperator.EQUALS, typeNameExpr);
GroovyExpression isSuperTypeExpr = new FunctionCallExpression(superTypeAttrExpr, CONTAINS, typeNameExpr);
GroovyExpression hasSuperTypeAttrExpr = superTypeAttrExpr;
GroovyExpression superTypeMatchesExpr = new TernaryOperatorExpression(hasSuperTypeAttrExpr, isSuperTypeExpr, LiteralExpression.FALSE);
return new LogicalExpression(typeMatchesExpr, LogicalOperator.OR, superTypeMatchesExpr);
}
@Override
public GroovyExpression generateSelectExpression(GroovyExpression parent, List<LiteralExpression> sourceNames,
List<GroovyExpression> srcExprs) {
GroovyExpression srcNamesExpr = new ListExpression(sourceNames);
List<GroovyExpression> selectArgs = new ArrayList<>();
selectArgs.add(srcNamesExpr);
for(GroovyExpression expr : srcExprs) {
selectArgs.add(new ClosureExpression(expr));
}
return new FunctionCallExpression(parent, SELECT_METHOD, selectArgs);
}
@Override
public GroovyExpression generateFieldExpression(GroovyExpression parent, FieldInfo fInfo, String propertyName, boolean inSelect) {
return new FieldExpression(parent, propertyName);
}
@Override
public GroovyExpression generateHasExpression(GraphPersistenceStrategies s, GroovyExpression parent, String propertyName, String symbol,
GroovyExpression requiredValue, FieldInfo fInfo) throws AtlasException {
GroovyExpression op = gremlin2CompOp(symbol);
GroovyExpression propertyNameExpr = new LiteralExpression(propertyName);
return new FunctionCallExpression(parent, HAS_METHOD, propertyNameExpr, op, requiredValue);
}
private GroovyExpression gremlin2CompOp(String op) throws AtlasException {
GroovyExpression tExpr = new IdentifierExpression("T");
if(op.equals("=")) {
return new FieldExpression(tExpr, "eq");
}
if(op.equals("!=")) {
return new FieldExpression(tExpr, "neq");
}
if(op.equals(">")) {
return new FieldExpression(tExpr, "gt");
}
if(op.equals(">=")) {
return new FieldExpression(tExpr, "gte");
}
if(op.equals("<")) {
return new FieldExpression(tExpr, "lt");
}
if(op.equals("<=")) {
return new FieldExpression(tExpr, "lte");
}
throw new AtlasException("Comparison operator " + op + " not supported in Gremlin");
}
@Override
protected GroovyExpression initialExpression(GraphPersistenceStrategies s, GroovyExpression varExpr) {
return new FunctionCallExpression(varExpr, "_");
}
@Override
public GroovyExpression generateLimitExpression(GroovyExpression parent, int offset, int totalRows) {
return new RangeExpression(parent, offset, totalRows);
}
@Override
public List<GroovyExpression> getOrderFieldParents() {
GroovyExpression itExpr = getItVariable();
List<GroovyExpression> result = new ArrayList<>(2);
result.add(new FieldExpression(itExpr, "a"));
result.add(new FieldExpression(itExpr, "b"));
return result;
}
@Override
public GroovyExpression generateOrderByExpression(GroovyExpression parent, List<GroovyExpression> translatedOrderBy, boolean isAscending) {
GroovyExpression itExpr = getItVariable();
GroovyExpression aPropertyExpr = translatedOrderBy.get(0);
GroovyExpression bPropertyExpr = translatedOrderBy.get(1);
GroovyExpression aPropertyNotNull = new ComparisonExpression(aPropertyExpr, ComparisonOperator.NOT_EQUALS, LiteralExpression.NULL);
GroovyExpression bPropertyNotNull = new ComparisonExpression(bPropertyExpr, ComparisonOperator.NOT_EQUALS, LiteralExpression.NULL);
GroovyExpression aCondition = new TernaryOperatorExpression(aPropertyNotNull, new FunctionCallExpression(aPropertyExpr,TO_LOWER_CASE_METHOD), aPropertyExpr);
GroovyExpression bCondition = new TernaryOperatorExpression(bPropertyNotNull, new FunctionCallExpression(bPropertyExpr,TO_LOWER_CASE_METHOD), bPropertyExpr);
GroovyExpression comparisonFunction = null;
if(isAscending) {
comparisonFunction = new ComparisonOperatorExpression(aCondition, bCondition);
}
else {
comparisonFunction = new ComparisonOperatorExpression(bCondition, aCondition);
}
return new FunctionCallExpression(parent, ORDER_METHOD, new ClosureExpression(comparisonFunction));
}
@Override
public GroovyExpression getAnonymousTraversalExpression() {
return new FunctionCallExpression("_");
}
@Override
public GroovyExpression getFieldInSelect() {
return getItVariable();
}
}
......@@ -21,23 +21,20 @@ package org.apache.atlas.query
import java.util
import java.util.Date
import scala.collection.JavaConversions._
import scala.collection.JavaConversions.seqAsJavaList
import scala.language.existentials
import org.apache.atlas.query.Expressions.{ComparisonExpression, ExpressionException}
import org.apache.atlas.groovy.GroovyExpression
import org.apache.atlas.query.TypeUtils.FieldInfo
import org.apache.atlas.repository.graph.{GraphHelper, GraphBackedMetadataRepository}
import org.apache.atlas.repository.RepositoryException
import org.apache.atlas.repository.graph.GraphHelper
import org.apache.atlas.repository.graphdb._
import org.apache.atlas.typesystem.persistence.Id
import org.apache.atlas.typesystem.types.DataTypes._
import org.apache.atlas.typesystem.ITypedInstance
import org.apache.atlas.typesystem.ITypedReferenceableInstance
import org.apache.atlas.typesystem.persistence.Id
import org.apache.atlas.typesystem.types._
import org.apache.atlas.typesystem.{ITypedInstance, ITypedReferenceableInstance}
import scala.collection.JavaConversions._
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import org.apache.atlas.typesystem.types.DataTypes._
/**
* Represents the Bridge between the QueryProcessor and the Graph Persistence scheme used.
......@@ -55,10 +52,10 @@ trait GraphPersistenceStrategies {
def getGraph() : AtlasGraph[_,_]
def getSupportedGremlinVersion() : GremlinVersion = getGraph().getSupportedGremlinVersion;
def generatePersisentToLogicalConversionExpression(expr: String, t: IDataType[_]) : String = getGraph().generatePersisentToLogicalConversionExpression(expr, t);
def generatePersisentToLogicalConversionExpression(expr: GroovyExpression, t: IDataType[_]) : GroovyExpression = getGraph().generatePersisentToLogicalConversionExpression(expr, t);
def isPropertyValueConversionNeeded(attrType: IDataType[_]) : Boolean = getGraph().isPropertyValueConversionNeeded(attrType);
def initialQueryCondition = if (getGraph().requiresInitialIndexedPredicate()) { s""".${getGraph().getInitialIndexedPredicate}""" } else {""};
def addInitialQueryCondition(parent: GroovyExpression) : GroovyExpression = if (getGraph().requiresInitialIndexedPredicate()) { getGraph().getInitialIndexedPredicate(parent) } else { parent };
/**
* Name of attribute used to store typeName in vertex
......@@ -87,11 +84,12 @@ trait GraphPersistenceStrategies {
def traitLabel(cls: IDataType[_], traitName: String): String
def instanceToTraitEdgeDirection : String = "out"
def traitToInstanceEdgeDirection = instanceToTraitEdgeDirection match {
case "out" => "in"
case "in" => "out"
case x => x
def instanceToTraitEdgeDirection : AtlasEdgeDirection = AtlasEdgeDirection.OUT;
def traitToInstanceEdgeDirection : AtlasEdgeDirection = instanceToTraitEdgeDirection match {
case AtlasEdgeDirection.OUT => AtlasEdgeDirection.IN;
case AtlasEdgeDirection.IN => AtlasEdgeDirection.OUT;
case x => AtlasEdgeDirection.IN;
}
/**
......@@ -115,27 +113,6 @@ trait GraphPersistenceStrategies {
case FieldInfo(dataType, null, null, traitName) => traitLabel(dataType, traitName)
}
def fieldPrefixInSelect(): String = {
if(getSupportedGremlinVersion() == GremlinVersion.THREE) {
//this logic is needed to remove extra results from
//what is emitted by repeat loops. Technically
//for queries that don't have a loop in them we could just use "it"
//the reason for this is that in repeat loops with an alias,
//although the alias gets set to the right value, for some
//reason the select actually includes all vertices that were traversed
//through in the loop. In these cases, we only want the last vertex
//traversed in the loop to be selected. The logic here handles that
//case by converting the result to a list and just selecting the
//last item from it.
"((it as Vertex[]) as List<Vertex>).last()"
}
else {
"it"
}
}
/**
* extract the Id from a Vertex.
* @param dataTypeNm the dataType of the instance that the given vertex represents
......@@ -146,50 +123,7 @@ trait GraphPersistenceStrategies {
def constructInstance[U](dataType: IDataType[U], v: java.lang.Object): U
def gremlinCompOp(op: ComparisonExpression) = {
if( getSupportedGremlinVersion() == GremlinVersion.TWO) {
gremlin2CompOp(op);
}
else {
gremlin3CompOp(op);
}
}
def gremlinPrimitiveOp(op: ComparisonExpression) = op.symbol match {
case "=" => "=="
case "!=" => "!="
case ">" => ">"
case ">=" => ">="
case "<" => "<"
case "<=" => "<="
case _ => throw new ExpressionException(op, "Comparison operator not supported in Gremlin")
}
private def gremlin2CompOp(op: ComparisonExpression) = op.symbol match {
case "=" => "T.eq"
case "!=" => "T.neq"
case ">" => "T.gt"
case ">=" => "T.gte"
case "<" => "T.lt"
case "<=" => "T.lte"
case _ => throw new ExpressionException(op, "Comparison operator not supported in Gremlin")
}
private def gremlin3CompOp(op: ComparisonExpression) = op.symbol match {
case "=" => "eq"
case "!=" => "neq"
case ">" => "gt"
case ">=" => "gte"
case "<" => "lt"
case "<=" => "lte"
case _ => throw new ExpressionException(op, "Comparison operator not supported in Gremlin")
}
def loopObjectExpression(dataType: IDataType[_]) = {
_typeTestExpression(dataType.getName, "it.object")
}
def addGraphVertexPrefix(preStatements : Traversable[String]) = !collectTypeInstancesIntoVar
def addGraphVertexPrefix(preStatements : Traversable[GroovyExpression]) = !collectTypeInstancesIntoVar
/**
* Controls behavior of how instances of a Type are discovered.
......@@ -213,76 +147,14 @@ trait GraphPersistenceStrategies {
*/
def collectTypeInstancesIntoVar = true
def typeTestExpression(typeName : String, intSeq : IntSequence) : Seq[String] = {
if (collectTypeInstancesIntoVar)
typeTestExpressionMultiStep(typeName, intSeq)
else
typeTestExpressionUsingFilter(typeName)
}
private def typeTestExpressionUsingFilter(typeName : String) : Seq[String] = {
Seq(s"""filter${_typeTestExpression(typeName, "it")}""")
}
/**
* type test expression that ends up in the emit clause in
* loop/repeat steps and a few other places
*/
private def _typeTestExpression(typeName: String, itRef: String): String = {
if( getSupportedGremlinVersion() == GremlinVersion.TWO) {
s"""{(${itRef}.'${typeAttributeName}' == '${typeName}') |
| (${itRef}.'${superTypeAttributeName}' ?
| ${itRef}.'${superTypeAttributeName}'.contains('${typeName}') : false)}""".
stripMargin.replace(System.getProperty("line.separator"), "")
}
else {
//gremlin 3
s"""has('${typeAttributeName}',eq('${typeName}')).or().has('${superTypeAttributeName}',eq('${typeName}'))"""
}
}
private def propertyValueSet(vertexRef : String, attrName: String) : String = {
s"""org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils.set(${vertexRef}.values('${attrName})"""
}
private def typeTestExpressionMultiStep(typeName : String, intSeq : IntSequence) : Seq[String] = {
val varName = s"_var_${intSeq.next}"
Seq(
newSetVar(varName),
fillVarWithTypeInstances(typeName, varName),
fillVarWithSubTypeInstances(typeName, varName),
if(getSupportedGremlinVersion() == GremlinVersion.TWO) {
s"$varName._()"
}
else {
//this bit of groovy magic converts the set of vertices in varName into
//a String containing the ids of all the vertices. This becomes the argument
//to g.V(). This is needed because Gremlin 3 does not support
// _()
//s"g.V(${varName}.collect{it.id()} as String[])"
s"g.V(${varName} as Object[])${initialQueryCondition}"
}
)
}
private def newSetVar(varName : String) = s"def $varName = [] as Set"
private def fillVarWithTypeInstances(typeName : String, fillVar : String) = {
s"""g.V().has("${typeAttributeName}", "${typeName}").fill($fillVar)"""
}
private def fillVarWithSubTypeInstances(typeName : String, fillVar : String) = {
s"""g.V().has("${superTypeAttributeName}", "${typeName}").fill($fillVar)"""
}
}
import scala.language.existentials;
import org.apache.atlas.repository.RepositoryException
import org.apache.atlas.repository.RepositoryException
import org.apache.atlas.repository.RepositoryException
import org.apache.atlas.repository.RepositoryException
case class GraphPersistenceStrategy1(g: AtlasGraph[_,_]) extends GraphPersistenceStrategies {
......@@ -458,8 +330,6 @@ case class GraphPersistenceStrategy1(g: AtlasGraph[_,_]) extends GraphPersistenc
}
}
private def mapVertexToCollectionEntry(instanceVertex: AtlasVertex[_,_], attributeInfo: AttributeInfo, elementType: IDataType[_], i: ITypedInstance, value: Any): Any = {
elementType.getTypeCategory match {
case DataTypes.TypeCategory.PRIMITIVE => value
......@@ -474,5 +344,6 @@ case class GraphPersistenceStrategy1(g: AtlasGraph[_,_]) extends GraphPersistenc
throw new UnsupportedOperationException(s"load for ${attributeInfo.dataType()} not supported")
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment