Skip to content

Commit 62b5b55

Browse files
author
João Gonçalves
committed
renamed mutation files (which had the same filename) to different class names and adapted remaining files
changed maven's groupId to com.github.jcfgonc corrected older swi-prolog's solution counter code to be compatible with the official source (instead of using custom code by us) added RangeSlider library code from https://github.com/ernieyu/Swing-range-slider
1 parent 6d95e06 commit 62b5b55

11 files changed

+835
-18
lines changed

.classpath

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9-
<classpathentry combineaccessrules="false" kind="src" path="/Libraries"/>
109
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
1110
<attributes>
1211
<attribute name="module" value="true"/>

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
22
<modelVersion>4.0.0</modelVersion>
3-
<groupId>jcfgonc.phd</groupId>
3+
<groupId>com.github.jcfgonc</groupId>
44
<artifactId>pattern-miner</artifactId>
55
<version>0.0.1-SNAPSHOT</version>
66
<build>
@@ -22,7 +22,7 @@
2222
<version>0.0.1-SNAPSHOT</version>
2323
</dependency>
2424
<dependency>
25-
<groupId>jcfgonc.phd</groupId>
25+
<groupId>com.github.jcfgonc</groupId>
2626
<artifactId>unolibrary</artifactId>
2727
<version>0.0.1-SNAPSHOT</version>
2828
</dependency>
@@ -46,5 +46,10 @@
4646
<artifactId>gs-ui</artifactId>
4747
<version>1.3</version>
4848
</dependency>
49+
<dependency>
50+
<groupId>jpl</groupId>
51+
<artifactId>jpl</artifactId>
52+
<version>7.4.0</version>
53+
</dependency>
4954
</dependencies>
5055
</project>

src/jcfgonc/patternminer/PatternChromosome.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import graph.StringGraph;
1313
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
14+
import jcfgonc.patternminer.moea.MutationCode;
1415
import structures.ListOfSet;
1516

