import org.vast.unit.Unit; //导入依赖的package包/类
/**
* Checks that source and destination components can be connected.
* This validates compatibility of units and structure of aggregates.
* @param src
* @param dest
* @return Warning message or null if no warning
* @throws SMLException
*/
public static String check(DataComponent src, DataComponent dest) throws SMLException
{
if (src == null || dest == null)
return null;
//System.out.println("Checking connection of " + src.getName() + " to " + dest.getName());
DataIterator it1 = new DataIterator(src);
DataIterator it2 = new DataIterator(dest);
StringBuffer msg = new StringBuffer();
while (it1.hasNext())
{
if (!it2.hasNext())
throw new SMLException("Structures of source and destination are not compatible");
DataComponent c1 = it1.next();
DataComponent c2 = it2.next();
//System.out.println("Checking sub component " + c1.getName());
// test that aggregates are the same
if (!c1.getClass().isInstance(c2) || c1.getComponentCount() != c2.getComponentCount())
throw new SMLException("Components '" + c1.getName() + "' and '" + c2.getName() + "' are not compatible");
// check that scalars are compatible
if (c1 instanceof HasUom && c2 instanceof HasUom)
{
Unit uom1 = ((HasUom)c1).getUom().getValue();
Unit uom2 = ((HasUom)c2).getUom().getValue();
if (uom1 != null && uom2 != null)
{
if (!uom1.isCompatible(uom2))
throw new SMLException("Unit of component '" + c1.getName() + "' is not compatible with unit of '" + c2.getName() + "'");
}
if (((DataValue)c1).getDataType() != ((DataValue)c2).getDataType())
msg.append("Data types of source and destination component are different. Conversion may lead to loss of precision");
}
}
if (msg.length() == 0)
return null;
else
return msg.toString();
}