blob: 84b9289f1dcb676f902f0a2bd3779e6f1472c0ce [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Akos Horvath, Gergely Varro and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Akos Horvath, Gergely Varro - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.rgg;
import java.util.Queue;
import java.util.Vector;
import org.eclipse.viatra2.gtasm.patternmatcher.IMatching;
import org.eclipse.viatra2.gtasm.patternmatcher.exceptions.PatternMatcherRuntimeException;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.IKeyGenerator;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.MatchingFrame;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.MatchingKey;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.PatternMatcherErrorStrings;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.callgraph.FlattenedPattern;
public class IndexedRule extends Rule {
private RemoteGoal right;
private HistoryList<MatchingKey, MatchingFrame> leftInputQueue;
private Integer[] parameterMapping;
private Boolean[] adornment;
private int arraySize;
private IKeyGenerator<MatchingKey, MatchingFrame> keyGenerator;
public IndexedRule(FlattenedPattern pattern,
IQueueContentProvider left,
RemoteGoal right,
Integer[] parameterMapping,
Boolean[] adornment) {
super(pattern, left);
arraySize = 0;
Vector<Integer> v = new Vector<Integer>();
for (int i = 0; i < adornment.length; i++) {
if (adornment[i]) {
v.add(parameterMapping[i]);
arraySize++;
}
}
final Integer[] keys = new Integer[arraySize];
v.toArray(keys);
keyGenerator = new IKeyGenerator<MatchingKey, MatchingFrame>() {
public MatchingKey calculateKey(MatchingFrame value) {
Object[] matchingKey = new Object[keys.length];
for (int i = 0; i < keys.length; i++) {
matchingKey[i] = value.getValue(keys[i]);
}
return new MatchingKey(matchingKey);
}
public int size() {
return keys.length;
}
};
this.right = right;
this.leftInputQueue = new HistoryList<MatchingKey, MatchingFrame>(keyGenerator);
this.parameterMapping = parameterMapping;
this.adornment = adornment;
}
public void traverseAll(Queue<MatchingFrame> resultQueue, MatchingFrame template) throws PatternMatcherRuntimeException {
left.traverseAll(leftInputQueue, template);
right.calculateAllDeltas(this, resultQueue);
}
MatchingKey getBoundHeader(MatchingFrame frame) {
return keyGenerator.calculateKey(frame);
}
void merge(MatchingFrame result, IMatching other)
throws PatternMatcherRuntimeException {
assert parameterMapping.length == adornment.length;
for (int i = 0; i < parameterMapping.length; i++) {
if (adornment[i]) {
if (!result.getValue(parameterMapping[i]).equals(other.lookup(i))) {
{
String[] context = {pattern.getParent().getPattern().getName()};
throw new PatternMatcherRuntimeException(
PatternMatcherErrorStrings.INTERNAL_PATTERNCALL_ADORNED_INPUT_PARAMS
,context
,pattern.getParent().getPattern());
}
}
} else {
result.setValue(parameterMapping[i], other.lookup(i));
}
}
}
HistoryList<MatchingKey, MatchingFrame> getQueue() {
return leftInputQueue;
}
@Override
public void synchronize() {
left.synchronize();
leftInputQueue.clear();
}
@Override
public void init(){
leftInputQueue.init();
left.init(); // cannot be initialized (again) as it creates an infinite loop
//right.init();
}
/*
public MatchingFrame traverse(Queue<MatchingFrame> resultQueue, MatchingFrame template) throws PatternMatcherRuntimeException {
// left.traverse(leftInputQueue, template);
// right.calculateDelta(this, resultQueue);
return null;
}
public void initalizeSingleTraversal(Queue<MatchingFrame> queue, MatchingFrame template) {
left.initalizeSingleTraversal(leftInputQueue, template);
}
public MatchingFrame traverse(MatchingFrame template) throws PatternMatcherRuntimeException {
return null;
}
*/
}