001package org.hl7.fhir.dstu3.utils;
002
003// remember group resolution
004// trace - account for which wasn't transformed in the source
005
006import java.io.IOException;
007import java.util.ArrayList;
008import java.util.EnumSet;
009import java.util.HashMap;
010import java.util.HashSet;
011import java.util.List;
012import java.util.Map;
013import java.util.Set;
014import java.util.UUID;
015
016import org.hl7.fhir.dstu3.conformance.ProfileUtilities;
017import org.hl7.fhir.dstu3.conformance.ProfileUtilities.ProfileKnowledgeProvider;
018import org.hl7.fhir.dstu3.context.IWorkerContext;
019import org.hl7.fhir.dstu3.context.IWorkerContext.ValidationResult;
020import org.hl7.fhir.dstu3.elementmodel.Element;
021import org.hl7.fhir.dstu3.elementmodel.Property;
022import org.hl7.fhir.dstu3.model.Base;
023import org.hl7.fhir.dstu3.model.BooleanType;
024import org.hl7.fhir.dstu3.model.CodeType;
025import org.hl7.fhir.dstu3.model.CodeableConcept;
026import org.hl7.fhir.dstu3.model.Coding;
027import org.hl7.fhir.dstu3.model.ConceptMap;
028import org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent;
029import org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupUnmappedMode;
030import org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent;
031import org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent;
032import org.hl7.fhir.dstu3.model.Constants;
033import org.hl7.fhir.dstu3.model.ContactDetail;
034import org.hl7.fhir.dstu3.model.ContactPoint;
035import org.hl7.fhir.dstu3.model.DecimalType;
036import org.hl7.fhir.dstu3.model.ElementDefinition;
037import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionMappingComponent;
038import org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent;
039import org.hl7.fhir.dstu3.model.Enumeration;
040import org.hl7.fhir.dstu3.model.Enumerations.ConceptMapEquivalence;
041import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus;
042import org.hl7.fhir.dstu3.model.ExpressionNode;
043import org.hl7.fhir.dstu3.model.ExpressionNode.CollectionStatus;
044import org.hl7.fhir.dstu3.model.IdType;
045import org.hl7.fhir.dstu3.model.IntegerType;
046import org.hl7.fhir.dstu3.model.Narrative.NarrativeStatus;
047import org.hl7.fhir.dstu3.model.PrimitiveType;
048import org.hl7.fhir.dstu3.model.Reference;
049import org.hl7.fhir.dstu3.model.Resource;
050import org.hl7.fhir.dstu3.model.ResourceFactory;
051import org.hl7.fhir.dstu3.model.StringType;
052import org.hl7.fhir.dstu3.model.StructureDefinition;
053import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionMappingComponent;
054import org.hl7.fhir.dstu3.model.StructureDefinition.TypeDerivationRule;
055import org.hl7.fhir.dstu3.model.StructureMap;
056import org.hl7.fhir.dstu3.model.StructureMap.StructureMapContextType;
057import org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupComponent;
058import org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupInputComponent;
059import org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleComponent;
060import org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleDependentComponent;
061import org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleSourceComponent;
062import org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleTargetComponent;
063import org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleTargetParameterComponent;
064import org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupTypeMode;
065import org.hl7.fhir.dstu3.model.StructureMap.StructureMapInputMode;
066import org.hl7.fhir.dstu3.model.StructureMap.StructureMapSourceListMode;
067import org.hl7.fhir.dstu3.model.StructureMap.StructureMapTargetListMode;
068import org.hl7.fhir.dstu3.model.StructureMap.StructureMapModelMode;
069import org.hl7.fhir.dstu3.model.StructureMap.StructureMapStructureComponent;
070import org.hl7.fhir.dstu3.model.StructureMap.StructureMapTransform;
071import org.hl7.fhir.dstu3.model.Type;
072import org.hl7.fhir.dstu3.model.TypeDetails;
073import org.hl7.fhir.dstu3.model.TypeDetails.ProfiledType;
074import org.hl7.fhir.dstu3.model.UriType;
075import org.hl7.fhir.dstu3.model.ValueSet;
076import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionContainsComponent;
077import org.hl7.fhir.dstu3.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
078import org.hl7.fhir.dstu3.utils.FHIRLexer.FHIRLexerException;
079import org.hl7.fhir.dstu3.utils.FHIRPathEngine.IEvaluationContext;
080import org.hl7.fhir.exceptions.DefinitionException;
081import org.hl7.fhir.exceptions.FHIRException;
082import org.hl7.fhir.exceptions.FHIRFormatError;
083import org.hl7.fhir.exceptions.PathEngineException;
084import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
085import org.hl7.fhir.utilities.TextFile;
086import org.hl7.fhir.utilities.Utilities;
087import org.hl7.fhir.utilities.xhtml.NodeType;
088import org.hl7.fhir.utilities.xhtml.XhtmlNode;
089
090/**
091 * Services in this class:
092 * 
093 * string render(map) - take a structure and convert it to text
094 * map parse(text) - take a text representation and parse it 
095 * getTargetType(map) - return the definition for the type to create to hand in 
096 * transform(appInfo, source, map, target) - transform from source to target following the map
097 * analyse(appInfo, map) - generate profiles and other analysis artifacts for the targets of the transform
098 * map generateMapFromMappings(StructureDefinition) - build a mapping from a structure definition with loigcal mappings
099 *  
100 * @author Grahame Grieve
101 *
102 */
103public class StructureMapUtilities {
104
105        public class ResolvedGroup {
106    public StructureMapGroupComponent target;
107    public StructureMap targetMap;
108  }
109  public static final String MAP_WHERE_CHECK = "map.where.check";
110        public static final String MAP_WHERE_EXPRESSION = "map.where.expression";
111        public static final String MAP_SEARCH_EXPRESSION = "map.search.expression";
112        public static final String MAP_EXPRESSION = "map.transform.expression";
113  private static final boolean RENDER_MULTIPLE_TARGETS_ONELINE = true;
114  private static final String AUTO_VAR_NAME = "vvv";
115
116        public interface ITransformerServices {
117                //    public boolean validateByValueSet(Coding code, String valuesetId);
118          public void log(String message); // log internal progress
119          public Base createType(Object appInfo, String name) throws FHIRException;
120    public Base createResource(Object appInfo, Base res); // an already created resource is provided; this is to identify/store it
121                public Coding translate(Object appInfo, Coding source, String conceptMapUrl) throws FHIRException;
122                //    public Coding translate(Coding code)
123                //    ValueSet validation operation
124                //    Translation operation
125                //    Lookup another tree of data
126                //    Create an instance tree
127                //    Return the correct string format to refer to a tree (input or output)
128    public Base resolveReference(Object appContext, String url);
129    public List<Base> performSearch(Object appContext, String url);
130        }
131
132        private class FFHIRPathHostServices implements IEvaluationContext{
133
134    public Base resolveConstant(Object appContext, String name) throws PathEngineException {
135      Variables vars = (Variables) appContext;
136      Base res = vars.get(VariableMode.INPUT, name);
137      if (res == null)
138        res = vars.get(VariableMode.OUTPUT, name);
139      return res;
140    }
141
142    @Override
143    public TypeDetails resolveConstantType(Object appContext, String name) throws PathEngineException {
144      if (!(appContext instanceof VariablesForProfiling)) 
145        throw new Error("Internal Logic Error (wrong type '"+appContext.getClass().getName()+"' in resolveConstantType)");
146      VariablesForProfiling vars = (VariablesForProfiling) appContext;
147      VariableForProfiling v = vars.get(null, name);
148      if (v == null)
149        throw new PathEngineException("Unknown variable '"+name+"' from variables "+vars.summary());
150      return v.property.types;
151    }
152
153    @Override
154    public boolean log(String argument, List<Base> focus) {
155      throw new Error("Not Implemented Yet");
156    }
157
158    @Override
159    public FunctionDetails resolveFunction(String functionName) {
160      return null; // throw new Error("Not Implemented Yet");
161    }
162
163    @Override
164    public TypeDetails checkFunction(Object appContext, String functionName, List<TypeDetails> parameters) throws PathEngineException {
165      throw new Error("Not Implemented Yet");
166    }
167
168    @Override
169    public List<Base> executeFunction(Object appContext, String functionName, List<List<Base>> parameters) {
170      throw new Error("Not Implemented Yet");
171    }
172
173    @Override
174    public Base resolveReference(Object appContext, String url) {
175      if (services == null)
176        return null;
177      return services.resolveReference(appContext, url);
178    }
179          
180        }
181        private IWorkerContext worker;
182        private FHIRPathEngine fpe;
183        private Map<String, StructureMap> library;
184        private ITransformerServices services;
185  private ProfileKnowledgeProvider pkp;
186  private Map<String, Integer> ids = new HashMap<String, Integer>(); 
187
188        public StructureMapUtilities(IWorkerContext worker, Map<String, StructureMap> library, ITransformerServices services, ProfileKnowledgeProvider pkp) {
189                super();
190                this.worker = worker;
191                this.library = library;
192                this.services = services;
193                this.pkp = pkp;
194                fpe = new FHIRPathEngine(worker);
195                fpe.setHostServices(new FFHIRPathHostServices());
196        }
197
198        public StructureMapUtilities(IWorkerContext worker, Map<String, StructureMap> library, ITransformerServices services) {
199                super();
200                this.worker = worker;
201                this.library = library;
202                this.services = services;
203                fpe = new FHIRPathEngine(worker);
204    fpe.setHostServices(new FFHIRPathHostServices());
205        }
206
207  public StructureMapUtilities(IWorkerContext worker, Map<String, StructureMap> library) {
208    super();
209    this.worker = worker;
210    this.library = library;
211    fpe = new FHIRPathEngine(worker);
212    fpe.setHostServices(new FFHIRPathHostServices());
213  }
214
215  public StructureMapUtilities(IWorkerContext worker) {
216    super();
217    this.worker = worker;
218    fpe = new FHIRPathEngine(worker);
219    fpe.setHostServices(new FFHIRPathHostServices());
220  }
221
222  public StructureMapUtilities(IWorkerContext worker, ITransformerServices services) {
223    super();
224    this.worker = worker;
225    this.library = new HashMap<String, StructureMap>();
226    for (org.hl7.fhir.dstu3.model.MetadataResource bc : worker.allConformanceResources()) {
227      if (bc instanceof StructureMap)
228        library.put(bc.getUrl(), (StructureMap) bc);
229    }
230    this.services = services;
231    fpe = new FHIRPathEngine(worker);
232    fpe.setHostServices(new FFHIRPathHostServices());
233  }
234
235        public static String render(StructureMap map) {
236                StringBuilder b = new StringBuilder();
237                b.append("map \"");
238                b.append(map.getUrl());
239                b.append("\" = \"");
240                b.append(Utilities.escapeJava(map.getName()));
241                b.append("\"\r\n\r\n");
242
243                renderConceptMaps(b, map);
244                renderUses(b, map);
245                renderImports(b, map);
246                for (StructureMapGroupComponent g : map.getGroup())
247                        renderGroup(b, g);
248                return b.toString();
249        }
250
251        private static void renderConceptMaps(StringBuilder b, StructureMap map) {
252    for (Resource r : map.getContained()) {
253      if (r instanceof ConceptMap) {
254        produceConceptMap(b, (ConceptMap) r);
255      }
256    }
257  }
258
259  private static void produceConceptMap(StringBuilder b, ConceptMap cm) {
260    b.append("conceptmap \"");
261    b.append(cm.getId());
262    b.append("\" {\r\n");
263    Map<String, String> prefixesSrc = new HashMap<String, String>();
264    Map<String, String> prefixesTgt = new HashMap<String, String>();
265    char prefix = 's';
266    for (ConceptMapGroupComponent cg : cm.getGroup()) {
267      if (!prefixesSrc.containsKey(cg.getSource())) {
268        prefixesSrc.put(cg.getSource(), String.valueOf(prefix));
269        b.append("  prefix ");
270        b.append(prefix);
271        b.append(" = \"");
272        b.append(cg.getSource());
273        b.append("\"\r\n");
274        prefix++;
275      }
276      if (!prefixesTgt.containsKey(cg.getTarget())) {
277        prefixesTgt.put(cg.getTarget(), String.valueOf(prefix));
278        b.append("  prefix ");
279        b.append(prefix);
280        b.append(" = \"");
281        b.append(cg.getTarget());
282        b.append("\"\r\n");
283        prefix++;
284      }
285    }
286    b.append("\r\n");
287    for (ConceptMapGroupComponent cg : cm.getGroup()) {
288      if (cg.hasUnmapped()) {
289        b.append("  unmapped for ");
290        b.append(prefix);
291        b.append(" = ");
292        b.append(cg.getUnmapped().getMode());
293        b.append("\r\n"); 
294      }   
295    }
296    
297    for (ConceptMapGroupComponent cg : cm.getGroup()) {
298      for (SourceElementComponent ce : cg.getElement()) {
299        b.append("  ");
300        b.append(prefixesSrc.get(cg.getSource()));
301        b.append(":");
302        b.append(ce.getCode());
303        b.append(" ");
304        b.append(getChar(ce.getTargetFirstRep().getEquivalence()));
305        b.append(" ");
306        b.append(prefixesTgt.get(cg.getTarget()));
307        b.append(":");
308        b.append(ce.getTargetFirstRep().getCode());
309        b.append("\r\n");
310      }
311    }
312    b.append("}\r\n\r\n");
313  }
314
315  private static Object getChar(ConceptMapEquivalence equivalence) {
316    switch (equivalence) {
317    case RELATEDTO: return "-";
318    case EQUAL: return "=";
319    case EQUIVALENT: return "==";
320    case DISJOINT: return "!=";
321    case UNMATCHED: return "--";
322    case WIDER: return "<=";
323    case SUBSUMES: return "<-";
324    case NARROWER: return ">=";
325    case SPECIALIZES: return ">-";
326    case INEXACT: return "~";
327    default: return "??";
328    }
329  }
330
331  private static void renderUses(StringBuilder b, StructureMap map) {
332                for (StructureMapStructureComponent s : map.getStructure()) {
333                        b.append("uses \"");
334                        b.append(s.getUrl());
335      b.append("\" ");
336      if (s.hasAlias()) {
337        b.append("alias ");
338        b.append(s.getAlias());
339        b.append(" ");
340      }
341      b.append("as ");
342                        b.append(s.getMode().toCode());
343                        b.append("\r\n");
344                        renderDoco(b, s.getDocumentation());
345                }
346                if (map.hasStructure())
347                        b.append("\r\n");
348        }
349
350        private static void renderImports(StringBuilder b, StructureMap map) {
351                for (UriType s : map.getImport()) {
352                        b.append("imports \"");
353                        b.append(s.getValue());
354                        b.append("\"\r\n");
355                }
356                if (map.hasImport())
357                        b.append("\r\n");
358        }
359
360  public static String groupToString(StructureMapGroupComponent g) {
361    StringBuilder b = new StringBuilder();
362    renderGroup(b, g);
363    return b.toString();
364  }
365  
366  private static void renderGroup(StringBuilder b, StructureMapGroupComponent g) {
367                b.append("group ");
368    switch (g.getTypeMode()) {
369      case TYPES:
370        b.append("for types");
371        break;
372      case TYPEANDTYPES:
373        b.append("for type+types ");
374        break;
375      case NONE:
376      case NULL:
377        break;
378    }
379      b.append("for types ");
380                b.append(g.getName());
381                if (g.hasExtends()) {
382                        b.append(" extends ");
383                        b.append(g.getExtends());
384                }
385                if (g.hasDocumentation()) 
386                        renderDoco(b, g.getDocumentation());
387                b.append("\r\n");
388                for (StructureMapGroupInputComponent gi : g.getInput()) {
389                        b.append("  input ");
390                        b.append(gi.getName());
391                        if (gi.hasType()) {
392                                b.append(" : ");
393                                b.append(gi.getType());
394                        }
395                        b.append(" as ");
396                        b.append(gi.getMode().toCode());
397                        b.append("\r\n");
398                }
399                if (g.hasInput())
400                        b.append("\r\n");
401                for (StructureMapGroupRuleComponent r : g.getRule()) {
402                        renderRule(b, r, 2);
403                }
404                b.append("\r\nendgroup\r\n");
405        }
406
407  public static String ruleToString(StructureMapGroupRuleComponent r) {
408    StringBuilder b = new StringBuilder();
409    renderRule(b, r, 0);
410    return b.toString();
411  }
412  
413        private static void renderRule(StringBuilder b, StructureMapGroupRuleComponent r, int indent) {
414                for (int i = 0; i < indent; i++)
415                        b.append(' ');
416                b.append(r.getName());
417                b.append(" : for ");
418                boolean canBeAbbreviated = checkisSimple(r);
419                
420                boolean first = true;
421                for (StructureMapGroupRuleSourceComponent rs : r.getSource()) {
422                        if (first)
423                                first = false;
424                        else
425                                b.append(", ");
426                        renderSource(b, rs, canBeAbbreviated);
427                }
428                if (r.getTarget().size() > 1) {
429                        b.append(" make ");
430                        first = true;
431                        for (StructureMapGroupRuleTargetComponent rt : r.getTarget()) {
432                                if (first)
433                                        first = false;
434                                else
435                                        b.append(", ");
436                                if (RENDER_MULTIPLE_TARGETS_ONELINE)
437                b.append(' ');
438                                else {
439                                b.append("\r\n");
440                                for (int i = 0; i < indent+4; i++)
441                                        b.append(' ');
442                                }
443                                renderTarget(b, rt, false);
444                        }
445                } else if (r.hasTarget()) { 
446                        b.append(" make ");
447                        renderTarget(b, r.getTarget().get(0), canBeAbbreviated);
448                }
449                if (!canBeAbbreviated) {
450                  if (r.hasRule()) {
451                    b.append(" then {\r\n");
452                    renderDoco(b, r.getDocumentation());
453                    for (StructureMapGroupRuleComponent ir : r.getRule()) {
454                      renderRule(b, ir, indent+2);
455                    }
456                    for (int i = 0; i < indent; i++)
457                      b.append(' ');
458                    b.append("}\r\n");
459                  } else {
460                    if (r.hasDependent()) {
461                      b.append(" then ");
462                      first = true;
463                      for (StructureMapGroupRuleDependentComponent rd : r.getDependent()) {
464                        if (first)
465                          first = false;
466                        else
467                          b.append(", ");
468                        b.append(rd.getName());
469                        b.append("(");
470                        boolean ifirst = true;
471                        for (StringType rdp : rd.getVariable()) {
472                          if (ifirst)
473                            ifirst = false;
474                          else
475                            b.append(", ");
476                          b.append(rdp.asStringValue());
477                        }
478                        b.append(")");
479                      }
480                    }
481                  }
482                }
483    renderDoco(b, r.getDocumentation());
484    b.append("\r\n");
485        }
486
487  private static boolean checkisSimple(StructureMapGroupRuleComponent r) {
488    return 
489          (r.getSource().size() == 1 && r.getSourceFirstRep().hasElement() && r.getSourceFirstRep().hasVariable()) && 
490          (r.getTarget().size() == 1 && r.getTargetFirstRep().hasVariable() && (r.getTargetFirstRep().getTransform() == null || r.getTargetFirstRep().getTransform() == StructureMapTransform.CREATE) && r.getTargetFirstRep().getParameter().size() == 0) &&
491          (r.getDependent().size() == 0);
492  }
493
494  public static String sourceToString(StructureMapGroupRuleSourceComponent r) {
495    StringBuilder b = new StringBuilder();
496    renderSource(b, r, false);
497    return b.toString();
498  }
499  
500        private static void renderSource(StringBuilder b, StructureMapGroupRuleSourceComponent rs, boolean abbreviate) {
501                b.append(rs.getContext());
502                if (rs.getContext().equals("@search")) {
503      b.append('(');
504      b.append(rs.getElement());
505      b.append(')');
506                } else if (rs.hasElement()) {
507                        b.append('.');
508                        b.append(rs.getElement());
509                }
510                if (rs.hasType()) {
511      b.append(" : ");
512      b.append(rs.getType());
513      if (rs.hasMin()) {
514        b.append(" ");
515        b.append(rs.getMin());
516        b.append("..");
517        b.append(rs.getMax());
518      }
519                }
520                
521                if (rs.hasListMode()) {
522                        b.append(" ");
523                                b.append(rs.getListMode().toCode());
524                }
525                if (rs.hasDefaultValue()) {
526                  b.append(" default ");
527                  assert rs.getDefaultValue() instanceof StringType;
528                  b.append("\""+Utilities.escapeJson(((StringType) rs.getDefaultValue()).asStringValue())+"\"");
529                }
530                if (!abbreviate && rs.hasVariable()) {
531                        b.append(" as ");
532                        b.append(rs.getVariable());
533                }
534                if (rs.hasCondition())  {
535                        b.append(" where ");
536                        b.append(rs.getCondition());
537                }
538                if (rs.hasCheck())  {
539                        b.append(" check ");
540                        b.append(rs.getCheck());
541                }
542        }
543
544  public static String targetToString(StructureMapGroupRuleTargetComponent rt) {
545    StringBuilder b = new StringBuilder();
546    renderTarget(b, rt, false);
547    return b.toString();
548  }
549        
550        private static void renderTarget(StringBuilder b, StructureMapGroupRuleTargetComponent rt, boolean abbreviate) {
551          if (rt.hasContext()) {
552            if (rt.getContextType() == StructureMapContextType.TYPE)
553              b.append("@");
554                b.append(rt.getContext());
555                if (rt.hasElement())  {
556                        b.append('.');
557                        b.append(rt.getElement());
558                }
559          }
560                if (!abbreviate && rt.hasTransform()) {
561            if (rt.hasContext()) 
562                        b.append(" = ");
563                        if (rt.getTransform() == StructureMapTransform.COPY && rt.getParameter().size() == 1) {
564                                renderTransformParam(b, rt.getParameter().get(0));
565      } else if (rt.getTransform() == StructureMapTransform.EVALUATE && rt.getParameter().size() == 1) {
566        b.append("(");
567        b.append("\""+((StringType) rt.getParameter().get(0).getValue()).asStringValue()+"\"");
568        b.append(")");
569                        } else if (rt.getTransform() == StructureMapTransform.EVALUATE && rt.getParameter().size() == 2) {
570                                b.append(rt.getTransform().toCode());
571                                b.append("(");
572                                b.append(((IdType) rt.getParameter().get(0).getValue()).asStringValue());
573                                b.append("\""+((StringType) rt.getParameter().get(1).getValue()).asStringValue()+"\"");
574                                b.append(")");
575                        } else {
576                                b.append(rt.getTransform().toCode());
577                                b.append("(");
578                                boolean first = true;
579                                for (StructureMapGroupRuleTargetParameterComponent rtp : rt.getParameter()) {
580                                        if (first)
581                                                first = false;
582                                        else
583                                                b.append(", ");
584                                        renderTransformParam(b, rtp);
585                                }
586                                b.append(")");
587                        }
588                }
589                if (!abbreviate && rt.hasVariable()) {
590                        b.append(" as ");
591                        b.append(rt.getVariable());
592                }
593                for (Enumeration<StructureMapTargetListMode> lm : rt.getListMode()) {
594                        b.append(" ");
595                        b.append(lm.getValue().toCode());
596                        if (lm.getValue() == StructureMapTargetListMode.SHARE) {
597                                b.append(" ");
598                                b.append(rt.getListRuleId());
599                        }
600                }
601        }
602
603  public static String paramToString(StructureMapGroupRuleTargetParameterComponent rtp) {
604    StringBuilder b = new StringBuilder();
605    renderTransformParam(b, rtp);
606    return b.toString();
607  }
608        
609        private static void renderTransformParam(StringBuilder b, StructureMapGroupRuleTargetParameterComponent rtp) {
610          try {
611                if (rtp.hasValueBooleanType())
612                        b.append(rtp.getValueBooleanType().asStringValue());
613                else if (rtp.hasValueDecimalType())
614                        b.append(rtp.getValueDecimalType().asStringValue());
615                else if (rtp.hasValueIdType())
616                        b.append(rtp.getValueIdType().asStringValue());
617                else if (rtp.hasValueDecimalType())
618                        b.append(rtp.getValueDecimalType().asStringValue());
619                else if (rtp.hasValueIntegerType())
620                        b.append(rtp.getValueIntegerType().asStringValue());
621                else 
622              b.append("\""+Utilities.escapeJava(rtp.getValueStringType().asStringValue())+"\"");
623          } catch (FHIRException e) {
624            e.printStackTrace();
625            b.append("error!");
626          }
627        }
628
629        private static void renderDoco(StringBuilder b, String doco) {
630                if (Utilities.noString(doco))
631                        return;
632                b.append(" // ");
633                b.append(doco.replace("\r\n", " ").replace("\r", " ").replace("\n", " "));
634        }
635
636        public StructureMap parse(String text) throws FHIRException {
637                FHIRLexer lexer = new FHIRLexer(text);
638                if (lexer.done())
639                        throw lexer.error("Map Input cannot be empty");
640                lexer.skipComments();
641                lexer.token("map");
642                StructureMap result = new StructureMap();
643                result.setUrl(lexer.readConstant("url"));
644                lexer.token("=");
645                result.setName(lexer.readConstant("name"));
646                lexer.skipComments();
647
648                while (lexer.hasToken("conceptmap"))
649                        parseConceptMap(result, lexer);
650
651                while (lexer.hasToken("uses"))
652                        parseUses(result, lexer);
653                while (lexer.hasToken("imports"))
654                        parseImports(result, lexer);
655
656                parseGroup(result, lexer);
657
658                while (!lexer.done()) {
659                        parseGroup(result, lexer);    
660                }
661
662                return result;
663        }
664
665        private void parseConceptMap(StructureMap result, FHIRLexer lexer) throws FHIRLexerException {
666                lexer.token("conceptmap");
667                ConceptMap map = new ConceptMap();
668                String id = lexer.readConstant("map id");
669                if (!id.startsWith("#"))
670                        lexer.error("Concept Map identifier must start with #");
671                map.setId(id);
672                map.setStatus(PublicationStatus.DRAFT); // todo: how to add this to the text format
673                result.getContained().add(map);
674                lexer.token("{");
675                lexer.skipComments();
676                //        lexer.token("source");
677                //        map.setSource(new UriType(lexer.readConstant("source")));
678                //        lexer.token("target");
679                //        map.setSource(new UriType(lexer.readConstant("target")));
680                Map<String, String> prefixes = new HashMap<String, String>();
681                while (lexer.hasToken("prefix")) {
682                        lexer.token("prefix");
683                        String n = lexer.take();
684                        lexer.token("=");
685                        String v = lexer.readConstant("prefix url");
686                        prefixes.put(n, v);
687                }
688                while (lexer.hasToken("unmapped")) {
689                  lexer.token("unmapped");
690      lexer.token("for");
691      String n = readPrefix(prefixes, lexer);
692      ConceptMapGroupComponent g = getGroup(map, n, null);
693                  lexer.token("=");
694      String v = lexer.take();
695      if (v.equals("provided")) {
696        g.getUnmapped().setMode(ConceptMapGroupUnmappedMode.PROVIDED);
697      } else
698        lexer.error("Only unmapped mode PROVIDED is supported at this time");
699                }
700                while (!lexer.hasToken("}")) {
701                  String srcs = readPrefix(prefixes, lexer);
702                        lexer.token(":");
703      String sc = lexer.getCurrent().startsWith("\"") ? lexer.readConstant("code") : lexer.take();
704                  ConceptMapEquivalence eq = readEquivalence(lexer);
705                  String tgts = (eq != ConceptMapEquivalence.UNMATCHED) ? readPrefix(prefixes, lexer) : "";
706                  ConceptMapGroupComponent g = getGroup(map, srcs, tgts);
707                        SourceElementComponent e = g.addElement();
708                        e.setCode(sc);
709      if (e.getCode().startsWith("\""))
710        e.setCode(lexer.processConstant(e.getCode()));
711                        TargetElementComponent tgt = e.addTarget();
712                        if (eq != ConceptMapEquivalence.EQUIVALENT)
713                          tgt.setEquivalence(eq);
714                        if (tgt.getEquivalence() != ConceptMapEquivalence.UNMATCHED) {
715                                lexer.token(":");
716                                tgt.setCode(lexer.take());
717                                if (tgt.getCode().startsWith("\""))
718                                  tgt.setCode(lexer.processConstant(tgt.getCode()));
719                        }
720                        if (lexer.hasComment())
721                                tgt.setComment(lexer.take().substring(2).trim());
722                }
723                lexer.token("}");
724        }
725
726
727        private ConceptMapGroupComponent getGroup(ConceptMap map, String srcs, String tgts) {
728          for (ConceptMapGroupComponent grp : map.getGroup()) {
729            if (grp.getSource().equals(srcs)) 
730              if ((tgts == null && !grp.hasTarget()) || (tgts != null && tgts.equals(grp.getTarget())))
731              return grp;
732          }
733          ConceptMapGroupComponent grp = map.addGroup(); 
734    grp.setSource(srcs);
735    grp.setTarget(tgts);
736    return grp;
737  }
738
739
740        private String readPrefix(Map<String, String> prefixes, FHIRLexer lexer) throws FHIRLexerException {
741                String prefix = lexer.take();
742                if (!prefixes.containsKey(prefix))
743                        throw lexer.error("Unknown prefix '"+prefix+"'");
744                return prefixes.get(prefix);
745        }
746
747
748        private ConceptMapEquivalence readEquivalence(FHIRLexer lexer) throws FHIRLexerException {
749                String token = lexer.take();
750    if (token.equals("-"))
751      return ConceptMapEquivalence.RELATEDTO;
752    if (token.equals("="))
753      return ConceptMapEquivalence.EQUAL;
754                if (token.equals("=="))
755                        return ConceptMapEquivalence.EQUIVALENT;
756                if (token.equals("!="))
757                        return ConceptMapEquivalence.DISJOINT;
758                if (token.equals("--"))
759                        return ConceptMapEquivalence.UNMATCHED;
760                if (token.equals("<="))
761                        return ConceptMapEquivalence.WIDER;
762                if (token.equals("<-"))
763                        return ConceptMapEquivalence.SUBSUMES;
764                if (token.equals(">="))
765                        return ConceptMapEquivalence.NARROWER;
766                if (token.equals(">-"))
767                        return ConceptMapEquivalence.SPECIALIZES;
768                if (token.equals("~"))
769                        return ConceptMapEquivalence.INEXACT;
770                throw lexer.error("Unknown equivalence token '"+token+"'");
771        }
772
773
774        private void parseUses(StructureMap result, FHIRLexer lexer) throws FHIRException {
775                lexer.token("uses");
776                StructureMapStructureComponent st = result.addStructure();
777                st.setUrl(lexer.readConstant("url"));
778                if (lexer.hasToken("alias")) {
779            lexer.token("alias");
780                  st.setAlias(lexer.take());
781                }
782                lexer.token("as");
783                st.setMode(StructureMapModelMode.fromCode(lexer.take()));
784                lexer.skipToken(";");
785                if (lexer.hasComment()) {
786                        st.setDocumentation(lexer.take().substring(2).trim());
787                }
788                lexer.skipComments();
789        }
790
791        private void parseImports(StructureMap result, FHIRLexer lexer) throws FHIRException {
792                lexer.token("imports");
793                result.addImport(lexer.readConstant("url"));
794                lexer.skipToken(";");
795                if (lexer.hasComment()) {
796                        lexer.next();
797                }
798                lexer.skipComments();
799        }
800
801        private void parseGroup(StructureMap result, FHIRLexer lexer) throws FHIRException {
802                lexer.token("group");
803                StructureMapGroupComponent group = result.addGroup();
804                if (lexer.hasToken("for")) {
805                  lexer.token("for");
806                  if ("type".equals(lexer.getCurrent())) {
807        lexer.token("type");
808        lexer.token("+");
809        lexer.token("types");
810        group.setTypeMode(StructureMapGroupTypeMode.TYPEANDTYPES);
811                  } else {
812                    lexer.token("types");
813        group.setTypeMode(StructureMapGroupTypeMode.TYPES);
814                  }
815                } else
816                  group.setTypeMode(StructureMapGroupTypeMode.NONE);
817                group.setName(lexer.take());
818                if (lexer.hasToken("extends")) {
819                        lexer.next();
820                        group.setExtends(lexer.take());
821                }
822                lexer.skipComments();
823                while (lexer.hasToken("input")) 
824                        parseInput(group, lexer);
825                while (!lexer.hasToken("endgroup")) {
826                        if (lexer.done())
827                                throw lexer.error("premature termination expecting 'endgroup'");
828                        parseRule(result, group.getRule(), lexer);
829                }
830                lexer.next();
831                lexer.skipComments();
832        }
833
834        private void parseInput(StructureMapGroupComponent group, FHIRLexer lexer) throws FHIRException {
835                lexer.token("input");
836                StructureMapGroupInputComponent input = group.addInput();
837                input.setName(lexer.take());
838                if (lexer.hasToken(":")) {
839                        lexer.token(":");
840                        input.setType(lexer.take());
841                }
842                lexer.token("as");
843                input.setMode(StructureMapInputMode.fromCode(lexer.take()));
844                if (lexer.hasComment()) {
845                        input.setDocumentation(lexer.take().substring(2).trim());
846                }
847                lexer.skipToken(";");
848                lexer.skipComments();
849        }
850
851        private void parseRule(StructureMap map, List<StructureMapGroupRuleComponent> list, FHIRLexer lexer) throws FHIRException {
852                StructureMapGroupRuleComponent rule = new StructureMapGroupRuleComponent(); 
853                list.add(rule);
854                rule.setName(lexer.takeDottedToken());
855                lexer.token(":");
856                lexer.token("for");
857                boolean done = false;
858                while (!done) {
859                        parseSource(rule, lexer);
860                        done = !lexer.hasToken(",");
861                        if (!done)
862                                lexer.next();
863                }
864                if (lexer.hasToken("make")) {
865                        lexer.token("make");
866                        done = false;
867                        while (!done) {
868                                parseTarget(rule, lexer);
869                                done = !lexer.hasToken(",");
870                                if (!done)
871                                        lexer.next();
872                        }
873                }
874                if (lexer.hasToken("then")) {
875                        lexer.token("then");
876                        if (lexer.hasToken("{")) {
877                                lexer.token("{");
878                                if (lexer.hasComment()) {
879                                        rule.setDocumentation(lexer.take().substring(2).trim());
880                                }
881                                lexer.skipComments();
882                                while (!lexer.hasToken("}")) {
883                                        if (lexer.done())
884                                                throw lexer.error("premature termination expecting '}' in nested group");
885                                        parseRule(map, rule.getRule(), lexer);
886                                }      
887                                lexer.token("}");
888                        } else {
889                                done = false;
890                                while (!done) {
891                                        parseRuleReference(rule, lexer);
892                                        done = !lexer.hasToken(",");
893                                        if (!done)
894                                                lexer.next();
895                                }
896                        }
897                } else if (lexer.hasComment()) {
898                        rule.setDocumentation(lexer.take().substring(2).trim());
899                }
900                if (isSimpleSyntax(rule)) {
901                  rule.getSourceFirstRep().setVariable(AUTO_VAR_NAME);
902                  rule.getTargetFirstRep().setVariable(AUTO_VAR_NAME);
903                  rule.getTargetFirstRep().setTransform(StructureMapTransform.CREATE); // with no parameter - e.g. imply what is to be created
904                  // no dependencies - imply what is to be done based on types
905                }
906                lexer.skipComments();
907        }
908
909        private boolean isSimpleSyntax(StructureMapGroupRuleComponent rule) {
910    return 
911        (rule.getSource().size() == 1 && rule.getSourceFirstRep().hasContext() && rule.getSourceFirstRep().hasElement() && !rule.getSourceFirstRep().hasVariable()) &&
912        (rule.getTarget().size() == 1 && rule.getTargetFirstRep().hasContext() && rule.getTargetFirstRep().hasElement() && !rule.getTargetFirstRep().hasVariable() && !rule.getTargetFirstRep().hasParameter()) &&
913        (rule.getDependent().size() == 0 && rule.getRule().size() == 0);
914  }
915
916  private void parseRuleReference(StructureMapGroupRuleComponent rule, FHIRLexer lexer) throws FHIRLexerException {
917                StructureMapGroupRuleDependentComponent ref = rule.addDependent();
918                ref.setName(lexer.take());
919                lexer.token("(");
920                boolean done = false;
921                while (!done) {
922                        ref.addVariable(lexer.take());
923                        done = !lexer.hasToken(",");
924                        if (!done)
925                                lexer.next();
926                }
927                lexer.token(")");
928        }
929
930        private void parseSource(StructureMapGroupRuleComponent rule, FHIRLexer lexer) throws FHIRException {
931                StructureMapGroupRuleSourceComponent source = rule.addSource();
932                source.setContext(lexer.take());
933                if (source.getContext().equals("search") && lexer.hasToken("(")) {
934            source.setContext("@search");
935      lexer.take();
936      ExpressionNode node = fpe.parse(lexer);
937      source.setUserData(MAP_SEARCH_EXPRESSION, node);
938      source.setElement(node.toString());
939      lexer.token(")");
940                } else if (lexer.hasToken(".")) {
941                        lexer.token(".");
942                        source.setElement(lexer.take());
943                }
944                if (lexer.hasToken(":")) {
945                  // type and cardinality
946                  lexer.token(":");
947                  source.setType(lexer.takeDottedToken());
948                  if (!lexer.hasToken("as", "first", "last", "not_first", "not_last", "only_one", "default")) {
949                    source.setMin(lexer.takeInt());
950                    lexer.token("..");
951                    source.setMax(lexer.take());
952                  }
953                }
954                if (lexer.hasToken("default")) {
955                  lexer.token("default");
956                  source.setDefaultValue(new StringType(lexer.readConstant("default value")));
957                }
958                if (Utilities.existsInList(lexer.getCurrent(), "first", "last", "not_first", "not_last", "only_one"))
959                        source.setListMode(StructureMapSourceListMode.fromCode(lexer.take()));
960
961                if (lexer.hasToken("as")) {
962                        lexer.take();
963                        source.setVariable(lexer.take());
964                }
965                if (lexer.hasToken("where")) {
966                        lexer.take();
967                        ExpressionNode node = fpe.parse(lexer);
968                        source.setUserData(MAP_WHERE_EXPRESSION, node);
969                        source.setCondition(node.toString());
970                }
971                if (lexer.hasToken("check")) {
972                        lexer.take();
973                        ExpressionNode node = fpe.parse(lexer);
974                        source.setUserData(MAP_WHERE_CHECK, node);
975                        source.setCheck(node.toString());
976                }
977        }
978
979        private void parseTarget(StructureMapGroupRuleComponent rule, FHIRLexer lexer) throws FHIRException {
980                StructureMapGroupRuleTargetComponent target = rule.addTarget();
981                String start = lexer.take();
982                if (lexer.hasToken(".")) {
983            target.setContext(start);
984            target.setContextType(StructureMapContextType.VARIABLE);
985            start = null;
986                        lexer.token(".");
987                        target.setElement(lexer.take());
988                }
989                String name;
990                boolean isConstant = false;
991                if (lexer.hasToken("=")) {
992                  if (start != null)
993              target.setContext(start);
994                        lexer.token("=");
995                        isConstant = lexer.isConstant(true);
996                        name = lexer.take();
997                } else 
998                  name = start;
999                
1000                if ("(".equals(name)) {
1001                  // inline fluentpath expression
1002      target.setTransform(StructureMapTransform.EVALUATE);
1003      ExpressionNode node = fpe.parse(lexer);
1004      target.setUserData(MAP_EXPRESSION, node);
1005      target.addParameter().setValue(new StringType(node.toString()));
1006      lexer.token(")");
1007                } else if (lexer.hasToken("(")) {
1008                                target.setTransform(StructureMapTransform.fromCode(name));
1009                                lexer.token("(");
1010                                if (target.getTransform() == StructureMapTransform.EVALUATE) {
1011                                        parseParameter(target, lexer);
1012                                        lexer.token(",");
1013                                        ExpressionNode node = fpe.parse(lexer);
1014                                        target.setUserData(MAP_EXPRESSION, node);
1015                                        target.addParameter().setValue(new StringType(node.toString()));
1016                                } else { 
1017                                        while (!lexer.hasToken(")")) {
1018                                                parseParameter(target, lexer);
1019                                                if (!lexer.hasToken(")"))
1020                                                        lexer.token(",");
1021                                        }       
1022                                }
1023                                lexer.token(")");
1024                } else if (name != null) {
1025                                target.setTransform(StructureMapTransform.COPY);
1026                                if (!isConstant) {
1027                                        String id = name;
1028                                        while (lexer.hasToken(".")) {
1029                                                id = id + lexer.take() + lexer.take();
1030                                        }
1031                                        target.addParameter().setValue(new IdType(id));
1032                                }
1033                                else 
1034                                        target.addParameter().setValue(readConstant(name, lexer));
1035                        }
1036                if (lexer.hasToken("as")) {
1037                        lexer.take();
1038                        target.setVariable(lexer.take());
1039                }
1040                while (Utilities.existsInList(lexer.getCurrent(), "first", "last", "share", "collate")) {
1041                        if (lexer.getCurrent().equals("share")) {
1042                                target.addListMode(StructureMapTargetListMode.SHARE);
1043                                lexer.next();
1044                                target.setListRuleId(lexer.take());
1045                        } else if (lexer.getCurrent().equals("first")) 
1046                                target.addListMode(StructureMapTargetListMode.FIRST);
1047                        else
1048                                target.addListMode(StructureMapTargetListMode.LAST);
1049                        lexer.next();
1050                }
1051        }
1052
1053
1054        private void parseParameter(StructureMapGroupRuleTargetComponent target, FHIRLexer lexer) throws FHIRLexerException, FHIRFormatError {
1055                if (!lexer.isConstant(true)) {
1056                        target.addParameter().setValue(new IdType(lexer.take()));
1057                } else if (lexer.isStringConstant())
1058                        target.addParameter().setValue(new StringType(lexer.readConstant("??")));
1059                else {
1060                        target.addParameter().setValue(readConstant(lexer.take(), lexer));
1061                }
1062        }
1063
1064        private Type readConstant(String s, FHIRLexer lexer) throws FHIRLexerException {
1065                if (Utilities.isInteger(s))
1066                        return new IntegerType(s);
1067                else if (Utilities.isDecimal(s))
1068                        return new DecimalType(s);
1069                else if (Utilities.existsInList(s, "true", "false"))
1070                        return new BooleanType(s.equals("true"));
1071                else 
1072                        return new StringType(lexer.processConstant(s));        
1073        }
1074
1075        public StructureDefinition getTargetType(StructureMap map) throws FHIRException {
1076          boolean found = false;
1077          StructureDefinition res = null;
1078          for (StructureMapStructureComponent uses : map.getStructure()) {
1079            if (uses.getMode() == StructureMapModelMode.TARGET) {
1080              if (found)
1081                throw new FHIRException("Multiple targets found in map "+map.getUrl());
1082              found = true;
1083              res = worker.fetchResource(StructureDefinition.class, uses.getUrl());
1084              if (res == null)
1085                throw new FHIRException("Unable to find "+uses.getUrl()+" referenced from map "+map.getUrl());      
1086            }
1087          }
1088          if (res == null)
1089      throw new FHIRException("No targets found in map "+map.getUrl());
1090          return res;
1091        }
1092
1093        public enum VariableMode {
1094                INPUT, OUTPUT
1095        }
1096
1097        public class Variable {
1098                private VariableMode mode;
1099                private String name;
1100                private Base object;
1101                public Variable(VariableMode mode, String name, Base object) {
1102                        super();
1103                        this.mode = mode;
1104                        this.name = name;
1105                        this.object = object;
1106                }
1107                public VariableMode getMode() {
1108                        return mode;
1109                }
1110                public String getName() {
1111                        return name;
1112                }
1113                public Base getObject() {
1114                        return object;
1115                }
1116    public String summary() {
1117      return name+": "+object.fhirType();
1118    }
1119        }
1120
1121        public class Variables {
1122                private List<Variable> list = new ArrayList<Variable>();
1123
1124                public void add(VariableMode mode, String name, Base object) {
1125                        Variable vv = null;
1126                        for (Variable v : list) 
1127                                if ((v.mode == mode) && v.getName().equals(name))
1128                                        vv = v;
1129                        if (vv != null)
1130                                list.remove(vv);
1131                        list.add(new Variable(mode, name, object));
1132                }
1133
1134                public Variables copy() {
1135                        Variables result = new Variables();
1136                        result.list.addAll(list);
1137                        return result;
1138                }
1139
1140                public Base get(VariableMode mode, String name) {
1141                        for (Variable v : list) 
1142                                if ((v.mode == mode) && v.getName().equals(name))
1143                                        return v.getObject();
1144                        return null;
1145                }
1146
1147    public String summary() {
1148      CommaSeparatedStringBuilder s = new CommaSeparatedStringBuilder();
1149      CommaSeparatedStringBuilder t = new CommaSeparatedStringBuilder();
1150      for (Variable v : list)
1151        if (v.mode == VariableMode.INPUT)
1152          s.append(v.summary());
1153        else
1154          t.append(v.summary());
1155      return "source variables ["+s.toString()+"], target variables ["+t.toString()+"]";
1156    }
1157        }
1158
1159        public class TransformContext {
1160                private Object appInfo;
1161
1162                public TransformContext(Object appInfo) {
1163                        super();
1164                        this.appInfo = appInfo;
1165                }
1166
1167                public Object getAppInfo() {
1168                        return appInfo;
1169                }
1170
1171        }
1172
1173        private void log(String cnt) {
1174          if (services != null)
1175            services.log(cnt);
1176        }
1177
1178        /**
1179         * Given an item, return all the children that conform to the pattern described in name
1180         * 
1181         * Possible patterns:
1182         *  - a simple name (which may be the base of a name with [] e.g. value[x])
1183         *  - a name with a type replacement e.g. valueCodeableConcept
1184         *  - * which means all children
1185         *  - ** which means all descendents
1186         *  
1187         * @param item
1188         * @param name
1189         * @param result
1190         * @throws FHIRException 
1191         */
1192        protected void getChildrenByName(Base item, String name, List<Base> result) throws FHIRException {
1193                for (Base v : item.listChildrenByName(name, true))
1194                        if (v != null)
1195                                result.add(v);
1196        }
1197
1198        public void transform(Object appInfo, Base source, StructureMap map, Base target) throws FHIRException {
1199                TransformContext context = new TransformContext(appInfo);
1200    log("Start Transform "+map.getUrl());
1201    StructureMapGroupComponent g = map.getGroup().get(0);
1202
1203                Variables vars = new Variables();
1204                vars.add(VariableMode.INPUT, getInputName(g, StructureMapInputMode.SOURCE, "source"), source);
1205                vars.add(VariableMode.OUTPUT, getInputName(g, StructureMapInputMode.TARGET, "target"), target);
1206
1207    executeGroup("", context, map, vars, g);
1208    if (target instanceof Element)
1209      ((Element) target).sort();
1210        }
1211
1212        private String getInputName(StructureMapGroupComponent g, StructureMapInputMode mode, String def) throws DefinitionException {
1213          String name = null;
1214    for (StructureMapGroupInputComponent inp : g.getInput()) {
1215      if (inp.getMode() == mode)
1216        if (name != null)
1217          throw new DefinitionException("This engine does not support multiple source inputs");
1218        else
1219          name = inp.getName();
1220    }
1221    return name == null ? def : name;
1222        }
1223
1224        private void executeGroup(String indent, TransformContext context, StructureMap map, Variables vars, StructureMapGroupComponent group) throws FHIRException {
1225                log(indent+"Group : "+group.getName());
1226    // todo: check inputs
1227                if (group.hasExtends()) {
1228                  ResolvedGroup rg = resolveGroupReference(map, group, group.getExtends());
1229                  executeGroup(indent+" ", context, rg.targetMap, vars, rg.target); 
1230                }
1231                  
1232                for (StructureMapGroupRuleComponent r : group.getRule()) {
1233                        executeRule(indent+"  ", context, map, vars, group, r);
1234                }
1235        }
1236
1237        private void executeRule(String indent, TransformContext context, StructureMap map, Variables vars, StructureMapGroupComponent group, StructureMapGroupRuleComponent rule) throws FHIRException {
1238                log(indent+"rule : "+rule.getName());
1239                if (rule.getName().contains("CarePlan.participant-unlink"))
1240                  System.out.println("debug");
1241                Variables srcVars = vars.copy();
1242                if (rule.getSource().size() != 1)
1243                        throw new FHIRException("Rule \""+rule.getName()+"\": not handled yet");
1244                List<Variables> source = processSource(rule.getName(), context, srcVars, rule.getSource().get(0));
1245                if (source != null) {
1246                        for (Variables v : source) {
1247                                for (StructureMapGroupRuleTargetComponent t : rule.getTarget()) {
1248                                        processTarget(rule.getName(), context, v, map, group, t, rule.getSource().size() == 1 ? rule.getSourceFirstRep().getVariable() : null);
1249                                }
1250                                if (rule.hasRule()) {
1251                                        for (StructureMapGroupRuleComponent childrule : rule.getRule()) {
1252                                                executeRule(indent +"  ", context, map, v, group, childrule);
1253                                        }
1254                                } else if (rule.hasDependent()) {
1255                                        for (StructureMapGroupRuleDependentComponent dependent : rule.getDependent()) {
1256                                                executeDependency(indent+"  ", context, map, v, group, dependent);
1257                                        }
1258                                } else if (rule.getSource().size() == 1 && rule.getSourceFirstRep().hasVariable() && rule.getTarget().size() == 1 && rule.getTargetFirstRep().hasVariable() && rule.getTargetFirstRep().getTransform() == StructureMapTransform.CREATE && !rule.getTargetFirstRep().hasParameter()) {
1259                                  // simple inferred, map by type
1260                                  Base src = v.get(VariableMode.INPUT, rule.getSourceFirstRep().getVariable());
1261                                  Base tgt = v.get(VariableMode.OUTPUT, rule.getTargetFirstRep().getVariable());
1262                                  String srcType = src.fhirType();
1263                                  String tgtType = tgt.fhirType();
1264                                  ResolvedGroup defGroup = resolveGroupByTypes(map, rule.getName(), group, srcType, tgtType);
1265                            Variables vdef = new Variables();
1266          vdef.add(VariableMode.INPUT, defGroup.target.getInput().get(0).getName(), src);
1267          vdef.add(VariableMode.OUTPUT, defGroup.target.getInput().get(1).getName(), tgt);
1268                                  executeGroup(indent+"  ", context, defGroup.targetMap, vdef, defGroup.target);
1269                                }
1270                        }
1271                }
1272        }
1273
1274        private void executeDependency(String indent, TransformContext context, StructureMap map, Variables vin, StructureMapGroupComponent group, StructureMapGroupRuleDependentComponent dependent) throws FHIRException {
1275          ResolvedGroup rg = resolveGroupReference(map, group, dependent.getName());
1276
1277                if (rg.target.getInput().size() != dependent.getVariable().size()) {
1278                        throw new FHIRException("Rule '"+dependent.getName()+"' has "+Integer.toString(rg.target.getInput().size())+" but the invocation has "+Integer.toString(dependent.getVariable().size())+" variables");
1279                }
1280                Variables v = new Variables();
1281                for (int i = 0; i < rg.target.getInput().size(); i++) {
1282                        StructureMapGroupInputComponent input = rg.target.getInput().get(i);
1283                        StringType rdp = dependent.getVariable().get(i);
1284      String var = rdp.asStringValue();
1285                        VariableMode mode = input.getMode() == StructureMapInputMode.SOURCE ? VariableMode.INPUT :   VariableMode.OUTPUT; 
1286                        Base vv = vin.get(mode, var);
1287      if (vv == null && mode == VariableMode.INPUT) //* once source, always source. but target can be treated as source at user convenient
1288        vv = vin.get(VariableMode.OUTPUT, var);
1289                        if (vv == null)
1290                                throw new FHIRException("Rule '"+dependent.getName()+"' "+mode.toString()+" variable '"+input.getName()+"' named as '"+var+"' has no value");
1291                        v.add(mode, input.getName(), vv);       
1292                }
1293                executeGroup(indent+"  ", context, rg.targetMap, v, rg.target);
1294        }
1295
1296  private String determineTypeFromSourceType(StructureMap map, StructureMapGroupComponent source, Base base, String[] types) throws FHIRException {
1297    String type = base.fhirType();
1298    String kn = "type^"+type;
1299    if (source.hasUserData(kn))
1300      return source.getUserString(kn);
1301    
1302    ResolvedGroup res = new ResolvedGroup();
1303    res.targetMap = null;
1304    res.target = null;
1305    for (StructureMapGroupComponent grp : map.getGroup()) {
1306      if (matchesByType(map, grp, type)) {
1307        if (res.targetMap == null) {
1308          res.targetMap = map;
1309          res.target = grp;
1310        } else 
1311          throw new FHIRException("Multiple possible matches looking for default rule for '"+type+"'");
1312      }
1313    }
1314    if (res.targetMap != null) {
1315      String result = getActualType(res.targetMap, res.target.getInput().get(1).getType());
1316      source.setUserData(kn, result);
1317      return result;
1318    }
1319
1320    for (UriType imp : map.getImport()) {
1321      List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
1322      if (impMapList.size() == 0)
1323        throw new FHIRException("Unable to find map(s) for "+imp.getValue());
1324      for (StructureMap impMap : impMapList) {
1325        if (!impMap.getUrl().equals(map.getUrl())) {
1326          for (StructureMapGroupComponent grp : impMap.getGroup()) {
1327            if (matchesByType(impMap, grp, type)) {
1328              if (res.targetMap == null) {
1329                res.targetMap = impMap;
1330                res.target = grp;
1331              } else 
1332                throw new FHIRException("Multiple possible matches for default rule for '"+type+"' in "+res.targetMap.getUrl()+" ("+res.target.getName()+") and "+impMap.getUrl()+" ("+grp.getName()+")");
1333            }
1334          }
1335        }
1336      }
1337    }
1338    if (res.target == null)
1339      throw new FHIRException("No matches found for default rule for '"+type+"' from "+map.getUrl());
1340    String result = getActualType(res.targetMap, res.target.getInput().get(1).getType()); // should be .getType, but R2...
1341    source.setUserData(kn, result);
1342    return result;
1343  }
1344
1345  private List<StructureMap> findMatchingMaps(String value) {
1346    List<StructureMap> res = new ArrayList<StructureMap>();
1347    if (value.contains("*")) {
1348      for (StructureMap sm : library.values()) {
1349        if (urlMatches(value, sm.getUrl())) {
1350          res.add(sm); 
1351        }
1352      }
1353    } else {
1354      StructureMap sm = library.get(value);
1355      if (sm != null)
1356        res.add(sm); 
1357    }
1358    Set<String> check = new HashSet<String>();
1359    for (StructureMap sm : res) {
1360      if (check.contains(sm.getUrl()))
1361        throw new Error("duplicate");
1362      else
1363        check.add(sm.getUrl());
1364    }
1365    return res;
1366  }
1367
1368  private boolean urlMatches(String mask, String url) {
1369    return url.length() > mask.length() && url.startsWith(mask.substring(0, mask.indexOf("*"))) && url.endsWith(mask.substring(mask.indexOf("*")+1)) ;
1370  }
1371
1372  private ResolvedGroup resolveGroupByTypes(StructureMap map, String ruleid, StructureMapGroupComponent source, String srcType, String tgtType) throws FHIRException {
1373    String kn = "types^"+srcType+":"+tgtType;
1374    if (source.hasUserData(kn))
1375      return (ResolvedGroup) source.getUserData(kn);
1376    
1377    ResolvedGroup res = new ResolvedGroup();
1378    res.targetMap = null;
1379    res.target = null;
1380    for (StructureMapGroupComponent grp : map.getGroup()) {
1381      if (matchesByType(map, grp, srcType, tgtType)) {
1382        if (res.targetMap == null) {
1383          res.targetMap = map;
1384          res.target = grp;
1385        } else 
1386          throw new FHIRException("Multiple possible matches looking for rule for '"+srcType+"/"+tgtType+"', from rule '"+ruleid+"'");
1387      }
1388    }
1389    if (res.targetMap != null) {
1390      source.setUserData(kn, res);
1391      return res;
1392    }
1393
1394    for (UriType imp : map.getImport()) {
1395      List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
1396      if (impMapList.size() == 0)
1397        throw new FHIRException("Unable to find map(s) for "+imp.getValue());
1398      for (StructureMap impMap : impMapList) {
1399        if (!impMap.getUrl().equals(map.getUrl())) {
1400          for (StructureMapGroupComponent grp : impMap.getGroup()) {
1401            if (matchesByType(impMap, grp, srcType, tgtType)) {
1402              if (res.targetMap == null) {
1403                res.targetMap = impMap;
1404                res.target = grp;
1405              } else 
1406                throw new FHIRException("Multiple possible matches for rule for '"+srcType+"/"+tgtType+"' in "+res.targetMap.getUrl()+" and "+impMap.getUrl()+", from rule '"+ruleid+"'");
1407            }
1408          }
1409        }
1410      }
1411    }
1412    if (res.target == null)
1413      throw new FHIRException("No matches found for rule for '"+srcType+"/"+tgtType+"' from "+map.getUrl()+", from rule '"+ruleid+"'");
1414    source.setUserData(kn, res);
1415    return res;
1416  }
1417
1418
1419  private boolean matchesByType(StructureMap map, StructureMapGroupComponent grp, String type) throws FHIRException {
1420    if (grp.getTypeMode() != StructureMapGroupTypeMode.TYPEANDTYPES)
1421      return false;
1422    if (grp.getInput().size() != 2 || grp.getInput().get(0).getMode() != StructureMapInputMode.SOURCE || grp.getInput().get(1).getMode() != StructureMapInputMode.TARGET)
1423      return false;
1424    return matchesType(map, type, grp.getInput().get(0).getType());
1425  }
1426
1427  private boolean matchesByType(StructureMap map, StructureMapGroupComponent grp, String srcType, String tgtType) throws FHIRException {
1428    if (grp.getTypeMode() == StructureMapGroupTypeMode.NONE)
1429      return false;
1430    if (grp.getInput().size() != 2 || grp.getInput().get(0).getMode() != StructureMapInputMode.SOURCE || grp.getInput().get(1).getMode() != StructureMapInputMode.TARGET)
1431      return false;
1432    if (!grp.getInput().get(0).hasType() || !grp.getInput().get(1).hasType())
1433      return false;
1434    return matchesType(map, srcType, grp.getInput().get(0).getType()) && matchesType(map, tgtType, grp.getInput().get(1).getType());
1435  }
1436
1437  private boolean matchesType(StructureMap map, String actualType, String statedType) throws FHIRException {
1438    // check the aliases
1439    for (StructureMapStructureComponent imp : map.getStructure()) {
1440      if (imp.hasAlias() && statedType.equals(imp.getAlias())) {
1441        StructureDefinition sd = worker.fetchResource(StructureDefinition.class, imp.getUrl());
1442        if (sd != null)
1443          statedType = sd.getType();
1444        break;
1445      }
1446    }
1447    
1448    return actualType.equals(statedType);
1449  }
1450
1451  private String getActualType(StructureMap map, String statedType) throws FHIRException {
1452    // check the aliases
1453    for (StructureMapStructureComponent imp : map.getStructure()) {
1454      if (imp.hasAlias() && statedType.equals(imp.getAlias())) {
1455        StructureDefinition sd = worker.fetchResource(StructureDefinition.class, imp.getUrl());
1456        if (sd == null)
1457          throw new FHIRException("Unable to resolve structure "+imp.getUrl());
1458        return sd.getId(); // should be sd.getType(), but R2...
1459      }
1460    }
1461    return statedType;
1462  }
1463
1464
1465  private ResolvedGroup resolveGroupReference(StructureMap map, StructureMapGroupComponent source, String name) throws FHIRException {
1466    String kn = "ref^"+name;
1467    if (source.hasUserData(kn))
1468      return (ResolvedGroup) source.getUserData(kn);
1469    
1470          ResolvedGroup res = new ResolvedGroup();
1471    res.targetMap = null;
1472    res.target = null;
1473    for (StructureMapGroupComponent grp : map.getGroup()) {
1474      if (grp.getName().equals(name)) {
1475        if (res.targetMap == null) {
1476          res.targetMap = map;
1477          res.target = grp;
1478        } else 
1479          throw new FHIRException("Multiple possible matches for rule '"+name+"'");
1480      }
1481    }
1482    if (res.targetMap != null) {
1483      source.setUserData(kn, res);
1484      return res;
1485    }
1486
1487    for (UriType imp : map.getImport()) {
1488      List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
1489      if (impMapList.size() == 0)
1490        throw new FHIRException("Unable to find map(s) for "+imp.getValue());
1491      for (StructureMap impMap : impMapList) {
1492        if (!impMap.getUrl().equals(map.getUrl())) {
1493          for (StructureMapGroupComponent grp : impMap.getGroup()) {
1494            if (grp.getName().equals(name)) {
1495              if (res.targetMap == null) {
1496                res.targetMap = impMap;
1497                res.target = grp;
1498              } else 
1499                throw new FHIRException("Multiple possible matches for rule '"+name+"' in "+res.targetMap.getUrl()+" and "+impMap.getUrl());
1500            }
1501          }
1502        }
1503      }
1504    }
1505    if (res.target == null)
1506      throw new FHIRException("No matches found for rule '"+name+"'. Reference found in "+map.getUrl());
1507    source.setUserData(kn, res);
1508    return res;
1509  }
1510
1511  private List<Variables> processSource(String ruleId, TransformContext context, Variables vars, StructureMapGroupRuleSourceComponent src) throws FHIRException {
1512    List<Base> items;
1513    if (src.getContext().equals("@search")) {
1514      ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_SEARCH_EXPRESSION);
1515      if (expr == null) {
1516        expr = fpe.parse(src.getElement());
1517        src.setUserData(MAP_SEARCH_EXPRESSION, expr);
1518      }
1519      String search = fpe.evaluateToString(vars, null, new StringType(), expr); // string is a holder of nothing to ensure that variables are processed correctly 
1520      items = services.performSearch(context.appInfo, search);
1521    } else {
1522      items = new ArrayList<Base>();
1523      Base b = vars.get(VariableMode.INPUT, src.getContext());
1524      if (b == null)
1525        throw new FHIRException("Unknown input variable "+src.getContext());
1526
1527      if (!src.hasElement()) 
1528        items.add(b);
1529      else { 
1530        getChildrenByName(b, src.getElement(), items);
1531        if (items.size() == 0 && src.hasDefaultValue())
1532          items.add(src.getDefaultValue());
1533      }
1534    }
1535    
1536                if (src.hasType()) {
1537            List<Base> remove = new ArrayList<Base>();
1538            for (Base item : items) {
1539              if (item != null && !isType(item, src.getType())) {
1540                remove.add(item);
1541              }
1542            }
1543            items.removeAll(remove);
1544                }
1545
1546    if (src.hasCondition()) {
1547      ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_EXPRESSION);
1548      if (expr == null) {
1549        expr = fpe.parse(src.getCondition());
1550        //        fpe.check(context.appInfo, ??, ??, expr)
1551        src.setUserData(MAP_WHERE_EXPRESSION, expr);
1552      }
1553      List<Base> remove = new ArrayList<Base>();
1554      for (Base item : items) {
1555        if (!fpe.evaluateToBoolean(vars, null, item, expr))
1556          remove.add(item);
1557      }
1558      items.removeAll(remove);
1559    }
1560
1561    if (src.hasCheck()) {
1562      ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_CHECK);
1563      if (expr == null) {
1564        expr = fpe.parse(src.getCheck());
1565        //        fpe.check(context.appInfo, ??, ??, expr)
1566        src.setUserData(MAP_WHERE_CHECK, expr);
1567      }
1568      List<Base> remove = new ArrayList<Base>();
1569      for (Base item : items) {
1570        if (!fpe.evaluateToBoolean(vars, null, item, expr))
1571          throw new FHIRException("Rule \""+ruleId+"\": Check condition failed");
1572      }
1573    } 
1574
1575                
1576                if (src.hasListMode() && !items.isEmpty()) {
1577                  switch (src.getListMode()) {
1578                  case FIRST:
1579                    Base bt = items.get(0);
1580        items.clear();
1581        items.add(bt);
1582                    break;
1583                  case NOTFIRST: 
1584        if (items.size() > 0)
1585          items.remove(0);
1586        break;
1587                  case LAST:
1588        bt = items.get(items.size()-1);
1589        items.clear();
1590        items.add(bt);
1591        break;
1592                  case NOTLAST: 
1593        if (items.size() > 0)
1594          items.remove(items.size()-1);
1595        break;
1596                  case ONLYONE:
1597                    if (items.size() > 1)
1598          throw new FHIRException("Rule \""+ruleId+"\": Check condition failed: the collection has more than one item");
1599        break;
1600      case NULL:
1601                  }
1602                }
1603                List<Variables> result = new ArrayList<Variables>();
1604                for (Base r : items) {
1605                        Variables v = vars.copy();
1606                        if (src.hasVariable())
1607                                v.add(VariableMode.INPUT, src.getVariable(), r);
1608                        result.add(v); 
1609                }
1610                return result;
1611        }
1612
1613
1614        private boolean isType(Base item, String type) {
1615    if (type.equals(item.fhirType()))
1616      return true;
1617    return false;
1618  }
1619
1620  private void processTarget(String ruleId, TransformContext context, Variables vars, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, String srcVar) throws FHIRException {
1621          Base dest = null;
1622          if (tgt.hasContext()) {
1623                dest = vars.get(VariableMode.OUTPUT, tgt.getContext());
1624                if (dest == null)
1625                        throw new FHIRException("Rule \""+ruleId+"\": target context not known: "+tgt.getContext());
1626                if (!tgt.hasElement())
1627                        throw new FHIRException("Rule \""+ruleId+"\": Not supported yet");
1628          }
1629                Base v = null;
1630                if (tgt.hasTransform()) {
1631                        v = runTransform(ruleId, context, map, group, tgt, vars, dest, tgt.getElement(), srcVar);
1632                        if (v != null && dest != null)
1633                                v = dest.setProperty(tgt.getElement().hashCode(), tgt.getElement(), v); // reset v because some implementations may have to rewrite v when setting the value
1634                } else if (dest != null) 
1635                        v = dest.makeProperty(tgt.getElement().hashCode(), tgt.getElement());
1636                if (tgt.hasVariable() && v != null)
1637                        vars.add(VariableMode.OUTPUT, tgt.getVariable(), v);
1638        }
1639
1640        private Base runTransform(String ruleId, TransformContext context, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, Variables vars, Base dest, String element, String srcVar) throws FHIRException {
1641          try {
1642            switch (tgt.getTransform()) {
1643            case CREATE :
1644              String tn;
1645              if (tgt.getParameter().isEmpty()) {
1646                // we have to work out the type. First, we see if there is a single type for the target. If there is, we use that
1647                String[] types = dest.getTypesForProperty(element.hashCode(), element);
1648                if (types.length == 1 && !"*".equals(types[0]) && !types[0].equals("Resource"))
1649                  tn = types[0];
1650                else if (srcVar != null) {
1651                  tn = determineTypeFromSourceType(map, group, vars.get(VariableMode.INPUT, srcVar), types);
1652                } else
1653                  throw new Error("Cannot determine type implicitly because there is no single input variable");
1654              } else
1655                tn = getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString());
1656              Base res = services != null ? services.createType(context.getAppInfo(), tn) : ResourceFactory.createResourceOrType(tn);
1657              if (res.isResource() && !res.fhirType().equals("Parameters")) {
1658//              res.setIdBase(tgt.getParameter().size() > 1 ? getParamString(vars, tgt.getParameter().get(0)) : UUID.randomUUID().toString().toLowerCase());
1659                if (services != null) 
1660                  res = services.createResource(context.getAppInfo(), res);
1661              }
1662              if (tgt.hasUserData("profile"))
1663                res.setUserData("profile", tgt.getUserData("profile"));
1664              return res;
1665            case COPY : 
1666              return getParam(vars, tgt.getParameter().get(0));
1667            case EVALUATE :
1668              ExpressionNode expr = (ExpressionNode) tgt.getUserData(MAP_EXPRESSION);
1669              if (expr == null) {
1670                expr = fpe.parse(getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString()));
1671                tgt.setUserData(MAP_WHERE_EXPRESSION, expr);
1672              }
1673              List<Base> v = fpe.evaluate(vars, null, tgt.getParameter().size() == 2 ? getParam(vars, tgt.getParameter().get(0)) : new BooleanType(false), expr);
1674              if (v.size() == 0)
1675                return null;
1676              else if (v.size() != 1)
1677                throw new FHIRException("Rule \""+ruleId+"\": Evaluation of "+expr.toString()+" returned "+Integer.toString(v.size())+" objects");
1678              else
1679                return v.get(0);
1680
1681            case TRUNCATE : 
1682              String src = getParamString(vars, tgt.getParameter().get(0));
1683              String len = getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString());
1684              if (Utilities.isInteger(len)) {
1685                int l = Integer.parseInt(len);
1686                if (src.length() > l)
1687                  src = src.substring(0, l);
1688              }
1689              return new StringType(src);
1690            case ESCAPE : 
1691              throw new Error("Rule \""+ruleId+"\": Transform "+tgt.getTransform().toCode()+" not supported yet");
1692            case CAST :
1693              throw new Error("Rule \""+ruleId+"\": Transform "+tgt.getTransform().toCode()+" not supported yet");
1694            case APPEND : 
1695              throw new Error("Rule \""+ruleId+"\": Transform "+tgt.getTransform().toCode()+" not supported yet");
1696            case TRANSLATE : 
1697              return translate(context, map, vars, tgt.getParameter());
1698            case REFERENCE :
1699              Base b = getParam(vars, tgt.getParameter().get(0));
1700              if (b == null)
1701                throw new FHIRException("Rule \""+ruleId+"\": Unable to find parameter "+((IdType) tgt.getParameter().get(0).getValue()).asStringValue());
1702              if (!b.isResource())
1703                throw new FHIRException("Rule \""+ruleId+"\": Transform engine cannot point at an element of type "+b.fhirType());
1704              else {
1705                String id = b.getIdBase();
1706                if (id == null) {
1707                  id = UUID.randomUUID().toString().toLowerCase();
1708                  b.setIdBase(id);
1709                }
1710                return new Reference().setReference(b.fhirType()+"/"+id);
1711              }
1712            case DATEOP :
1713              throw new Error("Rule \""+ruleId+"\": Transform "+tgt.getTransform().toCode()+" not supported yet");
1714            case UUID :
1715              return new IdType(UUID.randomUUID().toString());
1716            case POINTER :
1717              b = getParam(vars, tgt.getParameter().get(0));
1718              if (b instanceof Resource)
1719                return new UriType("urn:uuid:"+((Resource) b).getId());
1720              else
1721                throw new FHIRException("Rule \""+ruleId+"\": Transform engine cannot point at an element of type "+b.fhirType());
1722            case CC:
1723              CodeableConcept cc = new CodeableConcept();
1724              cc.addCoding(buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString())));
1725              return cc;
1726            case C: 
1727              Coding c = buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString()));
1728              return c;
1729            default:
1730              throw new Error("Rule \""+ruleId+"\": Transform Unknown: "+tgt.getTransform().toCode());
1731            }
1732          } catch (Exception e) {
1733            throw new FHIRException("Exception executing transform "+tgt.toString()+" on Rule \""+ruleId+"\": "+e.getMessage(), e);
1734          }
1735        }
1736
1737
1738  private Coding buildCoding(String uri, String code) throws FHIRException {
1739          // if we can get this as a valueSet, we will
1740          String system = null;
1741          String display = null;
1742          ValueSet vs = Utilities.noString(uri) ? null : worker.fetchResourceWithException(ValueSet.class, uri);
1743          if (vs != null) {
1744            ValueSetExpansionOutcome vse = worker.expandVS(vs, true, false);
1745            if (vse.getError() != null)
1746              throw new FHIRException(vse.getError());
1747            CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
1748            for (ValueSetExpansionContainsComponent t : vse.getValueset().getExpansion().getContains()) {
1749              if (t.hasCode())
1750                b.append(t.getCode());
1751              if (code.equals(t.getCode()) && t.hasSystem()) {
1752                system = t.getSystem();
1753          display = t.getDisplay();
1754                break;
1755              }
1756        if (code.equalsIgnoreCase(t.getDisplay()) && t.hasSystem()) {
1757          system = t.getSystem();
1758          display = t.getDisplay();
1759          break;
1760        }
1761            }
1762            if (system == null)
1763              throw new FHIRException("The code '"+code+"' is not in the value set '"+uri+"' (valid codes: "+b.toString()+"; also checked displays)");
1764          } else
1765            system = uri;
1766          ValidationResult vr = worker.validateCode(system, code, null);
1767          if (vr != null && vr.getDisplay() != null)
1768            display = vr.getDisplay();
1769   return new Coding().setSystem(system).setCode(code).setDisplay(display);
1770  }
1771
1772
1773  private String getParamStringNoNull(Variables vars, StructureMapGroupRuleTargetParameterComponent parameter, String message) throws FHIRException {
1774    Base b = getParam(vars, parameter);
1775    if (b == null)
1776      throw new FHIRException("Unable to find a value for "+parameter.toString()+". Context: "+message);
1777    if (!b.hasPrimitiveValue())
1778      throw new FHIRException("Found a value for "+parameter.toString()+", but it has a type of "+b.fhirType()+" and cannot be treated as a string. Context: "+message);
1779    return b.primitiveValue();
1780  }
1781
1782  private String getParamString(Variables vars, StructureMapGroupRuleTargetParameterComponent parameter) throws DefinitionException {
1783                Base b = getParam(vars, parameter);
1784                if (b == null || !b.hasPrimitiveValue())
1785                        return null;
1786                return b.primitiveValue();
1787        }
1788
1789
1790        private Base getParam(Variables vars, StructureMapGroupRuleTargetParameterComponent parameter) throws DefinitionException {
1791                Type p = parameter.getValue();
1792                if (!(p instanceof IdType))
1793                        return p;
1794                else { 
1795                        String n = ((IdType) p).asStringValue();
1796      Base b = vars.get(VariableMode.INPUT, n);
1797                        if (b == null)
1798                                b = vars.get(VariableMode.OUTPUT, n);
1799                        if (b == null)
1800        throw new DefinitionException("Variable "+n+" not found ("+vars.summary()+")");
1801                        return b;
1802                }
1803        }
1804
1805
1806        private Base translate(TransformContext context, StructureMap map, Variables vars, List<StructureMapGroupRuleTargetParameterComponent> parameter) throws FHIRException {
1807                Base src = getParam(vars, parameter.get(0));
1808                String id = getParamString(vars, parameter.get(1));
1809                String fld = parameter.size() > 2 ? getParamString(vars, parameter.get(2)) : null;
1810                return translate(context, map, src, id, fld);
1811        }
1812
1813        private class SourceElementComponentWrapper {
1814          private ConceptMapGroupComponent group;
1815    private SourceElementComponent comp;
1816    public SourceElementComponentWrapper(ConceptMapGroupComponent group, SourceElementComponent comp) {
1817      super();
1818      this.group = group;
1819      this.comp = comp;
1820    }
1821        }
1822        public Base translate(TransformContext context, StructureMap map, Base source, String conceptMapUrl, String fieldToReturn) throws FHIRException {
1823                Coding src = new Coding();
1824                if (source.isPrimitive()) {
1825                        src.setCode(source.primitiveValue());
1826                } else if ("Coding".equals(source.fhirType())) {
1827                        Base[] b = source.getProperty("system".hashCode(), "system", true);
1828                        if (b.length == 1)
1829                                src.setSystem(b[0].primitiveValue());
1830                        b = source.getProperty("code".hashCode(), "code", true);
1831                        if (b.length == 1)
1832                                src.setCode(b[0].primitiveValue());
1833                } else if ("CE".equals(source.fhirType())) {
1834                        Base[] b = source.getProperty("codeSystem".hashCode(), "codeSystem", true);
1835                        if (b.length == 1)
1836                                src.setSystem(b[0].primitiveValue());
1837                        b = source.getProperty("code".hashCode(), "code", true);
1838                        if (b.length == 1)
1839                                src.setCode(b[0].primitiveValue());
1840                } else
1841                        throw new FHIRException("Unable to translate source "+source.fhirType());
1842
1843                String su = conceptMapUrl;
1844                if (conceptMapUrl.equals("http://hl7.org/fhir/ConceptMap/special-oid2uri")) {
1845                        String uri = worker.oid2Uri(src.getCode());
1846                        if (uri == null)
1847                                uri = "urn:oid:"+src.getCode();
1848                        if ("uri".equals(fieldToReturn))
1849                                return new UriType(uri);
1850                        else
1851                                throw new FHIRException("Error in return code");
1852                } else {
1853                        ConceptMap cmap = null;
1854                        if (conceptMapUrl.startsWith("#")) {
1855                                for (Resource r : map.getContained()) {
1856                                        if (r instanceof ConceptMap && ((ConceptMap) r).getId().equals(conceptMapUrl.substring(1))) {
1857                                                cmap = (ConceptMap) r;
1858                                                su = map.getUrl()+conceptMapUrl;
1859                                        }
1860                                }
1861                                if (cmap == null)
1862                      throw new FHIRException("Unable to translate - cannot find map "+conceptMapUrl);
1863                        } else
1864                                cmap = worker.fetchResource(ConceptMap.class, conceptMapUrl);
1865                        Coding outcome = null;
1866                        boolean done = false;
1867                        String message = null;
1868                        if (cmap == null) {
1869                                if (services == null) 
1870                                        message = "No map found for "+conceptMapUrl;
1871                                else {
1872                                        outcome = services.translate(context.appInfo, src, conceptMapUrl);
1873                                        done = true;
1874                                }
1875                        } else {
1876                          List<SourceElementComponentWrapper> list = new ArrayList<SourceElementComponentWrapper>();
1877                          for (ConceptMapGroupComponent g : cmap.getGroup()) {
1878                            for (SourceElementComponent e : g.getElement()) {
1879                                                if (!src.hasSystem() && src.getCode().equals(e.getCode())) 
1880                                list.add(new SourceElementComponentWrapper(g, e));
1881                              else if (src.hasSystem() && src.getSystem().equals(g.getSource()) && src.getCode().equals(e.getCode()))
1882                                list.add(new SourceElementComponentWrapper(g, e));
1883                            }
1884                                }
1885                                if (list.size() == 0)
1886                                        done = true;
1887                                else if (list.get(0).comp.getTarget().size() == 0)
1888                                        message = "Concept map "+su+" found no translation for "+src.getCode();
1889                                else {
1890                                        for (TargetElementComponent tgt : list.get(0).comp.getTarget()) {
1891                                                if (tgt.getEquivalence() == null || EnumSet.of( ConceptMapEquivalence.EQUAL , ConceptMapEquivalence.RELATEDTO , ConceptMapEquivalence.EQUIVALENT, ConceptMapEquivalence.WIDER).contains(tgt.getEquivalence())) {
1892                                                        if (done) {
1893                                                                message = "Concept map "+su+" found multiple matches for "+src.getCode();
1894                                                                done = false;
1895                                                        } else {
1896                                                                done = true;
1897                                                                outcome = new Coding().setCode(tgt.getCode()).setSystem(list.get(0).group.getTarget());
1898                                                        }
1899                                                } else if (tgt.getEquivalence() == ConceptMapEquivalence.UNMATCHED) {
1900                                                        done = true;
1901                                                }
1902                                        }
1903                                        if (!done)
1904                                                message = "Concept map "+su+" found no usable translation for "+src.getCode();
1905                                }
1906                        }
1907                        if (!done) 
1908                                throw new FHIRException(message);
1909                        if (outcome == null)
1910                                return null;
1911                        if ("code".equals(fieldToReturn))
1912                                return new CodeType(outcome.getCode());
1913                        else
1914                                return outcome; 
1915                }
1916        }
1917
1918
1919        public Map<String, StructureMap> getLibrary() {
1920          return library;
1921        }
1922
1923        public class PropertyWithType {
1924    private String path;
1925    private Property baseProperty;
1926    private Property profileProperty;
1927          private TypeDetails types;
1928    public PropertyWithType(String path, Property baseProperty, Property profileProperty, TypeDetails types) {
1929      super();
1930      this.baseProperty = baseProperty;
1931      this.profileProperty = profileProperty;
1932      this.path = path;
1933      this.types = types;
1934    }
1935
1936    public TypeDetails getTypes() {
1937      return types;
1938    }
1939    public String getPath() {
1940      return path;
1941    }
1942
1943    public Property getBaseProperty() {
1944      return baseProperty;
1945    }
1946
1947    public void setBaseProperty(Property baseProperty) {
1948      this.baseProperty = baseProperty;
1949    }
1950
1951    public Property getProfileProperty() {
1952      return profileProperty;
1953    }
1954
1955    public void setProfileProperty(Property profileProperty) {
1956      this.profileProperty = profileProperty;
1957    }
1958
1959    public String summary() {
1960      return path;
1961    }
1962    
1963        }
1964        
1965        public class VariableForProfiling {
1966            private VariableMode mode;
1967            private String name;
1968            private PropertyWithType property;
1969            
1970            public VariableForProfiling(VariableMode mode, String name, PropertyWithType property) {
1971              super();
1972              this.mode = mode;
1973              this.name = name;
1974              this.property = property;
1975            }
1976            public VariableMode getMode() {
1977              return mode;
1978            }
1979            public String getName() {
1980              return name;
1981            }
1982      public PropertyWithType getProperty() {
1983        return property;
1984      }
1985      public String summary() {
1986        return name+": "+property.summary();
1987      }      
1988          }
1989
1990  public class VariablesForProfiling {
1991    private List<VariableForProfiling> list = new ArrayList<VariableForProfiling>();
1992    private boolean optional;
1993    private boolean repeating;
1994
1995    public VariablesForProfiling(boolean optional, boolean repeating) {
1996      this.optional = optional;
1997      this.repeating = repeating;
1998    }
1999
2000    public void add(VariableMode mode, String name, String path, Property property, TypeDetails types) {
2001      add(mode, name, new PropertyWithType(path, property, null, types));
2002    }
2003    
2004    public void add(VariableMode mode, String name, String path, Property baseProperty, Property profileProperty, TypeDetails types) {
2005      add(mode, name, new PropertyWithType(path, baseProperty, profileProperty, types));
2006    }
2007    
2008    public void add(VariableMode mode, String name, PropertyWithType property) {
2009      VariableForProfiling vv = null;
2010      for (VariableForProfiling v : list) 
2011        if ((v.mode == mode) && v.getName().equals(name))
2012          vv = v;
2013      if (vv != null)
2014        list.remove(vv);
2015      list.add(new VariableForProfiling(mode, name, property));
2016    }
2017
2018    public VariablesForProfiling copy(boolean optional, boolean repeating) {
2019      VariablesForProfiling result = new VariablesForProfiling(optional, repeating);
2020      result.list.addAll(list);
2021      return result;
2022    }
2023
2024    public VariablesForProfiling copy() {
2025      VariablesForProfiling result = new VariablesForProfiling(optional, repeating);
2026      result.list.addAll(list);
2027      return result;
2028    }
2029
2030    public VariableForProfiling get(VariableMode mode, String name) {
2031      if (mode == null) {
2032        for (VariableForProfiling v : list) 
2033          if ((v.mode == VariableMode.OUTPUT) && v.getName().equals(name))
2034            return v;
2035        for (VariableForProfiling v : list) 
2036          if ((v.mode == VariableMode.INPUT) && v.getName().equals(name))
2037            return v;
2038      }
2039      for (VariableForProfiling v : list) 
2040        if ((v.mode == mode) && v.getName().equals(name))
2041          return v;
2042      return null;
2043    }
2044
2045    public String summary() {
2046      CommaSeparatedStringBuilder s = new CommaSeparatedStringBuilder();
2047      CommaSeparatedStringBuilder t = new CommaSeparatedStringBuilder();
2048      for (VariableForProfiling v : list)
2049        if (v.mode == VariableMode.INPUT)
2050          s.append(v.summary());
2051        else
2052          t.append(v.summary());
2053      return "source variables ["+s.toString()+"], target variables ["+t.toString()+"]";
2054    }
2055  }
2056
2057  public class StructureMapAnalysis {
2058    private List<StructureDefinition> profiles = new ArrayList<StructureDefinition>();
2059    private XhtmlNode summary;
2060    public List<StructureDefinition> getProfiles() {
2061      return profiles;
2062    }
2063    public XhtmlNode getSummary() {
2064      return summary;
2065    }
2066    
2067  }
2068
2069        /**
2070         * Given a structure map, return a set of analyses on it. 
2071         * 
2072         * Returned:
2073         *   - a list or profiles for what it will create. First profile is the target
2074         *   - a table with a summary (in xhtml) for easy human undertanding of the mapping
2075         *   
2076         * 
2077         * @param appInfo
2078         * @param map
2079         * @return
2080         * @throws Exception
2081         */
2082  public StructureMapAnalysis analyse(Object appInfo, StructureMap map) throws Exception {
2083    ids.clear();
2084    StructureMapAnalysis result = new StructureMapAnalysis(); 
2085    TransformContext context = new TransformContext(appInfo);
2086    VariablesForProfiling vars = new VariablesForProfiling(false, false);
2087    StructureMapGroupComponent start = map.getGroup().get(0);
2088    for (StructureMapGroupInputComponent t : start.getInput()) {
2089      PropertyWithType ti = resolveType(map, t.getType(), t.getMode());
2090      if (t.getMode() == StructureMapInputMode.SOURCE)
2091       vars.add(VariableMode.INPUT, t.getName(), ti);
2092      else 
2093        vars.add(VariableMode.OUTPUT, t.getName(), createProfile(map, result.profiles, ti, start.getName(), start));
2094    }
2095
2096    result.summary = new XhtmlNode(NodeType.Element, "table").setAttribute("class", "grid");
2097    XhtmlNode tr = result.summary.addTag("tr");
2098    tr.addTag("td").addTag("b").addText("Source");
2099    tr.addTag("td").addTag("b").addText("Target");
2100    
2101    log("Start Profiling Transform "+map.getUrl());
2102    analyseGroup("", context, map, vars, start, result);
2103    ProfileUtilities pu = new ProfileUtilities(worker, null, pkp);
2104    for (StructureDefinition sd : result.getProfiles())
2105      pu.cleanUpDifferential(sd);
2106    return result;
2107  }
2108
2109
2110  private void analyseGroup(String indent, TransformContext context, StructureMap map, VariablesForProfiling vars, StructureMapGroupComponent group, StructureMapAnalysis result) throws Exception {
2111    log(indent+"Analyse Group : "+group.getName());
2112    // todo: extends
2113    // todo: check inputs
2114    XhtmlNode tr = result.summary.addTag("tr").setAttribute("class", "diff-title");
2115    XhtmlNode xs = tr.addTag("td");
2116    XhtmlNode xt = tr.addTag("td");
2117    for (StructureMapGroupInputComponent inp : group.getInput()) {
2118      if (inp.getMode() == StructureMapInputMode.SOURCE) 
2119        noteInput(vars, inp, VariableMode.INPUT, xs);
2120      if (inp.getMode() == StructureMapInputMode.TARGET) 
2121        noteInput(vars, inp, VariableMode.OUTPUT, xt);
2122    }
2123    for (StructureMapGroupRuleComponent r : group.getRule()) {
2124      analyseRule(indent+"  ", context, map, vars, group, r, result);
2125    }    
2126  }
2127
2128
2129  private void noteInput(VariablesForProfiling vars, StructureMapGroupInputComponent inp, VariableMode mode, XhtmlNode xs) {
2130    VariableForProfiling v = vars.get(mode, inp.getName());
2131    if (v != null)
2132      xs.addText("Input: "+v.property.getPath());
2133  }
2134
2135  private void analyseRule(String indent, TransformContext context, StructureMap map, VariablesForProfiling vars, StructureMapGroupComponent group, StructureMapGroupRuleComponent rule, StructureMapAnalysis result) throws Exception {
2136    log(indent+"Analyse rule : "+rule.getName());
2137    XhtmlNode tr = result.summary.addTag("tr");
2138    XhtmlNode xs = tr.addTag("td");
2139    XhtmlNode xt = tr.addTag("td");
2140
2141    VariablesForProfiling srcVars = vars.copy();
2142    if (rule.getSource().size() != 1)
2143      throw new Exception("Rule \""+rule.getName()+"\": not handled yet");
2144    VariablesForProfiling source = analyseSource(rule.getName(), context, srcVars, rule.getSourceFirstRep(), xs);
2145
2146    TargetWriter tw = new TargetWriter();
2147      for (StructureMapGroupRuleTargetComponent t : rule.getTarget()) {
2148      analyseTarget(rule.getName(), context, source, map, t, rule.getSourceFirstRep().getVariable(), tw, result.profiles, rule.getName());
2149      }
2150    tw.commit(xt);
2151
2152          for (StructureMapGroupRuleComponent childrule : rule.getRule()) {
2153      analyseRule(indent+"  ", context, map, source, group, childrule, result);
2154          }
2155//    for (StructureMapGroupRuleDependentComponent dependent : rule.getDependent()) {
2156//      executeDependency(indent+"  ", context, map, v, group, dependent); // do we need group here?
2157//    }
2158          }
2159
2160  public class StringPair {
2161    private String var;
2162    private String desc;
2163    public StringPair(String var, String desc) {
2164      super();
2165      this.var = var;
2166      this.desc = desc;
2167        }
2168    public String getVar() {
2169      return var;
2170      }
2171    public String getDesc() {
2172      return desc;
2173    }
2174  }
2175  public class TargetWriter {
2176    private Map<String, String> newResources = new HashMap<String, String>();
2177    private List<StringPair> assignments = new ArrayList<StringPair>();
2178    private List<StringPair> keyProps = new ArrayList<StringPair>();
2179    private CommaSeparatedStringBuilder txt = new CommaSeparatedStringBuilder();
2180
2181    public void newResource(String var, String name) {
2182      newResources.put(var, name);
2183      txt.append("new "+name);
2184    }
2185
2186    public void valueAssignment(String context, String desc) {
2187      assignments.add(new StringPair(context, desc));      
2188      txt.append(desc);
2189        }
2190
2191    public void keyAssignment(String context, String desc) {
2192      keyProps.add(new StringPair(context, desc));      
2193      txt.append(desc);
2194    }
2195    public void commit(XhtmlNode xt) {
2196      if (newResources.size() == 1 && assignments.size() == 1 && newResources.containsKey(assignments.get(0).getVar()) && keyProps.size() == 1 && newResources.containsKey(keyProps.get(0).getVar()) ) {
2197        xt.addText("new "+assignments.get(0).desc+" ("+keyProps.get(0).desc.substring(keyProps.get(0).desc.indexOf(".")+1)+")");
2198      } else if (newResources.size() == 1 && assignments.size() == 1 && newResources.containsKey(assignments.get(0).getVar()) && keyProps.size() == 0) {
2199        xt.addText("new "+assignments.get(0).desc);
2200      } else {
2201        xt.addText(txt.toString());        
2202    }
2203    }
2204  }
2205
2206  private VariablesForProfiling analyseSource(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMapGroupRuleSourceComponent src, XhtmlNode td) throws Exception {
2207    VariableForProfiling var = vars.get(VariableMode.INPUT, src.getContext());
2208    if (var == null)
2209      throw new FHIRException("Rule \""+ruleId+"\": Unknown input variable "+src.getContext());
2210    PropertyWithType prop = var.getProperty();
2211
2212    boolean optional = false;
2213    boolean repeating = false;
2214
2215    if (src.hasCondition()) {
2216      optional = true;
2217    }
2218
2219    if (src.hasElement()) {
2220      Property element = prop.getBaseProperty().getChild(prop.types.getType(), src.getElement());
2221      if (element == null)
2222        throw new Exception("Rule \""+ruleId+"\": Unknown element name "+src.getElement());
2223      if (element.getDefinition().getMin() == 0)
2224        optional = true;
2225      if (element.getDefinition().getMax().equals("*"))
2226        repeating = true;
2227      VariablesForProfiling result = vars.copy(optional, repeating);
2228      TypeDetails type = new TypeDetails(CollectionStatus.SINGLETON);
2229      for (TypeRefComponent tr : element.getDefinition().getType()) {
2230        if (!tr.hasCode())
2231          throw new Error("Rule \""+ruleId+"\": Element has no type");
2232        ProfiledType pt = new ProfiledType(tr.getCode());
2233        if (tr.hasProfile())
2234          pt.addProfile(tr.getProfile());
2235        if (element.getDefinition().hasBinding())
2236          pt.addBinding(element.getDefinition().getBinding());
2237        type.addType(pt);
2238    } 
2239      td.addText(prop.getPath()+"."+src.getElement()); 
2240      if (src.hasVariable())
2241        result.add(VariableMode.INPUT, src.getVariable(), new PropertyWithType(prop.getPath()+"."+src.getElement(), element, null, type));
2242    return result;
2243    } else {
2244      td.addText(prop.getPath()); // ditto!
2245      return vars.copy(optional, repeating);
2246    }
2247  }
2248
2249
2250  private void analyseTarget(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMap map, StructureMapGroupRuleTargetComponent tgt, String tv, TargetWriter tw, List<StructureDefinition> profiles, String sliceName) throws Exception {
2251    VariableForProfiling var = null;
2252    if (tgt.hasContext()) {
2253      var = vars.get(VariableMode.OUTPUT, tgt.getContext());
2254      if (var == null)
2255        throw new Exception("Rule \""+ruleId+"\": target context not known: "+tgt.getContext());
2256      if (!tgt.hasElement())
2257        throw new Exception("Rule \""+ruleId+"\": Not supported yet");
2258    }
2259
2260    
2261    TypeDetails type = null;
2262    if (tgt.hasTransform()) {
2263      type = analyseTransform(context, map, tgt, var, vars);
2264        // profiling: dest.setProperty(tgt.getElement().hashCode(), tgt.getElement(), v);
2265    } else {
2266      Property vp = var.property.baseProperty.getChild(tgt.getElement(),  tgt.getElement());
2267      if (vp == null)
2268        throw new Exception("Unknown Property "+tgt.getElement()+" on "+var.property.path);
2269      
2270      type = new TypeDetails(CollectionStatus.SINGLETON, vp.getType(tgt.getElement()));
2271    }
2272
2273    if (tgt.getTransform() == StructureMapTransform.CREATE) {
2274      String s = getParamString(vars, tgt.getParameter().get(0));
2275      if (worker.getResourceNames().contains(s))
2276        tw.newResource(tgt.getVariable(), s);
2277    } else { 
2278      boolean mapsSrc = false;
2279      for (StructureMapGroupRuleTargetParameterComponent p : tgt.getParameter()) {
2280        Type pr = p.getValue();
2281        if (pr instanceof IdType && ((IdType) pr).asStringValue().equals(tv)) 
2282          mapsSrc = true;
2283      }
2284      if (mapsSrc) { 
2285        if (var == null)
2286          throw new Error("Rule \""+ruleId+"\": Attempt to assign with no context");
2287        tw.valueAssignment(tgt.getContext(), var.property.getPath()+"."+tgt.getElement()+getTransformSuffix(tgt.getTransform()));
2288      } else if (tgt.hasContext()) {
2289        if (isSignificantElement(var.property, tgt.getElement())) {
2290          String td = describeTransform(tgt);
2291          if (td != null)
2292            tw.keyAssignment(tgt.getContext(), var.property.getPath()+"."+tgt.getElement()+" = "+td);
2293        }
2294      }
2295    }
2296    Type fixed = generateFixedValue(tgt);
2297    
2298    PropertyWithType prop = updateProfile(var, tgt.getElement(), type, map, profiles, sliceName, fixed, tgt);
2299    if (tgt.hasVariable())
2300      if (tgt.hasElement())
2301        vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop); 
2302      else
2303        vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop); 
2304  }
2305  
2306  private Type generateFixedValue(StructureMapGroupRuleTargetComponent tgt) {
2307    if (!allParametersFixed(tgt))
2308      return null;
2309    if (!tgt.hasTransform())
2310      return null;
2311    switch (tgt.getTransform()) {
2312    case COPY: return tgt.getParameter().get(0).getValue(); 
2313    case TRUNCATE: return null; 
2314    //case ESCAPE: 
2315    //case CAST: 
2316    //case APPEND: 
2317    case TRANSLATE: return null; 
2318  //case DATEOP, 
2319  //case UUID, 
2320  //case POINTER, 
2321  //case EVALUATE, 
2322    case CC: 
2323      CodeableConcept cc = new CodeableConcept();
2324      cc.addCoding(buildCoding(tgt.getParameter().get(0).getValue(), tgt.getParameter().get(1).getValue()));
2325      return cc;
2326    case C: 
2327      return buildCoding(tgt.getParameter().get(0).getValue(), tgt.getParameter().get(1).getValue());
2328    case QTY: return null; 
2329  //case ID, 
2330  //case CP, 
2331    default:
2332      return null;
2333    }
2334  }
2335
2336  @SuppressWarnings("rawtypes")
2337  private Coding buildCoding(Type value1, Type value2) {
2338    return new Coding().setSystem(((PrimitiveType) value1).asStringValue()).setCode(((PrimitiveType) value2).asStringValue()) ;
2339  }
2340
2341  private boolean allParametersFixed(StructureMapGroupRuleTargetComponent tgt) {
2342    for (StructureMapGroupRuleTargetParameterComponent p : tgt.getParameter()) {
2343      Type pr = p.getValue();
2344      if (pr instanceof IdType)
2345        return false;
2346    }
2347    return true;
2348  }
2349
2350  private String describeTransform(StructureMapGroupRuleTargetComponent tgt) throws FHIRException {
2351    switch (tgt.getTransform()) {
2352    case COPY: return null; 
2353    case TRUNCATE: return null; 
2354    //case ESCAPE: 
2355    //case CAST: 
2356    //case APPEND: 
2357    case TRANSLATE: return null; 
2358  //case DATEOP, 
2359  //case UUID, 
2360  //case POINTER, 
2361  //case EVALUATE, 
2362    case CC: return describeTransformCCorC(tgt); 
2363    case C: return describeTransformCCorC(tgt); 
2364    case QTY: return null; 
2365  //case ID, 
2366  //case CP, 
2367    default:
2368      return null;
2369    }
2370  }
2371
2372  @SuppressWarnings("rawtypes")
2373  private String describeTransformCCorC(StructureMapGroupRuleTargetComponent tgt) throws FHIRException {
2374    if (tgt.getParameter().size() < 2)
2375      return null;
2376    Type p1 = tgt.getParameter().get(0).getValue();
2377    Type p2 = tgt.getParameter().get(1).getValue();
2378    if (p1 instanceof IdType || p2 instanceof IdType)
2379      return null;
2380    if (!(p1 instanceof PrimitiveType) || !(p2 instanceof PrimitiveType))
2381      return null;
2382    String uri = ((PrimitiveType) p1).asStringValue();
2383    String code = ((PrimitiveType) p2).asStringValue();
2384    if (Utilities.noString(uri))
2385      throw new FHIRException("Describe Transform, but the uri is blank");
2386    if (Utilities.noString(code))
2387      throw new FHIRException("Describe Transform, but the code is blank");
2388    Coding c = buildCoding(uri, code);
2389    return NarrativeGenerator.describeSystem(c.getSystem())+"#"+c.getCode()+(c.hasDisplay() ? "("+c.getDisplay()+")" : "");
2390  }
2391
2392
2393  private boolean isSignificantElement(PropertyWithType property, String element) {
2394    if ("Observation".equals(property.getPath()))
2395      return "code".equals(element);
2396    else if ("Bundle".equals(property.getPath()))
2397      return "type".equals(element);
2398    else
2399      return false;
2400  }
2401
2402  private String getTransformSuffix(StructureMapTransform transform) {
2403    switch (transform) {
2404    case COPY: return ""; 
2405    case TRUNCATE: return " (truncated)"; 
2406    //case ESCAPE: 
2407    //case CAST: 
2408    //case APPEND: 
2409    case TRANSLATE: return " (translated)"; 
2410  //case DATEOP, 
2411  //case UUID, 
2412  //case POINTER, 
2413  //case EVALUATE, 
2414    case CC: return " (--> CodeableConcept)"; 
2415    case C: return " (--> Coding)"; 
2416    case QTY: return " (--> Quantity)"; 
2417  //case ID, 
2418  //case CP, 
2419    default:
2420      return " {??)";
2421    }
2422  }
2423
2424  private PropertyWithType updateProfile(VariableForProfiling var, String element, TypeDetails type, StructureMap map, List<StructureDefinition> profiles, String sliceName, Type fixed, StructureMapGroupRuleTargetComponent tgt) throws FHIRException {
2425    if (var == null) {
2426      assert (Utilities.noString(element));
2427      // 1. start the new structure definition
2428      StructureDefinition sdn = worker.fetchResource(StructureDefinition.class, type.getType());
2429      if (sdn == null)
2430        throw new FHIRException("Unable to find definition for "+type.getType());
2431      ElementDefinition edn = sdn.getSnapshot().getElementFirstRep();
2432      PropertyWithType pn = createProfile(map, profiles, new PropertyWithType(sdn.getId(), new Property(worker, edn, sdn), null, type), sliceName, tgt);
2433
2434//      // 2. hook it into the base bundle
2435//      if (type.getType().startsWith("http://hl7.org/fhir/StructureDefinition/") && worker.getResourceNames().contains(type.getType().substring(40))) {
2436//        StructureDefinition sd = var.getProperty().profileProperty.getStructure();
2437//        ElementDefinition ed = sd.getDifferential().addElement();
2438//        ed.setPath("Bundle.entry");
2439//        ed.setName(sliceName);
2440//        ed.setMax("1"); // well, it is for now...
2441//        ed = sd.getDifferential().addElement();
2442//        ed.setPath("Bundle.entry.fullUrl");
2443//        ed.setMin(1);
2444//        ed = sd.getDifferential().addElement();
2445//        ed.setPath("Bundle.entry.resource");
2446//        ed.setMin(1);
2447//        ed.addType().setCode(pn.getProfileProperty().getStructure().getType()).setProfile(pn.getProfileProperty().getStructure().getUrl());
2448//      }
2449      return pn; 
2450    } else {
2451      assert (!Utilities.noString(element));
2452      Property pvb = var.getProperty().getBaseProperty();
2453      Property pvd = var.getProperty().getProfileProperty();
2454      Property pc = pvb.getChild(element, var.property.types);
2455      if (pc == null)
2456        throw new DefinitionException("Unable to find a definition for "+pvb.getDefinition().getPath()+"."+element);
2457      
2458      // the profile structure definition (derived)
2459      StructureDefinition sd = var.getProperty().profileProperty.getStructure();
2460      ElementDefinition ednew = sd.getDifferential().addElement();
2461      ednew.setPath(var.getProperty().profileProperty.getDefinition().getPath()+"."+pc.getName());
2462      ednew.setUserData("slice-name", sliceName);
2463      ednew.setFixed(fixed);
2464      for (ProfiledType pt : type.getProfiledTypes()) {
2465        if (pt.hasBindings())
2466          ednew.setBinding(pt.getBindings().get(0));
2467        if (pt.getUri().startsWith("http://hl7.org/fhir/StructureDefinition/")) {
2468          String t = pt.getUri().substring(40);
2469          t = checkType(t, pc, pt.getProfiles());
2470          if (t != null) {
2471            if (pt.hasProfiles()) {
2472              for (String p : pt.getProfiles())
2473                if (t.equals("Reference"))
2474                  ednew.addType().setCode(t).setTargetProfile(p);
2475                else
2476                  ednew.addType().setCode(t).setProfile(p);
2477            } else 
2478            ednew.addType().setCode(t);
2479      }
2480        }
2481      }
2482      
2483      return new PropertyWithType(var.property.path+"."+element, pc, new Property(worker, ednew, sd), type);
2484    }
2485  }
2486  
2487
2488
2489  private String checkType(String t, Property pvb, List<String> profiles) throws FHIRException {
2490    if (pvb.getDefinition().getType().size() == 1 && isCompatibleType(t, pvb.getDefinition().getType().get(0).getCode()) && profilesMatch(profiles, pvb.getDefinition().getType().get(0).getProfile())) 
2491      return null;
2492    for (TypeRefComponent tr : pvb.getDefinition().getType()) {
2493      if (isCompatibleType(t, tr.getCode()))
2494        return tr.getCode(); // note what is returned - the base type, not the inferred mapping type
2495    }
2496    throw new FHIRException("The type "+t+" is not compatible with the allowed types for "+pvb.getDefinition().getPath()+" ("+pvb.getDefinition().typeSummary()+")");
2497  }
2498
2499  private boolean profilesMatch(List<String> profiles, String profile) {
2500    return profiles == null || profiles.size() == 0 || (profiles.size() == 1 && profiles.get(0).equals(profile));
2501  }
2502
2503  private boolean isCompatibleType(String t, String code) {
2504    if (t.equals(code))
2505      return true;
2506    if (t.equals("string")) {
2507      StructureDefinition sd = worker.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+code);
2508      if (sd != null && sd.getBaseDefinition().equals("http://hl7.org/fhir/StructureDefinition/string"))
2509        return true;
2510    }
2511    return false;
2512  }
2513
2514  private TypeDetails analyseTransform(TransformContext context, StructureMap map, StructureMapGroupRuleTargetComponent tgt, VariableForProfiling var, VariablesForProfiling vars) throws FHIRException {
2515    switch (tgt.getTransform()) {
2516    case CREATE :
2517      String p = getParamString(vars, tgt.getParameter().get(0));
2518      return new TypeDetails(CollectionStatus.SINGLETON, p);
2519    case COPY : 
2520      return getParam(vars, tgt.getParameter().get(0));
2521    case EVALUATE :
2522      ExpressionNode expr = (ExpressionNode) tgt.getUserData(MAP_EXPRESSION);
2523      if (expr == null) {
2524        expr = fpe.parse(getParamString(vars, tgt.getParameter().get(tgt.getParameter().size()-1)));
2525        tgt.setUserData(MAP_WHERE_EXPRESSION, expr);
2526      }
2527      return fpe.check(vars, null, expr);
2528
2529////case TRUNCATE : 
2530////  String src = getParamString(vars, tgt.getParameter().get(0));
2531////  String len = getParamString(vars, tgt.getParameter().get(1));
2532////  if (Utilities.isInteger(len)) {
2533////    int l = Integer.parseInt(len);
2534////    if (src.length() > l)
2535////      src = src.substring(0, l);
2536////  }
2537////  return new StringType(src);
2538////case ESCAPE : 
2539////  throw new Error("Transform "+tgt.getTransform().toCode()+" not supported yet");
2540////case CAST :
2541////  throw new Error("Transform "+tgt.getTransform().toCode()+" not supported yet");
2542////case APPEND : 
2543////  throw new Error("Transform "+tgt.getTransform().toCode()+" not supported yet");
2544    case TRANSLATE : 
2545      return new TypeDetails(CollectionStatus.SINGLETON, "CodeableConcept");
2546   case CC:
2547     ProfiledType res = new ProfiledType("CodeableConcept");
2548     if (tgt.getParameter().size() >= 2  && isParamId(vars, tgt.getParameter().get(1))) {
2549       TypeDetails td = vars.get(null, getParamId(vars, tgt.getParameter().get(1))).property.types;
2550       if (td != null && td.hasBinding())
2551         // todo: do we need to check that there's no implicit translation her? I don't think we do...
2552         res.addBinding(td.getBinding());
2553     }
2554     return new TypeDetails(CollectionStatus.SINGLETON, res);
2555   case C:
2556     return new TypeDetails(CollectionStatus.SINGLETON, "Coding");
2557   case QTY:
2558     return new TypeDetails(CollectionStatus.SINGLETON, "Quantity");
2559   case REFERENCE :
2560      VariableForProfiling vrs = vars.get(VariableMode.OUTPUT, getParamId(vars, tgt.getParameterFirstRep()));
2561      if (vrs == null)
2562        throw new FHIRException("Unable to resolve variable \""+getParamId(vars, tgt.getParameterFirstRep())+"\"");
2563      String profile = vrs.property.getProfileProperty().getStructure().getUrl();
2564     TypeDetails td = new TypeDetails(CollectionStatus.SINGLETON);
2565     td.addType("Reference", profile);
2566     return td;  
2567////case DATEOP :
2568////  throw new Error("Transform "+tgt.getTransform().toCode()+" not supported yet");
2569////case UUID :
2570////  return new IdType(UUID.randomUUID().toString());
2571////case POINTER :
2572////  Base b = getParam(vars, tgt.getParameter().get(0));
2573////  if (b instanceof Resource)
2574////    return new UriType("urn:uuid:"+((Resource) b).getId());
2575////  else
2576////    throw new FHIRException("Transform engine cannot point at an element of type "+b.fhirType());
2577    default:
2578      throw new Error("Transform Unknown or not handled yet: "+tgt.getTransform().toCode());
2579    }
2580  }
2581  private String getParamString(VariablesForProfiling vars, StructureMapGroupRuleTargetParameterComponent parameter) {
2582    Type p = parameter.getValue();
2583    if (p == null || p instanceof IdType)
2584      return null;
2585    if (!p.hasPrimitiveValue())
2586      return null;
2587    return p.primitiveValue();
2588  }
2589
2590  private String getParamId(VariablesForProfiling vars, StructureMapGroupRuleTargetParameterComponent parameter) {
2591    Type p = parameter.getValue();
2592    if (p == null || !(p instanceof IdType))
2593      return null;
2594    return p.primitiveValue();
2595  }
2596
2597  private boolean isParamId(VariablesForProfiling vars, StructureMapGroupRuleTargetParameterComponent parameter) {
2598    Type p = parameter.getValue();
2599    if (p == null || !(p instanceof IdType))
2600      return false;
2601    return vars.get(null, p.primitiveValue()) != null;
2602  }
2603
2604  private TypeDetails getParam(VariablesForProfiling vars, StructureMapGroupRuleTargetParameterComponent parameter) throws DefinitionException {
2605    Type p = parameter.getValue();
2606    if (!(p instanceof IdType))
2607      return new TypeDetails(CollectionStatus.SINGLETON, "http://hl7.org/fhir/StructureDefinition/"+p.fhirType());
2608    else { 
2609      String n = ((IdType) p).asStringValue();
2610      VariableForProfiling b = vars.get(VariableMode.INPUT, n);
2611      if (b == null)
2612        b = vars.get(VariableMode.OUTPUT, n);
2613      if (b == null)
2614        throw new DefinitionException("Variable "+n+" not found ("+vars.summary()+")");
2615      return b.getProperty().getTypes();
2616    }
2617  }
2618
2619  private PropertyWithType createProfile(StructureMap map, List<StructureDefinition> profiles, PropertyWithType prop, String sliceName, Base ctxt) throws DefinitionException {
2620    if (prop.getBaseProperty().getDefinition().getPath().contains(".")) 
2621      throw new DefinitionException("Unable to process entry point");
2622
2623    String type = prop.getBaseProperty().getDefinition().getPath();
2624    String suffix = "";
2625    if (ids.containsKey(type)) {
2626      int id = ids.get(type);
2627      id++;
2628      ids.put(type, id);
2629      suffix = "-"+Integer.toString(id);
2630    } else
2631      ids.put(type, 0);
2632    
2633    StructureDefinition profile = new StructureDefinition();
2634    profiles.add(profile);
2635    profile.setDerivation(TypeDerivationRule.CONSTRAINT);
2636    profile.setType(type);
2637    profile.setBaseDefinition(prop.getBaseProperty().getStructure().getUrl());
2638    profile.setName("Profile for "+profile.getType()+" for "+sliceName);
2639    profile.setUrl(map.getUrl().replace("StructureMap", "StructureDefinition")+"-"+profile.getType()+suffix);
2640    ctxt.setUserData("profile", profile.getUrl()); // then we can easily assign this profile url for validation later when we actually transform
2641    profile.setId(map.getId()+"-"+profile.getType()+suffix);
2642    profile.setStatus(map.getStatus());
2643    profile.setExperimental(map.getExperimental());
2644    profile.setDescription("Generated automatically from the mapping by the Java Reference Implementation");
2645    for (ContactDetail c : map.getContact()) {
2646      ContactDetail p = profile.addContact();
2647      p.setName(c.getName());
2648      for (ContactPoint cc : c.getTelecom()) 
2649        p.addTelecom(cc);
2650    }
2651    profile.setDate(map.getDate());
2652    profile.setCopyright(map.getCopyright());
2653    profile.setFhirVersion(Constants.VERSION);
2654    profile.setKind(prop.getBaseProperty().getStructure().getKind());
2655    profile.setAbstract(false);
2656    ElementDefinition ed = profile.getDifferential().addElement();
2657    ed.setPath(profile.getType());
2658    prop.profileProperty = new Property(worker, ed, profile);
2659    return prop;
2660  }
2661
2662  private PropertyWithType resolveType(StructureMap map, String type, StructureMapInputMode mode) throws Exception {
2663    for (StructureMapStructureComponent imp : map.getStructure()) {
2664      if ((imp.getMode() == StructureMapModelMode.SOURCE && mode == StructureMapInputMode.SOURCE) || 
2665          (imp.getMode() == StructureMapModelMode.TARGET && mode == StructureMapInputMode.TARGET)) {
2666        StructureDefinition sd = worker.fetchResource(StructureDefinition.class, imp.getUrl());
2667        if (sd == null)
2668          throw new Exception("Import "+imp.getUrl()+" cannot be resolved");
2669        if (sd.getId().equals(type)) {
2670          return new PropertyWithType(sd.getType(), new Property(worker, sd.getSnapshot().getElement().get(0), sd), null, new TypeDetails(CollectionStatus.SINGLETON, sd.getUrl()));
2671        }
2672      }
2673    }
2674    throw new Exception("Unable to find structure definition for "+type+" in imports");
2675  }
2676
2677
2678  public StructureMap generateMapFromMappings(StructureDefinition sd) throws IOException, FHIRException {
2679    String id = getLogicalMappingId(sd);
2680    if (id == null) 
2681        return null;
2682    String prefix = ToolingExtensions.readStringExtension(sd,  ToolingExtensions.EXT_MAPPING_PREFIX);
2683    String suffix = ToolingExtensions.readStringExtension(sd,  ToolingExtensions.EXT_MAPPING_SUFFIX);
2684    if (prefix == null || suffix == null)
2685      return null;
2686    // we build this by text. Any element that has a mapping, we put it's mappings inside it....
2687    StringBuilder b = new StringBuilder();
2688    b.append(prefix);
2689
2690    ElementDefinition root = sd.getSnapshot().getElementFirstRep();
2691    String m = getMapping(root, id);
2692    if (m != null)
2693      b.append(m+"\r\n");
2694    addChildMappings(b, id, "", sd, root, false);
2695    b.append("\r\n");
2696    b.append(suffix);
2697    b.append("\r\n");
2698    StructureMap map = parse(b.toString());
2699    map.setId(tail(map.getUrl()));
2700    if (!map.hasStatus())
2701      map.setStatus(PublicationStatus.DRAFT);
2702    map.getText().setStatus(NarrativeStatus.GENERATED);
2703    map.getText().setDiv(new XhtmlNode(NodeType.Element, "div"));
2704    map.getText().getDiv().addTag("pre").addText(render(map));
2705    return map;
2706  }
2707
2708
2709  private String tail(String url) {
2710    return url.substring(url.lastIndexOf("/")+1);
2711  }
2712
2713
2714  private void addChildMappings(StringBuilder b, String id, String indent, StructureDefinition sd, ElementDefinition ed, boolean inner) throws DefinitionException {
2715    boolean first = true;
2716    List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, ed);
2717    for (ElementDefinition child : children) {
2718      if (first && inner) {
2719        b.append(" then {\r\n");
2720        first = false;
2721      }
2722      String map = getMapping(child, id);
2723      if (map != null) {
2724        b.append(indent+"  "+child.getPath()+": "+map);
2725        addChildMappings(b, id, indent+"  ", sd, child, true);
2726        b.append("\r\n");
2727      }
2728    }
2729    if (!first && inner)
2730      b.append(indent+"}");
2731    
2732  }
2733
2734
2735  private String getMapping(ElementDefinition ed, String id) {
2736    for (ElementDefinitionMappingComponent map : ed.getMapping())
2737      if (id.equals(map.getIdentity()))
2738        return map.getMap();
2739    return null;
2740  }
2741
2742
2743  private String getLogicalMappingId(StructureDefinition sd) {
2744    String id = null;
2745    for (StructureDefinitionMappingComponent map : sd.getMapping()) {
2746      if ("http://hl7.org/fhir/logical".equals(map.getUri()))
2747        return map.getIdentity();
2748    }
2749    return null;
2750  }
2751        
2752}