Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
#359 cleaning xbase parts in runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
okrosa committed Nov 29, 2012
1 parent 116b49c commit 67c257d
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 178 deletions.
Expand Up @@ -8,7 +8,6 @@
* Contributors:
* Gabor Bergmann - initial API and implementation
*******************************************************************************/

package org.eclipse.incquery.runtime.internal.matcherbuilder;

import java.util.ArrayList;
Expand All @@ -28,101 +27,64 @@
import org.eclipse.xtext.xbase.XExpression;

/**
* XExpression check constraint: the given XExpression formed over the variables
* must evaluate to true.
*
* @author Bergmann Gábor
*
* XExpression check constraint: the given XExpression formed over the variables must evaluate to true.
*/
public class XBaseCheck<StubHandle> extends
BaseTypeSafePredicateCheck<Pattern, StubHandle> {

private final XExpression xExpression;
private final EPMBodyToPSystem<StubHandle, ?> pGraph;
private final Pattern pattern;

// private final IQualifiedNameProvider nameProvider;
// private final IExpressionInterpreter interpreter = 
// IncQueryRuntimePlugin.getDefault().getInjector().getInstance(IExpressionInterpreter.class);

/**
* @param pSystem
* @param affectedVariables
*/
public XBaseCheck(EPMBodyToPSystem<StubHandle, ?> pGraph,
XExpression xExpression, Pattern pattern) {
super(pGraph.pSystem, getExternalPNodeReferencesOfXExpression(pGraph,
xExpression));
this.pGraph = pGraph;
this.xExpression = xExpression;
this.pattern = pattern;
@SuppressWarnings("restriction")
public class XBaseCheck<StubHandle> extends BaseTypeSafePredicateCheck<Pattern, StubHandle> {

// Injector injector = XtextInjectorProvider.INSTANCE.getInjector();
// nameProvider = injector.getInstance(IQualifiedNameProvider.class);
}
private final XExpression xExpression;
private final EPMBodyToPSystem<StubHandle, ?> pGraph;
private final Pattern pattern;

@Override
protected Stub<StubHandle> doCheckOn(Stub<StubHandle> stub)
throws RetePatternBuildException {
Set<Integer> affectedIndices = new HashSet<Integer>();
// Map<QualifiedName, Integer> qualifiedNameMap = new
// HashMap<QualifiedName, Integer>();
Map<String, Integer> tupleNameMap = new HashMap<String, Integer>();
Set<Variable> variables = CorePatternLanguageHelper
.getReferencedPatternVariablesOfXExpression(xExpression);
for (Variable variable : variables) {
PVariable pNode = pGraph.getPNode(variable);
Integer position = stub.getVariablesIndex().get(pNode);
// qualifiedNameMap.put(
// QualifiedName.create(variable.getSimpleName()), position);
tupleNameMap.put(variable.getSimpleName(), position);
affectedIndices.add(position);
}
int[] indices = new int[affectedIndices.size()];
int k = 0;
for (Integer index : affectedIndices)
indices[k++] = index;
/**
* @param pSystem
* @param affectedVariables
*/
public XBaseCheck(EPMBodyToPSystem<StubHandle, ?> pGraph, XExpression xExpression, Pattern pattern) {
super(pGraph.pSystem, getExternalPNodeReferencesOfXExpression(pGraph, xExpression));
this.pGraph = pGraph;
this.xExpression = xExpression;
this.pattern = pattern;
}

// Map<String, String> variableEquivalence = new HashMap<String,
// String>();
// Set<String> variableNames = extractAffectedVariableNames(pGraph,
// topTerm);
// for (String name : variableNames) {
// PVariable pNode = pGraph.getPNode(name);
// variableEquivalence.put(name, pNode.getName());
// variableIndices.put(pNode.getName(),
// stub.getVariablesIndex().get(pNode));
// }
// return
// gtBuildable.buildGTASMTermChecker(topTerm, variableIndices,
// variableEquivalence, null, stub);
XBaseEvaluator evaluator = new XBaseEvaluator(xExpression,
tupleNameMap, pattern);
return buildable.buildPredicateChecker(evaluator, null, indices, stub);
@Override
protected Stub<StubHandle> doCheckOn(Stub<StubHandle> stub) throws RetePatternBuildException {
Set<Integer> affectedIndices = new HashSet<Integer>();
Map<String, Integer> tupleNameMap = new HashMap<String, Integer>();
Set<Variable> variables = CorePatternLanguageHelper.getReferencedPatternVariablesOfXExpression(xExpression);
for (Variable variable : variables) {
PVariable pNode = pGraph.getPNode(variable);
Integer position = stub.getVariablesIndex().get(pNode);
tupleNameMap.put(variable.getSimpleName(), position);
affectedIndices.add(position);
}
int[] indices = new int[affectedIndices.size()];
int k = 0;
for (Integer index : affectedIndices)
indices[k++] = index;

}
XBaseEvaluator evaluator = new XBaseEvaluator(xExpression, tupleNameMap, pattern);
return buildable.buildPredicateChecker(evaluator, null, indices, stub);
}

private static Set<PVariable> getExternalPNodeReferencesOfXExpression(
EPMBodyToPSystem<?, ?> pGraph, XExpression xExpression) {
Set<PVariable> result = new HashSet<PVariable>();
Set<Variable> variables = CorePatternLanguageHelper
.getReferencedPatternVariablesOfXExpression(xExpression);
for (Variable variable : variables) {
result.add(pGraph.getPNode(variable));
}
return result;
}
private static Set<PVariable> getExternalPNodeReferencesOfXExpression(EPMBodyToPSystem<?, ?> pGraph,
XExpression xExpression) {
Set<PVariable> result = new HashSet<PVariable>();
Set<Variable> variables = CorePatternLanguageHelper.getReferencedPatternVariablesOfXExpression(xExpression);
for (Variable variable : variables) {
result.add(pGraph.getPNode(variable));
}
return result;
}

@Override
protected String toStringRest() {
return new FlatTuple(
new ArrayList<PVariable>(getAffectedVariables()).toArray())
.toString()
+ "|=" + xExpression.toString();
}
@Override
protected String toStringRest() {
return new FlatTuple(new ArrayList<PVariable>(getAffectedVariables()).toArray()).toString() + "|="
+ xExpression.toString();
}

@Override
protected void doReplaceVariable(PVariable obsolete, PVariable replacement) {
}
@Override
protected void doReplaceVariable(PVariable obsolete, PVariable replacement) {
}

}
Expand Up @@ -8,7 +8,6 @@
* Contributors:
* Gabor Bergmann - initial API and implementation
*******************************************************************************/

package org.eclipse.incquery.runtime.internal.matcherbuilder;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -40,102 +39,87 @@

/**
* Evaluates an XBase XExpression inside Rete.
*
* @author Bergmann Gábor
*/
@SuppressWarnings("restriction")
public class XBaseEvaluator extends AbstractEvaluator {
private final XExpression xExpression;
private final Map<String, Integer> tupleNameMap;
private final Pattern pattern;
private final XExpression xExpression;
private final Map<String, Integer> tupleNameMap;
private final Pattern pattern;

private IMatchChecker matchChecker;
private IMatchChecker matchChecker;

private XbaseInterpreter interpreter;
private Provider<IEvaluationContext> contextProvider;
private XbaseInterpreter interpreter;
private Provider<IEvaluationContext> contextProvider;

/**
* @param xExpression
* the expression to evaluate
* @param qualifiedMapping
* maps variable qualified names to positions.
* @param pattern
*/
public XBaseEvaluator(XExpression xExpression,
Map<String, Integer> tupleNameMapping, Pattern pattern) {
super();
this.xExpression = xExpression;
this.tupleNameMap = tupleNameMapping;
this.pattern = pattern;
/**
* @param xExpression
* the expression to evaluate
* @param qualifiedMapping
* maps variable qualified names to positions.
* @param pattern
*/
public XBaseEvaluator(XExpression xExpression, Map<String, Integer> tupleNameMapping, Pattern pattern) {
super();
this.xExpression = xExpression;
this.tupleNameMap = tupleNameMapping;
this.pattern = pattern;

// First try to setup the generated code from the extension point
IConfigurationElement[] configurationElements = Platform
.getExtensionRegistry().getConfigurationElementsFor(
IExtensions.XEXPRESSIONEVALUATOR_EXTENSION_POINT_ID);
for (IConfigurationElement configurationElement : configurationElements) {
String id = configurationElement.getAttribute("id");
if (id.equals(CheckExpressionUtil.getExpressionUniqueID(pattern,
xExpression))) {
Object object = null;
try {
object = configurationElement
.createExecutableExtension("evaluatorClass");
} catch (CoreException e) {
e.printStackTrace();
}
if (object != null && object instanceof IMatchChecker) {
matchChecker = (IMatchChecker) object;
}
}
}
// First try to setup the generated code from the extension point
IConfigurationElement[] configurationElements = Platform.getExtensionRegistry().getConfigurationElementsFor(
IExtensions.XEXPRESSIONEVALUATOR_EXTENSION_POINT_ID);
for (IConfigurationElement configurationElement : configurationElements) {
String id = configurationElement.getAttribute("id");
if (id.equals(CheckExpressionUtil.getExpressionUniqueID(pattern, xExpression))) {
Object object = null;
try {
object = configurationElement.createExecutableExtension("evaluatorClass");
} catch (CoreException e) {
e.printStackTrace();
}
if (object != null && object instanceof IMatchChecker) {
matchChecker = (IMatchChecker) object;
}
}
}

// Second option, setup the attributes for the interpreted approach
if (matchChecker == null) {
Injector injector = XtextInjectorProvider.INSTANCE.getInjector();
interpreter = (XbaseInterpreter) injector
.getInstance(IExpressionInterpreter.class);
try {
ClassLoader classLoader = ClassLoaderUtil
.getClassLoader(CheckExpressionUtil.getIFile(pattern));
if (classLoader != null) {
interpreter.setClassLoader(ClassLoaderUtil
.getClassLoader(CheckExpressionUtil
.getIFile(pattern)));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
contextProvider = injector.getProvider(IEvaluationContext.class);
}
}
// Second option, setup the attributes for the interpreted approach
if (matchChecker == null) {
Injector injector = XtextInjectorProvider.INSTANCE.getInjector();
interpreter = (XbaseInterpreter) injector.getInstance(IExpressionInterpreter.class);
try {
ClassLoader classLoader = ClassLoaderUtil.getClassLoader(CheckExpressionUtil.getIFile(pattern));
if (classLoader != null) {
interpreter.setClassLoader(ClassLoaderUtil.getClassLoader(CheckExpressionUtil.getIFile(pattern)));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (CoreException e) {
e.printStackTrace();
}
contextProvider = injector.getProvider(IEvaluationContext.class);
}
}

@Override
public Object doEvaluate(Tuple tuple) throws Throwable {
// First option: try to evalute with the generated code
if (matchChecker != null) {
return matchChecker.evaluateXExpression(tuple, tupleNameMap);
}
@Override
public Object doEvaluate(Tuple tuple) throws Throwable {
// First option: try to evalute with the generated code
if (matchChecker != null) {
return matchChecker.evaluateXExpression(tuple, tupleNameMap);
}

// Second option: try to evaluate with the interpreted approach
IEvaluationContext context = contextProvider.get();
for (Entry<String, Integer> entry : tupleNameMap.entrySet()) {
context.newValue(QualifiedName.create(entry.getKey()),
tuple.get(entry.getValue()));
}
IEvaluationResult result = interpreter.evaluate(xExpression, context,
CancelIndicator.NullImpl);
if (result == null)
throw new IncQueryException(
String.format(
"XBase expression interpreter returned no result while evaluating expression %s in pattern %s.",
xExpression, pattern),
"XBase expression interpreter returned no result.");
if (result.getException() != null)
throw result.getException();
return result.getResult();
}
// Second option: try to evaluate with the interpreted approach
IEvaluationContext context = contextProvider.get();
for (Entry<String, Integer> entry : tupleNameMap.entrySet()) {
context.newValue(QualifiedName.create(entry.getKey()), tuple.get(entry.getValue()));
}
IEvaluationResult result = interpreter.evaluate(xExpression, context, CancelIndicator.NullImpl);
if (result == null)
throw new IncQueryException(String.format(
"XBase expression interpreter returned no result while evaluating expression %s in pattern %s.",
xExpression, pattern), "XBase expression interpreter returned no result.");
if (result.getException() != null)
throw result.getException();
return result.getResult();
}

}

0 comments on commit 67c257d

Please sign in to comment.