What is the markup language specification for umlet? - uml

I have been looking all over the web site, Internet, and of course SO and can't seem to find a description or specification for the markup language being used in umlet.
In the example sequence diagram for example:
title: sample
_alpha:A~id1_|_beta:B~id2_|_gamma:G~id3_
id1->>id2:id1,id2
id2-/>id1:async Msg.
id3->>>id1:id1,id3
id1.>id3:id1,id3:async return Msg
id1->id1:id1:self
iframe{:interaction frame
id2->id3:id1,id3:async Msg.
iframe}
The ->, -->, etc are fairly obvious, but what the colons do?
Why are underlines needed, etc. Inquiring minds would like to know as this looks like a useful tool for sketching.

Found an annotated script (that doesn't seem valid as of 14.2):
//UML2 style titles are optional.
title:sequence diagram
//the participating objects are separated using the pipe symbol "|". Their names may be underlined using underscores.
_alpha:A_|_beta:B_|_gamma:G_
//The following line describes a (synchonuous) message originating from the first object sent to the second object, while both objects are active.
1->>2:1,2
//This message is asynchronous being sent from the second object to the first object. The messages is named.
2-/>1:async Msg.
//A message from the third object to the first object; both objects are active.
3->>>1:1,3
//A named asynchronous return message in UML2 style (using a stick arrow head).
1.>3:1,3:async return Msg
//This is a self call of the first object.
1->1:1:self
//Interaction frames may be used to show optional or conditional blocks within sequence diagrams.
iframe{:interaction frame
2->3:2,3:async Msg.
iframe}
RE: BNF, there's umlet-elements/src/main/javacc/SequenceAllInOneParser.jj in the source:
options {
static = false;
IGNORE_CASE = false;
JAVA_UNICODE_ESCAPE = true;
JDK_VERSION = "1.6";
// Not yet detected as a valid option by the Eclipse plugin, but works nonetheless. This is essential for GWT compatible generated code.
JAVA_TEMPLATE_TYPE = "modern";
}
PARSER_BEGIN(SequenceAllInOneParser)
package com.baselet.element.facet.specific.sequence_aio.gen;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import com.baselet.control.enums.LineType;
import com.baselet.element.facet.specific.sequence_aio.Lifeline;
import com.baselet.element.facet.specific.sequence_aio.Message.ArrowType;
import com.baselet.element.facet.specific.sequence_aio.SequenceDiagramBuilder;
import com.baselet.element.facet.specific.sequence_aio.SequenceDiagramException;
public class SequenceAllInOneParser {
private static final Logger log = LoggerFactory.getLogger(SequenceAllInOneParser.class);
private boolean autoTick;
/**
* Replaces "\\\\" with "\\" in the output so that any further specified match replace pair can't match the replaced "\\".
* #param matchReplacePairs
* #return the string with the replacements
*/
public static String backslashReplace(String input, String... matchReplacePairs) {
if (matchReplacePairs.length % 2 == 1) {
throw new IllegalArgumentException("matchReplacePairs must have an even number of elements.");
}
String split = "\\\\";
StringBuilder strBuilder = new StringBuilder(input.length());
int firstIndex = 0;
// use the indexOf function instead of a split, because split uses regex and regex is not 100% supported by GWT
int foundIndex = input.indexOf(split, firstIndex);
while (firstIndex < input.length()) {
int lastIndex = foundIndex == -1 ? input.length() : foundIndex;
String tmp = input.substring(firstIndex, lastIndex);
for (int j = 0; j < matchReplacePairs.length - 1; j += 2) {
tmp = tmp.replace(matchReplacePairs[j], matchReplacePairs[j + 1]);
}
strBuilder.append(tmp);
if (foundIndex != -1) {
strBuilder.append('\\');
}
firstIndex = lastIndex + split.length();
foundIndex = input.indexOf(split, firstIndex);
}
return strBuilder.toString();
}
/**
* Small data container to pass all informations.
*/
private class MessageArrowInfo {
boolean fromLeftToRight;
LineType lineType;
ArrowType arrowType;
}
private class InteractionConstraint {
private String lifelineId = null;
private String text = "";
}
private class LifelineInterval {
private String startId;
private String endId;
}
}
PARSER_END(SequenceAllInOneParser)
/* defines input to be ignored */
< * > SKIP:
{
" "
| "\t"
}
< DIAGRAM_DEF_TEXT > SKIP:
{
"\n" : DEFAULT
| "\r\n" : DEFAULT
| "\r" : DEFAULT
}
< DEFAULT > TOKEN:
{
< DIAGRAM_OPTION_OVERRIDE_ID :"overrideIds=" >
| < DIAGRAM_OPTION_AUTO_TICK: "autoTick=" >
| < TRUE: "true" >
| < FALSE: "false" >
| < DIAGRAM_TITLE: "title=" > : DIAGRAM_DEF_TEXT
| < DIAGRAM_DESC: "desc=" > : DIAGRAM_DEF_TEXT
| < LIFELINE_DEFINITIONS: "obj=" > : LIFELINE_DEF
}
< DIAGRAM_DEF_TEXT > TOKEN:
{
< DIAGRAM_DEFINITION_TEXT:
(
~ ["\\","\r","\n"]
| ("\\" ["\\", "n"] )
)+> : DIAGRAM_DEF_TEXT
}
< LIFELINE_DEF > TOKEN :
{
< LL_DEF_NEW_LINE: "\n" | "\r\n" | "\r" > : DEFAULT
| < LIFELINE_DEF_DELIMITER: "|" >
/*| < LIFELINE_TITLE_DELIMITER: "~" > integrated in the title */
| < LIFELINE_ACTOR: "ACTOR" >
| < LIFELINE_ACTIVE: "ACTIVE" >
| < LIFELINE_CREATED_LATER :"CREATED_LATER" >
| < LIFELINE_EXEC_SPEC_FROM_START: "EXECUTION" >
| < LIFELINE_TITLE: /* since | and ~ are special characters which delimit the title they need to be escaped */
(
~["~","\\","\n","\r","|"]
| ("\\" ["\\","~","|","n"])
)+
"~" >
}
< DEFAULT > TOKEN :
{
< TEXT_DELIMITER: ":" > : DIAGRAM_SEQ_TEXT
| < LIST_DELIMITER: "," >
| < OPEN_CURLY_BRACKET: "{" >
| < CLOSE_CURLY_BRACKET: "}" >
| < COMMAND_DELIMITER: ";" >
| < T_DASH: "-" >
| < T_DOT: "." >
| < T_DDOT: ".." >
| < T_EQ: "=" >
| < MESSAGE_ARROW_LEFT_OPEN: "<" >
| < MESSAGE_ARROW_LEFT_FILLED: "<<<" >
| < MESSAGE_ARROW_RIGHT_OPEN: ">" >
| < MESSAGE_ARROW_RIGHT_FILLED: ">>>" >
| < MESSAGE_DURATION_INC: "+" >
| < LOST: "lost" >
| < FOUND: "found" >
| < GATE: "gate" >
| < START_COREGION: "coregionStart=" >
| < END_COREGION: "coregionEnd=" >
| < INVARIANT: "invariant=" >
| < STATE_INVARIANT: "stateInvariant=" >
| < EXEC_SPEC_START: "on=" >
| < EXEC_SPEC_END: "off=" >
| < LL_DESTROY: "destroy=" >
| < REF: "ref=" >
| < CONTINUATION: "continuation=" >
| < TEXT_ON_LIFELINE: "text=" >
| < TICK: "tick=" >
| < COMBINED_FRAGMENT: "combinedFragment=" > : CF_OPERATOR
| < INTERACTION_CONSTRAINT: "constraint=" >
| < UNSIGNED_INT_CONSTANT: ( ["0" - "9"] ) + >
| < DEFAULT_NEW_LINE: "\n" | "\r\n" | "\r" >
}
< LIFELINE_DEF, DEFAULT > TOKEN :
{
< LIFELINE_ID: ["a"-"z","A"-"Z"] (["a"-"z","A"-"Z","0"-"9"])* >
}
< DIAGRAM_SEQ_TEXT > TOKEN :
{
< TEXT_UNTIL_NEXT_COMMAND : ( /* since ; is special characters which delimit the text it must be escaped */
~["\\","\n","\r",";"]
| ("\\" ["\\",";","n"])
)+ > : DEFAULT
}
< CF_OPERATOR > TOKEN :
{
< COMBINED_FRAGMENT_OPERATOR: /* since ; and ~ are special characters which delimit the operator they need to be escaped */
(
~["~","\\","\n","\r",";"]
| ("\\" ["\\","~","|","n",";"])
)+
"~" > : DEFAULT
}
/* general Tokens */
/* skip comments */
< * > SKIP :
{
<"//"(~["\n","\r"])*>
}
< * > TOKEN :
{
<LAST_LINE_COMMENT: "//"(~["\n","\r"])*>
}
/**
* The main function which parses the whole diagram.
* Line comments are skipped (see Tokens)
*/
SequenceDiagramBuilder start() :
{
String titleText = "";
String descText = "";
SequenceDiagramBuilder diagram = new SequenceDiagramBuilder();
autoTick = true;
}
{
(
(
titleText=DiagramTitle() /* NL skip and change state to DEFAULT */
| descText = DiagramDescription() /* NL skip and change state to DEFAULT */
| Option(diagram) < DEFAULT_NEW_LINE >
| LifelineDefinitions(diagram) /* NL part of the nonterminal */
)+//diagram definition as new NT followed by NL alternating with sequence so that the sequence= can be removed
Sequence(diagram)
)
(< LAST_LINE_COMMENT >)?
<EOF>
{
diagram.setTitle(titleText);
diagram.setText(descText);
return diagram;
}
}
String DiagramTitle() :
{
String text = "";
}
{
< DIAGRAM_TITLE >
(
< DIAGRAM_DEFINITION_TEXT > { text = backslashReplace(token.image, "\\n","\n");}
)?
{return text;}
}
String DiagramDescription() :
{
String desc = "";
}
{
< DIAGRAM_DESC >
< DIAGRAM_DEFINITION_TEXT > { desc = backslashReplace(token.image, "\\n","\n");}
{return desc;}
}
/**
* Options for the whole diagram
*/
void Option(SequenceDiagramBuilder diagram) :
{
boolean overrideIds;
}
{
< DIAGRAM_OPTION_OVERRIDE_ID > overrideIds = booleanConstant()
{
diagram.setOverrideDefaultIds(overrideIds);
}
| < DIAGRAM_OPTION_AUTO_TICK > autoTick = booleanConstant()
}
/**
* Defines all Lifelines
*/
void LifelineDefinitions(SequenceDiagramBuilder diagram) :
{}
{
< LIFELINE_DEFINITIONS > LifelineDef(diagram) (< LIFELINE_DEF_DELIMITER > LifelineDef(diagram))* < LL_DEF_NEW_LINE >
}
/**
* Defines one Lifeline, the id can't be LIFELINE_ACTOR, LIFELINE_ACTIVE
* or LIFELINE_CREATED_LATER because these are keywords.
*/
void LifelineDef(SequenceDiagramBuilder diagram) :
{
String name = "";
String id = null;
boolean createdOnStart = true;
Lifeline.LifelineHeadType headType = Lifeline.LifelineHeadType.STANDARD;
boolean execSpecFromStart = false;
}
{
name = LifelineDefTitleText() (id=LifelineId())?
(
< LIFELINE_ACTOR > { headType = Lifeline.LifelineHeadType.ACTOR; }
| < LIFELINE_ACTIVE > { headType = Lifeline.LifelineHeadType.ACTIVE_CLASS; }
| < LIFELINE_CREATED_LATER > { createdOnStart = false; }
| < LIFELINE_EXEC_SPEC_FROM_START > { execSpecFromStart = true; }
) *
{
if("lost".equals(id) || "found".equals(id)) {
throw new SequenceDiagramException("'lost' and 'found' are keywords and can not be used as lifeline identifiers.");
}
diagram.addLiveline(name, id, headType, createdOnStart, execSpecFromStart);
}
}
/** can could be multiple lines */
String LifelineDefTitleText() :
{}
{
< LIFELINE_TITLE >
{
/* remove trailing ~ and handle the escaping of \, |, n and ~ */
return backslashReplace(token.image.substring(0, token.image.length() - 1), "\\n", "\n", "\\~", "~", "\\|", "|");
}
}
/**
* Can't be one of the following, because these are keywords in the lifeline definition!
* < LIFELINE_ACTOR: "ACTOR" >
* < LIFELINE_ACTIVE: "ACTIVE" >
* < LIFELINE_CREATED_LATER :"CREATED_LATER" >
*/
String LifelineId() :
{}
{
< LIFELINE_ID >
{
return token.image;
}
}
void Sequence(SequenceDiagramBuilder diagram) :
{}
{
(
SequenceTick(diagram) < DEFAULT_NEW_LINE >
| (
SequenceElement(diagram)
( LOOKAHEAD(2) < COMMAND_DELIMITER > SequenceElement(diagram))*
(< COMMAND_DELIMITER >)?
< DEFAULT_NEW_LINE >
{ if(autoTick) { diagram.tick(); } }
)
| < DEFAULT_NEW_LINE >
)*
}
void SequenceTick(SequenceDiagramBuilder diagram) :
{
int tickCount = 1;
}
{
< TICK > (tickCount = unsignedIntConstant())?
{ diagram.tick(tickCount); }
}
void SequenceElement(SequenceDiagramBuilder diagram) :
{}
{
(
MessageOrGeneralOrderingOrText(diagram) /* Message, GeneralOrdering have a common prefix for more clarity a new nonterminal was created*/
| Coregion(diagram)
| DestroyLL(diagram)
| ExecutionSpecification(diagram)
| StateInvariant(diagram)
//| (diagram) //general ordering, TimeConstraint TimeObservation and DurationConstraint Duration Observation missing
| InteractionUse(diagram)
| Continuation(diagram)
| CombinedFragment(diagram)
)
}
void MessageOrGeneralOrderingOrText(SequenceDiagramBuilder diagram) :
{}
{
/*
* (Message and GeneralOrdering) A common prefix is: <LIFELINE_ID> "." <LIFELINE_ID> "<"
* All three have <LIFELINE_ID > as common prefix
* therefore use LOOKAHEAD(2) if it is a text, if not use a LOOKAHEAD(5) to determine if it is a message or a general ordering
*/
LOOKAHEAD(2) TextOnLifeline(diagram)
| LOOKAHEAD(5) Message(diagram)
| GeneralOrdering(diagram)
}
void CombinedFragment(SequenceDiagramBuilder diagram) :
{
LifelineInterval interval = new LifelineInterval();
String operator = "";
String id = null;
}
{
(
< COMBINED_FRAGMENT >
(
< COMBINED_FRAGMENT_OPERATOR >
{ operator = backslashReplace(token.image.substring(0, token.image.length() - 1), "\\n", "\n", "\\;", ";", "\\~", "~"); }
)
[
id = LifelineId()
[interval = LifelineInterval()]
]
{ diagram.beginCombinedFragment(interval.startId, interval.endId, id, operator); }
)
| (
< T_DASH > < T_DASH > (< T_EQ > id=LifelineId())?
{ diagram.endCombinedFragment(id); }
)
| (
< T_DDOT > (< T_EQ > id=LifelineId())?
{ diagram.endAndBeginOperand(id); }
)
}
void Message(SequenceDiagramBuilder diagram) :
{
String leftLifelineId = null;
String rightLifelineId = null;
String leftLifelineLocalId = null;
String rightLifelineLocalId = null;
MessageArrowInfo messageArrowInfo;
String msgText = "";
int lostCount = 0;
int foundCount = 0;
int gateCount = 0;
int duration = 0;
}
{
(
(
leftLifelineId = LifelineId() [LOOKAHEAD(2)< T_DOT > leftLifelineLocalId = LifelineId()]
| < LOST > { leftLifelineId = "lost"; lostCount++; }
| < FOUND > { leftLifelineId = "found"; foundCount++; }
| < GATE > { leftLifelineId = "gate"; gateCount++; }
)
messageArrowInfo = MessageArrow()
(
rightLifelineId = LifelineId() [< T_DOT > rightLifelineLocalId = LifelineId()]
| < LOST > { rightLifelineId = "lost"; lostCount++; }
| < FOUND > { rightLifelineId = "found"; foundCount++; }
| < GATE > { rightLifelineId = "gate"; gateCount++; }
)
(duration = MessageDuration())?
(msgText = TextUntilNewLine())?
)
{
if(lostCount + foundCount + gateCount > 1) {
throw new SequenceDiagramException("Error: 'lost', 'found' and 'gate' can only occur once per message.");
}
String send;
String receive;
String sendLocalId;
String receiveLocalId;
if(messageArrowInfo.fromLeftToRight) {
send = leftLifelineId;
receive = rightLifelineId;
sendLocalId = leftLifelineLocalId;
receiveLocalId = rightLifelineLocalId;
}
else {
send = rightLifelineId;
receive = leftLifelineId;
sendLocalId = rightLifelineLocalId;
receiveLocalId = leftLifelineLocalId;
}
if(gateCount > 0) {
if(duration != 0) {
throw new SequenceDiagramException("Error: a messages with a gate can only have a duration of 0, but the duration was " + duration + ".");
}
if(send.equals("gate")) {
diagram.addSendGateMessage(receive, msgText, messageArrowInfo.lineType, messageArrowInfo.arrowType, receiveLocalId);
}
else {
diagram.addReceiveGateMessage(send, msgText, messageArrowInfo.lineType, messageArrowInfo.arrowType, sendLocalId);
}
}
else if(send.equals("lost")) {
throw new SequenceDiagramException("Error: 'lost' can only be on the receiving end of a message.");
}
else if(send.equals("found")) {
if(duration != 0) {
throw new SequenceDiagramException("Error: 'lost' and 'found' messages can only have a duration of 0, but the duration was " + duration + ".");
}
diagram.addFoundMessage(receive, msgText, messageArrowInfo.lineType, messageArrowInfo.arrowType, receiveLocalId);
}
else
{
if(receive.equals("lost")) {
if(duration != 0) {
throw new SequenceDiagramException("Error: 'lost' and 'found' messages can only have a duration of 0, but the duration was " + duration + ".");
}
diagram.addLostMessage(send, msgText, messageArrowInfo.lineType, messageArrowInfo.arrowType, sendLocalId);
}
else if(receive.equals("found")) {
throw new SequenceDiagramException("Error: 'found' can only be on the sending end of a message.");
}
else {
diagram.addMessage(send, receive, duration, msgText, messageArrowInfo.lineType, messageArrowInfo.arrowType, sendLocalId, receiveLocalId);
}
}
}
}
MessageArrowInfo MessageArrow():
{
MessageArrowInfo messageArrowInfo = new MessageArrowInfo();
}
{
(
(
messageArrowInfo.lineType = MessageArrowLineType()
(
< MESSAGE_ARROW_RIGHT_OPEN > { messageArrowInfo.arrowType = ArrowType.OPEN; }
| < MESSAGE_ARROW_RIGHT_FILLED > { messageArrowInfo.arrowType = ArrowType.FILLED; }
)
)
{
messageArrowInfo.fromLeftToRight = true;
}
| (
(
< MESSAGE_ARROW_LEFT_OPEN > { messageArrowInfo.arrowType = ArrowType.OPEN; }
| < MESSAGE_ARROW_LEFT_FILLED > { messageArrowInfo.arrowType = ArrowType.FILLED; }
)
messageArrowInfo.lineType = MessageArrowLineType()
)
{
messageArrowInfo.fromLeftToRight = false;
}
)
{ return messageArrowInfo; }
}
LineType MessageArrowLineType() :
{
LineType lineType;
}
{
(
< T_DASH > { lineType = LineType.SOLID; }
| < T_DOT > { lineType = LineType.DASHED; }
)
{
return lineType;
}
}
int MessageDuration() :
{
int duration;
}
{
(
(
< MESSAGE_DURATION_INC > { duration = 1; }
(
duration = unsignedIntConstant()
| (< MESSAGE_DURATION_INC > { duration++; })*
)
)
| (
< T_DASH > { duration = -1; }
(
duration = unsignedIntConstant() { duration = -duration; }
| (< T_DASH > { duration--; })*
)
)
)
{
return duration;
}
}
void GeneralOrdering(SequenceDiagramBuilder diagram):
{
String leftLifelineId = null;
String rightLifelineId = null;
String leftLifelineLocalId = null;
String rightLifelineLocalId = null;
boolean leftEarlier;
}
{
leftLifelineId = LifelineId()
< T_DOT >
leftLifelineLocalId = LifelineId()
(
< MESSAGE_ARROW_RIGHT_OPEN > { leftEarlier = true; }
| < MESSAGE_ARROW_LEFT_OPEN > { leftEarlier = false; }
)
rightLifelineId = LifelineId()
< T_DOT >
rightLifelineLocalId = LifelineId()
{
if(leftEarlier) {
diagram.addGeneralOrdering(leftLifelineId, leftLifelineLocalId, rightLifelineId, rightLifelineLocalId);
}
else {
diagram.addGeneralOrdering(rightLifelineId, rightLifelineLocalId, leftLifelineId, leftLifelineLocalId);
}
}
}
void Coregion(SequenceDiagramBuilder diagram) :
{
String lifelineId;
boolean start;
}
{
(
< START_COREGION > { start = true; }
| < END_COREGION > { start = false; }
)
lifelineId = LifelineId()
{
diagram.addCoregion(lifelineId, start);
}
}
void DestroyLL(SequenceDiagramBuilder diagram) :
{
String lifelineId;
}
{
< LL_DESTROY >
lifelineId = LifelineId()
{
diagram.destroyLifeline(lifelineId);
}
}
void ExecutionSpecification(SequenceDiagramBuilder diagram) :
{
String lifelineId;
boolean on;
}
{
(
< EXEC_SPEC_START > { on = true; }
| < EXEC_SPEC_END > { on = false; }
)
lifelineId = LifelineId()
{
diagram.changeExecutionSpecification(lifelineId, on);
}
( (< LIST_DELIMITER >)? lifelineId = LifelineId()
{
diagram.changeExecutionSpecification(lifelineId, on);
}
)*
}
void StateInvariant(SequenceDiagramBuilder diagram) :
{
String lifelineId;
String text = "";
boolean stateStyle;
}
{
(
< INVARIANT > { stateStyle = false; }
| < STATE_INVARIANT > { stateStyle = true; }
)
lifelineId = LifelineId()
(text = TextUntilNewLine())?
{
diagram.addStateInvariant(lifelineId, text, stateStyle);
}
}
void TextOnLifeline(SequenceDiagramBuilder diagram) :
{
String lifelineId;
String text = "";
}
{
[< TEXT_ON_LIFELINE >]
lifelineId = LifelineId()
text = TextUntilNewLine() /* text is mandatory, if not lookahead with message and general ordering would not work*/
{
diagram.addTextOnLifeline(lifelineId, text);
}
}
String TextUntilNewLine() :
{
}
{
< TEXT_DELIMITER >
< TEXT_UNTIL_NEXT_COMMAND >
{
return backslashReplace(token.image, "\\n", "\n", "\\;", ";");
}
}
void InteractionUse(SequenceDiagramBuilder diagram):
{
LifelineInterval interval;
String text = "";
}
{
(
< REF >
interval = LifelineInterval()
(text = TextUntilNewLine())?
)
{
diagram.addInteractionUse(interval.startId, interval.endId, text);
}
}
void Continuation(SequenceDiagramBuilder diagram):
{
LifelineInterval interval;
String text = "";
}
{
(
< CONTINUATION >
interval = LifelineInterval()
(text = TextUntilNewLine())?
)
{
diagram.addContinuation(interval.startId, interval.endId, text);
}
}
/**
* #return the start and end of the interval.
*/
LifelineInterval LifelineInterval():
{
LifelineInterval interval = new LifelineInterval();
}
{
interval.startId = LifelineId()
(< LIST_DELIMITER >)?
interval.endId = LifelineId()
{
return interval;
}
}
boolean booleanConstant() :
{ boolean value;}
{
(
<FALSE> { value = false;}
| <TRUE> { value = true;}
) {return value; }
}
int unsignedIntConstant() :
{}
{
<UNSIGNED_INT_CONSTANT> {
int value;
try {
value = Integer.parseInt(token.image);
} catch(NumberFormatException e) {
// only digits are accepted by the gramer, so the only reason for a NumberFormatException should be that the number is too big for int
throw (ParseException) new ParseException("Error: The string '" + token.image + "' couldn't be parsed as integer. The most probable reason is that the number is too big.").initCause(e);
}
return value;
}
}

Related

How to display text next to ascii art (in terminal) in NodeJS?

I would like to display some ascii art in this format:
_,-, Name: Powerful Axe
T_ | Rarity: Yellow Rarity (77.5)
||`-' Attack: 100
|| Defense: 100
|| Speed: 100
~~ Gold Value: 10
an axe that is really rare
I want it in a column layout, kind of like neofetch does, but in NodeJS.
I get all this data from a json file, and I am using the following function to format and color the key pair type values:
const chalk = require('chalk')
function keyValue(key, value) {
return chalk.green(key) + ": " + chalk.yellow(value)
}
This all needs to be in the terminal, and preferably not using node-curses or blessed.
I found a very inefficient and bloated solution:
function print2MultiLine(string, string2) {
var arr1 = string.split("\n");
var arr2 = string2.split("\n");
if (arr1.length > arr2.length) {
while (arr1.length > arr2.length) {
arr2.push("");
}
} else if (arr1.length < arr2.length) {
while (arr1.length < arr2.length) {
arr1.push("");
}
}
var longestLine = 0;
for (let i = 0; i < arr1.length; i++) {
if (arr1[i].length > longestLine) {
longestLine = arr1[i].length;
}
}
for (let i = 0; i < arr1.length; i++) {
while (arr1[i].length < longestLine) {
arr1[i] += " ";
}
}
for (let i = 0; i < arr1.length; i++) {
console.log(arr1[i] + " " + arr2[i]);
}
}
if you like comments:
function print2MultiLine(string, string2) {
// for each line in each string, print it out 3 spaces away from the other
var arr1 = string.split("\n");
var arr2 = string2.split("\n");
// if one string is longer than the other, make the shorter one the length of the longer one
if (arr1.length > arr2.length) {
while (arr1.length > arr2.length) {
arr2.push("");
}
} else if (arr1.length < arr2.length) {
while (arr1.length < arr2.length) {
arr1.push("");
}
}
// get the length of the longest line in arr1
var longestLine = 0;
for (let i = 0; i < arr1.length; i++) {
if (arr1[i].length > longestLine) {
longestLine = arr1[i].length;
}
}
// make all other lines in arr1 the length of the longest line
for (let i = 0; i < arr1.length; i++) {
while (arr1[i].length < longestLine) {
arr1[i] += " ";
}
}
// print out the strings
for (let i = 0; i < arr1.length; i++) {
console.log(arr1[i] + " " + arr2[i]);
}
}
Preview:

How do I keep the string value the same when the indicator buffer no longer gives a value?

I've coded a basic Expert Advisor to trigger a trade when the parameters are true. However, in this case the code will never meet all the requirements because the string value gives no value when the indicator buffer doesn't give a value.
What I need help with is to make the string value for saisignal stay the same when triggered by the indicator buffer, after the bar has passed the arrow indicator, so that when the other signals are eventually indicating a trade, it can trigger a trade.
double closeAllTradesThisPair()
{
for (int i=OrdersTotal();i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,clrNONE);
}
}
}
void OnTick()
{
double saiup = iCustom( Symbol(), PERIOD_H1, "super-arrow-indicator", 0, 0 );
double saidn = iCustom( Symbol(), PERIOD_H1, "super-arrow-indicator", 1, 0 );
double osma1 = iOsMA( Symbol(), PERIOD_H1, 12, 26, 9, PRICE_OPEN, 1 );
double osma0 = iOsMA( Symbol(), PERIOD_H1, 12, 26, 9, PRICE_OPEN, 0 );
double stup = iCustom( Symbol(), PERIOD_H1, "super-trend", 0, 0 );
double stdn = iCustom( Symbol(), PERIOD_H1, "super-trend", 1, 0 );
double sar = iSAR( Symbol(), PERIOD_H1, 0.02, 0.2, 0 );
double ma = iMA( Symbol(), PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE, 0 );
string saisignal = "";
string osmasignal = "";
string stsignal = "";
string sarsignal = "";
string masignal = "";
if(saiup < 1000)
{
saisignal = "123";
}
if(saidn < 1000)
{
saisignal = "321";
}
if(osma1 < osma0)
{
osmasignal = "123";
}
if(osma1 > osma0)
{
osmasignal = "321";
}
if(stup < 1000)
{
stsignal = "123";
}
if(stdn < 1000)
{
stsignal = "321";
}
if(sar < Bid)
{
sarsignal = "123";
}
if(sar > Bid)
{
sarsignal = "321";
}
if(ma < Bid)
{
masignal = "123";
}
if(ma > Bid)
{
masignal = "321";
}
for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(OrderStopLoss() < Ask - (150*_Point))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask-(150*_Point),OrderTakeProfit(),0,CLR_NONE);
}
}
if(OrderType()==OP_SELL)
{
if(OrderStopLoss() > Bid + (150*_Point))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid+(150*_Point),OrderTakeProfit(),0,CLR_NONE);
}
}
}
}
}
if(saisignal == "123")
{
if(osmasignal == "123")
{
if(stsignal == "123")
{
if(sarsignal == "123")
{
if(masignal == "123")
{
double buyticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,3,Ask-150*_Point,0,NULL,0,0,CLR_NONE);
}
}
}
}
}
if(saisignal == "321")
{
if(osmasignal == "321")
{
if(stsignal == "321")
{
if(sarsignal == "321")
{
if(masignal == "321")
{
double sellticket = OrderSend(Symbol(),OP_SELL,0.01,Ask,3,Bid+150*_Point,0,NULL,0,0,CLR_NONE);
}
}
}
}
}
Comment(" sai: ",saisignal," osma: ",osmasignal," st: ",stsignal," sar: ",sarsignal," ma: ",masignal);
}
Q : "How do I keep the string value the same...?"
This problem has a single cause and a pair of available steps for solutions:
The OnTick(){...} code as-is, per each entry, creates empty strings. This must be replaced anyway.
But then we have to declare these strings somehow somewhere.
First option
Still let them declared inside the "scope" ( the {...} ) of the OnTick(){...}-code-block, but declare them as static string saisignal = ""; // STATIC modifier is key here, as in this case the static makes the compiler to keep the actual value of such declared variable from one OnTick(){...} call to the other ( so never re-declaring and re-storing the initialiser value (here an empty (string)"") each such time.
#property strict
...
void OnTick(){
...
static string saisignal = "";
static string osmasignal = "";
static string stsignal = "";
static string sarsignal = "";
static string masignal = "";
...
}
Second option
Move such variable declaration onto the "Top-Level" scope (for globally visible variables), which makes compiler reserve a permanent memory location to store and access any such "globally declared/visible" variable(s).
#property strict
string saisignal = "";
string osmasignal = "";
string stsignal = "";
string sarsignal = "";
string masignal = "";
...
void OnTick(){
...
}

Separate delimited string values in array into boolean variables

I have a dataset that lists different activities done in each day in a string array, similar concept asked here. Each activity is delimited and can easily be separated into columns, as I've had no problem doing in Excel.
activites
Work | family | date | gaming | relax | good sleep | shopping
Work | family | date | Nature | Crusin | reading | gaming | relax | good sleep | cooking | laundry
family | date | movies & tv | gaming | sport | relax | medium sleep | cooking
Work | family | date | Photography | gaming | relax | good sleep | medium sleep | cooking
Work | family | date | Nature | reading | gaming | relax | good sleep | cleaning
What I am trying to do is make each activity into a boolean variable which has its own column as such, so it indicates 0 for not having done the activity on that day and 1 for having done the activity. It would look something like this:
Work Family Date Gaming Relax
1 1 0 1 0
1 1 1 0 0
0 0 1 0 1
So, what I ended up doing was using my knowledge of Java to reformat data. I first separated the activities into their own variables, each containing a numerical (binary) value to indicate whether or not that activity had been done that day. I had to treat sleep quality separately, so that part looks a little wonky. Here's the code, which produced the correct output:
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(new FileReader("activities.txt"));
String[] actList = { "Work", "school", "family", "friends", "date", "nature", "crusin", "photography",
"making music/piano", "movies & tv", "reading", "gaming", "sport", "relax", "sleep", "shopping", "cleaning",
"cooking", "laundry" };
int row = 0;
while (scan.hasNextLine()) {
row++;
int col = 0;
int activityNo = 0;
int[] actValue = new int[actList.length];
String pipeDelim = scan.nextLine();
String[] actName = pipeDelim.split(" \\| ");
int sleepTagsUsed = 0;
while (activityNo < actName.length) {
col = 0;
for (String a : actName) {
if (a.contains("sleep")) {
col = 14;
if (a.equalsIgnoreCase("bad sleep") || a.equalsIgnoreCase("bad sleep\t")) {
if (col < actList.length) {
actValue[col] = 0;
if (sleepTagsUsed == 0) {
col++;
}
sleepTagsUsed++;
} else {
break;
}
if (!(activityNo > actName.length)) {
activityNo++;
}
} else if (a.equalsIgnoreCase("medium sleep") || a.equalsIgnoreCase("medium sleep\t")) {
if (col < actList.length) {
actValue[col] = 1;
if (sleepTagsUsed == 0) {
col++;
}
sleepTagsUsed++;
} else {
break;
}
if (!(activityNo > actName.length)) {
activityNo++;
}
} else if (a.equalsIgnoreCase("good sleep") || a.equalsIgnoreCase("good sleep\t")) {
if (col < actList.length) {
actValue[col] = 2;
if (sleepTagsUsed == 0) {
col++;
}
sleepTagsUsed++;
} else {
break;
}
if (!(activityNo > actName.length)) {
activityNo++;
}
} else if (a.equalsIgnoreCase("sleep early") || a.equalsIgnoreCase("sleep early\t")) {
if (col < actList.length) {
actValue[col] = 3;
if (sleepTagsUsed == 0) {
col++;
}
sleepTagsUsed++;
} else {
break;
}
if (!(activityNo > actName.length)) {
activityNo++;
}
} else {
if (col < actList.length) {
actValue[col] = -1;
} else {
break;
}
System.out.println("No sleep logged error");
}
} else {
int j = 0;
for (String i : actList) {
if (a.equalsIgnoreCase(i) || a.equalsIgnoreCase(i + "\t")) {
actValue[col] = 1;
if (activityNo > actName.length) {
break;
} else {
activityNo++;
break;
}
} else {
if (col < actList.length) {
j++;
if (j > col) {
actValue[col] = 0;
col++;
}
} else {
break;
}
}
}
col++;
}
}
}
for (int p : actValue) {
System.out.print(p + "\t");
}
System.out.println();
}
scan.close();
}

REVIT Transfer floor sketch to Void Extrusion in Family

Struggling with some Revit code to copy the profile of a floor and use it as the sketch profile for a void extrusion in a family.
Here is the Full Sharp Development Code. It half works in my custom project template, when I try to use it in an out of the box project generated from revit default template it gives the error "a managed exception was thrown by revit or by one of its external applications"
In my template it cannot properly split the curves into a secondary array. It says the array elements are being changed but when the loop runs again the element i is back to it's original content??? The TaskDialog clearly says the elements have changed, until the loop iterates again.
Full code: To work it requires a generic family with the name "Void - Custom" to be in the project. The "If found" near the bottom last page and a half of code, is where the for loop is not behaving as expected.
/*
* Created by SharpDevelop.
* User: arautio
* Date: 4/30/2019
* Time: 11:10 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace ARC
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("3411F411-6FC1-4A4D-9CFD-37ABB2028A15")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public void FloorGrating()
{
StringBuilder sb = new StringBuilder();
Dictionary<Floor, List<ModelCurve>> dict_SketchLines = new Dictionary<Floor, List<ModelCurve>>();
UIDocument uidoc = this.ActiveUIDocument;
Document document = uidoc.Document;
View activev = document.ActiveView;
ElementId levelId = null;
levelId = activev.LevelId;
Element levelem = document.GetElement( levelId );
Level lev = document.ActiveView.GenLevel;
Reference refsel = uidoc.Selection.PickObject(ObjectType.Element, "Select Floor to Add Grating To");
Element elem = document.GetElement(refsel.ElementId);
Floor f = elem as Floor;
List<ElementId> _deleted = null;
using (Transaction t = new Transaction(document, "temp"))
{
t.Start();
document.Regenerate();
_deleted = document.Delete(elem.Id).ToList();
t.RollBack();
}
bool SketchLinesFound = false;
List<ModelCurve> _sketchCurves = new List<ModelCurve>();
foreach (var id in _deleted)
{
ModelCurve mc = document.GetElement(id) as ModelCurve;
if (mc != null)
{
_sketchCurves.Add(mc);
SketchLinesFound = true;
}
else
{
if (SketchLinesFound) break;
}
}
dict_SketchLines.Add(f, _sketchCurves);
foreach (Floor key in dict_SketchLines.Keys)
{
List<ModelCurve> _curves = dict_SketchLines[key];
sb.AppendLine(string.Format("floor {0} has sketchlines:", key.Id));
foreach (ModelCurve mc in _curves)
{
sb.AppendLine(string.Format("{0} <{1}>", mc.GetType(), mc.Id));
sb.AppendLine(string.Format("<{0}>", mc.GeometryCurve.IsBound.ToString()));
if (mc.GetType().ToString() == "Autodesk.Revit.DB.ModelArc" && mc.GeometryCurve.IsBound == false)
{
TaskDialog.Show("Revit", "Circle Found");
}
try
{
sb.AppendLine(string.Format("<{0} -- {1}>", mc.GeometryCurve.GetEndPoint(0), mc.GeometryCurve.GetEndPoint(1)));
}
catch
{
}
}
sb.AppendLine();
}
//TaskDialog.Show("debug", sb.ToString());
Document docfamily;
Family fam;
string ftitle = document.Title;
string fpath = document.PathName;
int ftitlelen = ftitle.Length + 4;
int fpathlen = fpath.Length;
int finpathlen = fpathlen - ftitlelen;
string sfinpath = fpath.Substring(0,finpathlen);
string famname = "GratingVoid";
string fext = ".rfa";
int counter = 1;
while (counter < 100)
{
famname = ("GratingVoid" + counter as String);
Family family = FindElementByName(document,typeof(Family),famname)as Family;
if( null == family )
{
sfinpath = (sfinpath + famname + fext);
counter = 1000;
}
counter += 1;
}
FilteredElementCollector collector0 = new FilteredElementCollector(document);
ICollection<Element> collection0 = collector0.WhereElementIsNotElementType().ToElements();
List<FamilySymbol> fsym0 = new FilteredElementCollector(document).OfClass(typeof(FamilySymbol)).Cast<FamilySymbol>().ToList();
FamilySymbol famsymb0 = null;
foreach (FamilySymbol symb in fsym0)
{
if (symb.Name == "Void - Custom")
{
famsymb0 = symb as FamilySymbol;
}
}
fam = famsymb0.Family;
docfamily = document.EditFamily(fam);
try
{
docfamily.SaveAs(#sfinpath);
}
catch
{
TaskDialog.Show("Revit", "Could Not Save Void Family");
}
using (Transaction trans = new Transaction(docfamily))
{
trans.Start("family");
bool circleflag = false;
ElementId delid = null;
FilteredElementCollector collector = new FilteredElementCollector( docfamily );
foreach(Element element in collector.OfClass(typeof(GenericForm)))
{
delid = element.Id;
}
docfamily.Delete(delid);
CurveArray loccurva = new CurveArray();
foreach (Floor key in dict_SketchLines.Keys)
{
List<ModelCurve> _curves = dict_SketchLines[key];
foreach (ModelCurve mc in _curves)
{
if (mc.GetType().ToString() == "Autodesk.Revit.DB.ModelArc" && mc.GeometryCurve.IsBound == false)
{
circleflag = true;
}
LocationCurve lcurve = mc.Location as LocationCurve;
Curve c = lcurve.Curve as Curve;
loccurva.Append(c);
}
}
try
{
if (circleflag == true && loccurva.Size == 2)
{
Curve tempc;
if (loccurva.get_Item(0).GetType().ToString() == "Autodesk.Revit.DB.Arc")
{
tempc = loccurva.get_Item(0);
}
else
{
tempc = loccurva.get_Item(1);
}
loccurva.Clear();
loccurva.Append(tempc);
}
CurveArrArray newcurarr = new CurveArrArray();
newcurarr.Append(loccurva);
SortCurvesContiguousArray(newcurarr);
TaskDialog.Show("Revit CurveArray Array Size" , newcurarr.Size.ToString());
foreach (CurveArray ca in newcurarr)
{
TaskDialog.Show("Revit CurveArray within Array Size" , ca.Size.ToString());
}
// Below is edited for error control - leaving out the secondary loops for now
CurveArrArray switcharr = new CurveArrArray();
//switcharr.Append(newcurarr.get_Item(1));
switcharr.Append(newcurarr.get_Item(0));
//SortCurvesContiguousArray(loccurva);
//CurveArrArray newcurarr = new CurveArrArray();
//newcurarr.Append(loccurva);
double end = 1;
SketchPlane sketch = FindElementByName( docfamily,typeof( SketchPlane ), "Ref. Level" ) as SketchPlane;
docfamily.FamilyCreate.NewExtrusion(false, switcharr, sketch, end);
}
catch
{
TaskDialog.Show("Revit", "Could Not Write to Curve Array or Create Extrusion");
}
trans.Commit();
}
docfamily.Save();
docfamily.LoadFamily(document, new CustomFamilyLoadOption());
docfamily.Close();
File.Delete(sfinpath);
Family familynew = FindElementByName(document,typeof(Family),famname)as Family;
if( null == familynew )
{
TaskDialog.Show("Revit", "Family Does Not Exist");
}
FilteredElementCollector collector1 = new FilteredElementCollector(document);
ICollection<Element> collection = collector1.WhereElementIsNotElementType().ToElements();
List<FamilySymbol> fsym = new FilteredElementCollector(document).OfClass(typeof(FamilySymbol)).Cast<FamilySymbol>().ToList();
FamilySymbol famsymb = null;
foreach (FamilySymbol symb in fsym)
{
if (symb.Name == famname)
{
famsymb = symb as FamilySymbol;
}
}
using (Transaction trans = new Transaction(document))
{
trans.Start("PlaceVoid");
if( ! famsymb.IsActive )
{
famsymb.Activate();
}
XYZ p = new XYZ(0,0,0);
FamilyInstance gratingvoid = document.Create.NewFamilyInstance( p, famsymb, lev, lev, StructuralType.NonStructural );
document.Regenerate();
trans.Commit();
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
static public Element FindElementByName(Document doc,Type targetType,string targetName)
{
return new FilteredElementCollector( doc ).OfClass( targetType ).FirstOrDefault<Element>(e => e.Name.Equals( targetName ) );
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
public class CustomFamilyLoadOption : IFamilyLoadOptions
{
public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
{
overwriteParameterValues = true;
return true;
}
public bool OnSharedFamilyFound(Family sharedFamily,bool familyInUse,out FamilySource source, out bool overwriteParameterValues)
{
source = FamilySource.Family;
overwriteParameterValues = true;
return true;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
const double _inch = 1.0 / 12.0;
const double _sixteenth = _inch / 16.0;
static Curve CreateReversedCurve(Curve orig )
{
//if( !IsSupported( orig ) )
//{
// throw new NotImplementedException("CreateReversedCurve for type " + orig.GetType().Name );
//}
if( orig is Line )
{
//return creapp.NewLineBound(orig.GetEndPoint( 1 ), orig.GetEndPoint( 0 ) );
return Line.CreateBound(orig.GetEndPoint( 1 ), orig.GetEndPoint( 0 ) );
}
else if( orig is Arc )
{
// return creapp.NewArc( orig.GetEndPoint( 1 ), orig.GetEndPoint( 0 ), orig.Evaluate( 0.5, true ) );
return Arc.Create( orig.GetEndPoint( 1 ), orig.GetEndPoint( 0 ), orig.Evaluate( 0.5, true ) );
}
else
{
throw new Exception(
"CreateReversedCurve - Unreachable" );
}
}
public static void SortCurvesContiguousArray(CurveArrArray curvesarr)
{
double _precision1 = 1.0 / 12.0 / 16.0; // around 0.00520833
double _precision2 = 0.001; // limit for CurveLoop.Create(...)
int cn = curvesarr.Size;
int ci = 0;
while (ci < cn)
{
CurveArray curves = curvesarr.get_Item(ci);
ci +=1;
// account for multiple curve loops with secondary array
CurveArray loop1 = new CurveArray();
CurveArray loop2 = new CurveArray();
int n = curves.Size;
int split = 1;
// Walk through each curve (after the first)
// to match up the curves in order
for (int i = 0; i < n; ++i)
{
TaskDialog.Show("Revit I Loop Run", i.ToString());
Curve curve = curves.get_Item(i);
if (curve.GetType().ToString() == "Autodesk.Revit.DB.Arc" && curve.IsBound == false)
{
break;
}
XYZ beginPoint = curve.GetEndPoint(0);
XYZ endPoint = curve.GetEndPoint(1);
XYZ p,q;
// Find curve with start point = end point
bool found = (i + 1 >= n);
for (int j = i + 1; j < n; ++j)
{
p = curves.get_Item(j).GetEndPoint(0);
q = curves.get_Item(j).GetEndPoint(1);
// If there is a match end->start,
// this is the next curve
if (p.DistanceTo(endPoint) < _precision1)
{
if (p.DistanceTo(endPoint) > _precision2)
{
XYZ intermediate = new XYZ((endPoint.X + p.X) / 2.0, (endPoint.Y + p.Y) / 2.0, (endPoint.Z + p.Z) / 2.0);
curves.set_Item(i, Line.CreateBound(beginPoint, intermediate));
curves.set_Item(j, Line.CreateBound(intermediate, q));
}
if (i + 1 != j)
{
Curve tmp = curves.get_Item(i + 1);
curves.set_Item(i + 1, curves.get_Item(j));
curves.set_Item(j, tmp);
}
found = true;
break;
}
// If there is a match end->end,
// reverse the next curve
if (q.DistanceTo(endPoint) < _precision1)
{
if (q.DistanceTo(endPoint) > _precision2)
{
XYZ intermediate = new XYZ((endPoint.X + q.X) / 2.0, (endPoint.Y + q.Y) / 2.0, (endPoint.Z + q.Z) / 2.0);
curves.set_Item(i, Line.CreateBound(beginPoint, intermediate));
curves.set_Item(j, Line.CreateBound(p, intermediate));
}
if (i + 1 == j)
{
curves.set_Item(i + 1, CreateReversedCurve(curves.get_Item(j)));
}
else
{
Curve tmp = curves.get_Item(i + 1);
curves.set_Item(i + 1, CreateReversedCurve(curves.get_Item(j)));
curves.set_Item(j, tmp);
}
found = true;
break;
}
}
if (!found)
{
// if not found, must be part of a new loop - move it to the back and keep going and add to second array
TaskDialog.Show("Revit No Match Found for item", i.ToString());
TaskDialog.Show("Revit", "Moveing it to back of list");
Curve tmp1 = curves.get_Item(i);
TaskDialog.Show("Revit tmp1 Current i item endpt", tmp1.GetEndPoint(0).ToString());
loop2.Append(tmp1);
Curve tmp2 = curves.get_Item(n - split);
TaskDialog.Show("Revit tmp2 Back of list item endpt", tmp2.GetEndPoint(0).ToString());
// set current item to rear
curves.set_Item(i, tmp2);
// set rear item to current
curves.set_Item(n - split, tmp1);
TaskDialog.Show("Revit new item i endpt", curves.get_Item(i).GetEndPoint(0).ToString());
TaskDialog.Show("Revit moved item endpt", curves.get_Item(n - split).GetEndPoint(0).ToString());
// error testing - try to append in a different manner and check values
//curves.set_Item(i, Line.CreateBound(curves.get_Item(i).GetEndPoint(0), curves.get_Item(i).GetEndPoint(1)));
//curves.set_Item(n - split, Line.CreateBound(curves.get_Item(n - split).GetEndPoint(0), curves.get_Item(n - split).GetEndPoint(1)));
//Curve ncurve = Line.CreateBound(curves.get_Item(n - split).GetEndPoint(0), curves.get_Item(n - split).GetEndPoint(1));
//TaskDialog.Show("Revit Appended to Loop2 Endpoint", ncurve.GetEndPoint(0).ToString());
//loop2.Append(ncurve);
//set the split off counter so items not fitting in first loop can be split to new array.
split += 1;
//reset the counter back so item moved from rear can be checked in next run of for loop
i -= 2;
}
//set counter to end for loop when all items that do not fit in first loop are processed
if (i >= n - (split + 1))
{
TaskDialog.Show("Revit", "End Of Looping");
TaskDialog.Show("Revit - The Split Number", split.ToString());
i = n;
}
}
int counter = 0;
// recreate array with only items from first loop found
while (counter <= (n - split))
{
loop1.Append(curves.get_Item(counter));
counter += 1;
}
TaskDialog.Show("Revit loop1 Size", loop1.Size.ToString());
curvesarr.Clear();
curvesarr.Append(loop1);
if (loop2.Size > 0)
{
string stringinfo = "";
// run the loop detection on a second array that was split from the first
TaskDialog.Show("Revit loop2 Size", loop2.Size.ToString());
CurveArrArray tmpcurvesarr = new CurveArrArray();
tmpcurvesarr.Append(loop2);
SortCurvesContiguousArray(tmpcurvesarr);
loop2.Clear();
loop2 = tmpcurvesarr.get_Item(0);
curvesarr.Append(loop2);
foreach (Curve ccc in loop2)
{
stringinfo = (stringinfo + " " + ccc.GetEndPoint(0).ToString() + " - " + ccc.GetEndPoint(1).ToString());
}
TaskDialog.Show("Revit", stringinfo);
}
}
}
}
}
Thanks for any and all help.
Shane

span insertion logic help

I have a text like this
"You are advised to grant access to these settings only if you are sure you want to allow this program to run automatically when your computer starts. Otherwise it is better to deny access."
and i have 3 selections,
1. StartIndex = 8, Length = 16 // "advised to grant"
2. StartIndex = 16, Length = 33 //"to grant access to these settings"
3. StartIndex = 35, Length = 26 // "these settings only if you"
i need to insert span tags and highlight selections, and intersections of words should be highlighted in different color,
result should be something like this
"You are <span style= 'background-color:#F9DA00'>advised </span> <span id = 'intersection' style= 'background-color:#F9DA00'>to grant </span> <span style= 'background-color:#F9DA00'>advised </span> <span style= 'background-color:#F9DA00'>access to </span>
<span id = 'intersection' style= 'background-color:#F9DA00'>these settings</span>
<span style= 'background-color:#F9DA00'>only if you </span> sure you want to allow this program to run automatically when your computer starts. Otherwise it is better to deny access."
please help me to sort this out, i have been trying to figure-out a logic for a long time now and no luck so far
Markup comment: IDs need to be unique. Make them classes instead: class="intersection".
finally manage to find a solution on my own, hope this help someone also in the future
public enum SortDirection
{
Ascending, Descending
}
public enum SpanType
{
Intersection, InnerSpan, Undefined
}
class Program
{
static void Main(string[] args)
{
string MessageText = #"You are advised to grant access to these settings only if you are sure you want to allow this program to run automatically when your computer starts. Otherwise it is better to deny access.";
List<Span> spanList = new List<Span>();
List<Span> intersectionSpanList = new List<Span>();
spanList.Add(new Span() { SpanID = "span1", Text = "advised to grant", ElementType = SpanType.Undefined, StartIndex = 8, Length = 16 });
spanList.Add(new Span() { SpanID = "span2", Text = "to grant access to these settings", ElementType = SpanType.Undefined, StartIndex = 16, Length = 33 });
spanList.Add(new Span() { SpanID = "span3", Text = "these settings only if you", ElementType = SpanType.Undefined, StartIndex = 35, Length = 26 });
// simple interseciotn
//spanList.Add(new Span() { SpanID = "span1", Text = "advised to grant", ElementType = TagType.Undefined, StartIndex = 8, Length = 16 });
//spanList.Add(new Span() { SpanID = "span2", Text = "to grant access", ElementType = TagType.Undefined, StartIndex = 16, Length = 15 });
// two different spans
//spanList.Add(new Span() { SpanID = "span1", Text = "advised to grant", ElementType = TagType.Undefined, StartIndex = 8, Length = 16 });
//spanList.Add(new Span() { SpanID = "span2", Text = "only if you are ", ElementType = TagType.Undefined, StartIndex = 50, Length = 16 });
// inner span
//spanList.Add(new Span() { SpanID = "span1", Text = "to grant access to these settings", ElementType = TagType.Undefined , StartIndex = 16, Length = 33 });
//spanList.Add(new Span() { SpanID = "span2", Text = "access to these", ElementType = TagType.Undefined, StartIndex = 25, Length = 15 });
// one inner span, and complex
//spanList.Add(new Span() { SpanID = "span1", Text = "to grant access to these settings only ", ElementType = TagType.Undefined, StartIndex = 16, Length = 39 });
//spanList.Add(new Span() { SpanID = "span2", Text = "access to these", ElementType = TagType.Undefined, StartIndex = 25, Length = 15 });
//spanList.Add(new Span() { SpanID = "span3", Text = "only if you are sure", ElementType = TagType.Undefined, StartIndex = 50, Length = 20 });
// one large span, and two intersections
//spanList.Add(new Span() { SpanID = "span1", Text = "grant access to these settings only if you are sure you want to allow this program to run automatically when", ElementType = SpanType.Undefined, StartIndex = 19, Length = 108 });
//spanList.Add(new Span() { SpanID = "span2", Text = "these settings only", ElementType = SpanType.Undefined, StartIndex = 35, Length = 19 });
//spanList.Add(new Span() { SpanID = "span3", Text = "you want to allow this", ElementType = SpanType.Undefined, StartIndex = 71, Length = 22 });
spanList.Sort("StartIndex asc");
foreach (var item in spanList)
item.SplitSpans(ref spanList, ref intersectionSpanList, ref MessageText);
// join intersections with span collection
foreach (var item in intersectionSpanList)
spanList.Add(item);
//remove duplicates
spanList = RemoveDuplicateSpans(spanList);
// sort spans by index ..
spanList.Sort("StartIndex asc"); //desc
foreach (var item in spanList)
{
item.InsertStartSpan(ref spanList, ref MessageText);
}
foreach (var item in spanList)
{
item.InsertEndSpan(ref spanList, ref MessageText);
}
//int count = spanList.Count -1;
//while (count > 0)
//{
// Span currentSpan = spanList[count];
// currentSpan.InsertEndSpan(ref spanList, ref MessageText);
// count--;
//}
}
internal static List<Span> RemoveDuplicateSpans(List<Span> list)
{
List<int> uniqueList = new List<int>();
for (int i = 0; i < list.Count; i++)
{
for (int j = 0; j < list.Count ; j++)
{
if (list[i].SpanID != list[j].SpanID)
{
if (list[i].StartIndex == list[j].StartIndex && list[i].EndPossition == list[j].EndPossition && list[i].ElementType == SpanType.Undefined)
{
uniqueList.Add(i);
}
}
}
}
foreach (var item in uniqueList)
{
list.RemoveAt(item);
}
return list;
}
}
public static class Extensions
{
public static void Sort<T>(this List<T> list, string sortExpression)
{
string[] sortExpressions = sortExpression.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
List<GenericComparer> comparers = new List<GenericComparer>();
foreach (string sortExpress in sortExpressions)
{
string sortProperty = sortExpress.Trim().Split(' ')[0].Trim();
string sortDirection = sortExpress.Trim().Split(' ')[1].Trim();
Type type = typeof(T);
PropertyInfo PropertyInfo = type.GetProperty(sortProperty);
if (PropertyInfo == null)
{
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo info in props)
{
if (info.Name.ToString().ToLower() == sortProperty.ToLower())
{
PropertyInfo = info;
break;
}
}
if (PropertyInfo == null)
{
throw new Exception(String.Format("{0} is not a valid property of type: \"{1}\"", sortProperty, type.Name));
}
}
SortDirection SortDirection = SortDirection.Ascending;
if (sortDirection.ToLower() == "asc" || sortDirection.ToLower() == "ascending")
{
SortDirection = SortDirection.Ascending;
}
else if (sortDirection.ToLower() == "desc" || sortDirection.ToLower() == "descending")
{
SortDirection = SortDirection.Descending;
}
else
{
throw new Exception("Valid SortDirections are: asc, ascending, desc and descending");
}
comparers.Add(new GenericComparer { SortDirection = SortDirection, PropertyInfo = PropertyInfo, comparers = comparers });
}
list.Sort(comparers[0].Compare);
}
}
public class GenericComparer
{
public List<GenericComparer> comparers { get; set; }
int level = 0;
public SortDirection SortDirection { get; set; }
public PropertyInfo PropertyInfo { get; set; }
public int Compare<T>(T t1, T t2)
{
int ret = 0;
if (level >= comparers.Count)
return 0;
object t1Value = comparers[level].PropertyInfo.GetValue(t1, null);
object t2Value = comparers[level].PropertyInfo.GetValue(t2, null);
if (t1 == null || t1Value == null)
{
if (t2 == null || t2Value == null)
{
ret = 0;
}
else
{
ret = -1;
}
}
else
{
if (t2 == null || t2Value == null)
{
ret = 1;
}
else
{
ret = ((IComparable)t1Value).CompareTo(((IComparable)t2Value));
}
}
if (ret == 0)
{
level += 1;
ret = Compare(t1, t2);
level -= 1;
}
else
{
if (comparers[level].SortDirection == SortDirection.Descending)
{
ret *= -1;
}
}
return ret;
}
}
public class Span
{
string _Color = "#F9DA00";
public const int SPAN_START_LENGTH = 40;
public const int SPAN_END_LENGTH = 7;
public const int SPAN_TOTAL_LENGTH = 47;
public string Color
{
get
{
return _Color;
}
set
{
_Color = value;
}
}
public string SpanID { get; set; }
public int StartIndex { get; set; }
public int HTMLTagEndPossition { get; set; }
public Span ParentSpan { get; set; }
public int Length { get; set; }
public SpanType ElementType { get; set; }
public string Text { get; set; }
public int EndPossition
{
get
{
return StartIndex + Length;
}
}
public string GetStartSpanHtml()
{
return "<span style= 'background-color:" + Color + "'>" + this.Text;
}
public string GetEndSpanHtml()
{
return "</span>";
}
public bool IsProcessed { get; set; }
internal void PostProcess(Span span, ref List<Span> spanList, ref string MessageText)
{
MessageText = MessageText.Remove(span.StartIndex, span.Length);
MessageText = MessageText.Insert(span.StartIndex, span.GetStartSpanHtml());
int offset = Span.SPAN_TOTAL_LENGTH;
AdjustStartOffsetOfSpans(spanList, span, offset);
}
internal void SplitSpans(ref List<Span> spanList, ref List<Span> intersectionSpanList, ref string MessageText)
{
foreach (var item in spanList)
{
if (this.SpanID == item.SpanID)
continue;
if (this.StartIndex < item.StartIndex && this.EndPossition > item.EndPossition)
{
// inner
int innerSpanLength = this.EndPossition - item.StartIndex;
int innerSpanStartPos = this.StartIndex;
string innerSpanText = MessageText.Substring(item.StartIndex, item.Length);
Span innerSpan = new Span();
innerSpan.SpanID = "innerSpan" + Guid.NewGuid().ToString().Replace("-", "");
innerSpan.ElementType = SpanType.InnerSpan;
innerSpan.Text = innerSpanText;
innerSpan.Length = item.Length;
innerSpan.StartIndex = item.StartIndex;
innerSpan.ParentSpan = this;
intersectionSpanList.Add(innerSpan);
}
if (this.StartIndex < item.StartIndex && item.EndPossition > this.EndPossition && this.EndPossition > item.StartIndex)
{
// end is overlapping
int intersectionLength = this.EndPossition - item.StartIndex;
int intersectionStartPos = item.StartIndex;
string intersectionText = MessageText.Substring(item.StartIndex, intersectionLength);
// Build intersection span
Span intersectonSpan = new Span();
intersectonSpan.SpanID = "intersectonSpan" + Guid.NewGuid().ToString().Replace("-", "");
intersectonSpan.Text = intersectionText;
intersectonSpan.Length = intersectionLength;
intersectonSpan.StartIndex = intersectionStartPos;
intersectonSpan.ElementType = SpanType.Intersection;
intersectionSpanList.Add(intersectonSpan);
// adjust my end pos.
this.Length = this.Length - intersectionLength;
this.Text = this.Text.Substring(0, this.Length);
item.StartIndex += intersectionLength;
item.Length -= intersectionLength;
item.Text = item.Text.Substring(intersectionLength, item.Length);
}
else if (this.StartIndex < item.StartIndex && item.EndPossition > this.EndPossition && this.EndPossition < item.StartIndex)
{
// two spans are not over lapping,
}
//if (this.EndPossition > item.StartIndex && this.EndPossition < item.EndPossition)
//{
// if (item.StartIndex < this.StartIndex && item.EndPossition > this.EndPossition)
// {
// int innerSpanLength = this.EndPossition - this.StartIndex;
// int innerSpanStartPos = this.StartIndex;
// string innerSpanText = MessageText.Substring(this.StartIndex, this.Length);
// // we are dealing with a inner span..
// Span innerSpan = new Span();
// innerSpan.SpanID = "innerSpan";
// innerSpan.Text = innerSpanText;
// innerSpan.Length = innerSpanLength;
// innerSpan.StartIndex = innerSpanStartPos;
// innerSpan.IsIntersection = true;
// intersectionSpanList.Add(innerSpan);
// MessageText = MessageText.Remove(innerSpanStartPos, innerSpanLength);
// MessageText = MessageText.Insert(innerSpanStartPos, innerSpan.GetStartSpanHtml());
// break;
// }
// int intersectionLength = this.EndPossition - item.StartIndex;
// int intersectionStartPos = item.StartIndex;
// string intersectionText = MessageText.Substring(item.StartIndex, intersectionLength);
// // adjust the string.
// if (!this.IsProcessed)
// {
// this.Text = this.Text.Substring(0, this.Length - intersectionLength);
// this.Length = this.Length - intersectionLength;
// MessageText = MessageText.Remove(this.StartIndex, this.Length);
// MessageText = MessageText.Insert(this.StartIndex, this.GetStartSpanHtml());
// // readjust intersection after insertion of the first span..
// intersectionStartPos = Span.SPAN_START_LENGTH + intersectionStartPos;
// }
// // Build intersection span
// Span intersectonSpan = new Span();
// intersectonSpan.SpanID = "intersectonSpan";
// intersectonSpan.Text = intersectionText;
// intersectonSpan.Length = intersectionLength;
// intersectonSpan.StartIndex = intersectionStartPos;
// intersectonSpan.IsIntersection = true;
// intersectionSpanList.Add(intersectonSpan);
// MessageText = MessageText.Remove(intersectionStartPos, intersectionLength);
// MessageText = MessageText.Insert(intersectionStartPos, intersectonSpan.GetStartSpanHtml());
// if (!this.IsProcessed)
// item.StartIndex = item.StartIndex + intersectionLength + Span.SPAN_START_LENGTH + Span.SPAN_START_LENGTH;
// else
// item.StartIndex = item.StartIndex + intersectionLength + Span.SPAN_START_LENGTH;
// item.Length = item.Length - intersectionLength;
// item.Text = item.Text.Substring(intersectionLength, item.Length);
// //MessageText = MessageText.Remove(item.StartIndex, item.Length);
// //MessageText = MessageText.Insert(item.StartIndex, item.GetOuterHtml());
// int offset;
// if (!this.IsProcessed)
// offset = Span.SPAN_START_LENGTH + Span.SPAN_START_LENGTH;
// else
// offset = Span.SPAN_START_LENGTH;
// AdjustOffsetSpans(spanList, item, offset);
// this.IsProcessed = true;
// break;
//}
//else if (item.StartIndex > this.StartIndex && item.EndPossition < this.EndPossition)
//{
// // bigger span, inside there are children span(s)
// MessageText = MessageText.Remove(this.StartIndex, this.Length);
// MessageText = MessageText.Insert(this.StartIndex, this.GetStartSpanHtml());
// // since this span is the big guy.
// AdjustOffsetForInnerSpansAndExternalSpans(spanList, this, this.StartIndex, this.EndPossition);
// this.StartIndex += Span.SPAN_START_LENGTH;
// this.IsProcessed = true;
//}
}
}
//internal static void AdjustOffsetForInnerSpansAndExternalSpans(List<Span> spanList, Span parentSpan, int parentStartIndex, int parentEndIndex)
//{
// bool adjustAfterThisSpan = false;
// foreach (var item in spanList)
// {
// if (item.SpanID == parentSpan.SpanID)
// {
// adjustAfterThisSpan = true;
// continue;
// }
// if (adjustAfterThisSpan)
// {
// // is this span in the middle of the parent ?
// if (item.StartIndex > parentSpan.StartIndex && item.EndPossition < parentSpan.EndPossition)
// {
// item.StartIndex += SPAN_START_LENGTH;
// }
// else
// {
// // after parent tag ?
// item.StartIndex += SPAN_START_LENGTH;
// }
// }
// }
//}
private void AdjustEndOffsetOfSpans(List<Span> spanList, Span span, int SPAN_END_LENGTH)
{
bool adjustAfterThisSpan = false;
foreach (var item in spanList)
{
if (item.SpanID == span.SpanID)
{
adjustAfterThisSpan = true;
continue;
}
if (adjustAfterThisSpan)
{
if (item.ParentSpan == null)
{
item.HTMLTagEndPossition += SPAN_END_LENGTH;
}
else if (span.ParentSpan != null && this.SpanID == item.ParentSpan.SpanID)
{ }
}
}
}
internal static void AdjustStartOffsetOfSpans(List<Span> spanList, Span fromSpan, int offset)
{
bool adjustAfterThisSpan = false;
foreach (var item in spanList)
{
if (item.SpanID == fromSpan.SpanID)
{
adjustAfterThisSpan = true;
continue;
}
if (adjustAfterThisSpan)
item.StartIndex += offset;
}
}
internal void InsertStartSpan(ref List<Span> spanList, ref string MessageText)
{
MessageText = MessageText.Remove(this.StartIndex, this.Length);
MessageText = MessageText.Insert(this.StartIndex, this.GetStartSpanHtml());
AdjustStartOffsetOfSpans(spanList, this, SPAN_START_LENGTH);
// Adjust end element tag
switch (this.ElementType)
{
case SpanType.Intersection:
{
this.HTMLTagEndPossition = this.Length + SPAN_START_LENGTH + this.StartIndex;
break;
}
case SpanType.InnerSpan:
{
this.HTMLTagEndPossition = this.Length + SPAN_START_LENGTH + this.StartIndex;
// increase the parent's tag offset conent length
this.ParentSpan.HTMLTagEndPossition += SPAN_START_LENGTH;
break;
}
case SpanType.Undefined:
{
this.HTMLTagEndPossition = this.Length + SPAN_START_LENGTH + this.StartIndex;
break;
}
default:
break;
}
}
internal void InsertEndSpan(ref List<Span> spanList, ref string MessageText)
{
switch (this.ElementType)
{
case SpanType.Intersection:
{
MessageText = MessageText.Insert(this.HTMLTagEndPossition, this.GetEndSpanHtml());
break;
}
case SpanType.InnerSpan:
{
MessageText = MessageText.Insert(this.HTMLTagEndPossition, this.GetEndSpanHtml());
break;
}
case SpanType.Undefined:
{
MessageText = MessageText.Insert(this.HTMLTagEndPossition, this.GetEndSpanHtml());
break;
}
default:
break;
}
AdjustEndOffsetOfSpans(spanList, this, SPAN_END_LENGTH);
}
}

Resources