blob: 9f79979ee780514e221f08d670630032487de17b [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Gabor Bergmann 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:
* Gabor Bergmann - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.patternmatcher.incremental.simple;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.emf.common.util.EList;
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.adapters.GTASMBuildable;
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.hardwired.HardwiredDirectInstantiation;
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.hardwired.HardwiredTransitiveClosure;
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.construction.IRetePatternBuilder;
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.construction.RetePatternBuildException;
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherStringTypedContext;
import org.eclipse.viatra2.gtasm.support.helper.GTASMHelper;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.gt.GTPattern;
// import org.eclipse.viatra2.gtasm.patternmatcher.term.TermHandler;
/**
* @author Gabor Bergmann
*
*/
public class SimpleReteBuilder<StubHandle, Collector> implements IRetePatternBuilder<GTPattern, StubHandle, Collector> {
protected GTASMBuildable<StubHandle, Collector> baseBuildable;
protected IPatternMatcherStringTypedContext<GTPattern> context;
/**
* Prerequisite: engine has its network and boundary fields initialized
*
* @param logger
*/
public SimpleReteBuilder(GTASMBuildable<StubHandle, Collector> baseBuildable, IPatternMatcherStringTypedContext<GTPattern> context) {
super();
this.baseBuildable = baseBuildable;
this.context = context;
//refresh();
}
public void refresh() {
baseBuildable.reinitialize();
// this.context = engine.getContext();
// this.reteNet = engine.getReteNet();
// this.headContainer = reteNet.getHeadContainer();
// this.boundary = engine.getBoundary();
}
public Collector construct(GTPattern gtPattern) throws RetePatternBuildException {
Collector production = baseBuildable.putOnTab(gtPattern).patternCollector(gtPattern);
Map<String, String> incrementalAnnotation = GTASMHelper.extractLowerCaseRuntimeAnnotation(gtPattern, "@Incremental");
if (incrementalAnnotation!=null && incrementalAnnotation.get("reinterpret")!=null) {
if (incrementalAnnotation.get("reinterpret").equals("directInstantiation")) {
return new HardwiredDirectInstantiation<StubHandle, Collector>(baseBuildable, context, gtPattern, production).run();
} else if (incrementalAnnotation.get("reinterpret").equals("transitiveClosure")) {
String ofPatternName = incrementalAnnotation.get("ofPattern");
EList<GTPattern> gtPatternDefinitions = gtPattern.getContainer().getGtPatternDefinitions();
for (GTPattern ofPattern : gtPatternDefinitions) {
if (ofPattern.getName().equals(ofPatternName))
return new HardwiredTransitiveClosure<StubHandle, Collector>(
baseBuildable, context, gtPattern, ofPattern, production).run();
}
throw new RetePatternBuildException(
"Sibling pattern not found: {1}",
new String[]{ofPatternName}, gtPattern);
} else {
throw new RetePatternBuildException(
"Unsupported reinterpret annotation: {1}",
new String[]{incrementalAnnotation.get("reinterpret")}, gtPattern);
}
} else return new PatternScaffold<StubHandle, Collector>(this, gtPattern, production).run();
}
// public Address<? extends Production> constructScoper(
// Collector unscopedProduction,
// Map<Integer, Scope> additionalScopeMap,
// Collector production)
// throws PatternMatcherCompileTimeException {
//
// return new ScopeExtensionScaffold(this, unscopedProduction, additionalScopeMap, production).run();
// }
public HashMap<Object, Integer> getPosMapping(GTPattern gtPattern) {
HashMap<Object, Integer> posMapping = new HashMap<Object, Integer>();
int l = 0;
for (Object o : gtPattern.getSymParameters()) {
posMapping.put(o, l++);
}
return posMapping;
}
// /* (non-Javadoc)
// * @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.construction.IRetePatternBuilder#getBuildable()
// */
// @Override
// public GTASMBuildable<StubHandle, Collector> getBuildable() {
// return baseBuildable;
// }
/* (non-Javadoc)
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.construction.IRetePatternBuilder#getContext()
*/
@Override
public IPatternMatcherStringTypedContext<GTPattern> getContext() {
return context;
}
}