Checking schemas
Some primitive operators require that the output port has the same schema as the input port. To comply with this requirement, you must check the schema.
Schema checks can be achieved as follows:
my $dataInputPort = $model->getInputPortAt(0);
my $dataOutputPort = $model->getOutputPortAt(1);
# output port 1 schema must be the same as input port 0 schema
SPL::CodeGen::exitln("Output port 1 and input port 0 schemas must match",
$dataInputPort->getSourceLocation())
if($dataInputPort->getCppTupleType() ne $dataOutputPort->getCppTupleType());
- A minimum set of attributes and types are present.
- Only certain attributes and types are permitted.
- An attribute, if present, must have a given type or set of types.
SPL::CodeGen
module
that can simplify this checking.The helper routine SPL::CodeGen::checkMinimalSchema
can
be used to ensure that certain attributes and types must be present
in a port schema. checkMinimalSchema
takes as parameters
an input or output port, and a list of requirements for the port.
For each requirement, there must exist at least one attribute in the
port schema that satisfies it.
Here is an example use:
SPL::CodeGen::checkMinimalSchema ($model->getInputPortAt(0),
{ name => "bar" },
{ type => "tuple<int32 x,rstring y>" },
{ name => "foo", type => ["rstring", "int32"] });
- An attribute with name
bar
, which can be of any type; - An attribute of type
tuple<int32 x,rstring y>
- An attribute with name
foo
and of typerstring
orint32
For the type strings, the only blanks that appear are the ones that separate an attribute from its type. Additionally, type aliases that are defined at the SPL source code might not appear in the type string. These rules also apply for the two related routines reported..
The helper routine SPL::CodeGen::checkMaximalSchema
can
be used to ensure that only certain attributes and types are permitted
in a port schema. checkMaximalSchema
takes as parameters
an input or output port, and a list of requirements for the port.
For each attribute in the port schema, there must exist at least one
requirement that is satisfied by it.
Here is an example use:
SPL::CodeGen::checkMaximalSchema ($model->getInputPortAt(0),
{ name => "bar" },
{ type => "tuple<int32 x,rstring y>" },
{ name => "foo", type => ["rstring","int32"] });
- The attribute has name
bar
or - The attribute is of type
tuple<int32 x,rstring y>
or - The attribute is named
foo
and is of typerstring
orint32
The routine SPL::CodeGen::checkAnySchema
checks
that certain attributes, if present, have the correct type. checkAnySchema
takes
as parameters an input or output port, and a list of requirements
for the port. For each attribute in the port schema, if a requirement
with a matching attribute name exists, then it must be satisfied by
the attribute.
An example use is:
SPL::CodeGen::checkAnySchema ($model->getOutputPortAt(1),
{ name => "foo", type => ["rstring", "int32"] });
For this example, the tuple type for output port 1 must satisfy
the following rule. If an attribute with name foo
is
present, then it must be of type rstring
or int32
.
If the type is not one of these types, an error is reported.