Commit 4fa96731 authored by Evgeny Kusmenko's avatar Evgeny Kusmenko

Merge branch 'haala-dev' into 'master'

add cocos ConstantConnectorTargetRangeValid and DistinctTargetPorts

See merge request !22
parents abef1bfb fb1cf6c6
Pipeline #196633 passed with stage
in 3 minutes and 33 seconds
package de.monticore.lang.embeddedmontiarc.cocos;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._ast.*;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._cocos.EmbeddedMontiArcASTComponentCoCo;
import de.monticore.lang.monticar.types2._ast.ASTElementType;
import de.se_rwth.commons.logging.Log;
import java.util.List;
import java.util.Optional;
public class ConstantConnectorTargetRangeValid implements EmbeddedMontiArcASTComponentCoCo {
/**
* This CoCo detects if a static numeric source in a connector violates the target's range. Example:
* port in Q(0:255) a;
* connect 275 -> a;
*/
@Override
public void check(ASTComponent node) {
for (ASTConnector connector : node.getConnectors()) {
if (connector.isPresentUnitNumberResolution()) {
Optional<Double> optNumber = connector.getUnitNumberResolution().getNumber();
if (optNumber.isPresent()) {
double constantValue = optNumber.get().doubleValue();
List<ASTQualifiedNameWithArrayAndStar> list = connector.getTargets().getQualifiedNameWithArrayAndStarList();
for (ASTQualifiedNameWithArrayAndStar target : list) {
String targetPortName = target.getQualifiedNameWithArray().getPortName();
double[] range = getRange(targetPortName, node.getPortsList());
if (range != null) {
boolean b = constantValue >= range[0] && constantValue <= range[1];
if (b == false) {
Log.error("0xCC101 Constant value is not in target's range: connect " + constantValue + " -> " + targetPortName);
}
}
}
}
}
}
}
private static double[] getRange(String targetPortName, List<ASTPort> portsList) {
double[] retVal = null;
for (ASTPort port : portsList) {
if (port.getName().equals(targetPortName)) {
if (port.getType() instanceof ASTElementType) {
ASTElementType type = (ASTElementType) port.getType();
retVal = new double[]{Integer.MIN_VALUE, Integer.MAX_VALUE};
if (type.getRange().hasNoLowerLimit() == false) {
retVal[0] = type.getRange().getMin().getNumber().get();
}
if (type.getRange().hasNoUpperLimit() == false) {
retVal[1] = type.getRange().getMax().getNumber().get();
}
break;
}
}
}
return retVal;
}
}
\ No newline at end of file
package de.monticore.lang.embeddedmontiarc.cocos;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._ast.ASTComponent;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._ast.ASTConnector;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._ast.ASTQualifiedNameWithArrayAndStar;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._cocos.EmbeddedMontiArcASTComponentCoCo;
import de.monticore.lang.monticar.common2._ast.ASTQualifiedNameWithArray;
import de.se_rwth.commons.logging.Log;
import java.util.HashMap;
/**
* This CoCo detects if two distinct connectors access the same target port
*/
public class DistinctTargetPorts implements EmbeddedMontiArcASTComponentCoCo {
@Override
public void check(ASTComponent astComponent) {
HashMap<String, Integer> targets = new HashMap<String, Integer>();
for (ASTConnector connector : astComponent.getConnectors()) {
Object[] connectorTargets = connector.getTargets().toArrayQualifiedNameWithArrayAndStars();
for (Object target : connectorTargets) {
String portName = "";
int lineNr = -1;
if (target instanceof ASTQualifiedNameWithArrayAndStar) {
portName = ((ASTQualifiedNameWithArrayAndStar) target).getQualifiedNameWithArray().getPortName();
lineNr = ((ASTQualifiedNameWithArrayAndStar) target).get_SourcePositionStart().getLine();
if (((ASTQualifiedNameWithArrayAndStar) target).getQualifiedNameWithArray().getCompNameOpt().isPresent())
portName = ((ASTQualifiedNameWithArrayAndStar) target).getQualifiedNameWithArray().getCompNameOpt().get() + "." + portName;
if (((ASTQualifiedNameWithArrayAndStar) target).getQualifiedNameWithArray().getPortArrayOpt().isPresent() && ((ASTQualifiedNameWithArrayAndStar) target).getQualifiedNameWithArray().getPortArrayOpt().get().getIntLiteralOpt().isPresent() &&
((ASTQualifiedNameWithArrayAndStar) target).getQualifiedNameWithArray().getPortArrayOpt().get().getIntLiteralOpt().get().getNumber().isPresent())
portName = portName + "[" + ((ASTQualifiedNameWithArrayAndStar) target).getQualifiedNameWithArray().getPortArrayOpt().get().getIntLiteralOpt().get().getNumber().get().intValue() + "]";
} else if (target instanceof ASTQualifiedNameWithArray) {
portName = ((ASTQualifiedNameWithArray) target).getPortName();
lineNr = ((ASTQualifiedNameWithArray) target).get_SourcePositionStart().getLine();
if (((ASTQualifiedNameWithArray) target).getCompNameOpt().isPresent())
portName = ((ASTQualifiedNameWithArray) target).getCompNameOpt().get() + "." + portName;
if (((ASTQualifiedNameWithArray) target).getPortArrayOpt().isPresent() && ((ASTQualifiedNameWithArray) target).getPortArrayOpt().get().getIntLiteralOpt().isPresent() &&
((ASTQualifiedNameWithArray) target).getPortArrayOpt().get().getIntLiteralOpt().get().getNumber().isPresent())
portName = portName + "[" + ((ASTQualifiedNameWithArray) target).getPortArrayOpt().get().getIntLiteralOpt().get().getNumber().get().intValue() + "]";
}else{
continue;
}
if (portName != "" && targets.containsKey(portName)) {
Log.error(String.format("0xCC100 Collision of target port '%1$s' in lines: '%2$s' and '%3$s'", portName, targets.get(portName), lineNr));
} else if (portName != "")
targets.put(portName, lineNr);
}
}
}
}
......@@ -28,6 +28,8 @@ public class EmbeddedMontiArcCoCos {
.addCoCo(new ReferencedSubComponentExists())
.addCoCo(new PortTypeOnlyBooleanOrSIUnit())
.addCoCo(new OnlyIncomingPortIsConfig())
.addCoCo(new InRosPortRosSender());
.addCoCo(new InRosPortRosSender())
.addCoCo(new ConstantConnectorTargetRangeValid())
.addCoCo((new DistinctTargetPorts()));
}
}
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