chrek marcos aurélio e-mail: [email protected]@gmail.com msn:...
TRANSCRIPT
Baixando o CHREK
• Abrir a pasta \\profile\scratch_maas$\ na rede do CIN
• Copiar a pasta chrek para C:\temp\
• Executar o context
Configurando o Context
• Configurar atalhos em:
• Options > Environmento Options > Execute Keys
• Atalhos configurados no editor
–F9 – GUI para acompanhamento passo da execução
–F10 – Explicação do raciocínio no Console do Context e no arquivo console.txt
Acompanhando a execução de um programa CHR
RestriçõesDefinidas pelo usuário
RestriçõesBuilt-in
Regras doprograma
Execução
Exemplos - Max
:- explain
max(X,Y,Z) <=> le(X,Y) | Z = Y.
max(X,Y,Z) <=> le(Y,X) | Z = X.
max(X,Y,Z) ==> true | le(X,Z), le(Y,Z).
main() <=> true | max(17,139,M), writeLn(M).
Ativa Explicação do Raciocínio!
Exemplos - Fibonacci
primes(1) <=> true | true .
primes(N) <=> gt(N,1) | M = N~1, prime(N), primes(M).
prime(I) \ prime(J) ==> modEqual(J, I, 0) | true.
main() <=> true | primes(100).
Restrições built-in
• A = B• true• false• ground(X)• unbound(X)• ne(X,Y) X != Y• le(X,Y) X>=Y• ge(X,Y) X<=Y• lt(X,Y) X<Y• gt(X,Y) X>Y
• writeLn(X)• write(X)• readInt(X)• instanceOf(X,Str)
Diretivas de compilação
• :- <diretiva> “<valor>”– explain – standalone– bicpackage “package”– goal “goal constraint name”– stats
• running_time
Criando predicados built-in
• Criar uma classe que esteja dentro de um pacote• Extender chord.constraint.builtin.
BuiltInConstraint• Implementar o método:
public boolean execute(boolean guard)
throws VariableNotBoundException
• Cada built-in pode ser executado várias vezes durante o programa
• Se guard = true a execução não deve ter efeitos colaterais
Exemplo - Built-inpackage builtin;import chord.variable.*;import chord.constraint.builtin.*;
public class MyBuiltIn extends BuiltInConstraint {
public boolean execute(boolean guard) throws VariableNotBoundException {Object obj1 = getArgument(0);Variable val1 = null;
if (obj1 instanceof Variable) {val1 = ((Variable) obj1);
}
boolean ret = true;if (!guard) {
ret = VariableTable.getInstance().bind(val1, new Integer((int)(Math.random() * 100)));
}return ret;
}
}
:- bicpackage "builtin"
main() <=> true | myBuiltIn(X), writeLn(X).
• Para compilar:
• Programa usando built-in
• Mais exemplos:
javac -classpath runtime.jar builtin/MyBuiltIn.java
Exemplo - Built-in
http://chord.cvs.sourceforge.net/chord/CHORD/src/chord/constraint/builtin/
Case Study - Triangram
• Triangram– Basic parts are only of a type: triangles– Uses three types of triangles
Case Study - Triangram
• Triangram– The objective is to form geometric figures from
the basic types of triangles– It's only allowed to form a restricted number of
types of figures: the straight hexagons, pentagons and the quadrilaterals
Case Study - Triangram
• Implementation– Goal
• Scenary being tested• Class hierarchy• Classes signatures
– Rules• Invariants• Methods
– Query propagation rules with the F-Atom of the method in the body of the rule and pre-conditions in the head
– Transactional simpagation rules with pre-conditions in the head, removals in simplificateHead and pos-conditions in the body
Case Study - Triangram
scaleneTriangle::triangle, isoscelesTriangle::triangle, equilateralTriangle::isoscelesTriangle, ...
CHORD Implementation - Goal
main() <=> true | ...
Class Hierarchy
tri1 : equilateralTriangle [side ->> s11 [ adjacent ->> s12,
adjacent ->> s13, length -> 3 ],
... ],
...
Scenary being tested
triangle[side *=>> segmentLine, triangleSide *=>> triangleSide, hypotenuse *=> segmentLine], ...
Class signatures
Case Study - Triangram
...
X:segmentLine[adjacent->>S] ==> true | S:segmentLine[adjacent->>X].
X:triangle, X[side->>S1], X[side->>S2], S1[length->L1], S2[length->L2] ==> ne(S1,S2), L1 = L2 | X:regularPolygon.
...
Invariants
Case Study - Triangram
Query Methods
...
Pol1 : rectangle [ side ->> SidePol1[ length -> L, adjacent ->> A1[ length -> LA1 ] ],
parts ->> P1,parts ->> P2 ],
Pol2 : isoscelesTriangle [ triangleSide ->> TS1[ side -> SidePol2 [ length -> L,
adjacent ->> A2 [length -> LA1 ] ], position -> base ] ]
==> ne(P1,Pol2), ne(P2,Pol2) |Pol1[isCompatible(Pol2, SidePol1, SidePol2)->void].
...
Case Study - Triangram
Transactional Methods
...Pol11:triangle[isCompatible(Pol2, SidePol1, SidePol2)->void], Pol2 : triangle, ... \
A11[adjacent ->> SidePol1], A12[adjacent ->> SidePol1],A21[adjacent ->> SidePol2], A22[adjacent ->> SidePol2]
==> A11 != A12, A21 != A22
|A11[adjacent ->> A22], A12[adjacent ->> A21],A21[adjacent ->> A12], A22[adjacent ->> A11],
Pol3 : rectangle[side ->> {A11,A12,A21,A22}, parts ->> {Pol1,Pol2}],
...