Skip to content

Elementary Example

The below example has three parts:

  • an input type schema,
  • an output type schema,
  • and a list of expressions to compute, returning the last one (or in this case, the only one).

These are the only required top-level fields.

In [1]:
from titus.genpy import PFAEngine

Simple PFA Document from YAML string

In [2]:
pfaYAMLDocument = """
input: double
output: double
action:
  - {+: [input, 100]}"""

The action routine is called once for every input datum, and a symbol(variable) named input references that datum.

The above action calls the + function and passes input and 100 as arguments: {"+": ["input", 100]}.

Much like Lisp, PFA has no infix operators — everything is laid out as a syntax tree in Polish notation.

You can write the PFA document in JSON or YAML or PrettyPFA.

Initializing the Scoring Engine

In [3]:
engine, = PFAEngine.fromYaml(pfaYAMLDocument)

Viewing Engine Configuration

In [4]:
engine.config
Out[4]:
EngineConfig(name=Engine_1,
    method=map,
    inputPlaceholder="double",
    outputPlaceholder="double",
    begin=[],
    action=[Call('+', [Ref('input'), LiteralInt(100)])],
    end=[],
    fcns={},
    zero=None,
    merge=None,
    cells={},
    pools={},
    randseed=None,
    doc=None,
    version=None,
    metadata={},
    options={})

Simple PFA Document from JSON

The PFA Document can also be provided as a stringified JSON.

In [5]:
pfaJSONDocument = """
{ 
    "input": "double",
     "output": "double",
     "action": [
       {"+": ["input", 100]}
     ]
}
"""
engine2, = PFAEngine.fromJson(pfaJSONDocument)
In [6]:
engine2.config
Out[6]:
EngineConfig(name=Engine_2,
    method=map,
    inputPlaceholder="double",
    outputPlaceholder="double",
    begin=[],
    action=[Call('+', [Ref('input'), LiteralInt(100)])],
    end=[],
    fcns={},
    zero=None,
    merge=None,
    cells={},
    pools={},
    randseed=None,
    doc=None,
    version=None,
    metadata={},
    options={})

Scoring Engine in Action

In [7]:
l = [1.0, 2.0, 3.0, 4.0, 5.0]

for n in l:
    print(n, engine.action(n), engine2.action(n))
1.0 101.0 101.0
2.0 102.0 102.0
3.0 103.0 103.0
4.0 104.0 104.0
5.0 105.0 105.0