Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Test::More is only required for the tests. If you trust the code, you could go ahead and install it and hope it works.

How to find out everyone who owns gold, or who does not.. or is that what _check_path does?

That feature isn't implemented yet. It's what is called "unification", where data gets associated with a variable in a query. To find out who owns gold, a query like the following would probably be made:

$pg->owns( qw/ $Who gold / );

Because Perlog would know that arguments beginning with a dollar sign are variables, it would know to grab the appropriate data and return it. I've been very busy this week or I would have put that in -- which is really the first thing that needs to go in to actually make this useful.

As a side note: I hate the proposed syntax above, so I am open for suggestions.

_check_path() is called from the following:

my $a = shift @important; while( my $z = shift @important ) { return if !$self->_check_path($predicate_id,$a,$z,@args[$a,$z] +); $a = $z; } return 1;

That loops over the arguments supplied in a query and says foreach argument, return false if I don't find a path to the next argument. The actual method call had a bunch of comments that I hadn't bothered to clean out since I wasn't planning on releasing this yet. You can ignore them. There was also a leftover line of debugging code that I forgot to clean out :) Here's the cleaned up method:

sub _check_path { my ( $self, $predicate, $lvl1, $lvl2, $arg1, $arg2 ) = @_; return 1 if exists $self->{_arg_levels}{$predicate}[$lvl1]{$arg1}[$lvl +2]{$arg2}; return; # nope, didn't find it }

If you want to try and figure out that hideous data structure, you can read this brief explanation.

If you really, really are foolish enough to work on this, thank you :) Other than that, here's what you need to know about the object keys.

_next_vertex
Every unique argument and predicate is given this value, and then the value is incremented. This ensures that everything has a unique id.
_object_vertex
This is a mapping of object names (keys) to their vertex ids (values).
_predicates
Poorly named. I should have named it _predicate_vertex. I'll probably change it. It's a mapping of predicate names to their unique ids.
_arg_levels
Too complicated to explain here. See the link above.

If it's not clear, a predicate can be thought of as the name of a rule.

$pg->add_fact( owns => qw/ Ovid socks / ); $pg->add_fact( gives => qw/ grep grief Ovid / );

In the above expample, "owns" and "gives" are the predicates.

FWIW I'd like to specify "*" as an alias to "_". Or would that mess up inside a qw()? Also I feel scared by the underscore since it seems like the mysterious used $_ without the $ and always wonder if something will get evaled when I wasn't watching. Though maybe intentional?

As for your comments about the underscore: that's borrowed from Prolog. If someone wants to make the query gives( qw/ grep grief _ / ); (which translates to "does grep give grief to anyone?"), then you cannot use the qw// syntax if only the empty string and undef are skipped. As for the asterisk suggestion, I like it, but for practical reasons, I can only use one character for that and I may as well use the standard one.

What else needed to solve the thief problem?

Unification.

What has been your experience using a graph?

I was using a graph at first, but I realized that it was lacking some of the things that I needed so I rolled my own data structure. Right now, even though Graph.pm is listed as a prerequisite, I actually pulled it out of the module completely. I was mostly using it for tests. Pulling it out also had the nice side effect of doubling the speed of my code.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to AI::Perlog -- Q&A by Ovid
in thread AI::Perlog -- Simple implementation by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (9)
    As of 2014-04-19 03:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (477 votes), past polls