Source code: alice/respect/Library.java
1 /*
2 * Library.java
3 *
4 * Copyright 2000-2001 deis.unibo.it
5 *
6 * This software is the proprietary information of deis.unibo.it
7 * Use is subject to license terms.
8 *
9 */
10 package alice.respect;
11 import alice.tuprolog.*;
12
13 import alice.logictuple.*;
14 import alice.tuplemedium.EventDirection;
15 import alice.tuplemedium.Event;
16
17 import java.util.*;
18 import java.io.*;
19
20 /**
21 * this tuProlog library defines the behaviour
22 * of ReSpecT primitives, used inside ReSpecT sources.
23 *
24 */
25 public class Library extends alice.tuprolog.Library {
26
27 alice.respect.TupleCentreVM vm;
28 alice.respect.Reactor coord;
29
30 public Library(){
31 theory=
32 ":- op( 550, xfx, '@'). \n";
33 }
34
35 public void init(alice.respect.Reactor c, alice.respect.TupleCentreVM m){
36 vm=m;
37 coord=c;
38 }
39
40 public boolean in_r_1(Struct g){
41 //spy("in_r "+g+" BEGIN");
42 Term arg0=g.getTerm(0);
43 LogicTuple tuArg=new LogicTuple(arg0);
44 alice.tuplemedium.Tuple tuple=vm.getTSet().getMatchingTuple(tuArg);
45 if (tuple!=null){
46 Term term=((LogicTuple)tuple).toTerm();
47 unify(arg0,term);
48 vm.fetchTriggeredReactions(new Event(coord.currentEvent.id,coord.currentEvent.idAgent,alice.respect.Reactor.IN_R,
49 coord.currentEvent.idTupleCentre, EventDirection.FROMSPACE_SUCCESS,tuArg,null));
50 //spy("in_r "+arg0+" success");
51 return true;
52 }
53 else {
54 return false;
55 }
56 }
57
58 public boolean rd_r_1(Struct g){
59 //spy("rd_r "+g+" BEGIN");
60 Term arg0=g.getTerm(0);
61 LogicTuple tuArg=new LogicTuple(arg0);
62 alice.tuplemedium.Tuple tuple=vm.getTSet().readMatchingTuple(tuArg);
63 if (tuple!=null){
64 Term term=((LogicTuple)tuple).toTerm();
65 unify(arg0,term);
66 vm.fetchTriggeredReactions(new Event(coord.currentEvent.id,coord.currentEvent.idAgent,alice.respect.Reactor.RD_R,
67 coord.currentEvent.idTupleCentre, EventDirection.FROMSPACE_SUCCESS,tuArg,null));
68 //spy("rd_r "+arg0+" success");
69 return true;
70 } else {
71 //spy("rd_r "+arg0+" fail");
72 return false;
73 }
74 }
75
76 public boolean no_r_1(Struct g){
77 //spy("no_r "+g+" BEGIN");
78 Term arg0=g.getTerm(0);
79 LogicTuple tuArg=new LogicTuple(arg0);
80 alice.tuplemedium.Tuple tuple=vm.getTSet().readMatchingTuple(tuArg);
81 if (tuple==null){
82 vm.fetchTriggeredReactions(new Event(coord.currentEvent.id,coord.currentEvent.idAgent,
83 alice.respect.Reactor.NO_R, coord.currentEvent.idTupleCentre, EventDirection.FROMSPACE_SUCCESS,tuArg,null));
84 //spy("no_r "+arg0+" success");
85 return true;
86 } else {
87 //spy("no_r "+arg0+" fail");
88 return false;
89 }
90 }
91
92 public boolean out_r_1(Struct g){
93 //spy("out_r "+g+" BEGIN");
94 Term arg0=g.getTerm(0);
95 // get a renamed copy
96 alice.util.LinkedList l=new alice.util.LinkedList();
97 Term newArg=arg0.copy(l);
98 alice.tuprolog.Var.rename(l,0,true);
99
100 LogicTuple tuArg=new LogicTuple(newArg);
101 vm.getTSet().add(tuArg);
102 vm.fetchTriggeredReactions(new Event(coord.currentEvent.id,coord.currentEvent.idAgent,alice.respect.Reactor.OUT_R,
103 coord.currentEvent.idTupleCentre, EventDirection.TOSPACE,tuArg,null));
104 //spy("out_r "+arg0+" success");
105 return true;
106 }
107
108 public boolean current_agent_1(Struct g){
109 Term arg0=g.getTerm(0);
110 Term term=new Struct(coord.currentEvent.idAgent.toString());
111 return unify(arg0,term);
112 }
113
114 public boolean current_tc_1(Struct g){
115 Term arg0=g.getTerm(0);
116 Term term=new Struct(coord.currentEvent.idTupleCentre.toString());
117 return unify(arg0,term);
118 }
119
120 public boolean current_op_1(Struct g){
121 Term arg0=g.getTerm(0);
122 return unify(arg0,coord.getTerm(coord.currentEvent));
123 }
124
125 public boolean current_tuple_1(Struct g){
126 Term arg0=g.getTerm(0);
127 return unify(arg0,((LogicTuple)coord.currentEvent.tuple).toTerm());
128 }
129
130 public boolean pre_0(Struct g){
131 return (coord.currentEvent.direction==EventDirection.TOSPACE);
132 }
133
134 public boolean post_0(Struct g){
135 return (coord.currentEvent.direction==EventDirection.FROMSPACE_SUCCESS || coord.currentEvent.direction==EventDirection.FROMSPACE_FAIL);
136 }
137
138 public boolean success_0(Struct g){
139 return (coord.currentEvent.direction==EventDirection.FROMSPACE_SUCCESS || coord.currentEvent.direction==EventDirection.TOSPACE);
140 }
141
142 public boolean failure_0(Struct g){
143 return (coord.currentEvent.direction==EventDirection.FROMSPACE_FAIL);
144 }
145
146 // extensions
147
148 public boolean spawn_1(Struct g){
149 try {
150 if (!g.isStruct()){
151 return false;
152 }
153 Struct what=g.getStruct(0);
154
155 Term args=(Term)what.getTerm(1).clone();
156 args.renameVars(0);
157
158 if (what.getName().equals("prolog")){
159 coord.addActivityToSpawn(
160 new PrologSpawner(what.getTerm(0).toRawString(),
161 new LogicTuple(args),
162 new LogicTuple(what)));
163 } else if (what.getName().equals("java")){
164 coord.addActivityToSpawn(
165 new JavaSpawner(((Struct)what.getTerm(0)).getName(),
166 new LogicTuple(args),
167 new LogicTuple(what)));
168 }
169 return true;
170 } catch (Exception ex){
171 ex.printStackTrace();
172 return false;
173 }
174 }
175
176 public boolean out_tc_1(Struct g){
177 try {
178 if (!g.isStruct()){
179 return false;
180 }
181 Struct tuple=g.getStruct(0);
182 if (!tuple.getName().equals("@")){
183 return false;
184 }
185 Struct dest=(Struct)tuple.getTerm(1);
186 Struct what=(Struct)tuple.getTerm(0);
187 alice.util.LinkedList l=new alice.util.LinkedList();
188 Term newArg=what.copy(l);
189 alice.tuprolog.Var.rename(l,0,true);
190 coord.addActivity_outTC(new LogicTuple("out_tc",
191 new TupleArgument(dest),
192 new TupleArgument(newArg),
193 new Value("false")),
194 new LogicTuple(tuple));
195 return true;
196 } catch (Exception ex){
197 ex.printStackTrace();
198 return false;
199 }
200 }
201
202 public boolean current_time_1(Struct g){
203 Term arg0=g.getTerm(0);
204 long time=vm.getCurrentTime();
205 return unify(arg0,new Term(time));
206 }
207
208 //
209
210 protected void spy(String msg){
211 //System.out.println("[ react ] "+msg);
212 }
213 }