counter

0 views
Skip to first unread message

gaurav v bagga

unread,
Jan 30, 2007, 5:28:58 AM1/30/07
to Tefkat
hi,

how to implement a counter in tefkat
so that i get unique names link1,link2 ......

gaurav

michael lawley

unread,
Jan 30, 2007, 7:54:37 PM1/30/07
to Tef...@googlegroups.com
On 30/01/07, gaurav v bagga <gaurav....@gmail.com> wrote:

> how to implement a counter in tefkat
> so that i get unique names link1,link2 ......

A very interesting question. Assuming that the things you want to
"count" correspond to a set of source instances that can be matched in
a single query, then the rule below should do what you want.

More specifically, the rule is intended to make one Link instance for
every Bar instance that is referenced by the (singleton) Foo. It will
not work as intended (i.e., uniquely numbered names for the Link
instances) if there is more than one Foo instance.

In your specific case, I imagine there will only be a single root
instance, BusinessProcessDiagram and you can use a path expression to
do multiple steps to navigate to the relevant source items to
construct list. Eg WHERE BusinessProcessDiagram diag AND list =
diag.foo.bar.baz{}

TRANSFORMATION example: src -> tgt

RULE makeLinks
WHERE Foo root // match the singleton root instance
AND list = root.reference{} // compute the (ordered) set of
source instances
AND element = root.reference // "iterate" over the individual elements
AND i = list.indexOf(element) // find its index in the list
MAKE Link l FROM link(element) // make one Link per element
SET l.name = append("link", i) // set its name
;

If you're just after unique names, then there is a simple solution:

RULE links
FORALL Foo f
MAKE Link l
SET l.name = append("link", f.hashCode())
;

Cheers,

michael

omnamohbhairav

unread,
Feb 1, 2007, 6:15:39 AM2/1/07
to Tefkat
hi michael ,

MAKE Link l FROM link(element)

in above statement , what does link(element) means?
thanks
Brajesh


On Jan 31, 5:54 am, "michael lawley" <mich...@lawley.id.au> wrote:

michael lawley

unread,
Feb 1, 2007, 6:39:05 AM2/1/07
to Tef...@googlegroups.com
The FROM clause is used to specify the number of instances that get
created. If you leave out a FROM clause, then an implicit one is
added constructed from the variables listed in the rule's FORALL and
the rule's name.

In short, it means make one Link instance per unique element instance.

michael

David

unread,
Feb 1, 2007, 7:12:09 AM2/1/07
to Tefkat

Just to add a bit more to Michael's post. The syntax of the FROM part
is FROM <id>(<exp 1>, ..., <exp n>). This part is called an
'injection'. The semantics of the FROM clause is that there should be
a unique target object (in the MAKE part) for each unique tuple of
values (<v1>, ..., <vn>), where each value is the result of the
appropriate expression (these expressions are almost always just
variables anyway). This is the same concept as injective functions,
where different elements of the domain must map to different elements
of the range. The name of the injection, the <id> part, is just an
arbitrary name that has transformation-wide scope. This allows you to
partition the identity-determining tuples across rules however you
like, for example by using the same injection in multiple rules. e.g.

RULE R1
WHERE p(x)
MAKE Foo y FROM f(x)
;

RULE R2
WHERE q(a)
MAKE Foo b FROM f(a)

Both rules use the same injection name, 'f', meaning that both rules
will 'MAKE' the same object if x = a. However if the injections have
different names, then the target objects will be distinct. So for
example, the following three rules all create distinct ys:

RULE R1
WHERE p(x)
MAKE Foo y FROM f(x)
;

RULE R2
WHERE q(x)
MAKE Foo y FROM g(x)
;

RULE R3
WHERE r(x)
MAKE Foo y FROM f(x, "hello")
;

Finally, if you don't specify an injection (a FROM part) in a rule,
then one gets inserted implicitly for you based on the variables that
appear in the FORALL part of the rule.

RULE R
FORALL Foo x, Bar y
MAKE Baz z
;

is essentially equivalent to:

RULE R1
FORALL Foo x, Bar y
MAKE Baz z FROM f(x, y)
;

i.e. you get a unique z for each unique combination of x and y. This
means that all the following rules have different semantics.

RULE R2
FORALL Foo x
WHERE Bar y
MAKE Baz z
;

RULE R3
FORALL Bar y
WHERE Foo x
MAKE Baz z
;

RULE R4
WHERE Foo x AND Bar y
MAKE Baz z
;

In rule R2, you get a unique z for each unique x (regardless of how
many ys there are), in R3 you get a unique z for each unique y, and in
R4 you get just the one z. Of course you can override this by
explicitly adding your own FROM part.

Hope this helps,

-David

PS If you like logic programming, then a quick explanation is that the
FROM clause describes the logical identity of target objects in terms
of a function symbol.

Reply all
Reply to author
Forward
0 new messages