1/* BEGIN LICENSE BLOCK
2 * Version: CMPL 1.1
3 *
4 * The contents of this file are subject to the Cisco-style Mozilla Public
5 * License Version 1.1 (the "License"); you may not use this file except
6 * in compliance with the License.  You may obtain a copy of the License
7 * at www.eclipse-clp.org/license.
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11 * the License for the specific language governing rights and limitations
12 * under the License.
13 *
14 * The Original Code is  The ECLiPSe Constraint Logic Programming System.
15 * The Initial Developer of the Original Code is  Cisco Systems, Inc.
16 * Portions created by the Initial Developer are
17 * Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18 *
19 * Contributor(s): Pascal Brisset
20 *
21 * END LICENSE BLOCK */
22// $Id: scheduler.cc,v 1.1 2006/09/23 01:54:05 snovello Exp $
23
24
25#include <assert.h> //;
26#include <ilsched/intact.h>
27#include "sched_cstrs.h"
28#include "ec2il.h"
29
30IlcSchedule
31schedule_of_handle(EC_word W)
32{
33  IlcSchedule schedule;
34  int is_schedule = W.is_handle(schedule_method, (void**)&schedule);
35  assert(is_schedule == EC_succeed);
36  return schedule;
37}
38
39
40extern "C"
41int
42c_schedule() // c_schedule(+Min, +Max, -S)
43{
44  IlcSchedule schedule(m, EC_long(EC_arg(1)), EC_long(EC_arg(2)));
45  return unify(EC_arg(3), handle(schedule_method, schedule.getImpl()));
46}
47
48extern "C"
49int
50c_interval_activity() // c_interval_activity(+S, +Start, +End, +Duration, -A)
51{
52  try {
53    IlcSchedule schedule = schedule_of_handle(EC_arg(1));
54    IlcIntVar
55      start = var_or_int(EC_arg(2)),
56      end = var_or_int(EC_arg(3)),
57      duration = var_or_int(EC_arg(4));
58
59    IlcIntervalActivity activity(schedule, start, end, duration);
60
61    return unify(EC_arg(5), handle(activity_method, activity.getImpl()));
62  }
63  catch(Ec2ilException) { // raised by var_or_int()
64    return INSTANTIATION_FAULT;
65  }
66  catch(IlcFailException) { // raised by activity()
67    return EC_fail;
68  }
69}
70
71extern "C"
72int
73c_schedule_add() //c_schedule_add(H, Constraint, Handle)
74{
75  try {
76    m.pushState();
77    IlcConstraint constraint = sched_cstr(EC_arg(2));
78    m.add(constraint);
79    trail_undo(ec_arg(1).val.ptr, ilog_fail);
80    return unify(EC_arg(3), handle(constraint_method, constraint.getImpl()));
81  }
82  catch (IlcFailException) {
83    m.popState();
84    return EC_fail;
85  }
86  catch(SchedCstrException) {
87    return INSTANTIATION_FAULT;
88  }
89}
90
91extern "C"
92int
93c_schedule_set() //c_schedule_set(H, Setting)
94{
95  try {
96    ReturnTryPushTrailCatchPop(sched_void(EC_arg(2)), ec_arg(1).val.ptr);
97  }
98  catch(SchedCstrException) {
99    return INSTANTIATION_FAULT;
100  }
101}
102
103extern "C"
104int
105c_discrete_resource() // c_discrete_resource(S, Capacity, R)
106{
107  IlcSchedule schedule = schedule_of_handle(EC_arg(1));
108  IlcInt capacity = EC_long(EC_arg(2));
109
110  IlcDiscreteResource resource;
111
112  if (capacity == 1) // More efficient: 2 times faster on bridge problem
113    resource = IlcUnaryResource(schedule);
114  else
115    resource = IlcDiscreteResource(schedule, capacity);
116
117  return unify(EC_arg(3), handle(resource_method, resource.getImpl()));
118}
119
120extern "C"
121int
122c_alt_res_set() // c_alt_res_set(S, Rs, ARS)
123{
124  IlcSchedule schedule = schedule_of_handle(EC_arg(1));
125  EC_word Rs = EC_arg(2);
126
127  // computes the length of the list
128  EC_word p = Rs; int size = 0;
129  while(p.is_nil() != EC_succeed) {
130    EC_word head, tail;
131    if (p.is_list(head, tail) != EC_succeed) { return INSTANTIATION_FAULT; }
132    p = tail; size++;
133  }
134
135  // Make the resource
136  IlcAltResSet resource(schedule, size);
137  // Initialize the resource
138  p = Rs;
139  for(int i = 0; i < size; i++) {
140    EC_word head, tail;
141    p.is_list(head, tail); // check already done
142    resource[i] = resource_of_handle(head);
143    p = tail;
144  }
145
146  return unify(EC_arg(3), handle(altresset_method, resource.getImpl()));
147}
148