public void FromStream(Stream s) { ExtractDelimiterInfo(s); head = tail = 0; buffer = new byte[BufferSize]; EndOfFile = false; int docLine = 1; var segText = ReadNextLine(s); LoopsList currentLoopList = null; LoopsList previousLoopList = null; while (segText != null) { List <string> segSplit = segText.Split(delimiters.Element).ToList(); LoopSegment foundDef = FindSingleDefMatch(currentLoopList, segSplit, segText, ref docLine); if (foundDef != null) { if (currentLoopList != null && foundDef.OwningLoop != null && currentLoopList.LoopName != foundDef.OwningLoop.LoopName) { previousLoopList = currentLoopList; } if ((currentLoopList == null && foundDef.OwningLoop != null) || (currentLoopList != null && foundDef.OwningLoop != null && currentLoopList.LoopName != foundDef.OwningLoop.LoopName)) { currentLoopList = foundDef.OwningLoop; currentLoopList.Add(new baseLoop(currentLoopList)); } //if (thisLoopMatches == null || !thisLoopMatches.Any() || thisLoopMatches.Count() > 1) // thisLoopMatches = possibleSegmentMatches; baseStdSegment newSegInst = foundDef.QualifiedSegments[0].CreateInstance (segText, delimiters, foundDef.OwningLoop); if (!newSegInst.Validate()) { Errors.Add(new X12Error { DocumentLine = docLine, ErrorLevel = X12ErrorLevel.Segment, ErrorType = X12ErrorTypes.ValidationFailed, SegmentText = segText, CurrentLoopName = currentLoopList?.LoopName, }); } } segText = ReadNextLine(s); docLine++; } }
List <ICuttableEdge> MakeLoop(TopologyIntersection start_from, BopMap map) { var res = new List <ICuttableEdge>(); TopologyIntersection last = start_from; int deadloop = 0; do { if (deadloop++ > DeadloopMaxIters) { Debug.LogError("deadloop"); throw new UnityException("deadloop"); } LoopSegment seg = MakeLoopSegment(last, map); last = seg.end; res.AddRange(seg.edges); } while (last != start_from); // TODO: filter degen loops float total_len = 0; foreach (var edge in res) { total_len += edge.GetLen(); } // strict filter. such loops are useless for camera return(res.Count == 0 || total_len < 1.0e-2 ? null : res); }
private LoopSegment FindSingleDefMatch(LoopsList currentLoopList, List <string> segSplit, string segText, ref int docLine) { LoopSegment retVal = null; List <LoopSegment> possibleSegmentMatches = new List <LoopSegment>(); if (currentLoopList == null) //this should be envelope segments { var segMatch = new LoopSegment { OwningLoop = null, QualifiedSegments = EnvelopseDefinitions.Where(c => c.IsQualified(segSplit)).ToList() }; if (segMatch.QualifiedSegments.Any()) { retVal = segMatch; return(retVal); } } possibleSegmentMatches.AddRange(FindQualifiedSegments(segSplit)); if (possibleSegmentMatches == null || !possibleSegmentMatches.Any()) { Errors.Add(new X12Error { DocumentLine = docLine, ErrorLevel = X12ErrorLevel.Loop, ErrorType = X12ErrorTypes.NotDefined, SegmentText = segText, CurrentLoopName = currentLoopList?.LoopName, }); } List <LoopSegment> thisLoopMatches = null; if (currentLoopList != null && possibleSegmentMatches.Count() != 1) { //first find segments that are in the same loop thisLoopMatches = possibleSegmentMatches.Where(c => c.OwningLoop.LoopName == currentLoopList.LoopName).ToList(); if (thisLoopMatches.Count != 1) { thisLoopMatches = possibleSegmentMatches.Where(c => c.OwningLoop.StartingSegment.SegmentName == segSplit[0]) .ToList(); //see if the parent of one of our matches is the same as the loop we are in now if (thisLoopMatches.Count != 1) { thisLoopMatches = thisLoopMatches.Where( c => c.OwningLoop.Parent.LoopName == currentLoopList.LoopName).ToList(); } //maybe this is the start of another sub loop within the same parent loop if (thisLoopMatches.Count != 1 && currentLoopList.Parent != null) { thisLoopMatches = possibleSegmentMatches.Where(c => c.OwningLoop.StartingSegment.SegmentName == segSplit[0]) .ToList(); thisLoopMatches = thisLoopMatches.Where(c => currentLoopList.Parent[0].Parent.LoopName == c.OwningLoop.Parent.LoopName).ToList(); } //it could be that this loop exists in 2 subloops that defined the same (IE 837.2000B.2300-ClmInfo and 837.2000C.2300-ClmInfo) if (thisLoopMatches.Count != 1 && currentLoopList.Parent != null) { thisLoopMatches = possibleSegmentMatches.Where( c => c.OwningLoop.Parent.LoopName == currentLoopList.Parent.LoopName).ToList(); } } } //if the current loop is null but we have possibleSegment out of the Find function //this should a start segment for our first loop else if (currentLoopList == null && possibleSegmentMatches.Count == 1 && possibleSegmentMatches[0].OwningLoop.StartingSegment.SegmentName == segSplit[0]) { thisLoopMatches = possibleSegmentMatches; } else if (currentLoopList != null && possibleSegmentMatches.Count == 1) { thisLoopMatches = possibleSegmentMatches; } if (thisLoopMatches != null && thisLoopMatches.Count == 1) { retVal = thisLoopMatches[0]; } if (possibleSegmentMatches.Count > 1 && (thisLoopMatches == null || thisLoopMatches.Count != 1)) { Errors.Add(new X12Error { DocumentLine = docLine, ErrorLevel = X12ErrorLevel.Loop, ErrorType = X12ErrorTypes.MultipleDefintions, SegmentText = segText, CurrentLoopName = currentLoopList?.LoopName, }); } return(retVal); }