Manybubbles has uploaded a new change for review.
https://gerrit.wikimedia.org/r/187858
Change subject: Add codenarc to complain about groovy code
......................................................................
Add codenarc to complain about groovy code
I put together the start of a rules file and got it running. I also put
some constraints on the report so we wouldn't add more failures. We might
want to also fix the ones that we have!
Change-Id: Ib89ecc8b7bd59a2257cf5784976627406ab86799
---
M pom.xml
A src/build/codenarc.groovy
D src/main/groovy/org/wikidata/gremlin/ConsoleInit.groovy
M src/main/groovy/org/wikidata/gremlin/Loader.groovy
M src/test/java/org/wikidata/gremlin/ImportTest.groovy
5 files changed, 111 insertions(+), 139 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/wikidata/gremlin
refs/changes/58/187858/1
diff --git a/pom.xml b/pom.xml
index ae021e4..0ba53e8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -265,7 +265,7 @@
<executions>
<execution>
<id>validate</id>
- <phase>validate</phase>
+ <phase>test</phase>
<configuration>
<configLocation>src/build/checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
@@ -283,6 +283,48 @@
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>6.2</version>
+ </dependency>
+ </dependencies>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-antrun-plugin</artifactId>
+ <version>1.8</version>
+ <executions>
+ <execution>
+ <id>codeNarc</id>
+ <phase>test</phase>
+ <configuration>
+ <target>
+ <taskdef name="codenarc"
classname="org.codenarc.ant.CodeNarcTask"
+ classpathref="maven.runtime.classpath"/>
+ <codenarc
+
ruleSetFiles="file:${basedir}/src/build/codenarc.groovy"
+ maxPriority1Violations="0"
maxPriority2Violations="6" maxPriority3Violations="0">
+ <!-- TODO these should go to 0 -->
+ <report type="html">
+ <option name="outputFile"
value="target/codenarc/CodeNarcAntReport.html"/>
+ <option name="title" value="Wikidata
Gremlin"/>
+ </report>
+ <report type="text">
+ <option name="writeToStandardOut"
value="true"/>
+ </report>
+ <fileset dir="src">
+ <include name="**/*.groovy"/>
+ </fileset>
+ </codenarc>
+ </target>
+ </configuration>
+ <goals>
+ <goal>run</goal>
+ </goals>
+ </execution>
+ </executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.codenarc</groupId>
+ <artifactId>CodeNarc</artifactId>
+ <version>0.22</version>
</dependency>
</dependencies>
</plugin>
@@ -331,15 +373,6 @@
</testResource>
</testResources>
</build>
- <reporting>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>codenarc-maven-plugin</artifactId>
- <version>0.18-1</version>
- </plugin>
- </plugins>
- </reporting>
<profiles>
<profile>
<id>skipTests</id>
diff --git a/src/build/codenarc.groovy b/src/build/codenarc.groovy
new file mode 100644
index 0000000..7ff9e6c
--- /dev/null
+++ b/src/build/codenarc.groovy
@@ -0,0 +1,51 @@
+/**
+ * Copyright (C) 2014 Wikimedia Foundation
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ruleset {
+ description 'Rules for Wikidata Gremlin!'
+
+ // Javadoc
+
+
+ // Formatting
+
+
+ // Naming Conventions
+ AbstractClassName {
+ regex = /^Abstract.*$/
+ }
+ ClassName
+ MethodName
+
+
+ // Import
+
+
+ // Common coding problems/opinionates stuff
+ AssertWithinFinallyBlock
+ AssignmentInConditional
+ BigDecimalInstantiation
+ BitwiseOperatorInConditional
+ BooleanGetBoolean
+ BrokenNullCheck
+ BrokenOddnessCheck
+ ClassForName
+ ConfusingTernary
+
+
+ // Really opinionated. Probably wrong.
+ CyclomaticComplexity {
+ maxMethodComplexity = 10 // Same as Java
+ }
+}
diff --git a/src/main/groovy/org/wikidata/gremlin/ConsoleInit.groovy
b/src/main/groovy/org/wikidata/gremlin/ConsoleInit.groovy
deleted file mode 100644
index 49d8a66..0000000
--- a/src/main/groovy/org/wikidata/gremlin/ConsoleInit.groovy
+++ /dev/null
@@ -1,112 +0,0 @@
-/**
- * Copyright (C) 2014 Wikimedia Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wikidata.gremlin
-
-import groovy.lang.Closure
-import groovy.util.logging.Slf4j
-
-@Slf4j
-class ConsoleInit extends RexsterInit {
- // TODO Do we need this in Tinkerpop 3 at all?
- protected final def script
-
- private ConsoleInit(script) {
- super()
- this.script = script
- script.random = new Random()
- }
-
- static def init(script) {
- return new ConsoleInit(script).setup()
- }
-
- protected ConsoleInit setup()
- {
- log.info "Starting console setup..."
- super.setup()
- graph()
- schema()
- this
- }
-
- /**
- * Set the g global with the graph contents.
- */
- private def graph() {
- // First lets just see if we're already in a graph scope like on the web
console.
- // Try initializing like we're in rexster command line console.
- try {
- if (script.g) {
- log.info "Initialized for Rexster Web Console"
- }
- } catch (MissingPropertyException e) {
- // OK .g isn't defined. Go to next option
- log.info "script.g not found, proceeding..."
- }
- try {
- script.g = script.rexster.getGraph('wikidata')
- log.info "Initialized for Rexster Console"
- } catch (MissingPropertyException e) {
- // OK .rexster isn't defined. Go to next option
- log.info "script.rexster not found, proceeding..."
- }
- if(!script.g) {
- try {
- log.info "Initializing using in memory graph"
- // Use reflection so we don't need to depend on TinkerGraph which is
no longer bundled with tinkerpop
- script.g =
Class.forName("com.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph").newInstance()
- } catch (Exception e) {
- log.error "Something is wrong loading or creating the graph. Here is
the exception, good luck:"
- throw e
- }
- }
- return script.g
- }
-
- /**
- * Setup the graph serve wikidata items.
- */
- private void schema() {
- try {
- def schema = new Schema(script.g)
- schema.setupSchema()
- schema.setupConstantData()
- } catch (Exception e) {
- log.error "Something went wrong updating the schema. Here is the
exception, good luck:"
- e.printStackTrace()
- System.exit(1)
- }
- }
-
- void test () {
- script.g.V('vid', 0).remove()
- script.g.V('vid', 1).remove()
- def v0 = script.g.addVertex([vid: 0, type: 'start'])
- def random = new Random()
- for(i in 1..10000000) {
- def v = script.g.addVertex([vid: i, type: 'claim'])
- v.addEdge('is-a', v0)
- def n = random.nextInt(i)
- def vr = script.g.V('vid', n).next()
- //println "$n: $vr"
- v.addEdge('test', vr)
- if (i%10000 == 0) {
- println "Done $i"
- script.g.commit()
- }
- }
- }
-
-}
diff --git a/src/main/groovy/org/wikidata/gremlin/Loader.groovy
b/src/main/groovy/org/wikidata/gremlin/Loader.groovy
index 93a729d..def6ab3 100644
--- a/src/main/groovy/org/wikidata/gremlin/Loader.groovy
+++ b/src/main/groovy/org/wikidata/gremlin/Loader.groovy
@@ -127,6 +127,7 @@
def revision = item.lastrevid == null ? 'at an unknown revision' : "at
revision ${item.lastrevid}"
log.info "Creating $id ${revision}"
isNew = true
+ v.singleProperty('stub', false)
} else {
if(item.lastrevid && v.lastrevid && v.lastrevid >= item.lastrevid) {
log.info "Ignoring update for $id - it's rev ${item.lastrevid} and we
already have ${v.lastrevid}"
@@ -139,22 +140,22 @@
updateClaims(v, item, isNew)
updateLinks(v, item, isNew)
v.singleProperty('type', item['type'])
- if(item['datatype']) {
- v.singleProperty('datatype', item['datatype'])
- }
- if(item['lastrevid']) {
- v.singleProperty('lastrevid',item['lastrevid'])
- }
- if(isNew) {
- v.singleProperty('stub', false)
- }
- if(item.modified) {
- v.singleProperty('modified',
DATE_FORMAT.parseDateTime(item.modified).getMillis())
- } else {
- v.property('modified').remove()
- }
+ syncProperty(v, 'datatype', item.datatype)
+ syncProperty(v, 'lastrevid', item.lastrevid)
+ syncProperty(v, 'modified', item.modified == null ? null :
DATE_FORMAT.parseDateTime(item.modified).getMillis())
// Not committing here to allow users to manage their own transactions
return v
+ }
+
+ /**
+ * Syncs nullable properties to an element.
+ */
+ void syncProperty(e, name, value) {
+ if (value == null) {
+ e.property(name).remove()
+ } else {
+ e.singleProperty(name, value)
+ }
}
void setEntitySource(String src) {
@@ -976,8 +977,7 @@
* @param claim
* @return
*/
- public def expand(claim)
- {
+ def expand(claim) {
if(!claim.qualifiers) {
return [claim]
}
diff --git a/src/test/java/org/wikidata/gremlin/ImportTest.groovy
b/src/test/java/org/wikidata/gremlin/ImportTest.groovy
index 72f5f42..c6b3ed6 100644
--- a/src/test/java/org/wikidata/gremlin/ImportTest.groovy
+++ b/src/test/java/org/wikidata/gremlin/ImportTest.groovy
@@ -107,7 +107,7 @@
// Multiple qualifiers claim
assert v.outE('P580').values('P580value').toSet() == [-435494942167219200]
as Set
assert v.outE('P580').values('P459q').toSet() == ['Q15605', 'Q76250'] as
Set
- assert v.outE('P580').values('P805q').toSet() == ['Q500699', 'Q500699'] as
Set
+ assert v.outE('P580').values('P805q').toSet() == ['Q500699'] as Set
// Sitelinks
assert v.Lenwiki == 'Universe'
assert v.Lruwiki == 'Вселенная'
--
To view, visit https://gerrit.wikimedia.org/r/187858
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib89ecc8b7bd59a2257cf5784976627406ab86799
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/gremlin
Gerrit-Branch: master
Gerrit-Owner: Manybubbles <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits