-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from objectionary/cond-attrs
Conditional attributes
- Loading branch information
Showing
17 changed files
with
436 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/main/kotlin/org/objectionary/ddr/transform/CondNodesResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2022 Olesia Subbotina | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package org.objectionary.ddr.transform | ||
|
||
import org.objectionary.ddr.graph.base | ||
import org.objectionary.ddr.graph.line | ||
import org.objectionary.ddr.graph.pos | ||
import org.objectionary.ddr.graph.ref | ||
import org.objectionary.ddr.graph.repr.Graph | ||
import org.objectionary.ddr.graph.repr.IGraphCondNode | ||
import org.objectionary.ddr.graph.repr.IGraphNode | ||
import org.w3c.dom.Document | ||
import org.w3c.dom.Element | ||
import org.w3c.dom.Node | ||
import java.io.FileOutputStream | ||
|
||
/** | ||
* Conditional nodes resolver | ||
* @todo #41:30min the code here needs refactoring and documentation | ||
*/ | ||
class CondNodesResolver( | ||
private val graph: Graph, | ||
private val documents: MutableMap<Document, String> | ||
) { | ||
/** | ||
* Aggregate process of conditional nodes resolving | ||
* @todo #40:30min remove extra "> @" and add one to .if | ||
*/ | ||
fun resolveCondNodes() { | ||
processObjects() | ||
documents.forEach { | ||
val objects: MutableList<Node> = mutableListOf() | ||
val docObjects = it.key.getElementsByTagName("o") | ||
for (i in 0 until docObjects.length) { | ||
objects.add(docObjects.item(i)) | ||
} | ||
graph.initialObjects.addAll(objects) | ||
} | ||
documents.forEach { doc -> | ||
val outputStream = FileOutputStream(doc.value) | ||
outputStream.use { XslTransformer().writeXml(it, doc.key) } | ||
} | ||
} | ||
|
||
private fun processObjects() { | ||
val objects = graph.initialObjects | ||
val condNodes: List<IGraphCondNode> = graph.igNodes.filterIsInstance(IGraphCondNode::class.java) | ||
condNodes.forEach { node -> | ||
objects.filter { ref(it) == line(node.body) }.forEach { insert(it, node) } | ||
} | ||
} | ||
|
||
// @todo #39:30min this method should be used | ||
private fun traverseDotChain( | ||
node: Node, | ||
abstract: IGraphNode | ||
) { | ||
var sibling = node.nextSibling?.nextSibling | ||
while (base(sibling)?.startsWith(".") == true) { | ||
val base = base(sibling) | ||
val attr = abstract.attributes.find { it.name == base?.substring(1) } | ||
if (attr != null && sibling != null) { | ||
// insert(sibling, attr) | ||
} | ||
sibling = sibling?.nextSibling | ||
} | ||
} | ||
|
||
private fun collectDotChain( | ||
node: Node | ||
): MutableList<Node?> { | ||
val res: MutableList<Node?> = mutableListOf() | ||
var sibling = node.nextSibling?.nextSibling | ||
while (base(sibling)?.startsWith(".") == true) { | ||
res.add(sibling) | ||
sibling = sibling?.nextSibling | ||
} | ||
return res | ||
} | ||
|
||
private fun insert(node: Node, igNode: IGraphCondNode) { | ||
val expr = collectDotChain(node) | ||
|
||
// @todo #45:30min remove duplicated code | ||
val parent = node.parentNode | ||
val siblings = mutableSetOf(node) | ||
var tmpNode = node | ||
while (tmpNode.nextSibling != null) { | ||
siblings.add(tmpNode.nextSibling) | ||
tmpNode = tmpNode.nextSibling | ||
} | ||
siblings.forEach { | ||
parent.removeChild(it) | ||
} | ||
val document = parent.ownerDocument | ||
val child = addDocumentChild(document, igNode, node, expr) | ||
parent.appendChild(child) | ||
siblings.forEach { parent.appendChild(it) } | ||
parent.removeChild(node) | ||
expr.forEach { parent.removeChild(it) } | ||
} | ||
|
||
private fun addDocumentChild( | ||
document: Document, | ||
igNode: IGraphCondNode, | ||
node: Node, | ||
expr: MutableList<Node?> | ||
): Element { | ||
val ifChild: Element = document.createElement("o") | ||
ifChild.setAttribute("base", ".if") | ||
ifChild.setAttribute("line", line(node)) // @todo #42:30min add method="" attribute | ||
ifChild.setAttribute("pos", pos(node)) | ||
igNode.cond.forEach { ifChild.appendChild(it.cloneNode(true)) } | ||
val ref1 = document.createAttribute("ref").apply { value = ref(igNode.fstOption[0]) } // @todo #46:30min remove duplicates from code | ||
val fstNode = node.cloneNode(true).apply { attributes.setNamedItem(ref1) } | ||
ifChild.appendChild(fstNode) | ||
expr.forEach { ifChild.appendChild(it?.cloneNode(true)) } | ||
val ref2 = document.createAttribute("ref").apply { value = ref(igNode.sndOption[0]) } | ||
val sndNode = node.cloneNode(true).apply { attributes.setNamedItem(ref2) } | ||
ifChild.appendChild(sndNode) | ||
expr.forEach { ifChild.appendChild(it?.cloneNode(true)) } | ||
return ifChild | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
41-51bb4399
discovered insrc/main/kotlin/org/objectionary/ddr/transform/CondNodesResolver.kt
) and submitted as #39. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
40-384e3e8f
discovered insrc/main/kotlin/org/objectionary/ddr/transform/CondNodesResolver.kt
) and submitted as #40. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
39-308dddcb
discovered insrc/main/kotlin/org/objectionary/ddr/transform/CondNodesResolver.kt
) and submitted as #41. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
45-d09d96b0
discovered insrc/main/kotlin/org/objectionary/ddr/transform/CondNodesResolver.kt
) and submitted as #42. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
42-50a65b2b
discovered insrc/main/kotlin/org/objectionary/ddr/transform/CondNodesResolver.kt
) and submitted as #43. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
46-3505aef4
discovered insrc/main/kotlin/org/objectionary/ddr/transform/CondNodesResolver.kt
) and submitted as #44. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
43-7ea31038
discovered insrc/main/kotlin/org/objectionary/ddr/transform/BasicDecoratorsResolver.kt
) and submitted as #45. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
44-e067a6d7
discovered insrc/main/kotlin/org/objectionary/ddr/graph/InnerPropagator.kt
) and submitted as #46. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.56cf695
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
47-62e09889
discovered insrc/test/kotlin/org/objectionary/ddr/integration/resolver/ResolverTest.kt
) and submitted as #47. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.