/******************************************************************************* | |
* 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.adapters; | |
import java.lang.ref.WeakReference; | |
import java.util.Collection; | |
import org.eclipse.viatra2.core.IEntity; | |
import org.eclipse.viatra2.core.IModelElement; | |
import org.eclipse.viatra2.core.IModelManager; | |
import org.eclipse.viatra2.core.IRelation; | |
import org.eclipse.viatra2.framework.IFramework; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.boundary.IManipulationListener; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherStringTypedContext; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherRuntimeContext; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.ReteEngine; | |
import org.eclipse.viatra2.logger.Logger; | |
public abstract class VPMContext<PatternDescription> | |
implements IPatternMatcherStringTypedContext<PatternDescription>, | |
IPatternMatcherRuntimeContext<PatternDescription> | |
{ | |
WeakReference<IFramework> framework; | |
IModelManager manager; | |
/** | |
* @param framework | |
*/ | |
public VPMContext(IFramework framework) { | |
super(); | |
this.framework = new WeakReference<IFramework>(framework); | |
this.manager = framework.getTopmodel().getModelManager(); | |
} | |
public EdgeInterpretation edgeInterpretation() { | |
return EdgeInterpretation.TERNARY; | |
} | |
public GeneralizationQueryDirection allowedGeneralizationQueryDirection() { | |
return GeneralizationQueryDirection.BOTH; | |
}; | |
public void enumerateAllUnaries(final ModelElementCrawler crawler) { | |
for (IEntity entity : manager.getEntities()) crawler.crawl(entity); | |
} | |
public void enumerateAllBinaryEdges(final ModelElementPairCrawler crawler) { | |
throw new UnsupportedOperationException(); | |
} | |
public void enumerateAllTernaryEdges(final ModelElementCrawler crawler) { | |
for (IRelation relation : manager.getRelations()) { | |
crawler.crawl(relation); | |
} | |
} | |
public Collection<? extends Object> enumerateDirectSupertypes(Object typeObject) { | |
return ((IModelElement)typeObject).getSupertypes(); | |
} | |
public Collection<? extends Object> enumerateDirectSubtypes(Object typeObject) { | |
return ((IModelElement)typeObject).getSubtypes(); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext#isUnaryType(java.lang.Object) | |
*/ | |
@Override | |
public boolean isUnaryType(Object typeObject) { | |
return typeObject instanceof IEntity; | |
} | |
public void enumerateDirectUnaryInstances(Object typeObject, final ModelElementCrawler crawler) { | |
for (IModelElement modelElement : ((IEntity)typeObject).getInstances()) crawler.crawl(modelElement); | |
} | |
public void enumerateAllUnaryInstances(Object typeObject, final ModelElementCrawler crawler) { | |
for (IModelElement modelElement : ((IEntity)typeObject).getAllInstances()) crawler.crawl(modelElement); | |
} | |
public Collection<? extends Object> enumerateDirectUnarySubtypes(Object typeObject) { | |
return ((IEntity)typeObject).getSubtypes(); | |
} | |
public Collection<? extends Object> enumerateDirectUnarySupertypes(Object typeObject) { | |
return ((IEntity)typeObject).getSupertypes(); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext#isBinaryType(java.lang.Object) | |
*/ | |
@Override | |
public boolean isBinaryEdgeType(Object typeObject) { | |
return false; | |
} | |
public void enumerateDirectBinaryEdgeInstances(Object typeObject, final ModelElementPairCrawler crawler) { | |
throw new UnsupportedOperationException(); | |
} | |
public void enumerateAllBinaryEdgeInstances(Object typeObject, final ModelElementPairCrawler crawler) { | |
throw new UnsupportedOperationException(); | |
} | |
public Collection<? extends Object> enumerateDirectBinaryEdgeSubtypes(Object typeObject) { | |
throw new UnsupportedOperationException(); | |
} | |
public Collection<? extends Object> enumerateDirectBinaryEdgeSupertypes(Object typeObject) { | |
throw new UnsupportedOperationException(); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext#isTernaryType(java.lang.Object) | |
*/ | |
@Override | |
public boolean isTernaryEdgeType(Object typeObject) { | |
return typeObject instanceof IRelation; | |
} | |
public void enumerateDirectTernaryEdgeInstances(Object typeObject, final ModelElementCrawler crawler) { | |
for (IModelElement modelElement : ((IRelation)typeObject).getInstances()) crawler.crawl(modelElement); | |
} | |
public void enumerateAllTernaryEdgeInstances(Object typeObject, final ModelElementCrawler crawler) { | |
for (IModelElement modelElement : ((IRelation)typeObject).getAllInstances()) crawler.crawl(modelElement); | |
} | |
public Collection<? extends Object> enumerateDirectTernaryEdgeSubtypes(Object typeObject) { | |
return ((IRelation)typeObject).getSubtypes(); | |
} | |
public Collection<? extends Object> enumerateDirectTernaryEdgeSupertypes(Object typeObject) { | |
return ((IRelation)typeObject).getSupertypes(); | |
} | |
public IFramework getFramework() { | |
return framework.get(); | |
} | |
public boolean inputConstructionEmbargo() { | |
return false; | |
} | |
public Object ternaryEdgeSource(Object relation) { | |
return ((IRelation)relation).getFrom(); | |
} | |
public Object ternaryEdgeTarget(Object relation) { | |
return ((IRelation)relation).getTo(); | |
} | |
public Object ternaryEdgeSourceType(Object typeObject) { | |
IRelation relation = (IRelation)typeObject; | |
return relation == null || relation.getIsAnyFrom() ? null : relation.getFrom(); | |
} | |
public Object ternaryEdgeTargetType(Object typeObject) { | |
IRelation relation = (IRelation)typeObject; | |
return relation == null || relation.getIsAnyTo() ? null : relation.getTo(); | |
} | |
public Object binaryEdgeSourceType(Object typeObject) { | |
throw new UnsupportedOperationException(); | |
} | |
public Object binaryEdgeTargetType(Object typeObject) { | |
throw new UnsupportedOperationException(); | |
} | |
public Object retrieveUnaryType(String fullyQualifiedName) { | |
return manager.getElementByName(fullyQualifiedName); | |
} | |
public Object retrieveBinaryEdgeType(String fullyQualifiedName) { | |
throw new UnsupportedOperationException(); // no references over VPM | |
//return framework.get().getTopmodel().getModelManager().getElementByName(fullyQualifiedName); | |
} | |
public Object retrieveTernaryEdgeType(String fullyQualifiedName) { | |
return manager.getElementByName(fullyQualifiedName); | |
} | |
public String retrieveUnaryTypeFQN(Object typeObject) { | |
return ((IEntity)typeObject).getFullyQualifiedName(); | |
} | |
public String retrieveBinaryEdgeTypeFQN(Object typeObject) { | |
throw new UnsupportedOperationException(); // no references over VPM | |
} | |
public String retrieveTernaryEdgeTypeFQN(Object typeObject) { | |
return ((IRelation)typeObject).getFullyQualifiedName(); | |
} | |
public Object resolveConstant(String fullyQualifiedName) { | |
return manager.getElementByName(fullyQualifiedName); | |
} | |
public void enumerateAllUnaryContainments(final ModelElementPairCrawler crawler) { | |
for (IEntity container : framework.get().getTopmodel() | |
.getModelManager().getEntities()) | |
for (IModelElement element : container.getContents()) | |
if (element instanceof IEntity) | |
crawler.crawl(container, element); | |
} | |
public void enumerateAllInstantiations(final ModelElementPairCrawler crawler) { | |
for (IModelElement type : framework.get().getTopmodel() | |
.getModelManager().getElements()) { | |
for (IModelElement instance : type.getInstances()) { | |
crawler.crawl(type, instance); | |
} | |
} | |
} | |
public void enumerateAllGeneralizations(final ModelElementPairCrawler crawler) { | |
for (IModelElement superType : framework.get().getTopmodel() | |
.getModelManager().getElements()) { | |
for (IModelElement subType : superType.getSubtypes()) { | |
crawler.crawl(superType, subType); | |
} | |
} | |
} | |
public void modelReadLock() { | |
framework.get().getTopmodel().getModelManager().getLock().readLock().lock(); | |
} | |
public void modelReadUnLock() { | |
framework.get().getTopmodel().getModelManager().getLock().readLock().unlock(); | |
} | |
public IManipulationListener subscribePatternMatcherForUpdates(ReteEngine<PatternDescription> engine) { | |
return new GlobalModelspaceListener(engine, framework.get()); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext#logDebug(java.lang.String) | |
*/ | |
public void logDebug(String message) { | |
framework.get().getLogger().debug(message); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext#logError(java.lang.String) | |
*/ | |
public void logError(String message) { | |
framework.get().getLogger().error(message); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext#logError(java.lang.String, java.lang.Throwable) | |
*/ | |
public void logError(String message, Throwable cause) { | |
framework.get().getLogger().message(Logger.ERROR, message, cause); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext#logWarning(java.lang.String) | |
*/ | |
public void logWarning(String message) { | |
framework.get().getLogger().warning(message); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext#logWarning(java.lang.String, java.lang.Throwable) | |
*/ | |
public void logWarning(String message, Throwable cause) { | |
framework.get().getLogger().message(Logger.WARNING, message, cause); | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherRuntimeContext#stopCoalescing() | |
*/ | |
public void finishCoalescing() { | |
// NO-OP over VPM | |
} | |
/* (non-Javadoc) | |
* @see org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherRuntimeContext#startCoalescing() | |
*/ | |
public void startCoalescing() { | |
// NO-OP over VPM | |
} | |
// public boolean checkBelowContainer(Object container, Object contained) { | |
// return ((IModelElement)contained).isBelowNamespace((IModelElement)container); | |
// } | |
// | |
// | |
// public boolean checkInContainer(Object container, Object contained) { | |
// return container.equals(((IModelElement) contained).getNamespace()); | |
// } | |
} |