Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GenericVisitor::setInlineOperator with a callable? #38

Open
1e1 opened this issue Mar 15, 2016 · 2 comments
Open

GenericVisitor::setInlineOperator with a callable? #38

1e1 opened this issue Mar 15, 2016 · 2 comments
Labels

Comments

@1e1
Copy link

1e1 commented Mar 15, 2016

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?

@K-Phoen
Copy link
Owner

K-Phoen commented Mar 15, 2016

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
Copy link
Owner

K-Phoen commented Mar 16, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants