AI: A Modern Approach brings up the Rete algorithm when discussing inference in first-order logic.
However, all descriptions of the Rete algorithm I found seem to use rules free of function symbols.
In other words, rules look like
a(X) ∧ b(X, Y) → c(Y)
but not
a(f(X)) ∧ b(X, Y) → c(f(Y))
(The difference can be fundamental, as it is the difference between Prolog and Datalog, only one of which is Turing-complete)
Is the Rete algorithm limited to rules free of function symbols?
Do modern rule engines like Drools and CLIPS handle function symbols?
In CLIPS, this is how you'd implement the rule "For every person, there exists one and only one father of said person, and if a person's father is rich, then so is he/she":
(defrule inherited-wealth
(forall (person ?p)
(father ?p ?f)
(not (father ?p ~?f)))
(person ?p)
(father ?p ?f)
(rich ?f)
=>
(assert (rich ?p)))
The problem with your example is that Drools works against Java objects. The conditional "every person has exactly one father" is taken care of by defining the Father variable as an instance and not as a List. That simplifies the check to just being a null check.
In Drools, this is how you'd implement this plain English use case:
For every person, there exists one and only one father of said person, and if a person's father is rich, then so is he/she.
rule "A person has a rich father and is rich"
when
not( Person(father == null ))
$person: Person( $father: father,
isRich == true )
Person( isRich == true ) from $father
then
// Insert consequences here
end
The right hand side of this rule (the consequence) will trigger for each person who is rich, and whose father is rich. The not clause at the beginning verifies that all Person instances in your working memory have father's. Each person passed into working memory is evaluated on their individual merits, even if multiple persons are passed in.
Basically how you'd read it would be: "All persons have a father. Every rich person has a rich father." If you inert the design of the object and check children you could assert something like "if person is rich then all children are rich".
For completeness' sake, the Java class modelled here looks like this:
public class Person {
private Person father;
private boolean isRich;
public Person getFather() { return this.father; }
public Person getIsRich() { return this.isRich; }
// Setter methods omitted for brevity
}
Of course, if instead you're trying to test for situations you don't meet this condition, you could of course invert it:
rule "A person exists without a father"
when
exists( Person( father == null) )
then
// Do something for situation if there's a father-less person
end
rule "A person is rich and their father is not rich"
when
Person( $father: father != null,
isRich == true )
Person( isRich == false ) from $father
then
// Do something for the poor father
end
... where of course you could combine them into a single rule but it's considered bad rule design.
Drools is a business rules language. It's intended to use on combinations of conditions and consequences -- "if these conditions are true, then do this". Your scenario is a simple statement instead of a conditional+consequence, so it's a little hard to model here since it lacks consequences.
It does have the benefit of implicitly being applied against every input without having to specify loops or recursive functions, however. The way the above (both the positive and negative cases) would be invoked, would be to create a collection of Person instances and pass them in all at once into the session. Drools will evaluate each Person instance on its own merits against the inputs. Any changes to these instances without calling one of the special Drools 'refresh' functions (update, insert, modify, retract) will be ignored in terms of evaluating if the rule is valid. So for example, if I pass in a father-less Person instance, and then in one of my rules add a Person, from the "when" clause perspective my Person is still fatherless; the rules are evaluated against the immaculate inputs unless I specifically inform Drools otherwise using the above-mentioned functions.
Related
I have this app where there is a Games table and a Players table, and they share an n:n association.
This association is mapped in Phoenix through a GamesPlayers schema.
What I'm wondering how to do is actually quite simple: I'd like there to be an adjustable limit of how many players are allowed per game.
If you need more details, carry on reading, but if you already know an answer feel free to skip the rest!
What I've Tried
I've taken a look at adding check constraints, but without much success. Here's what the check constraint would have to look something like:
create constraint("games_players", :limit_players, check: "count(players) <= player_limit")
Problem here is, the check syntax is very much invalid and I don't think there actually is a valid way to achieve this using this call.
I've also looked into adding a trigger to the Postgres database directly in order to enforce this (something very similar to what this answer proposes), but I am very wary of directly fiddling with the DB since I should only be using ecto's interface.
Table Schemas
For the purposes of this question, let's assume this is what the tables look like:
Games
Property
Type
id
integer
player_limit
integer
Players
Property
Type
id
integer
GamesPlayers
Property
Type
game_id
references(Games)
player_id
references(Players)
As I mentioned in my comment, I think the cleanest way to enforce this is via business logic inside the code, not via a database constraint. I would approach this using a database transaction, which Ecto supports via Ecto.Repo.transaction/2. This will prevent any race conditions.
In this case I would do something like the following:
begin the transaction
perform a SELECT query counting the number of players in the given game; if the game is already full, abort the transaction, otherwise, continue
perform an INSERT query to add the player to the game
complete the transaction
In code, this would boil down to something like this (untested):
import Ecto.Query
alias MyApp.Repo
alias MyApp.GamesPlayers
#max_allowed_players 10
def add_player_to_game(player_id, game_id, opts \\ []) do
max_allowed_players = Keyword.get(opts, :max_allowed_players, #max_allowed_players)
case is_game_full?(game_id, max_allowed_players) do
false -> %GamesPlayers{
game_id: game_id,
player_id: player_id
}
|> Repo.insert!()
# Raising an error causes the transaction to fail
true -> raise "Game #{inspect(game_id)} full; cannot add player #{inspect(player_id)}"
end
end
defp is_game_full?(game_id, max_allowed_players) do
current_players = from(r in GamesPlayers,
where: r.game_id == game_id,
select: count(r.id)
)
|> Repo.one()
current_players >= max_allowed_players
end
In protege a reflexive property is assigned to all individuals relgardless of domain and range and the class to which individuals belongs.
so what is the use of this restriction?
P.S: lets say there is three individuals:
NamedIndividual( :John )
NamedIndividual( :alex )
NamedIndividual( :BMW )
and an object proeprty:
ObjectProperty( :hasFriend )
ReflexiveObjectProperty(:hasFriend)
running pellet deduce that :
BMW hasFriend BMW
This inference is conceptually meaningless
Papers like The even more irresistible SROIQ and Foundations of Description Logics point out that reflexive and irreflexive properties are closely related to the exists r.Self concept. I.e. Narcissist can be defined as Narcissist \sqsubseteq loves.Self.
The SROIQ paper actually mentions that the main use cases for reflexive and irreflexive properties are limited and only make sense when used along with cardinality restrictions. I.e. if you define PopularPerson as someone with at least 2 friends, and hasFriend is reflexive, then by asserting an individual has 1 known friend will result in that individual being classified as a PopularPerson because the individual is already assumed to be its own friend.
Interestingly, the paper also mentions that reflexive(r) is equivalent to adding the GCI top \sqsubseteq exists r.Self to the TBox. Personally for me this is more intuitive and provides the control I think you seem to want to achieve. In particular this allows you to replace \top with whatever class of your choice. A similar equivalent exists for irreflexive properties.
In OWL, is it possible to query a class that does not have a property?
Suppose I have an object property R and I want to retrieve all classes that do not have the property R. Also, suppose that I have already closed all classes with closures.
I was trying this:
suppose the propety in question is hasProperty, my query was this
hasProperty only Nothing
But it doesn't work
What do you mean by "class that does not have a property"?
Semantically, a class is a set of individuals and a property is a set of pairs of individuals. Given these two sets, what do you mean by "class has / has not a property"?
Regarding your example query
hasProperty only Nothing
lets rewrite it a bit so that we can think about it in natural language. This gives better intuition about the meaning of this query.
First lets rename hasProperty to follows (or whatever English verb), then we have:
follows only Nothing
This is semantically equivalent to
follows only (not Thing)
which is semantically equivalent to
not (follows some Thing)
Now we have gotten rid of the only which is a confusing part of OWL and is better avoided. So now we can verbalize the query in English as:
those who do not follow anything
or
who does not follow anything?
or more formally
which individuals of the ontology are known
not to have a follow relationship to any other individual
E.g. if your ontology states that
John does not follow Mary.
John does not follow himself.
Every individual in the ontology is either John or is Mary.
then John will be the answer to the above query.
You can also get a named class as the query answer if the asked restriction holds for a (named) group of individuals. In any case, the following must hold: if you asserted that the answer (or a member of it) does have a follow-relation to some individual then it would render the ontology inconsistent.
OWL does not have any means for querying the data. SPARQL is used for that. So the SPARQL query to find all class definitions that do not have the property :R would be:
SELECT ?cls
WHERE {
?cls a owl:Class .
FILTER NOT EXISTS {?cls :R ?o}
}
Let’s assume I have a online computer shop and I want to add a new feature - advertising.
As a manager of the shop I want to create an advert about particular product. Let’s say it goes like this: “IF a client is already signed up for at least 2 months AND during purchase the value of his order exceeded 150$ OR product A OR B is already in his shopping cart THEN show him this ad”.
My question is how to store such statements (condition “A or B”, “A and B”, “(A or B) and C”, etc.) in a database and then, how to select the records and display (or not) desired ad?
One of my idea:
Adverts
1. id,
2. name,
3. description,
…
4. criterias_pattern [i.e “(1 OR 5) AND 4”]
Second table:
AdvertsCriterias
1. id
...
2. type
3. value
In short:
I parse the pattern stored in “criterias_pattern” field, extract criterias_id and then I check the conditions.
It should work but it has many obvious drawbacks.
random thought -
why not store actual sql WHERE clauses that would do the evaluation. at least then you do not need to reinvent your own syntax and parsers.
You have two problems: Modeling the conditions, and storing them in a DB.
Modeling the conditions: The easier would probably be if you could use an existing scripting language model, or something like that. If you can't you can model the conditions like:
interface Expression
{
public T Evaluate(Context context);
}
class OrExpression : Expression
{
Expressionleft;
Expressionright;
}
class AndExpression : Expression
{
Expressionleft;
Expressionright;
}
class IfExpression : Expression
{
Expressioncondition;
ExpressionthenClause;
ExpressionelseClause;
}
class EqualExpression : Expression
{
Expression left;
Expression right;
}
class ContextVariableValue : Expression
{
}
“IF a client is already signed up for at least 2 months AND during purchase the value of his order exceeded 150$ OR product A OR B is already in his shopping cart THEN show him this ad”
would be something like that:
var clientIsAlreadySignedUpFor2Months = new GreaterThanExpression(new ContextVariableValue("ClientSignedUpLengthInMonths"), new ConstantExpression(2));
var purchaseExceeds150 = new GreaterThanExpression(new ContextVariableValue("PurchaseAmount"),new ConstantExpression(150));
new IfExpression(new ) )
var result = new AndExpression(clientIsAlreadySignedUpFor2Months, purchaseExceeds150, etc...)
To store it in the DB you can either serialize them to some equation like text and then parse them, or you could use something like
Conditions
Id Type ParametersType Operand1 Operand2
1 GreaterThanExpression Number 2 3
2 ContextVariableValue Number ClientSignedUpLengthInMonths null
3 ConstantExpression Number 2 null
etc...
I think your best bet, is to preload all the advertisments with their conditions in memory, and run them every time you could be displaying an add...
If you detect that a lot of adds have "similar" conditions, then you could add some logic to only evaluate those conditions once, to speed up things, etc...
Preface: I don't have experience with rules engines, building rules, modeling rules, implementing data structures for rules, or whatnot. Therefore, I don't know what I'm doing or if what I attempted below is way off base.
I'm trying to figure out how to store and process the following hypothetical scenario. To simplify my problem, say that I have a type of game where a user purchases an object, where there could be 1000's of possible objects, and the objects must be purchased in a specified sequence and only in certain groups. For example, say I'm the user and I want to purchase object F. Before I can purchase object F, I must have previously purchased object A OR (B AND C). I cannot buy F and A at the same time, nor F and B,C. They must be in the sequence the rule specifies. A first, then F later. Or, B,C first, then F later. I'm not concerned right now with the span of time between purchases, or any other characteristics of the user, just that they are the correct sequence for now.
What is the best way to store this information for potentially thousands of objects that allows me to read in the rules for the object being purchased, and then check it against the user's previous purchase history?
I've attempted this, but I'm stuck at trying to implement the groupings such as A OR (B AND C). I would like to store the rules in a database where I have these tables:
Objects
(ID(int),Description(char))
ObjectPurchRules
(ObjectID(int),ReqirementObjectID(int),OperatorRule(char),Sequence(int))
But obviously as you process through the results, without the grouping, you get the wrong answer. I would like to avoid excessive string parsing if possible :). One object could have an unknown number of previous required purchases. SQL or psuedocode snippets for processing the rules would be appreciated. :)
It seems like your problem breaks down to testing whether a particular condition has been satisfied.
You will have compound conditions.
So given a table of items:
ID_Item Description
----------------------
1 A
2 B
3 C
4 F
and given a table of possible actions:
ID_Action VerbID ItemID ConditionID
----------------------------------------
1 BUY 4 1
We construct a table of conditions:
ID_Condition VerbA ObjectA_ID Boolean VerbB ObjectB_ID
---------------------------------------------------------------------
1 OWNS 1 OR MEETS_CONDITION 2
2 OWNS 2 AND OWNS 3
So OWNS means the id is a key to the Items table, and MEETS_CONDITION means that the id is a key to the Conditions table.
This isn't meant to restrict you. You can add other tables with quests or whatever, and add extra verbs to tell you where to look. Or, just put quests into your Items table when you complete them, and then interpret a completed quest as owning a particular badge. Then you can handle both items and quests with the same code.
This is a very complex problem that I'm not qualified to answer, but I've seen lots of references to. The fundamental problem is that for games, quests and items and "stats" for various objects can have non-relational dependencies. This thread may help you a lot.
You might want to pick up a couple books on the topic, and look into using LUA as a rules processor.
Personally I would do this in code, not in SQL. Each item should be its own class implementing an interface (i.e. IItem). IItem would have a method called OkToPurchase that would determine if it is OK to purchase that item. To do that, it would use one or more of a collection of rules (i.e. HasPreviouslyPurchased(x), CurrentlyOwns(x), etc.) that you can build.
The nice thing is that it is easy to extend this approach with new rules without breaking all the existing logic.
Here's some pseudocode:
bool OkToPurchase()
{
if( HasPreviouslyPurchased('x') && !CurrentlyOwns('y') )
return true;
else
return false;
}
bool HasPreviouslyPurchased( item )
{
return purchases.contains( item )
}
bool CurrentlyOwns( item )
{
return user.Items.contains( item )
}