| Title: Not
Description: None
Copyright (c) 1999 Steven J. Metsker.
Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
A Not is a structure that fails if it can prove itself
against a program.
This simple behavior can have surprising results. For
example:
goodBachelor(X) :- male (X), not married(X);
badBachelor(X) :- not married(X), male(X);
married(jim);
male(jeremy);
male(jim);
Against this program, a query badBachelor(X)
will find no bachelors. This rule negates married(X)
which will bind uninstantiated X to
jim every time. Thus married(X)
will always be true, not married(X) will
always be false, and there are no bad bachelors, which is
untrue. The good rule instantiates X to a good
candidate first, and the negation will prove or disprove
itself for that candidate.
(A parser of the logic code given above will produce
engine calls similar to the following code.)
Program p = new Program();
Variable x = new Variable("X");
p.addAxiom(new Rule(new Structure[]{
new Structure("goodBachelor", new Term[]{x}),
new Structure("male", new Term[]{x}),
new Not ("married", new Term[]{x})}));
p.addAxiom(new Rule(new Structure[]{
new Structure("badBachelor", new Term[]{x}),
new Not ("married", new Term[]{x}),
new Structure("male", new Term[]{x})}));
p.addAxiom(new Fact("married", "jim"));
p.addAxiom(new Fact("male", "jeremy"));
p.addAxiom(new Fact("male", "jim"));
Query qGood = new Query(
p, new Structure("goodBachelor", new Term[]{x}));
while(qGood.canFindNextProof()) {
System.out.println(
"goodBachelor query finds: " + qGood.lookup("X"));
}
Query qBad = new Query(
p, new Structure("badBachelor", new Term[]{x}));
while(qBad.canFindNextProof()) {
System.out.println(
"badBachelor query finds: " + qBad.lookup("X"));
}
Running this code prints out:
goodBachelor query finds: jeremy
author: Steven J. Metsker version: 1.0 |