Skip to content

Commit

Permalink
[513391] Added the revised Query testing API.
Browse files Browse the repository at this point in the history
Change-Id: Ie88472f25aba634a2a4a0a8c15070d9a3ea19f89
Signed-off-by: lunkpeter <[email protected]>
(cherry picked from commit 2eff724)
  • Loading branch information
lunkpeter authored and ujhelyiz committed Mar 16, 2017
1 parent fd68026 commit 094a8c3
Show file tree
Hide file tree
Showing 49 changed files with 6,919 additions and 6,101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ package org.eclipse.viatra.query.testing.core

import com.google.common.collect.Sets
import java.util.Set
import org.eclipse.viatra.query.testing.core.internal.MatchRecordEquvalence
import org.eclipse.viatra.query.testing.core.api.MatchRecordEquivalence
import org.eclipse.viatra.query.testing.core.internal.DefaultMatchRecordEquivalence
import org.eclipse.viatra.query.testing.snapshot.MatchRecord
import org.eclipse.viatra.query.testing.snapshot.MatchSetRecord

Expand Down Expand Up @@ -39,17 +40,21 @@ class MatchSetRecordDiff {
def boolean isEmpty(){
additions.empty && removals.empty
}

def static compute(MatchSetRecord expected, MatchSetRecord actual) {
val expectedSet = MatchRecordEquvalence::INSTANCE.wrap(expected.matches)
val actualSet = MatchRecordEquvalence::INSTANCE.wrap(actual.matches)
compute(expected, actual, new DefaultMatchRecordEquivalence())
}

def static compute(MatchSetRecord expected, MatchSetRecord actual, MatchRecordEquivalence equivalence) {
val expectedSet = equivalence.wrap(expected.matches)
val actualSet = equivalence.wrap(actual.matches)

val unexpected = Sets::difference(actualSet, expectedSet)
val missing = Sets::difference(expectedSet, actualSet)

new MatchSetRecordDiff(
MatchRecordEquvalence::INSTANCE.unwrap(unexpected),
MatchRecordEquvalence::INSTANCE.unwrap(missing)
equivalence.unwrap(unexpected),
equivalence.unwrap(missing)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
package org.eclipse.viatra.query.testing.core;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine;
import org.eclipse.viatra.query.runtime.api.IPatternMatch;
Expand All @@ -18,14 +21,21 @@
import org.eclipse.viatra.query.runtime.emf.EMFScope;
import org.eclipse.viatra.query.runtime.exception.ViatraQueryException;
import org.eclipse.viatra.query.runtime.matchers.backend.QueryEvaluationHint;
import org.eclipse.viatra.query.testing.core.api.JavaObjectAccess;
import org.eclipse.viatra.query.testing.snapshot.MatchSetRecord;

public class PatternBasedMatchSetModelProvider implements IMatchSetModelProvider {

private QueryEvaluationHint hint;
private SnapshotHelper helper;

public PatternBasedMatchSetModelProvider(QueryEvaluationHint hint) {
this(hint,new HashMap<String, JavaObjectAccess>());
}

public PatternBasedMatchSetModelProvider(QueryEvaluationHint hint, Map<String, JavaObjectAccess> accessmap) {
this.hint = hint;
this.helper = new SnapshotHelper(accessmap);
}

private AdvancedViatraQueryEngine engine;
Expand All @@ -40,7 +50,7 @@ public <Match extends IPatternMatch> MatchSetRecord getMatchSetRecord(EMFScope s
}
ViatraQueryMatcher<Match> matcher = (ViatraQueryMatcher<Match>) ((AdvancedViatraQueryEngine) engine)
.getMatcher(querySpecification, hint);
return new SnapshotHelper().createMatchSetRecordForMatcher(matcher,
return helper.createMatchSetRecordForMatcher(matcher,
filter == null ? matcher.newEmptyMatch() : filter);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
/**
* Copyright (c) 2010-2016, Gabor Bergmann, IncQuery Labs Ltd.
* 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.viatra.query.testing.core

import org.eclipse.viatra.query.testing.core.QueryPerformanceTest

/**
* A variant of {@link QueryPerformanceTest}, where an ensemble of queries is measured together, instead of as individual queries.
*
* @author Gabor Bergmann
*/
abstract class QueryGroupPerformanceTest extends QueryPerformanceTest {
override measureEntireGroup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

package org.eclipse.viatra.query.testing.core

import com.google.common.collect.Maps
import java.util.ArrayList
import java.util.Date
import org.eclipse.emf.ecore.EEnumLiteral
import java.util.Map
import org.eclipse.emf.common.util.Enumerator
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
Expand All @@ -22,6 +24,7 @@ import org.eclipse.viatra.query.runtime.api.IQuerySpecification
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher
import org.eclipse.viatra.query.runtime.emf.EMFScope
import org.eclipse.viatra.query.testing.core.api.JavaObjectAccess
import org.eclipse.viatra.query.testing.snapshot.BooleanSubstitution
import org.eclipse.viatra.query.testing.snapshot.DateSubstitution
import org.eclipse.viatra.query.testing.snapshot.DoubleSubstitution
Expand All @@ -36,13 +39,30 @@ import org.eclipse.viatra.query.testing.snapshot.MatchSetRecord
import org.eclipse.viatra.query.testing.snapshot.MatchSubstitutionRecord
import org.eclipse.viatra.query.testing.snapshot.MiscellaneousSubstitution
import org.eclipse.viatra.query.testing.snapshot.QuerySnapshot
import org.eclipse.viatra.query.testing.snapshot.SerializedJavaObjectSubstitution
import org.eclipse.viatra.query.testing.snapshot.SnapshotFactory
import org.eclipse.viatra.query.testing.snapshot.StringSubstitution

/**
* Helper methods for dealing with snapshots and match set records.
*/
class SnapshotHelper {

private final Map<String, JavaObjectAccess> accessMap;

new(){
this(Maps.newHashMap)
}

/**
* Initializes a {@link SnapshotHelper} with a map containing {@link JavaObjectAccess} objects for
* serialization and deserialization of plain Java types.
*
* @since 1.6
*/
new(Map<String, JavaObjectAccess> accessMap){
this.accessMap = accessMap
}

/**
* Returns the actual value of the substitution based on its type
Expand All @@ -59,6 +79,7 @@ class SnapshotHelper {
LongSubstitution: substitution.value
MiscellaneousSubstitution: substitution.value
StringSubstitution: substitution.value
SerializedJavaObjectSubstitution: substitution
}
}

Expand Down Expand Up @@ -113,31 +134,31 @@ class SnapshotHelper {
default: InputSpecification::UNSET
}
}

/**
* Saves the matches of the given matcher (using the partial match) into the given snapshot.
* If the input specification is not yet filled, it is now filled based on the engine of the matcher.
*/
def saveMatchesToSnapshot(ViatraQueryMatcher matcher, IPatternMatch partialMatch, QuerySnapshot snapshot){
val patternFQN = matcher.patternName
val actualRecord = SnapshotFactory::eINSTANCE.createMatchSetRecord
actualRecord.patternQualifiedName = patternFQN
// 1. put actual match set record in the same model with the expected
snapshot.matchSetRecords.add(actualRecord)
// 2. store model roots
if(snapshot.inputSpecification == InputSpecification::UNSET){
snapshot.modelRoots.addAll(matcher.engine.modelRootsForEngine)
snapshot.modelRoots.remove(snapshot)
snapshot.inputSpecification = matcher.inputSpecificationForMatcher
}
actualRecord.filter = partialMatch.createMatchRecordForMatch
* Saves the matches of the given matcher (using the partial match) into the given snapshot.
* If the input specification is not yet filled, it is now filled based on the engine of the matcher.
*/
def saveMatchesToSnapshot(ViatraQueryMatcher matcher, IPatternMatch partialMatch, QuerySnapshot snapshot){
val patternFQN = matcher.patternName
val actualRecord = SnapshotFactory::eINSTANCE.createMatchSetRecord
actualRecord.patternQualifiedName = patternFQN
// 1. put actual match set record in the same model with the expected
snapshot.matchSetRecords.add(actualRecord)
// 2. store model roots
if(snapshot.inputSpecification == InputSpecification::UNSET){
snapshot.modelRoots.addAll(matcher.engine.modelRootsForEngine)
snapshot.modelRoots.remove(snapshot)
snapshot.inputSpecification = matcher.inputSpecificationForMatcher
}
actualRecord.filter = partialMatch.createMatchRecordForMatch

// 3. create match set records
matcher.forEachMatch(partialMatch)[match |
actualRecord.matches.add(match.createMatchRecordForMatch)
]
return actualRecord
}
// 3. create match set records
matcher.forEachMatch(partialMatch)[match |
actualRecord.matches.add(match.createMatchRecordForMatch)
]
return actualRecord
}

/**
* Creates a match record that corresponds to the given match.
Expand Down Expand Up @@ -168,6 +189,7 @@ class SnapshotHelper {
/**
* Creates a partial match that corresponds to the given match record.
* Each substitution is used as a value for the parameter with the same name.
*
* @deprecated use createMatchForMatchRecord(IQuerySpecification, MatchRecord) instead
*/
@Deprecated
Expand Down Expand Up @@ -199,13 +221,14 @@ class SnapshotHelper {
return match
}


/**
* Saves all matches of the given matcher into the given snapshot.
* If the input specification is not yet filled, it is now filled based on the engine of the matcher.
*/
def saveMatchesToSnapshot(ViatraQueryMatcher matcher, QuerySnapshot snapshot){
matcher.saveMatchesToSnapshot(matcher.newEmptyMatch, snapshot)
}
* Saves all matches of the given matcher into the given snapshot.
* If the input specification is not yet filled, it is now filled based on the engine of the matcher.
*/
def saveMatchesToSnapshot(ViatraQueryMatcher matcher, QuerySnapshot snapshot){
matcher.saveMatchesToSnapshot(matcher.newEmptyMatch, snapshot)
}

/**
* Returns the match set record for the given pattern FQN from the snapshot,
Expand Down Expand Up @@ -233,13 +256,12 @@ class SnapshotHelper {
*/
def createSubstitution(String parameterName, Object value){
return switch(value) {
EEnumLiteral: {
val sub = SnapshotFactory::eINSTANCE.createEnumSubstitution
sub.setValueLiteral((value as EEnumLiteral).literal)
sub.setEnumType((value as EEnumLiteral).EEnum)
sub.setParameterName(parameterName)
sub
}
Enumerator: {
val sub = SnapshotFactory::eINSTANCE.createEnumSubstitution
sub.setValueLiteral((value as Enumerator).literal)
sub.setParameterName(parameterName)
sub
}
EObject : {
val sub = SnapshotFactory::eINSTANCE.createEMFSubstitution
sub.setValue(value as EObject)
Expand Down Expand Up @@ -289,10 +311,17 @@ class SnapshotHelper {
sub
}
default : {
val sub = SnapshotFactory::eINSTANCE.createMiscellaneousSubstitution
sub.setValue(value)
sub.setParameterName(parameterName)
sub
val access = accessMap.get(value.class.name)
if(access!=null){
val sub = access.toSubstitution(value)
sub.parameterName = parameterName
sub
}else{
val sub = SnapshotFactory::eINSTANCE.createMiscellaneousSubstitution
sub.setValue(value)
sub.setParameterName(parameterName)
sub
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import org.eclipse.viatra.query.patternlanguage.emf.eMFPatternLanguage.PatternMo
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher
import org.eclipse.viatra.query.runtime.emf.EMFScope
import org.eclipse.viatra.query.testing.core.api.MatchRecordEquivalence
import org.eclipse.viatra.query.testing.core.internal.DefaultMatchRecordEquivalence
import org.eclipse.viatra.query.testing.snapshot.MatchRecord
import org.eclipse.viatra.query.testing.snapshot.MatchSetRecord
import org.eclipse.viatra.query.testing.snapshot.QuerySnapshot
Expand Down Expand Up @@ -73,14 +75,26 @@ class TestExecutor {
return true

}

/**
* Compares the match set of a given matcher with the given match record
* using VIATRA Query as a compare tool.
* Therefore the comparison depends on correct VIATRA Query query evaluation
* (for a given limited pattern language feature set).
*/
def compareResultSetsAsRecords(ViatraQueryMatcher matcher, MatchSetRecord expected){
compareResultSetsAsRecords(matcher, expected, new DefaultMatchRecordEquivalence())
}

/**
* Compares the match set of a given matcher with the given match record
* using VIATRA Query as a compare tool.
* Therefore the comparison depends on correct VIATRA Query query evaluation
* (for a given limited pattern language feature set).
* using VIATRA Query as a compare tool.
*
* The comparison logic can be specified via the equivalence parameter.
*
* @since 1.6
*/
def compareResultSetsAsRecords(ViatraQueryMatcher matcher, MatchSetRecord expected){
def compareResultSetsAsRecords(ViatraQueryMatcher matcher, MatchSetRecord expected, MatchRecordEquivalence equivalence){
val diff = newHashSet

// 1. Validate match set record against matcher
Expand All @@ -100,7 +114,7 @@ class TestExecutor {
val actual = matcher.saveMatchesToSnapshot(partialMatch,snapshot)

// 3. Compute diff
val matchdiff = MatchSetRecordDiff::compute(expected, actual)
val matchdiff = MatchSetRecordDiff::compute(expected, actual, equivalence)

// 4. Print results
diff.addAll(matchdiff.additions.map[UNEXPECTED_MATCH + " (" + it.prettyPrint + ")"])
Expand Down
Loading

0 comments on commit 094a8c3

Please sign in to comment.