1617
public class PatternChromosome implements Variable {
@@ -94,7 +95,7 @@ public String toString() {
9495
}
9596

9697
public void mutate() {
97-
PatternMutation.mutation(kbGraph, random, pattern, false); // do the mutation IN-PLACE
98+
MutationCode.mutation(kbGraph, random, pattern, false); // do the mutation IN-PLACE
9899
PatternFinderUtils.removeAdditionalComponents(this, null); // check for components (old repairing operator)
99100
}
100101

src/jcfgonc/patternminer/PatternFinderSwiProlog.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
import graph.StringEdge;
1414
import graph.StringGraph;
15+
import jcfgonc.patternminer.moea.MutationCode;
1516
import structures.ObjectIndex;
1617
import structures.Ticker;
1718

18-
@Deprecated
1919
public class PatternFinderSwiProlog {
2020

2121
/**
@@ -34,7 +34,7 @@ public static void findPatterns(final StringGraph graph) {
3434
// generate a graph pattern
3535
// do {
3636
for (int i = 0; i < 3; i++) {
37-
PatternMutation.mutation(graph, random, pattern, true);
37+
MutationCode.mutation(graph, random, pattern, true);
3838
}
3939
// match the pattern in the graph
4040
long count = countPatternMatches(pattern, 200000000);
@@ -47,8 +47,7 @@ public static void findPatterns(final StringGraph graph) {
4747
/**
4848
*
4949
* @param graph
50-
* @param concepts
51-
* if null concepts are stored as text, else stored as unique integers
50+
* @param concepts if null concepts are stored as text, else stored as unique integers
5251
*/
5352
public static void createKnowledgeBase(final StringGraph graph, final ObjectIndex<String> concepts) {
5453
JPL.init();
@@ -70,7 +69,8 @@ public static void createKnowledgeBase(final StringGraph graph, final ObjectInde
7069

7170
Compound factCompound = new Compound("assertz", new Term[] { relationCompound });
7271
Query fact = new Query(factCompound);
73-
fact.putQuery_jcfgonc();
72+
fact.oneSolution();
73+
// fact.putQuery_jcfgonc(); //this was a custom jpl7 function which added the query regardless of return values
7474
}
7575
System.out.println("SWI KB creation took " + t.getTimeDeltaLastCall() + " s");
7676
}
@@ -140,10 +140,21 @@ public static long countPatternMatches(final StringGraph pattern, final int solu
140140
Query q = new Query(rootCompound);
141141
// Query qtest = new Query("isa(X3,X0),isa(X3,X1),isa(X2,X1)."); //test query
142142
Ticker t = new Ticker();
143-
long matches = q.countSolutions_jcfgonc(solutionLimit);
143+
144+
// this code is commented to be compatible with the original jpl7 source
145+
// this was a custom jpl7 function in which the C side call asked the engine for next
146+
// solutions until a limit is reached (and returned the count). Hence it minimized the java-C hasMoreSolutions/nextSolution call overhead
147+
// long matches = q.countSolutions_jcfgonc(solutionLimit);
148+
149+
long matches = 0;
150+
while (q.hasMoreSolutions()) {
151+
q.nextSolution();
152+
matches++;
153+
}
144154
double time = t.getElapsedTime();
145-
System.out.println("pattern edges\t" + patternWithVars.numberOfEdges() + "\tpattern vars\t" + patternWithVars.numberOfVertices() + "\ttime\t" + time + "\tmatches\t"
146-
+ matches + "\tsolutions/s\t" + (matches / time) + "\tpattern\t" + patternWithVars.toString(64, Integer.MAX_VALUE));
155+
System.out.println("pattern edges\t" + patternWithVars.numberOfEdges() + "\tpattern vars\t" + patternWithVars.numberOfVertices() + "\ttime\t"
156+
+ time + "\tmatches\t" + matches + "\tsolutions/s\t" + (matches / time) + "\tpattern\t"
157+
+ patternWithVars.toString(64, Integer.MAX_VALUE));
147158
return matches;
148159
}
149160
}

src/jcfgonc/patternminer/PatternFinderUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import graph.StringEdge;
2121
import graph.StringGraph;
2222
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
23+
import jcfgonc.patternminer.moea.MutationCode;
2324
import structures.ListOfSet;
2425
import structures.Ticker;
2526
import structures.UnorderedPair;
@@ -303,7 +304,7 @@ public static void calculateRelationHistogram(PatternChromosome patternChromosom
303304
public static StringGraph initializePattern(StringGraph kbGraph, StringGraph pattern, RandomGenerator random) {
304305
// randomly add edges to an empty graph
305306
for (int i = 0; i < 3; i++) { // add N edges
306-
PatternMutation.mutation(kbGraph, random, pattern, true);
307+
MutationCode.mutation(kbGraph, random, pattern, true);
307308
}
308309
return pattern;
309310
}

src/jcfgonc/patternminer/PatternMutation.java renamed to src/jcfgonc/patternminer/moea/MutationCode.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jcfgonc.patternminer;
1+
package jcfgonc.patternminer.moea;
22

33
import java.util.ArrayList;
44
import java.util.HashSet;
@@ -10,9 +10,11 @@
1010
import graph.StringEdge;
1111
import graph.StringGraph;
1212
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
13+
import jcfgonc.patternminer.PatternFinderUtils;
14+
import jcfgonc.patternminer.PatternMinerConfig;
1315
import utils.VariousUtils;
1416

15-
public class PatternMutation {
17+
public class MutationCode {
1618

1719
/**
1820
* mutates the pattern IN-PLACE

src/jcfgonc/patternminer/moea/PatternMutation.java renamed to src/jcfgonc/patternminer/moea/MutationOperator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import jcfgonc.patternminer.PatternChromosome;
77

8-
public class PatternMutation implements Variation {
8+
public class MutationOperator implements Variation {
99

1010
/**
1111
* The probability of mutating each variable in a solution.
@@ -17,7 +17,7 @@ public class PatternMutation implements Variation {
1717
*
1818
* @param probability the probability of occurring the mutation
1919
*/
20-
public PatternMutation(double probability) {
20+
public MutationOperator(double probability) {
2121
super();
2222
this.probability = probability;
2323
}

src/jcfgonc/patternminer/moea/PatternMinerLauncher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public Variation getVariation(String name, Properties properties, Problem proble
8484

8585
if (name.equalsIgnoreCase("PatternMutation")) {
8686
double probability = typedProperties.getDouble("PatternMutation.Rate", 1.0);
87-
PatternMutation pm = new PatternMutation(probability);
87+
MutationOperator pm = new MutationOperator(probability);
8888
return pm;
8989
}
9090

src/slider/RangeSlider.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package slider;
2+
3+
import javax.swing.JSlider;
4+
5+
/**
6+
* An extension of JSlider to select a range of values using two thumb controls.
7+
* The thumb controls are used to select the lower and upper value of a range
8+
* with predetermined minimum and maximum values.
9+
*
10+
* <p>Note that RangeSlider makes use of the default BoundedRangeModel, which
11+
* supports an inner range defined by a value and an extent. The upper value
12+
* returned by RangeSlider is simply the lower value plus the extent.</p>
13+
*/
14+
public class RangeSlider extends JSlider {
15+
16+
/**
17+
* Constructs a RangeSlider with default minimum and maximum values of 0
18+
* and 100.
19+
*/
20+
public RangeSlider() {
21+
initSlider();
22+
}
23+
24+
/**
25+
* Constructs a RangeSlider with the specified default minimum and maximum
26+
* values.
27+
*/
28+
public RangeSlider(int min, int max) {
29+
super(min, max);
30+
initSlider();
31+
}
32+
33+
/**
34+
* Initializes the slider by setting default properties.
35+
*/
36+
private void initSlider() {
37+
setOrientation(HORIZONTAL);
38+
}
39+
40+
/**
41+
* Overrides the superclass method to install the UI delegate to draw two
42+
* thumbs.
43+
*/
44+
@Override
45+
public void updateUI() {
46+
setUI(new RangeSliderUI(this));
47+
// Update UI for slider labels. This must be called after updating the
48+
// UI of the slider. Refer to JSlider.updateUI().
49+
updateLabelUIs();
50+
}
51+
52+
/**
53+
* Returns the lower value in the range.
54+
*/
55+
@Override
56+
public int getValue() {
57+
return super.getValue();
58+
}
59+
60+
/**
61+
* Sets the lower value in the range.
62+
*/
63+
@Override
64+
public void setValue(int value) {
65+
int oldValue = getValue();
66+
if (oldValue == value) {
67+
return;
68+
}
69+
70+
// Compute new value and extent to maintain upper value.
71+
int oldExtent = getExtent();
72+
int newValue = Math.min(Math.max(getMinimum(), value), oldValue + oldExtent);
73+
int newExtent = oldExtent + oldValue - newValue;
74+
75+
// Set new value and extent, and fire a single change event.
76+
getModel().setRangeProperties(newValue, newExtent, getMinimum(),
77+
getMaximum(), getValueIsAdjusting());
78+
}
79+
80+
/**
81+
* Returns the upper value in the range.
82+
*/
83+
public int getUpperValue() {
84+
return getValue() + getExtent();
85+
}
86+
87+
/**
88+
* Sets the upper value in the range.
89+
*/
90+
public void setUpperValue(int value) {
91+
// Compute new extent.
92+
int lowerValue = getValue();
93+
int newExtent = Math.min(Math.max(0, value - lowerValue), getMaximum() - lowerValue);
94+
95+
// Set extent to set upper value.
96+
setExtent(newExtent);
97+
}
98+
}

src/slider/RangeSliderDemo.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package slider;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Dimension;
5+
import java.awt.GridBagConstraints;
6+
import java.awt.GridBagLayout;
7+
import java.awt.Insets;
8+
9+
import javax.swing.BorderFactory;
10+
import javax.swing.JFrame;
11+
import javax.swing.JLabel;
12+
import javax.swing.JPanel;
13+
import javax.swing.SwingUtilities;
14+
import javax.swing.UIManager;
15+
import javax.swing.event.ChangeEvent;
16+
import javax.swing.event.ChangeListener;
17+
18+
/**
19+
* Demo application panel to display a range slider.
20+
*/
21+
public class RangeSliderDemo extends JPanel {
22+
23+
private JLabel rangeSliderLabel1 = new JLabel();
24+
private JLabel rangeSliderValue1 = new JLabel();
25+
private JLabel rangeSliderLabel2 = new JLabel();
26+
private JLabel rangeSliderValue2 = new JLabel();
27+
private RangeSlider rangeSlider = new RangeSlider();
28+
29+
public RangeSliderDemo() {
30+
setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
31+
setLayout(new GridBagLayout());
32+
33+
rangeSliderLabel1.setText("Lower value:");
34+
rangeSliderLabel2.setText("Upper value:");
35+
rangeSliderValue1.setHorizontalAlignment(JLabel.LEFT);
36+
rangeSliderValue2.setHorizontalAlignment(JLabel.LEFT);
37+
38+
rangeSlider.setPreferredSize(new Dimension(240, rangeSlider.getPreferredSize().height));
39+
rangeSlider.setMinimum(0);
40+
rangeSlider.setMaximum(10);
41+
42+
// Add listener to update display.
43+
rangeSlider.addChangeListener(new ChangeListener() {
44+
public void stateChanged(ChangeEvent e) {
45+
RangeSlider slider = (RangeSlider) e.getSource();
46+
rangeSliderValue1.setText(String.valueOf(slider.getValue()));
47+
rangeSliderValue2.setText(String.valueOf(slider.getUpperValue()));
48+
}
49+
});
50+
51+
add(rangeSliderLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
52+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0));
53+
add(rangeSliderValue1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
54+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 0), 0, 0));
55+
add(rangeSliderLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
56+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0));
57+
add(rangeSliderValue2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
58+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 6, 0), 0, 0));
59+
add(rangeSlider , new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0,
60+
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
61+
}
62+
63+
public void display() {
64+
// Initialize values.
65+
rangeSlider.setValue(3);
66+
rangeSlider.setUpperValue(7);
67+
68+
// Initialize value display.
69+
rangeSliderValue1.setText(String.valueOf(rangeSlider.getValue()));
70+
rangeSliderValue2.setText(String.valueOf(rangeSlider.getUpperValue()));
71+
72+
// Create window frame.
73+
JFrame frame = new JFrame();
74+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
75+
frame.setResizable(false);
76+
frame.setTitle("Range Slider Demo");
77+
78+
// Set window content and validate.
79+
frame.getContentPane().setLayout(new BorderLayout());
80+
frame.getContentPane().add(this, BorderLayout.CENTER);
81+
frame.pack();
82+
83+
// Set window location and display.
84+
frame.setLocationRelativeTo(null);
85+
frame.setVisible(true);
86+
}
87+
88+
/**
89+
* Main application method.
90+
* @param args String[]
91+
*/
92+
public static void main(String[] args) {
93+
try {
94+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
95+
} catch (Exception ex) {
96+
ex.printStackTrace();
97+
}
98+
99+
SwingUtilities.invokeLater(new Runnable() {
100+
public void run() {
101+
new RangeSliderDemo().display();
102+
}
103+
});
104+
}
105+
}

0 commit comments

Comments
 (0)