//Otherwise, we have to create a new one from a template
Structure signature = Structure.parse(operatorSignature);
//If a template exists matching the supplied indicator
if(this.operatorTemplates.containsKey(signature.getPredicateIndicator().toString())) {
//We get the template
OperatorImpl template = (OperatorImpl) this.operatorTemplates.get(signature.getPredicateIndicator().toString());
//And start copying it
Structure templateSignature = new Structure(template);
Unifier un = new Unifier();
//The signature should unify with the template
if(!un.unifies(signature, templateSignature)) {
//This should not happen in a properly described domain
throw new OperatorFactoryException("Failed to unify "+templateSignature+" with operator "+templateSignature+" instantiating "+operatorSignature);
}
templateSignature.apply(un);
PropositionFactory propositionFactory = PropositionFactory.getInstance();
//Then get the preconditions in the template
List<Proposition> templatePreconds = template.getPreconds();
List<Proposition> concretePreconds = new ArrayList<Proposition>(templatePreconds.size());
//And apply the unifier to them
for (Iterator iter = templatePreconds.iterator(); iter
.hasNext();) {
PropositionImpl precond = (PropositionImpl) iter.next();
Literal literal = new LiteralImpl(precond);
literal.apply(un);
PropositionImpl concretePrecond = (PropositionImpl)propositionFactory.getProposition(literal.toString());
// XXX Uncomment the code to debug operator instantiation, it is removed to avoid wasting time here
// if(!concretePrecond.isGround()) {
// //throw new OperatorFactoryException("We have a non-concrete precond in operator "+signature+": "+concretePrecond);
// System.err.println("We have a non-concrete precond in operator "+signature+": "+concretePrecond);
// }
concretePreconds.add(concretePrecond);
}
List<Proposition> templateEffects = template.getEffects();
List<Proposition> concreteEffects = new ArrayList<Proposition>(templateEffects.size());
for (Iterator iter = templateEffects.iterator(); iter
.hasNext();) {
PropositionImpl effect = (PropositionImpl) iter.next();
Literal literal = new LiteralImpl(effect);
literal.apply(un);
PropositionImpl concreteEffect = (PropositionImpl)propositionFactory.getProposition(literal.toString());
// XXX Uncomment the code to debug operator instantiation, it is removed to avoid wasting time here
// if(!concreteEffect.isGround()) {
// //throw new OperatorFactoryException("We have a non-concrete effect in operator "+signature+": "+concreteEffect);
// System.err.println("We have a non-concrete effect in operator "+signature+": "+concreteEffect);
// }
concreteEffects.add(concreteEffect);
}
OperatorImpl operatorImpl = new OperatorImpl(templateSignature, concretePreconds, concreteEffects);
this.operatorInstances.put(operatorImpl.getSignature(), operatorImpl);
return operatorImpl;
} else {
throw new OperatorFactoryException("No operator template for "+operatorSignature);
}
//return null;