| /******************************************************************************* |
| * 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.ArrayList; |
| import java.util.HashSet; |
| import java.util.Iterator; |
| import java.util.LinkedList; |
| import java.util.List; |
| import java.util.Queue; |
| |
| import org.eclipse.viatra2.gtasm.patternmatcher.exceptions.PatternMatcherRuntimeException; |
| import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.MatchingFrame; |
| import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.MatchingKey; |
|
|
|
|
| public class MagicSet extends RuleGoalGraphNode
|
| implements IQueueContentProvider {
|
| private ArrayList<MatchingKey> old;
|
| private List<MatchingKey> next;
|
| //private int firstNew;
|
| private Boolean[] adornment; |
| private HashSet<MatchingKey> cache;
|
|
|
| public MagicSet(Boolean[] adornment) {
|
| this.old = new ArrayList<MatchingKey>();
|
| this.next = new LinkedList<MatchingKey>();
|
| //this.firstNew = 0;
|
| this.adornment = adornment; |
| this.cache = new HashSet<MatchingKey>(); //stores the old values already matched, only one instance per matching
|
| }
|
|
|
| public void traverseAll(Queue<MatchingFrame> queue, MatchingFrame template)
|
| throws PatternMatcherRuntimeException {
|
| //ListIterator<MatchingKey> li = old.listIterator(firstNew);
|
| Iterator<MatchingKey> li = old.iterator(); |
| while (li.hasNext()) {
|
| Object[] array = li.next().toArray();
|
| MatchingFrame frame = (MatchingFrame) template.clone();
|
| int counter = 0;
|
| for (int i = 0; i < adornment.length; i++) {
|
| if (adornment[i]) {
|
| frame.setValue(i, array[counter++]);
|
| }
|
| }
|
| queue.offer(frame);
|
| } |
| }
|
|
|
| public void addArray(MatchingKey array) {
|
| next.add(array);
|
| }
|
|
|
| public void synchronize() {}
|
|
|
| boolean synchronizeFromGoal() {
|
| boolean result = false; |
| old.clear();
|
| // firstNew = old.size(); |
| for (MatchingKey element : next) {
|
| if (!cache.contains(element)) { |
| // if (!old.contains(element)) { |
|
|
| old.add(element); |
| cache.add(element);
|
| result = true;
|
| }
|
| }
|
| next.clear();
|
| return result;
|
| }
|
|
|
| public void initalizeSingleTraversal(Queue<MatchingFrame> queue, MatchingFrame template) {
|
| //ListIterator<MatchingKey> li = old.listIterator(firstNew); |
| Iterator<MatchingKey> li = old.iterator();
|
| while (li.hasNext()) {
|
| Object[] array = li.next().toArray();
|
| MatchingFrame frame = (MatchingFrame) template.clone();
|
| int counter = 0;
|
| for (int i = 0; i < adornment.length; i++) {
|
| if (adornment[i]) {
|
| frame.setValue(i, array[counter++]);
|
| }
|
| }
|
| queue.offer(frame);
|
| } |
| }
|
|
|
| public MatchingFrame traverse() throws PatternMatcherRuntimeException {
|
| return null;
|
| }
|
|
|
| public void init() {
|
| // firstNew = 0; |
| cache.clear();
|
| old.clear();
|
| }
|
| }
|