rulerz icon indicating copy to clipboard operation
rulerz copied to clipboard

GenericVisitor::setInlineOperator with a callable?

Open 1e1 opened this issue 8 years ago • 2 comments

Hi,

I use RulerZ in a tracker system of the IoT. I would alias an operator to an inlineOperator for a clearer view: :point ∈ :shape" instead of include(:point, :shape)

    public function initRulerz()
    {
        $ƒinside = function ($geoLocation, $shapeCode) {
            if (null === $geoLocation) {
                return false;
            }

            return $this->geoFencing
                ->setShapeByCode($shapeCode)
                ->isInside(
                    $geoLocation['latitude'],
                    $geoLocation['longitude']
                );
        };

        $ƒoutside = function ($geoLocation, $shapeCode) use ($ƒinside) {
            return !$ƒinside($geoLocation, $shapeCode);
        };


        $visitor = new ArrayVisitor();

        $visitor->setOperator('inside', $ƒinside);
        $visitor->setOperator('outside', $ƒoutside);

        // <-- insert

        $compiler = new FileCompiler(new HoaParser());
        $this->rulerz = new RulerZ($compiler, [ $visitor ]);

        return $this;
    }

I would like to write:

        $visitor->setInlineOperator('∈', $ƒinside);
        $visitor->setInlineOperator('∉', $ƒoutside);

How is it possible?

1e1 avatar Mar 15 '16 14:03 1e1

There are two issues that prevent you from doing what you want.

The first is a misunderstanding of what "inline operators" are for RulerZ compilation targets. For RulerZ, an operator is a callback that will be used at runtime to determine if a location is inside a shapeCode (for instance). On the other hand, an inline operator is a callback that is used at compile-time to generate code.

You can see examples of inline operators in the GenericSqlVisitor.

In your example, the ∈ and ∉ operators can NOT be defined as inline as their implementation rely on the $this->geoFencing attribute.

I guess that there is still plenty of room for improvements in the documentation.

If the thing that you really want to do is simplify the usage of your operator (ie: using :point ∈ :shape instead of ∈(:point, :shape), there isn't anything you can do at the moment. I don't think that grammar of the DSL will recognize it as a valid rule. This is something on which I'm willing to work though :)

K-Phoen avatar Mar 15 '16 16:03 K-Phoen

If the thing that you really want to do is simplify the usage of your operator (ie: using :point ∈ :shape instead of ∈(:point, :shape), there isn't anything you can do at the moment. I don't think that grammar of the DSL will recognize it as a valid rule. This is something on which I'm willing to work though :)

I'm just wrong. It's already how built-in operators like =, >, … work. The grammar already supports this kind of simplification.

K-Phoen avatar Mar 16 '16 20:03 K-Phoen