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 23// $Id: sched_cstrs.cc,v 1.1 2006/09/23 01:54:05 snovello Exp $ 24 25#include <strings.h> 26#include <assert.h> //; 27#include <ilsched/intact.h> 28#include "ec2il.h" 29#include "sched_cstrs.h" 30 31IlcCapResource 32resource_of_handle(EC_word W) 33{ 34 IlcCapResource resource; 35 int is_resource = W.is_handle(resource_method, (void**)&resource); 36 if (is_resource != EC_succeed) throw SchedCstrException(); 37 return resource; 38} 39 40IlcDiscreteResource 41discrete_resource_of_handle(EC_word W) 42{ 43 IlcDiscreteResource resource; 44 int is_resource = W.is_handle(resource_method, (void**)&resource); 45 if (is_resource != EC_succeed) throw SchedCstrException(); 46 return resource; 47} 48 49IlcAltResSet 50altresset_of_handle(EC_word W) 51{ 52 IlcAltResSet resource; 53 int is_resource = W.is_handle(altresset_method, (void**)&resource); 54 if (is_resource != EC_succeed) throw SchedCstrException(); 55 return resource; 56} 57 58IlcAltResConstraint 59altresconstraint_of_handle(EC_word W) 60{ 61 IlcAltResConstraint c; 62 int is_c = W.is_handle(constraint_method, (void**)&c); 63 if (is_c != EC_succeed) throw SchedCstrException(); 64 return c; 65} 66 67IlcIntervalActivity 68activity_of_handle(EC_word W) 69{ 70 IlcIntervalActivity activity; 71 int is_activity = W.is_handle(activity_method, (void**)&activity); 72 if (is_activity != EC_succeed) throw SchedCstrException(); 73 return activity; 74} 75 76IlcBool 77is_atom(EC_word w, char *name) 78{ 79 EC_atom a; 80 return (w.is_atom(&a) == EC_succeed && strcmp(a.name(), name) == 0); 81} 82 83 84 85 86IlcConstraint 87starts_after_end(EC_word C) // starts_after_end(A1, A2, Delay) 88{ 89 IlcActivity activity1 = activity_of_handle(EC_argument(C,1)); 90 IlcActivity activity2 = activity_of_handle(EC_argument(C,2)); 91 IlcInt delay = EC_long(EC_argument(C,3)); 92 93 return(activity1.startsAfterEnd(activity2, delay)); 94} 95 96IlcConstraint 97consumes(EC_word C) // consumes(A, Resource, Capacity, cap or ars) 98{ 99 IlcIntervalActivity activity = activity_of_handle(EC_argument(C, 1)); 100 IlcInt capacity = EC_long(EC_argument(C, 3)); 101 102 if (is_atom(EC_argument(C, 4), "cap")) { 103 IlcCapResource resource = resource_of_handle(EC_argument(C, 2)); 104 return activity.consumes(resource, capacity); 105 } 106 107 assert(is_atom(EC_argument(C, 4), "ars")); 108 IlcAltResSet resource = altresset_of_handle(EC_argument(C, 2)); 109 return activity.consumes(resource, capacity); 110} 111 112IlcConstraint 113requires(EC_word C) // requires(A, Resource, Capacity, cap or ars) 114{ 115 IlcIntervalActivity activity = activity_of_handle(EC_argument(C, 1)); 116 IlcInt capacity = EC_long(EC_argument(C, 3)); 117 118 if (is_atom(EC_argument(C, 4), "cap")) { 119 IlcCapResource resource = resource_of_handle(EC_argument(C, 2)); 120 return activity.requires(resource, capacity); 121 } 122 123 assert(is_atom(EC_argument(C, 4), "ars")); 124 IlcAltResSet resource = altresset_of_handle(EC_argument(C, 2)); 125 return activity.requires(resource, capacity); 126} 127 128IlcConstraint 129sched_cstr(EC_word C) 130{ 131 EC_functor f; 132 133 if (C.functor(&f) == EC_succeed) { 134 switch (f.arity()) { 135 case 3: { 136 if (strcmp(f.name(), "starts_after_end") == 0) { 137 return starts_after_end(C); 138 } 139 140 // Unknown ternary constraints 141 throw SchedCstrException(); 142 } 143 case 4: { 144 if (strcmp(f.name(), "consumes") == 0) { 145 return consumes(C); 146 } else if (strcmp(f.name(), "requires") == 0) { 147 return requires(C); 148 } 149 150 // Unknown 4-ary constraints 151 throw SchedCstrException(); 152 } 153 default: 154 // Unknown constraint 155 throw SchedCstrException(); 156 } 157 } 158 159 // Unknown constraint 160 throw SchedCstrException(); 161} 162 163 164void 165set_capacity_max(EC_word C) //set_capacity_max(Resource, Min, Max, Capacity) 166{ 167 IlcInt min = EC_long(EC_argument(C, 2)); 168 IlcInt max = EC_long(EC_argument(C, 3)); 169 IlcInt capacity = EC_long(EC_argument(C, 4)); 170 discrete_resource_of_handle(EC_argument(C, 1)).setCapacityMax(min, max, capacity); 171} 172 173void 174sched_void(EC_word C) 175{ 176 EC_functor f; 177 178 if (C.functor(&f) == EC_succeed) { 179 switch (f.arity()) { 180 case 1: { 181 if (strcmp(f.name(), "close_resource") == 0) { 182 resource_of_handle(EC_argument(C, 1)).close(); 183 return; 184 } 185 // Unknown unary constraint 186 throw SchedCstrException(); 187 } 188 case 2: { 189 if (strcmp(f.name(), "set_not_possible") == 0) { // set_not_possible(C, R) 190 altresconstraint_of_handle(EC_argument(C, 1)).setNotPossible(resource_of_handle(EC_argument(C, 2))); 191 return; 192 } 193 // Unknown binary constraint 194 throw SchedCstrException(); 195 } 196 case 4: { 197 if (strcmp(f.name(), "set_capacity_max") == 0) { 198 set_capacity_max(C); 199 return; 200 } 201 202 // Unknown 4-ary constraint 203 throw SchedCstrException(); 204 } 205 default: 206 // Unknown constraint 207 throw SchedCstrException(); 208 } 209 } 210 211 // Unknown constraint 212 throw SchedCstrException(); 213} 214