Commit 21c9ef64 authored by Ludwig Dickmanns's avatar Ludwig Dickmanns
Browse files

MCDC: getAtoms( ... )



* The getAtoms method was added
* A simple test for the getAtoms method was added

Issue-Ref: &{task.key}
Issue-Url: &{task.url}
Signed-off-by: Ludwig Dickmanns's avatarLudwig Dickmanns <dickmanns@fortiss.org>
parent 1f73f960
......@@ -17,15 +17,20 @@ package org.fortiss.af3.testing.mcdc;
import static org.fortiss.af3.project.utils.VariableScopeUtils.getVarType;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.fortiss.af3.expression.model.terms.BoolConst;
import org.fortiss.af3.expression.model.terms.Const;
import org.fortiss.af3.expression.model.terms.DoubleConst;
import org.fortiss.af3.expression.model.terms.EOperator;
import org.fortiss.af3.expression.model.terms.FunctionCall;
import org.fortiss.af3.expression.model.terms.IExpressionTerm;
import org.fortiss.af3.expression.model.terms.IntConst;
import org.fortiss.af3.expression.model.terms.PredefinedFunction;
import org.fortiss.af3.expression.model.terms.Var;
......@@ -37,6 +42,7 @@ import org.fortiss.af3.project.model.typesystem.IFunction;
import org.fortiss.af3.project.model.typesystem.ITerm;
import org.fortiss.af3.project.model.typesystem.IType;
import org.fortiss.af3.project.model.typesystem.VarBase;
import org.fortiss.af3.testing.model.mcdc.FormalRequirementMCDC;
import com.microsoft.z3.ArithExpr;
import com.microsoft.z3.BoolExpr;
......@@ -250,4 +256,60 @@ public final class MCDCUtils {
return null;
}
}
/**
* @param guard
* - the guard from which the atoms should be extracted.
* @return a list of Atoms, which are a equation or inequation only containing a single argument
* on both sides (either a {@link Var} or a {@link Const}).
*/
public static List<FunctionCall> getAtomsFromGuardMCDC(IExpressionTerm guard) {
List<FunctionCall> atoms = new ArrayList<FunctionCall>();
if(isAtom(guard)) {
// Wild cast works: see isAtom(guard).
atoms.add((FunctionCall)guard);
return atoms;
}
TreeIterator<EObject> iterator = EcoreUtil.getAllContents(guard, true);
while(iterator.hasNext()) {
EObject eObject = iterator.next();
if(isAtom(eObject)) {
// Wild cast works: see isAtom(guard).
atoms.add((FunctionCall)eObject);
}
}
return atoms;
}
/** Helper method checking whether the object is an atom. */
private static boolean isAtom(EObject eObject) {
if(eObject instanceof FunctionCall) {
FunctionCall functionCall = (FunctionCall)eObject;
IFunction function = functionCall.getFunction();
if(function instanceof PredefinedFunction && functionCall.getArguments().size() == 2) {
PredefinedFunction predefinedFunction = (PredefinedFunction)function;
EOperator operator = predefinedFunction.getOperator();
switch(operator) {
case LOWER_THAN:
case GREATER_THAN:
case LOWER_EQUAL:
case GREATER_EQUAL:
case EQUAL:
case NOT_EQUAL:
// The condition above ensures that there are exactly two arguments
ITerm lhs = functionCall.getArguments().get(0);
ITerm rhs = functionCall.getArguments().get(1);
boolean isLhsCorrect = lhs instanceof Const || lhs instanceof Var;
boolean isRhsCorrect = rhs instanceof Const || rhs instanceof Var;
if(isLhsCorrect && isRhsCorrect) {
return true;
}
break;
default:
break;
}
}
}
return false;
}
}
/*-------------------------------------------------------------------------+
| Copyright 2018 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package test.org.fortiss.af3.testing.mcdc;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.createVar;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.intConst;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.lowerEqual;
import static org.fortiss.af3.testing.mcdc.MCDCUtils.getAtomsFromGuardMCDC;
import org.fortiss.af3.expression.model.terms.FunctionCall;
import org.fortiss.af3.expression.model.terms.IExpressionTerm;
import org.fortiss.af3.expression.model.terms.Var;
import org.fortiss.af3.expression.utils.ExpressionModelElementFactory;
import org.junit.Test;
/**
*
* @author ludwig
*/
public class TestGetAtomsFromGuardMCDC {
@Test
public void test() {
IExpressionTerm guard, a, b;
Var v1 = createVar("v1");
Var v2 = createVar("v2");
a = lowerEqual(v1, intConst(42));
b = ExpressionModelElementFactory.greaterThan(intConst(42), v2);
guard = ExpressionModelElementFactory.and(a, b);
System.out.println(guard);
System.out.println();
System.out.println("Guard");
for(FunctionCall functionCall : getAtomsFromGuardMCDC(guard)) {
System.out.println(functionCall);
}
System.out.println();
System.out.println("a");
for(FunctionCall functionCall : getAtomsFromGuardMCDC(a)) {
System.out.println(functionCall);
}
System.out.println();
System.out.println("b");
for(FunctionCall functionCall : getAtomsFromGuardMCDC(b)) {
System.out.println(functionCall);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment